Exit after else

if_statement_parsing
Tristan B. V. Kildaire 2021-03-20 18:36:25 +02:00
parent 6edae88d76
commit bbe52bb287
2 changed files with 8 additions and 53 deletions

View File

@ -96,20 +96,9 @@ public final class Parser
ulong i = 0; ulong i = 0;
while (hasTokens()) while (hasTokens())
{ {
i++; /* This will only be called once (it is what caused a call to parseIf()) */
import std.stdio; if (getSymbolType(getCurrentToken()) == SymbolType.IF)
writeln("Token: "~getCurrentToken().getToken());
writeln("Current parseIf while: "~to!(string)(i));
writeln("Bababooey: "~to!(string)(getSymbolType(getCurrentToken()) == SymbolType.ELSE && hasIf));
// /* If else has been reached then exit */
// if(reachedElse)
// {
// break;
// }
/* TODO: Add tracking to not allow startign with `else` with no initial `if` */
if (getSymbolType(getCurrentToken()) == SymbolType.IF && !hasIf)
{ {
/* Pop off the `if` */ /* Pop off the `if` */
nextToken(); nextToken();
@ -130,11 +119,8 @@ public final class Parser
parseBody(); parseBody();
expect(SymbolType.CCURLY, getCurrentToken()); expect(SymbolType.CCURLY, getCurrentToken());
nextToken(); nextToken();
writeln("American boy: "~getCurrentToken().getToken());
hasIf = true;
} }
/* If we get an else as the next symbol */
else if (getSymbolType(getCurrentToken()) == SymbolType.ELSE) else if (getSymbolType(getCurrentToken()) == SymbolType.ELSE)
{ {
/* Pop off the `else` */ /* Pop off the `else` */
@ -171,17 +157,15 @@ public final class Parser
expect(SymbolType.CCURLY, getCurrentToken()); expect(SymbolType.CCURLY, getCurrentToken());
nextToken(); nextToken();
/* Don't allow anything to follow after this */ /* Exit, this is the end of the if statement as an else is reached */
reachedElse = true; break;
//break;
} }
/* Error out for unexpected symbol or invalid reachedElse */ /* Error out for unexpected symbol or invalid reachedElse */
else else
{ {
/* TODO: Add error */ /* TODO: Add error */
/* TODO: DO we need this here? */ /* TODO: DO we need this here? */
expect("fok"); expect("Expected either if (for else if) or { for (else)");
} }
} }

View File

@ -29,38 +29,9 @@ class clazz_2_1
else else
{ {
print("Bhjkfd");
} }
else
{
}
if(1)
{
print(1);
}
else
{
}
if(1)
{
print(1);
}
else if(1)
{
if(1)
{
}
}
else
{
}
} }