This commit is contained in:
nub31
2025-09-16 16:02:08 +02:00
parent ab53073273
commit 826f8ffad3
8 changed files with 130 additions and 36 deletions

View File

@@ -156,7 +156,6 @@ public sealed class Parser
BlockSyntax? body = null;
if (CurrentToken is SymbolToken { Symbol: Symbol.OpenBrace })
{
Next();
body = ParseBlock();
}
@@ -216,6 +215,8 @@ public sealed class Parser
return ParseIf();
case Symbol.While:
return ParseWhile();
case Symbol.For:
return ParseFor();
case Symbol.Let:
return ParseVariableDeclaration();
case Symbol.Break:
@@ -319,6 +320,25 @@ public sealed class Parser
return new WhileSyntax(GetTokens(startIndex), condition, body);
}
private ForSyntax ParseFor()
{
var startIndex = _tokenIndex;
ExpectSymbol(Symbol.For);
var elementIdent = ExpectIdentifier().Value;
string? indexIndent = null;
if (TryExpectSymbol(Symbol.Comma))
{
indexIndent = ExpectIdentifier().Value;
}
ExpectSymbol(Symbol.In);
var target = ParseExpression();
var body = ParseBlock();
return new ForSyntax(GetTokens(startIndex), elementIdent, indexIndent, target, body);
}
private ExpressionSyntax ParseExpression(int precedence = 0)
{
var startIndex = _tokenIndex;
@@ -595,6 +615,7 @@ public sealed class Parser
{
var startIndex = _tokenIndex;
List<StatementSyntax> statements = [];
ExpectSymbol(Symbol.OpenBrace);
while (!TryExpectSymbol(Symbol.CloseBrace))
{
try