global variables

This commit is contained in:
nub31
2026-02-10 20:51:28 +01:00
parent 7872a4b6b8
commit d0c31ad17f
4 changed files with 49 additions and 17 deletions

View File

@@ -121,6 +121,21 @@ public sealed class Parser(string fileName, List<Token> tokens)
return new NodeDefinitionStruct(TokensFrom(startIndex), exported, packed, name, fields);
}
if (TryExpectKeyword(Keyword.Let))
{
var exported = modifiers.Remove(Keyword.Export);
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for global variable").At(fileName, modifier.Value).Build());
var name = ExpectIdent();
ExpectSymbol(Symbol.Colon);
var type = ParseType();
return new NodeDefinitionGlobalVariable(TokensFrom(startIndex), exported, name, type);
}
throw new CompileException(Diagnostic.Error("Not a valid definition").At(fileName, Peek()).Build());
}
@@ -622,6 +637,13 @@ public sealed class NodeDefinitionStruct(List<Token> tokens, bool exported, bool
}
}
public sealed class NodeDefinitionGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
{
public bool Exported { get; } = exported;
public TokenIdent Name { get; } = name;
public NodeType Type { get; } = type;
}
public abstract class NodeStatement(List<Token> tokens) : Node(tokens);
public sealed class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens)