This commit is contained in:
nub31
2025-09-29 16:23:39 +02:00
parent 857e29d77b
commit c0948e856a
13 changed files with 568 additions and 474 deletions

View File

@@ -86,6 +86,7 @@ public sealed class Parser
{
Symbol.Func => ParseFunc(startIndex, exported, null),
Symbol.Struct => ParseStruct(startIndex, exported),
Symbol.Let => ParseGlobalVariable(startIndex, exported),
_ => throw new ParseException(Diagnostic
.Error($"Expected 'func' or 'struct' but found '{keyword.Symbol}'")
.WithHelp("Valid definition keywords are 'func' and 'struct'")
@@ -113,6 +114,22 @@ public sealed class Parser
return definitions;
}
private GlobalVariableSyntax ParseGlobalVariable(int startIndex, bool exported)
{
var name = ExpectIdentifier();
TypeSyntax? type = null;
if (TryExpectSymbol(Symbol.Colon))
{
type = ParseType();
}
ExpectSymbol(Symbol.Assign);
var value = ParseExpression();
return new GlobalVariableSyntax(GetTokens(startIndex), name.Value, exported, type, value);
}
private FuncSignatureSyntax ParseFuncSignature()
{
var startIndex = _tokenIndex;

View File

@@ -17,3 +17,5 @@ public record StructFuncSyntax(List<Token> Tokens, string Name, string? Hook, Fu
public record StructSyntax(List<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
public record StructTemplateSyntax(List<Token> Tokens, List<string> TemplateArguments, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
public record GlobalVariableSyntax(List<Token> Tokens, string Name, bool Exported, TypeSyntax? ExplicitType, ExpressionSyntax Value) : DefinitionSyntax(Tokens, Name, Exported);