Rearrange control expression parsing

This commit is contained in:
Brandon Dyck 2023-06-28 17:25:28 -06:00
parent 84a888738a
commit c0767bc386

View File

@ -163,6 +163,25 @@ class Parser
return ifExpr(); return ifExpr();
} }
private Expr control()
{
switch (peek().type)
{
case TokenType.If:
advance();
Expr condition = expression();
consume(TokenType.Then, "Expect 'then' after condition.");
Expr thenCase = expression();
consume(TokenType.Else, "Expect 'else' after 'then' case.");
Expr elseCase = expression();
return new If { Condition = condition, Then = thenCase, Else = elseCase };
case TokenType.When:
throw new NotImplementedException();
}
return primary();
}
private Expr ifExpr() private Expr ifExpr()
{ {
if (match(TokenType.If)) if (match(TokenType.If))