From 808c12472465fe49a50ba430c8b8d2b3794b3d20 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Thu, 18 Mar 2021 21:06:10 +0200 Subject: [PATCH] WIP: If statements Something is consuming too much if statement wise (only when `else` is present though) --- source/tlang/compiler/parser.d | 20 ++++++++++++++++---- source/tlang/testing/basic1.t | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/source/tlang/compiler/parser.d b/source/tlang/compiler/parser.d index 7a165dc8..08ca07a8 100644 --- a/source/tlang/compiler/parser.d +++ b/source/tlang/compiler/parser.d @@ -131,9 +131,12 @@ public final class Parser /* Pop off the `else` */ nextToken(); - /* Check if we have an `if` after the `else` (so an "else if" statement) */ + /* Check if we have an `if` after the `{` (so an "else if" statement) */ if (getSymbolType(getCurrentToken()) == SymbolType.IF && !reachedElse) { + /* Pop off the `if` */ + nextToken(); + /* Expect an opening brace `(` */ expect(SymbolType.LBRACE, getCurrentToken()); nextToken(); @@ -151,20 +154,26 @@ public final class Parser expect(SymbolType.CCURLY, getCurrentToken()); nextToken(); } - /* Check for opening brace (just an "else" statement) */ - else if (getSymbolType(getCurrentToken()) == SymbolType.ELSE) + /* Check for opening curly (just an "else" statement) */ + else if (getSymbolType(getCurrentToken()) == SymbolType.OCURLY) { /* TODO: Implement me */ + import std.stdio; + writeln("EYO ELSE"); + /* Opening { */ + writeln("Bruh"~to!(string)(getCurrentToken())); nextToken(); - expect(SymbolType.OCURLY, getCurrentToken()); /* Parse the if' statement's body AND expect a closing curly */ parseBody(); expect(SymbolType.CCURLY, getCurrentToken()); nextToken(); + writeln("EYO ELSE (END):"); + writeln("Bruh"~to!(string)(getCurrentToken())); + /* Don't allow anything to follow after this */ reachedElse = true; } @@ -173,6 +182,7 @@ public final class Parser { /* TODO: Add error */ /* TODO: DO we need this here? */ + expect("fok"); } } @@ -191,6 +201,8 @@ public final class Parser else { /* TODO: Add error */ + import std.stdio; + writeln("Bruh"~to!(string)(getCurrentToken())); break; } } diff --git a/source/tlang/testing/basic1.t b/source/tlang/testing/basic1.t index b01b3b67..c5257c09 100644 --- a/source/tlang/testing/basic1.t +++ b/source/tlang/testing/basic1.t @@ -29,7 +29,7 @@ class clazz_2_1 if(1) { - print(1); + } else { @@ -38,7 +38,7 @@ class clazz_2_1 if(1) { - + print(1); } else {