From fd3a4539fbdba97a0c0796cf3347a64c05fd716b Mon Sep 17 00:00:00 2001 From: Chris Anders Date: Fri, 7 Feb 2025 06:54:43 +0000 Subject: [PATCH] feat: Add global variable declarations --- .../Parsing/Parser.ParsePoints.cs | 1 + .../Listeners/CollectFunctionsListener.cs | 2 +- .../CollectGlobalVariablesListener.cs | 18 ++++++++++++++++++ .../SocordiaC/Stages/CollectTypesStage.cs | 8 ++++++-- NewSource/SocordiaC/hello.scs | 2 ++ 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 NewSource/SocordiaC/Compilation/Listeners/CollectGlobalVariablesListener.cs diff --git a/NewSource/Socordia.CodeAnalysis/Parsing/Parser.ParsePoints.cs b/NewSource/Socordia.CodeAnalysis/Parsing/Parser.ParsePoints.cs index b1248386..2c0faaac 100644 --- a/NewSource/Socordia.CodeAnalysis/Parsing/Parser.ParsePoints.cs +++ b/NewSource/Socordia.CodeAnalysis/Parsing/Parser.ParsePoints.cs @@ -30,6 +30,7 @@ public void InitParsePoints() AddDeclarationParsePoint(TokenType.Function); AddDeclarationParsePoint(TokenType.Rules); + AddDeclarationParsePoint(TokenType.Let); /* AddDeclarationParsePoint(TokenType.Constructor); AddDeclarationParsePoint(TokenType.Destructor); diff --git a/NewSource/SocordiaC/Compilation/Listeners/CollectFunctionsListener.cs b/NewSource/SocordiaC/Compilation/Listeners/CollectFunctionsListener.cs index c8e0607d..69b6168e 100644 --- a/NewSource/SocordiaC/Compilation/Listeners/CollectFunctionsListener.cs +++ b/NewSource/SocordiaC/Compilation/Listeners/CollectFunctionsListener.cs @@ -22,7 +22,7 @@ protected override void ListenToNode(Driver context, FunctionDefinition node) var returnType = GetReturnType(node, type); var method = type.CreateMethod(node.Signature.Name.Name, - returnType, [..parameters], attrs); + returnType, [.. parameters], attrs); if (!node.Modifiers.Contains(Modifier.Extern)) { diff --git a/NewSource/SocordiaC/Compilation/Listeners/CollectGlobalVariablesListener.cs b/NewSource/SocordiaC/Compilation/Listeners/CollectGlobalVariablesListener.cs new file mode 100644 index 00000000..ac00aa9a --- /dev/null +++ b/NewSource/SocordiaC/Compilation/Listeners/CollectGlobalVariablesListener.cs @@ -0,0 +1,18 @@ +namespace SocordiaC.Compilation.Listeners; + +public class CollectGlobalVariablesListener : Listeners +{ + protected override void ListenToNode(Driver context, VariableStatement node) + { + var type = context.GetFunctionType(context.GetNamespaceOf(node)); + var varType = Utils.GetTypeFromNode(node.Type, type); + var field = type.CreateField(node.Name, varType, FieldAttributes.Public | FieldAttributes.Static); + + Mappings.Variables.Add(node, field); + } + + protected override bool ShouldListenToChildren(Driver context, VariableStatement node) + { + return false; + } +} diff --git a/NewSource/SocordiaC/Stages/CollectTypesStage.cs b/NewSource/SocordiaC/Stages/CollectTypesStage.cs index 91ff31ed..4da67f2c 100644 --- a/NewSource/SocordiaC/Stages/CollectTypesStage.cs +++ b/NewSource/SocordiaC/Stages/CollectTypesStage.cs @@ -26,10 +26,14 @@ public async Task HandleAsync(Driver context, Func> } var functionCollector = new CollectFunctionsListener(); + var globalVarListener = new CollectGlobalVariablesListener(); foreach (var tree in context.Trees) - foreach (var decl in tree.Declarations.Children) { - functionCollector.Listen(context, decl); + foreach (var decl in tree.Declarations.Children) + { + functionCollector.Listen(context, decl); + globalVarListener.Listen(context, decl); + } } return await next.Invoke(context); diff --git a/NewSource/SocordiaC/hello.scs b/NewSource/SocordiaC/hello.scs index d50ff63a..061c428f 100644 --- a/NewSource/SocordiaC/hello.scs +++ b/NewSource/SocordiaC/hello.scs @@ -5,6 +5,8 @@ public enum Color { Blue } +let defaultColor: Color; + func main() { let mut x = 12;