Allow for short variable assignment

This commit is contained in:
nub31
2025-05-27 15:21:41 +02:00
parent ce6b59a4b3
commit 3d095ec648
2 changed files with 26 additions and 7 deletions

View File

@@ -3,15 +3,22 @@ namespace main
/// # Documentation /// # Documentation
/// ## Documentation subtitle /// ## Documentation subtitle
export func main(args: []string) { 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 { while i < args.count {
// c::printf("%s\n", args[i]) c::printf("%s\n", args[i])
// i = i + 1 i === 1
// } }
let arr = new [10]i64 let arr = new [10]i64
i = 0
while i < arr.count {
c::printf("%d\n", arr[i])
i === 1
}
} }

View File

@@ -216,6 +216,17 @@ public class Parser
private StatementNode ParseStatementIdentifier(int startIndex, IdentifierToken identifier) private StatementNode ParseStatementIdentifier(int startIndex, IdentifierToken identifier)
{ {
var symbol = ExpectSymbol(); 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) switch (symbol.Symbol)
{ {
case Symbol.DoubleColon: case Symbol.DoubleColon:
@@ -286,7 +297,7 @@ public class Parser
{ {
value = ParseExpression(); value = ParseExpression();
} }
return new VariableDeclarationNode(GetTokensForNode(startIndex), name, type, value); return new VariableDeclarationNode(GetTokensForNode(startIndex), name, type, value);
} }
@@ -643,6 +654,7 @@ public class Parser
{ {
@namespace = ExpectIdentifier().Value; @namespace = ExpectIdentifier().Value;
} }
return new NubStructType(@namespace, name); return new NubStructType(@namespace, name);
} }
} }