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,17 +148,14 @@ 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)
{ {
@ -186,7 +195,8 @@ public final class Parser
/* Error out */ /* Error out */
else else
{ {
gprintln("parseBody(): Unknown symbol: "~getCurrentToken().getToken(), DebugType.ERROR); gprintln("parseBody(): Unknown symbol: " ~ getCurrentToken()
.getToken(), DebugType.ERROR);
} }
} }
@ -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();
@ -242,9 +254,12 @@ public final class Parser
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 ~ ")",
DebugType.WARNING);
gprintln("ParseFuncDef: Parameter count: " ~ to!(string)(parameterCount)); gprintln("ParseFuncDef: Parameter count: " ~ to!(string)(parameterCount));
} }
gprintln("parseFuncDef(): Leave", DebugType.WARNING);
} }
/** /**
@ -262,6 +277,8 @@ 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());
@ -316,22 +333,22 @@ 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());
@ -347,7 +364,8 @@ public final class Parser
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)
@ -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,6 +404,8 @@ 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();
@ -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 */
@ -477,6 +506,8 @@ public final class Parser
expect("parse(): Unknown '" ~ tok.getToken() ~ "'"); expect("parse(): Unknown '" ~ tok.getToken() ~ "'");
} }
} }
gprintln("parse(): Leave", DebugType.WARNING);
} }
} }