for in syntax
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -8,6 +8,8 @@ public enum Symbol
|
||||
If,
|
||||
Else,
|
||||
While,
|
||||
For,
|
||||
In,
|
||||
Break,
|
||||
Continue,
|
||||
Return,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user