Skip to content

Commit

Permalink
feat: Add global variable declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Feb 7, 2025
1 parent 7e9eb02 commit fd3a453
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void InitParsePoints()

AddDeclarationParsePoint<FunctionDefinitionParser>(TokenType.Function);
AddDeclarationParsePoint<RulesForDeclarationParser>(TokenType.Rules);
AddDeclarationParsePoint<VariableStatementParser>(TokenType.Let);

/* AddDeclarationParsePoint<ConstructorDeclarationParser>(TokenType.Constructor);
AddDeclarationParsePoint<DestructorDeclaration>(TokenType.Destructor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SocordiaC.Compilation.Listeners;

public class CollectGlobalVariablesListener : Listeners<Driver, AstNode, VariableStatement>
{
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;
}
}
8 changes: 6 additions & 2 deletions NewSource/SocordiaC/Stages/CollectTypesStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>>
}

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);
Expand Down
2 changes: 2 additions & 0 deletions NewSource/SocordiaC/hello.scs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public enum Color {
Blue
}

let defaultColor: Color;

func main()
{
let mut x = 12;
Expand Down

0 comments on commit fd3a453

Please sign in to comment.