Added more enter/leave debug messages

expression_parsing.sync-conflict-20210316-090018-O3W7KWN
Tristan B. V. Kildaire 2021-03-05 12:35:58 +02:00
parent 2b7dd2fce3
commit 6267ba1505
1 changed files with 80 additions and 49 deletions

View File

@ -16,6 +16,10 @@ public final class Parser
private Token currentToken; private Token currentToken;
private ulong tokenPtr; private ulong tokenPtr;
/**
* Crashes the program if the given token is not a symbol
* the same as the givne expected one
*/
public static void expect(SymbolType symbol, Token token) public static void expect(SymbolType symbol, Token token)
{ {
/* TODO: Do checking here to see if token is a type of given symbol */ /* TODO: Do checking here to see if token is a type of given symbol */
@ -30,12 +34,18 @@ public final class Parser
} }
} }
/**
* Crashes the parser with the given message
*/
public static void expect(string message) public static void expect(string message)
{ {
gprintln(message, DebugType.ERROR); gprintln(message, DebugType.ERROR);
exit(0); exit(0); /* TODO: Exit code */ /* TODO: Version that returns or asserts for unit tests */
} }
/**
* Costructs a new parser with the given set of Tokens
*/
this(Token[] tokens) this(Token[] tokens)
{ {
this.tokens = tokens; this.tokens = tokens;
@ -64,6 +74,11 @@ public final class Parser
return tokens[tokenPtr]; return tokens[tokenPtr];
} }
/**
* Parses if statements
*
* TODO: Check kanban
*/
private void parseIf() private void parseIf()
{ {
gprintln("parseIf(): Enter", DebugType.WARNING); gprintln("parseIf(): Enter", DebugType.WARNING);
@ -118,8 +133,6 @@ public final class Parser
gprintln("parseWhile(): Leave", DebugType.WARNING); gprintln("parseWhile(): Leave", DebugType.WARNING);
} }
private void parseBody() private void parseBody()
{ {
gprintln("parseBody(): Enter", DebugType.WARNING); gprintln("parseBody(): Enter", DebugType.WARNING);
@ -127,7 +140,6 @@ public final class Parser
/* TODO: Implement body parsing */ /* TODO: Implement body parsing */
nextToken(); nextToken();
/** /**
* If we were able to get a closing token, `}`, then * If we were able to get a closing token, `}`, then
* this will be set to true, else it will be false by * this will be set to true, else it will be false by
@ -136,16 +148,13 @@ public final class Parser
*/ */
bool closedBeforeExit; bool closedBeforeExit;
while (hasTokens())
while(hasTokens())
{ {
/* Get the token */ /* Get the token */
Token tok = getCurrentToken(); Token tok = getCurrentToken();
SymbolType symbol = getSymbolType(tok); SymbolType symbol = getSymbolType(tok);
gprintln("parseBody(): SymbolType=" ~ to!(string)(symbol));
gprintln("parseBody(): SymbolType="~to!(string)(symbol));
/* If it is a type */ /* If it is a type */
if (symbol == SymbolType.TYPE) if (symbol == SymbolType.TYPE)
@ -154,22 +163,22 @@ public final class Parser
parseTypedDeclaration(); parseTypedDeclaration();
} }
/* If it is a branch */ /* If it is a branch */
else if(symbol == SymbolType.IF) else if (symbol == SymbolType.IF)
{ {
parseIf(); parseIf();
} }
/* If it is a while loop */ /* If it is a while loop */
else if(symbol == SymbolType.WHILE) else if (symbol == SymbolType.WHILE)
{ {
parseWhile(); parseWhile();
} }
/* If it is a function call */ /* If it is a function call */
else if(symbol == SymbolType.IDENTIFIER) else if (symbol == SymbolType.IDENTIFIER)
{ {
parseFuncCall(); parseFuncCall();
} }
/* If it is closing the body `}` */ /* If it is closing the body `}` */
else if(symbol == SymbolType.CCURLY) else if (symbol == SymbolType.CCURLY)
{ {
// gprintln("Error"); // gprintln("Error");
// nextToken(); // nextToken();
@ -179,19 +188,20 @@ public final class Parser
break; break;
} }
/* If it is a class definition */ /* If it is a class definition */
else if(symbol == SymbolType.CLASS) else if (symbol == SymbolType.CLASS)
{ {
parseClass(); parseClass();
} }
/* Error out */ /* Error out */
else else
{ {
gprintln("parseBody(): Unknown symbol: "~getCurrentToken().getToken(), DebugType.ERROR); gprintln("parseBody(): Unknown symbol: " ~ getCurrentToken()
.getToken(), DebugType.ERROR);
} }
} }
/* TODO: We can sometimes run out of tokens before getting our closing brace, we should fix that here */ /* TODO: We can sometimes run out of tokens before getting our closing brace, we should fix that here */
if(!closedBeforeExit) if (!closedBeforeExit)
{ {
expect("Expected closing } but ran out of tokens"); expect("Expected closing } but ran out of tokens");
} }
@ -201,6 +211,8 @@ public final class Parser
private void parseFuncDef() private void parseFuncDef()
{ {
gprintln("parseFuncDef(): Enter", DebugType.WARNING);
/* TODO: Implement function parsing */ /* TODO: Implement function parsing */
nextToken(); nextToken();
@ -208,7 +220,7 @@ public final class Parser
ulong parameterCount; ulong parameterCount;
/* Get command-line arguments */ /* Get command-line arguments */
while(hasTokens()) while (hasTokens())
{ {
/* Expect a type */ /* Expect a type */
string type = getCurrentToken().getToken(); string type = getCurrentToken().getToken();
@ -223,7 +235,7 @@ public final class Parser
parameterCount++; parameterCount++;
/* Check if RBRACE/ `)` */ /* Check if RBRACE/ `)` */
if(getSymbolType(getCurrentToken()) == SymbolType.RBRACE) if (getSymbolType(getCurrentToken()) == SymbolType.RBRACE)
{ {
nextToken(); nextToken();
expect(SymbolType.OCURLY, getCurrentToken()); expect(SymbolType.OCURLY, getCurrentToken());
@ -232,19 +244,22 @@ public final class Parser
parseBody(); parseBody();
nextToken(); nextToken();
} }
else if(getSymbolType(getCurrentToken()) == SymbolType.COMMA) else if (getSymbolType(getCurrentToken()) == SymbolType.COMMA)
{ {
nextToken(); nextToken();
} }
else else
{ {
/* TODO: Error */ /* TODO: Error */
expect("Expecting either ) or , but got "~getCurrentToken().getToken()); expect("Expecting either ) or , but got " ~ getCurrentToken().getToken());
} }
gprintln("ParseFuncDef: ParameterDec: (Type: "~type~", Identifier: "~identifier~")",DebugType.WARNING); gprintln("ParseFuncDef: ParameterDec: (Type: " ~ type ~ ", Identifier: " ~ identifier ~ ")",
gprintln("ParseFuncDef: Parameter count: "~to!(string)(parameterCount)); DebugType.WARNING);
gprintln("ParseFuncDef: Parameter count: " ~ to!(string)(parameterCount));
} }
gprintln("parseFuncDef(): Leave", DebugType.WARNING);
} }
/** /**
@ -262,18 +277,20 @@ public final class Parser
*/ */
private void parseExpression() private void parseExpression()
{ {
gprintln("parseExpression(): Enter", DebugType.WARNING);
/* TODO: Implement expression parsing */ /* TODO: Implement expression parsing */
SymbolType symbol = getSymbolType(getCurrentToken()); SymbolType symbol = getSymbolType(getCurrentToken());
/* If it is a number literal */ /* If it is a number literal */
if(symbol == SymbolType.NUMBER_LITERAL) if (symbol == SymbolType.NUMBER_LITERAL)
{ {
/* Get the next token */ /* Get the next token */
nextToken(); nextToken();
/* Check if the token is a mathematical operator */ /* Check if the token is a mathematical operator */
if(isMathOp(getCurrentToken())) if (isMathOp(getCurrentToken()))
{ {
/* TODO:check math op */ /* TODO:check math op */
nextToken(); nextToken();
@ -287,20 +304,20 @@ public final class Parser
} }
} }
/* If it is a string literal */ /* If it is a string literal */
else if(symbol == SymbolType.STRING_LITERAL) else if (symbol == SymbolType.STRING_LITERAL)
{ {
/* Get the next token */ /* Get the next token */
nextToken(); nextToken();
} }
/* If it is an identifier */ /* If it is an identifier */
else if(symbol == SymbolType.IDENTIFIER) else if (symbol == SymbolType.IDENTIFIER)
{ {
string identifier = getCurrentToken().getToken(); string identifier = getCurrentToken().getToken();
nextToken(); nextToken();
/* If the symbol is `(` then function call */ /* If the symbol is `(` then function call */
if(getSymbolType(getCurrentToken()) == SymbolType.LBRACE) if (getSymbolType(getCurrentToken()) == SymbolType.LBRACE)
{ {
/* TODO: Implement function call parsing */ /* TODO: Implement function call parsing */
} }
@ -316,41 +333,42 @@ public final class Parser
/* TODO: Something isn't right here */ /* TODO: Something isn't right here */
} }
gprintln("ParseExpression: Finished", DebugType.WARNING); gprintln("parseExpression(): Leave", DebugType.WARNING);
} }
private void parseTypedDeclaration() private void parseTypedDeclaration()
{ {
gprintln("parseTypedDeclaration(): Enter", DebugType.WARNING);
/* TODO: Save type */ /* TODO: Save type */
string type = getCurrentToken().getToken(); string type = getCurrentToken().getToken();
string identifier; string identifier;
/* Expect an identifier */ /* Expect an identifier */
nextToken(); nextToken();
expect(SymbolType.IDENTIFIER, getCurrentToken()); expect(SymbolType.IDENTIFIER, getCurrentToken());
identifier = getCurrentToken().getToken(); identifier = getCurrentToken().getToken();
nextToken(); nextToken();
gprintln("ParseTypedDec: DecisionBtwn FuncDef/VarDef: "~getCurrentToken().getToken()); gprintln("ParseTypedDec: DecisionBtwn FuncDef/VarDef: " ~ getCurrentToken().getToken());
/* Check if it is `(` (func dec) */ /* Check if it is `(` (func dec) */
SymbolType symbolType = getSymbolType(getCurrentToken()); SymbolType symbolType = getSymbolType(getCurrentToken());
gprintln("ParseTypedDec: SymbolType="~to!(string)(symbolType)); gprintln("ParseTypedDec: SymbolType=" ~ to!(string)(symbolType));
if(symbolType == SymbolType.LBRACE) if (symbolType == SymbolType.LBRACE)
{ {
parseFuncDef(); parseFuncDef();
} }
/* Check for semi-colon (var dec) */ /* Check for semi-colon (var dec) */
else if(symbolType == SymbolType.SEMICOLON) else if (symbolType == SymbolType.SEMICOLON)
{ {
nextToken(); nextToken();
gprintln("ParseTypedDec: VariableDeclaration: (Type: "~type~", Identifier: "~identifier~")", DebugType.WARNING); gprintln("ParseTypedDec: VariableDeclaration: (Type: " ~ type ~ ", Identifier: " ~ identifier ~ ")",
DebugType.WARNING);
} }
/* Check for `=` (var dec) */ /* Check for `=` (var dec) */
else if(symbolType == SymbolType.ASSIGN) else if (symbolType == SymbolType.ASSIGN)
{ {
nextToken(); nextToken();
@ -365,7 +383,8 @@ public final class Parser
nextToken(); nextToken();
gprintln("ParseTypedDec: VariableDeclarationWithAssingment: (Type: "~type~", Identifier: "~identifier~")", DebugType.WARNING); gprintln("ParseTypedDec: VariableDeclarationWithAssingment: (Type: "
~ type ~ ", Identifier: " ~ identifier ~ ")", DebugType.WARNING);
} }
else else
{ {
@ -374,7 +393,7 @@ public final class Parser
/* TODO: If we outta tokens we should not call this */ /* TODO: If we outta tokens we should not call this */
// gprintln(getCurrentToken()); // gprintln(getCurrentToken());
gprintln("ParseTypedDec: Je suis fini"); gprintln("parseTypedDeclaration(): Leave", DebugType.WARNING);
} }
/** /**
@ -385,13 +404,15 @@ public final class Parser
*/ */
private void parseClass() private void parseClass()
{ {
gprintln("parseClass(): Enter", DebugType.WARNING);
/* Pop off the `class` */ /* Pop off the `class` */
nextToken(); nextToken();
/* Get the class's name */ /* Get the class's name */
expect(SymbolType.IDENTIFIER, getCurrentToken()); expect(SymbolType.IDENTIFIER, getCurrentToken());
string className = getCurrentToken().getToken(); string className = getCurrentToken().getToken();
gprintln("parseClass(): Class name found '"~className~"'"); gprintln("parseClass(): Class name found '" ~ className ~ "'");
/* Expect a `{` */ /* Expect a `{` */
nextToken(); nextToken();
@ -402,10 +423,14 @@ public final class Parser
/* Pop off the ending `}` */ /* Pop off the ending `}` */
nextToken(); nextToken();
gprintln("parseClass(): Leave", DebugType.WARNING);
} }
private void parseStatement() private void parseStatement()
{ {
gprintln("parseStatement(): Enter", DebugType.WARNING);
/* TODO: Implement parse statement */ /* TODO: Implement parse statement */
/** /**
@ -415,15 +440,16 @@ public final class Parser
* 1. parseFuncCall() * 1. parseFuncCall()
* 2. expect(;) * 2. expect(;)
*/ */
gprintln("parseStatement(): Leave", DebugType.WARNING);
} }
private void parseFuncCall() private void parseFuncCall()
{ {
gprintln("parseFuncCall(): Doing function call parsing"); gprintln("parseFuncCall(): Enter", DebugType.WARNING);
nextToken(); nextToken();
/* Expect an opening brace `(` */ /* Expect an opening brace `(` */
expect(SymbolType.LBRACE, getCurrentToken()); expect(SymbolType.LBRACE, getCurrentToken());
nextToken(); nextToken();
@ -437,6 +463,7 @@ public final class Parser
expect(SymbolType.SEMICOLON, getCurrentToken()); expect(SymbolType.SEMICOLON, getCurrentToken());
nextToken(); nextToken();
gprintln("parseFuncCall(): Leave", DebugType.WARNING);
} }
/* Almost like parseBody but has more */ /* Almost like parseBody but has more */
@ -448,6 +475,8 @@ public final class Parser
/* TODO: Variables should be allowed to have letters in them and underscores */ /* TODO: Variables should be allowed to have letters in them and underscores */
public void parse() public void parse()
{ {
gprintln("parse(): Enter", DebugType.WARNING);
/* TODO: Do parsing here */ /* TODO: Do parsing here */
/* We can have an import or vardef or funcdef */ /* We can have an import or vardef or funcdef */
@ -457,7 +486,7 @@ public final class Parser
Token tok = getCurrentToken(); Token tok = getCurrentToken();
SymbolType symbol = getSymbolType(tok); SymbolType symbol = getSymbolType(tok);
gprintln("parse(): Token: "~tok.getToken()); gprintln("parse(): Token: " ~ tok.getToken());
/* If it is a type */ /* If it is a type */
if (symbol == SymbolType.TYPE) if (symbol == SymbolType.TYPE)
@ -465,18 +494,20 @@ public final class Parser
/* Might be a function, might be a variable */ /* Might be a function, might be a variable */
parseTypedDeclaration(); parseTypedDeclaration();
gprintln("parse()::woah: Current token: "~tok.getToken()); gprintln("parse()::woah: Current token: " ~ tok.getToken());
} }
/* If it is a class */ /* If it is a class */
else if(symbol == SymbolType.CLASS) else if (symbol == SymbolType.CLASS)
{ {
parseClass(); parseClass();
} }
else else
{ {
expect("parse(): Unknown '"~tok.getToken()~"'"); expect("parse(): Unknown '" ~ tok.getToken() ~ "'");
} }
} }
gprintln("parse(): Leave", DebugType.WARNING);
} }
} }