Added support for while loops

This commit is contained in:
Tristan B. Velloza Kildaire 2021-03-03 20:50:06 +02:00
parent 302017e051
commit f679a6dd83
3 changed files with 44 additions and 2 deletions

View File

@ -83,6 +83,30 @@ public final class Parser
gprintln("parseIf(): PARSING OF IF STTAMENT BODY DONE");
}
private void parseWhile()
{
/* Expect an opening brace `(` */
expect(SymbolType.LBRACE, getCurrentToken());
nextToken();
/* Parse an expression (for the condition) */
parseExpression();
expect(SymbolType.RBRACE, getCurrentToken());
/* Openening { */
nextToken();
expect(SymbolType.OCURLY, getCurrentToken());
/* Parse the while' statement's body AND expect a closing curly */
parseBody();
expect(SymbolType.CCURLY, getCurrentToken());
nextToken();
gprintln("parseWhile(): PARSING OF WHILE STTAMENT BODY DONE");
}
private void parseBody()
{
/* TODO: Implement body parsing */
@ -120,6 +144,12 @@ public final class Parser
nextToken();
parseIf();
}
/* If it is a while loop */
else if(symbol == SymbolType.WHILE)
{
nextToken();
parseWhile();
}
/* If it is a function call */
else if(symbol == SymbolType.IDENTIFIER)
{

View File

@ -24,6 +24,7 @@ public enum SymbolType
OCURLY,
CCURLY,
IF,
WHILE,
UNKNOWN
}
@ -101,6 +102,11 @@ public static SymbolType getSymbolType(Token tokenIn)
{
return SymbolType.IF;
}
/* `while` */
else if(cmp(token, "while") == 0)
{
return SymbolType.WHILE;
}
/* Identifier check (TODO: Track vars) */
else if (isAlpha(token))
{

View File

@ -23,9 +23,15 @@ void main(int hello, byte d)
print(1+1);
}
if(2)
if(2+222222/2)
{
while(1)
{
while(2)
{
}
}
}
}