From 4c201c40856b4d80581c1c611bae3ff619b6245e Mon Sep 17 00:00:00 2001 From: Oliver Stene Date: Sun, 8 Feb 2026 15:45:53 +0100 Subject: [PATCH] ... --- compiler/Compiler/.gitignore | 1 + compiler/Compiler/Generator.cs | 9 +++++++++ compiler/Compiler/Parser.cs | 17 +++++++++++++++++ compiler/Compiler/Program.cs | 3 ++- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 compiler/Compiler/.gitignore diff --git a/compiler/Compiler/.gitignore b/compiler/Compiler/.gitignore new file mode 100644 index 0000000..eb306b3 --- /dev/null +++ b/compiler/Compiler/.gitignore @@ -0,0 +1 @@ +out.c \ No newline at end of file diff --git a/compiler/Compiler/Generator.cs b/compiler/Compiler/Generator.cs index d869002..5711184 100644 --- a/compiler/Compiler/Generator.cs +++ b/compiler/Compiler/Generator.cs @@ -65,6 +65,9 @@ public sealed class Generator(List nodes) case NodeStatementReturn statement: EmitStatementReturn(statement); break; + case NodeStatementVariableDeclaration statement: + EmitStatementVariableDeclaration(statement); + break; default: throw new ArgumentOutOfRangeException(nameof(node), node, null); } @@ -95,6 +98,12 @@ public sealed class Generator(List nodes) writer.WriteLine($"return {value};"); } + private void EmitStatementVariableDeclaration(NodeStatementVariableDeclaration statement) + { + var value = EmitExpression(statement.Value); + writer.WriteLine($"{CType(statement.Type)} {statement.Name.Ident} = {value};"); + } + private string EmitExpression(NodeExpression node) { return node switch diff --git a/compiler/Compiler/Parser.cs b/compiler/Compiler/Parser.cs index 197d9d3..bbf8f09 100644 --- a/compiler/Compiler/Parser.cs +++ b/compiler/Compiler/Parser.cs @@ -70,6 +70,16 @@ public sealed class Parser(List tokens) return new NodeStatementReturn(TokensFrom(startIndex), value); } + if (TryExpectKeyword(Keyword.Let)) + { + var name = ExpectIdent(); + ExpectSymbol(Symbol.Colon); + var type = ParseType(); + ExpectSymbol(Symbol.Equal); + var value = ParseExpression(); + return new NodeStatementVariableDeclaration(TokensFrom(startIndex), name, type, value); + } + var expression = ParseExpression(); var parameters = new List(); @@ -324,6 +334,13 @@ internal class NodeStatementReturn(List tokens, NodeExpression value) : N public readonly NodeExpression Value = value; } +internal class NodeStatementVariableDeclaration(List tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens) +{ + public readonly TokenIdent Name = name; + public readonly NodeType Type = type; + public readonly NodeExpression Value = value; +} + public abstract class NodeExpression(List tokens) : Node(tokens); public sealed class NodeExpressionIntLiteral(List tokens, TokenIntLiteral value) : NodeExpression(tokens) diff --git a/compiler/Compiler/Program.cs b/compiler/Compiler/Program.cs index 26bfc8d..9e1e27c 100644 --- a/compiler/Compiler/Program.cs +++ b/compiler/Compiler/Program.cs @@ -2,6 +2,7 @@ const string contents = """ func main(): i32 { + let x: i32 = 23 do_something("test") return 69 } @@ -14,4 +15,4 @@ var tokens = Tokenizer.Tokenize(contents); var nodes = Parser.Parse(tokens); var output = Generator.Emit(nodes); -Console.WriteLine(output); \ No newline at end of file +File.WriteAllText("C:/Users/oliste/repos/nub-lang/compiler/Compiler/out.c", output);