From 3d095ec6489dc2ba52c6c65b29f0bde2b90dbe39 Mon Sep 17 00:00:00 2001 From: nub31 Date: Tue, 27 May 2025 15:21:41 +0200 Subject: [PATCH] Allow for short variable assignment --- example/program.nub | 19 +++++++++++++------ src/lang/Nub.Lang/Frontend/Parsing/Parser.cs | 14 +++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/example/program.nub b/example/program.nub index c64628e..be5459c 100644 --- a/example/program.nub +++ b/example/program.nub @@ -3,15 +3,22 @@ namespace main /// # Documentation /// ## Documentation subtitle export func main(args: []string) { - // let i: i64 + let i: i64 - // c::printf("%d\n", args.count) + c::printf("%d\n", args.count) - // while i < args.count { - // c::printf("%s\n", args[i]) + while i < args.count { + c::printf("%s\n", args[i]) - // i = i + 1 - // } + i === 1 + } let arr = new [10]i64 + i = 0 + + while i < arr.count { + c::printf("%d\n", arr[i]) + + i === 1 + } } \ No newline at end of file diff --git a/src/lang/Nub.Lang/Frontend/Parsing/Parser.cs b/src/lang/Nub.Lang/Frontend/Parsing/Parser.cs index cc6e4e9..5f2fb80 100644 --- a/src/lang/Nub.Lang/Frontend/Parsing/Parser.cs +++ b/src/lang/Nub.Lang/Frontend/Parsing/Parser.cs @@ -216,6 +216,17 @@ public class Parser private StatementNode ParseStatementIdentifier(int startIndex, IdentifierToken identifier) { var symbol = ExpectSymbol(); + + if (TryGetBinaryOperator(symbol.Symbol, out var binaryOperator) && Peek().TryGetValue(out var next) && next is SymbolToken { Symbol: Symbol.Assign }) + { + Next(); + var left = new IdentifierNode(GetTokensForNode(startIndex), identifier.Value); + var right = ParseExpression(); + var binOp = new BinaryExpressionNode(GetTokensForNode(startIndex), left, binaryOperator.Value, right); + + return new VariableAssignmentNode(GetTokensForNode(startIndex), identifier.Value, binOp); + } + switch (symbol.Symbol) { case Symbol.DoubleColon: @@ -286,7 +297,7 @@ public class Parser { value = ParseExpression(); } - + return new VariableDeclarationNode(GetTokensForNode(startIndex), name, type, value); } @@ -643,6 +654,7 @@ public class Parser { @namespace = ExpectIdentifier().Value; } + return new NubStructType(@namespace, name); } }