Type syntax when parsing
This commit is contained in:
@@ -96,8 +96,8 @@ public abstract class NubSimpleType : NubType
|
|||||||
|
|
||||||
public class NubFuncType(NubType returnType, List<NubType> parameters) : NubSimpleType
|
public class NubFuncType(NubType returnType, List<NubType> parameters) : NubSimpleType
|
||||||
{
|
{
|
||||||
public NubType ReturnType { get; } = returnType;
|
|
||||||
public List<NubType> Parameters { get; } = parameters;
|
public List<NubType> Parameters { get; } = parameters;
|
||||||
|
public NubType ReturnType { get; } = returnType;
|
||||||
|
|
||||||
public override StorageSize StorageSize => StorageSize.U64;
|
public override StorageSize StorageSize => StorageSize.U64;
|
||||||
|
|
||||||
@@ -225,11 +225,10 @@ public class NubStringType : NubComplexType
|
|||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubStringType));
|
public override int GetHashCode() => HashCode.Combine(typeof(NubStringType));
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubCustomType(string @namespace, string name, IReadOnlyList<NubType> parameters) : NubComplexType
|
public class NubCustomType(string @namespace, string name) : NubComplexType
|
||||||
{
|
{
|
||||||
public string Namespace { get; } = @namespace;
|
public string Namespace { get; } = @namespace;
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
public IReadOnlyList<NubType> Parameters { get; } = parameters;
|
|
||||||
|
|
||||||
public CustomTypeKind Kind(BoundDefinitionTable definitionTable)
|
public CustomTypeKind Kind(BoundDefinitionTable definitionTable)
|
||||||
{
|
{
|
||||||
@@ -289,22 +288,9 @@ public class NubCustomType(string @namespace, string name, IReadOnlyList<NubType
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => $"{Namespace}::{Name}{(Parameters.Any() ? $"<{string.Join(",", Parameters)}>" : "")}";
|
public override string ToString() => $"{Namespace}::{Name}";
|
||||||
public override bool Equals(NubType? other) => other is NubCustomType custom && Namespace == custom.Namespace && Name == custom.Name && Parameters.SequenceEqual(custom.Parameters);
|
public override bool Equals(NubType? other) => other is NubCustomType custom && Namespace == custom.Namespace && Name == custom.Name;
|
||||||
|
public override int GetHashCode() => HashCode.Combine(typeof(NubCustomType), Namespace, Name);
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
var hash = new HashCode();
|
|
||||||
hash.Add(typeof(NubFuncType));
|
|
||||||
hash.Add(Namespace);
|
|
||||||
hash.Add(Name);
|
|
||||||
foreach (var param in Parameters)
|
|
||||||
{
|
|
||||||
hash.Add(param);
|
|
||||||
}
|
|
||||||
|
|
||||||
return hash.ToHashCode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum CustomTypeKind
|
public enum CustomTypeKind
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ public abstract record DefinitionSyntax(IReadOnlyList<Token> Tokens, string Name
|
|||||||
|
|
||||||
public abstract record DefinitionMemberSyntax(IReadOnlyList<Token> Tokens) : SyntaxNode(Tokens);
|
public abstract record DefinitionMemberSyntax(IReadOnlyList<Token> Tokens) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
public record FuncParameterSyntax(IReadOnlyList<Token> Tokens, string Name, NubType Type) : DefinitionMemberSyntax(Tokens);
|
public record FuncParameterSyntax(IReadOnlyList<Token> Tokens, string Name, TypeSyntax Type) : DefinitionMemberSyntax(Tokens);
|
||||||
|
|
||||||
public record FuncSignatureSyntax(IReadOnlyList<Token> Tokens, IReadOnlyList<FuncParameterSyntax> Parameters, NubType ReturnType) : DefinitionMemberSyntax(Tokens);
|
public record FuncSignatureSyntax(IReadOnlyList<Token> Tokens, IReadOnlyList<FuncParameterSyntax> Parameters, TypeSyntax ReturnType) : DefinitionMemberSyntax(Tokens);
|
||||||
|
|
||||||
public record LocalFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : DefinitionSyntax(Tokens, Namespace);
|
public record LocalFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : DefinitionSyntax(Tokens, Namespace);
|
||||||
|
|
||||||
public record ExternFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, string CallName, FuncSignatureSyntax Signature) : DefinitionSyntax(Tokens, Namespace);
|
public record ExternFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, string CallName, FuncSignatureSyntax Signature) : DefinitionSyntax(Tokens, Namespace);
|
||||||
|
|
||||||
public record StructFieldSyntax(IReadOnlyList<Token> Tokens, int Index, string Name, NubType Type, Optional<ExpressionSyntax> Value) : DefinitionMemberSyntax(Tokens);
|
public record StructFieldSyntax(IReadOnlyList<Token> Tokens, int Index, string Name, TypeSyntax Type, Optional<ExpressionSyntax> Value) : DefinitionMemberSyntax(Tokens);
|
||||||
|
|
||||||
public record StructSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, IReadOnlyList<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, Namespace);
|
public record StructSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, IReadOnlyList<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, Namespace);
|
||||||
|
|
||||||
@@ -25,4 +25,4 @@ public record TraitSyntax(IReadOnlyList<Token> Tokens, string Namespace, string
|
|||||||
|
|
||||||
public record TraitFuncImplSyntax(IReadOnlyList<Token> Tokens, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : DefinitionMemberSyntax(Tokens);
|
public record TraitFuncImplSyntax(IReadOnlyList<Token> Tokens, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : DefinitionMemberSyntax(Tokens);
|
||||||
|
|
||||||
public record TraitImplSyntax(IReadOnlyList<Token> Tokens, string Namespace, NubType TraitType, NubType ForType, IReadOnlyList<TraitFuncImplSyntax> Functions) : DefinitionSyntax(Tokens, Namespace);
|
public record TraitImplSyntax(IReadOnlyList<Token> Tokens, string Namespace, TypeSyntax TraitType, TypeSyntax ForType, IReadOnlyList<TraitFuncImplSyntax> Functions) : DefinitionSyntax(Tokens, Namespace);
|
||||||
@@ -33,7 +33,7 @@ public record FuncCallSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Expre
|
|||||||
|
|
||||||
public record IdentifierSyntax(IReadOnlyList<Token> Tokens, Optional<string> Namespace, string Name) : ExpressionSyntax(Tokens);
|
public record IdentifierSyntax(IReadOnlyList<Token> Tokens, Optional<string> Namespace, string Name) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record ArrayInitializerSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Capacity, NubType ElementType) : ExpressionSyntax(Tokens);
|
public record ArrayInitializerSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Capacity, TypeSyntax ElementType) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record ArrayIndexAccessSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Target, ExpressionSyntax Index) : ExpressionSyntax(Tokens);
|
public record ArrayIndexAccessSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Target, ExpressionSyntax Index) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
@@ -47,6 +47,6 @@ public record LiteralSyntax(IReadOnlyList<Token> Tokens, string Literal, Literal
|
|||||||
|
|
||||||
public record MemberAccessSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens);
|
public record MemberAccessSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record StructInitializerSyntax(IReadOnlyList<Token> Tokens, NubType StructType, Dictionary<string, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
public record StructInitializerSyntax(IReadOnlyList<Token> Tokens, TypeSyntax StructType, Dictionary<string, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record DereferenceSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Expression) : ExpressionSyntax(Tokens);
|
public record DereferenceSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Expression) : ExpressionSyntax(Tokens);
|
||||||
@@ -13,7 +13,7 @@ public record AssignmentSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Tar
|
|||||||
|
|
||||||
public record IfSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body, Optional<Variant<IfSyntax, BlockSyntax>> Else) : StatementSyntax(Tokens);
|
public record IfSyntax(IReadOnlyList<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body, Optional<Variant<IfSyntax, BlockSyntax>> Else) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
public record VariableDeclarationSyntax(IReadOnlyList<Token> Tokens, string Name, Optional<NubType> ExplicitType, Optional<ExpressionSyntax> Assignment) : StatementSyntax(Tokens);
|
public record VariableDeclarationSyntax(IReadOnlyList<Token> Tokens, string Name, Optional<TypeSyntax> ExplicitType, Optional<ExpressionSyntax> Assignment) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
public record ContinueSyntax(IReadOnlyList<Token> Tokens) : StatementSyntax(Tokens);
|
public record ContinueSyntax(IReadOnlyList<Token> Tokens) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
|
|||||||
23
src/compiler/NubLang/Syntax/Parsing/Node/TypeSyntax.cs
Normal file
23
src/compiler/NubLang/Syntax/Parsing/Node/TypeSyntax.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using NubLang.Syntax.Tokenization;
|
||||||
|
|
||||||
|
namespace NubLang.Syntax.Parsing.Node;
|
||||||
|
|
||||||
|
public abstract record TypeSyntax(IReadOnlyList<Token> Tokens) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
|
public record FuncTypeSyntax(IReadOnlyList<Token> Tokens, IReadOnlyList<TypeSyntax> Parameters, TypeSyntax ReturnType) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record PointerTypeSyntax(IReadOnlyList<Token> Tokens, TypeSyntax BaseType) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record VoidTypeSyntax(IReadOnlyList<Token> Tokens) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record PrimitiveTypeSyntax(IReadOnlyList<Token> Tokens, PrimitiveTypeKind Kind) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record CStringTypeSyntax(IReadOnlyList<Token> Tokens) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record StringTypeSyntax(IReadOnlyList<Token> Tokens) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record CustomTypeSyntax(IReadOnlyList<Token> Tokens, string Name, string Namespace) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record TemplateTypeSyntax(IReadOnlyList<Token> Tokens, string Name, string Namespace, IReadOnlyList<TypeSyntax> Arguments) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
|
public record ArrayTypeSyntax(IReadOnlyList<Token> Tokens, TypeSyntax BaseType) : TypeSyntax(Tokens);
|
||||||
@@ -117,7 +117,7 @@ public sealed class Parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new NubVoidType();
|
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new VoidTypeSyntax([Peek().GetValue()]);
|
||||||
|
|
||||||
return new FuncSignatureSyntax(GetTokens(startIndex), parameters, returnType);
|
return new FuncSignatureSyntax(GetTokens(startIndex), parameters, returnType);
|
||||||
}
|
}
|
||||||
@@ -295,7 +295,7 @@ public sealed class Parser
|
|||||||
ExpectSymbol(Symbol.Let);
|
ExpectSymbol(Symbol.Let);
|
||||||
var name = ExpectIdentifier().Value;
|
var name = ExpectIdentifier().Value;
|
||||||
|
|
||||||
var explicitType = Optional<NubType>.Empty();
|
var explicitType = Optional<TypeSyntax>.Empty();
|
||||||
if (TryExpectSymbol(Symbol.Colon))
|
if (TryExpectSymbol(Symbol.Colon))
|
||||||
{
|
{
|
||||||
explicitType = ParseType();
|
explicitType = ParseType();
|
||||||
@@ -649,30 +649,32 @@ public sealed class Parser
|
|||||||
return new BlockSyntax(GetTokens(startIndex), statements);
|
return new BlockSyntax(GetTokens(startIndex), statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
private NubType ParseType()
|
private TypeSyntax ParseType()
|
||||||
{
|
{
|
||||||
|
var startIndex = _tokenIndex;
|
||||||
|
|
||||||
if (TryExpectIdentifier(out var name))
|
if (TryExpectIdentifier(out var name))
|
||||||
{
|
{
|
||||||
return name.Value switch
|
return name.Value switch
|
||||||
{
|
{
|
||||||
"void" => new NubVoidType(),
|
"void" => new VoidTypeSyntax(GetTokens(startIndex)),
|
||||||
"string" => new NubStringType(),
|
"string" => new StringTypeSyntax(GetTokens(startIndex)),
|
||||||
"cstring" => new NubCStringType(),
|
"cstring" => new CStringTypeSyntax(GetTokens(startIndex)),
|
||||||
"i64" => new NubPrimitiveType(PrimitiveTypeKind.I64),
|
"i64" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.I64),
|
||||||
"i32" => new NubPrimitiveType(PrimitiveTypeKind.I32),
|
"i32" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.I32),
|
||||||
"i16" => new NubPrimitiveType(PrimitiveTypeKind.I16),
|
"i16" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.I16),
|
||||||
"i8" => new NubPrimitiveType(PrimitiveTypeKind.I8),
|
"i8" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.I8),
|
||||||
"u64" => new NubPrimitiveType(PrimitiveTypeKind.U64),
|
"u64" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.U64),
|
||||||
"u32" => new NubPrimitiveType(PrimitiveTypeKind.U32),
|
"u32" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.U32),
|
||||||
"u16" => new NubPrimitiveType(PrimitiveTypeKind.U16),
|
"u16" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.U16),
|
||||||
"u8" => new NubPrimitiveType(PrimitiveTypeKind.U8),
|
"u8" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.U8),
|
||||||
"f64" => new NubPrimitiveType(PrimitiveTypeKind.F64),
|
"f64" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.F64),
|
||||||
"f32" => new NubPrimitiveType(PrimitiveTypeKind.F32),
|
"f32" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.F32),
|
||||||
"bool" => new NubPrimitiveType(PrimitiveTypeKind.Bool),
|
"bool" => new PrimitiveTypeSyntax(GetTokens(startIndex), PrimitiveTypeKind.Bool),
|
||||||
_ => ParseCustomType()
|
_ => ParseCustomType()
|
||||||
};
|
};
|
||||||
|
|
||||||
NubCustomType ParseCustomType()
|
TypeSyntax ParseCustomType()
|
||||||
{
|
{
|
||||||
var @namespace = _namespace;
|
var @namespace = _namespace;
|
||||||
if (TryExpectSymbol(Symbol.DoubleColon))
|
if (TryExpectSymbol(Symbol.DoubleColon))
|
||||||
@@ -680,7 +682,7 @@ public sealed class Parser
|
|||||||
@namespace = ExpectIdentifier().Value;
|
@namespace = ExpectIdentifier().Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
var parameters = new List<NubType>();
|
var parameters = new List<TypeSyntax>();
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.LessThan))
|
if (TryExpectSymbol(Symbol.LessThan))
|
||||||
{
|
{
|
||||||
@@ -688,22 +690,24 @@ public sealed class Parser
|
|||||||
{
|
{
|
||||||
parameters.Add(ParseType());
|
parameters.Add(ParseType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new TemplateTypeSyntax(GetTokens(startIndex), @namespace, name.Value, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NubCustomType(@namespace, name.Value, parameters);
|
return new CustomTypeSyntax(GetTokens(startIndex), @namespace, name.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Caret))
|
if (TryExpectSymbol(Symbol.Caret))
|
||||||
{
|
{
|
||||||
var baseType = ParseType();
|
var baseType = ParseType();
|
||||||
return new NubPointerType(baseType);
|
return new PointerTypeSyntax(GetTokens(startIndex), baseType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Func))
|
if (TryExpectSymbol(Symbol.Func))
|
||||||
{
|
{
|
||||||
ExpectSymbol(Symbol.OpenParen);
|
ExpectSymbol(Symbol.OpenParen);
|
||||||
List<NubType> parameters = [];
|
List<TypeSyntax> parameters = [];
|
||||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||||
{
|
{
|
||||||
var parameter = ParseType();
|
var parameter = ParseType();
|
||||||
@@ -718,16 +722,16 @@ public sealed class Parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new NubVoidType();
|
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new VoidTypeSyntax(GetTokens(startIndex));
|
||||||
|
|
||||||
return new NubFuncType(returnType, parameters);
|
return new FuncTypeSyntax(GetTokens(startIndex), parameters, returnType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.OpenBracket))
|
if (TryExpectSymbol(Symbol.OpenBracket))
|
||||||
{
|
{
|
||||||
ExpectSymbol(Symbol.CloseBracket);
|
ExpectSymbol(Symbol.CloseBracket);
|
||||||
var baseType = ParseType();
|
var baseType = ParseType();
|
||||||
return new NubArrayType(baseType);
|
return new ArrayTypeSyntax(GetTokens(startIndex), baseType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Peek().TryGetValue(out var peekToken))
|
if (!Peek().TryGetValue(out var peekToken))
|
||||||
@@ -735,7 +739,7 @@ public sealed class Parser
|
|||||||
throw new ParseException(Diagnostic
|
throw new ParseException(Diagnostic
|
||||||
.Error("Unexpected end of file while parsing type")
|
.Error("Unexpected end of file while parsing type")
|
||||||
.WithHelp("Expected a type name")
|
.WithHelp("Expected a type name")
|
||||||
.At(_tokens.Last())
|
.At(_tokens[^1])
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user