for in syntax

This commit is contained in:
nub31
2025-10-22 18:32:45 +02:00
parent 0f96e83e44
commit fd1f4a0130
8 changed files with 172 additions and 25 deletions

View File

@@ -241,6 +241,8 @@ public sealed class Parser
return ParseIf(startIndex);
case Symbol.While:
return ParseWhile(startIndex);
case Symbol.For:
return ParseFor(startIndex);
case Symbol.Let:
return ParseVariableDeclaration(startIndex);
case Symbol.Defer:
@@ -330,6 +332,23 @@ public sealed class Parser
return new WhileSyntax(GetTokens(startIndex), condition, body);
}
private ForSyntax ParseFor(int startIndex)
{
var itemName = ExpectIdentifier().Value;
string? indexName = null;
if (TryExpectSymbol(Symbol.Comma))
{
indexName = ExpectIdentifier().Value;
}
ExpectSymbol(Symbol.In);
var target = ParseExpression();
var body = ParseBlock();
return new ForSyntax(GetTokens(startIndex), itemName, indexName, target, body);
}
private ExpressionSyntax ParseExpression(int precedence = 0)
{
var startIndex = _tokenIndex;

View File

@@ -74,6 +74,8 @@ public record DeferSyntax(List<Token> Tokens, StatementSyntax Statement) : State
public record WhileSyntax(List<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body) : StatementSyntax(Tokens);
public record ForSyntax(List<Token> Tokens, string ElementName, string? IndexName, ExpressionSyntax Target, BlockSyntax Body) : StatementSyntax(Tokens);
#endregion
#region Expressions

View File

@@ -8,6 +8,8 @@ public enum Symbol
If,
Else,
While,
For,
In,
Break,
Continue,
Return,

View File

@@ -10,6 +10,8 @@ public sealed class Tokenizer
["if"] = Symbol.If,
["else"] = Symbol.Else,
["while"] = Symbol.While,
["for"] = Symbol.For,
["in"] = Symbol.In,
["break"] = Symbol.Break,
["continue"] = Symbol.Continue,
["return"] = Symbol.Return,