From c0767bc3861d3afbb647f54b0a6297de139ce339 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 28 Jun 2023 17:25:28 -0600 Subject: [PATCH] Rearrange control expression parsing --- Parser.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Parser.cs b/Parser.cs index c9f0098..d4e2022 100644 --- a/Parser.cs +++ b/Parser.cs @@ -163,6 +163,25 @@ class Parser 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() { if (match(TokenType.If))