2021-03-30 17:02:13 +01:00
|
|
|
module compiler.parsing.core;
|
2021-03-03 09:08:34 +00:00
|
|
|
|
|
|
|
import gogga;
|
|
|
|
import std.conv : to;
|
2021-03-03 12:04:26 +00:00
|
|
|
import std.string : isNumeric, cmp;
|
2021-03-30 16:35:16 +01:00
|
|
|
import compiler.symbols.check;
|
|
|
|
import compiler.symbols.data;
|
2021-03-03 11:30:56 +00:00
|
|
|
import compiler.lexer : Token;
|
2021-03-03 13:32:28 +00:00
|
|
|
import core.stdc.stdlib;
|
2021-03-17 08:05:56 +00:00
|
|
|
import misc.exceptions : TError;
|
|
|
|
|
|
|
|
// public final class ParserError : TError
|
|
|
|
// {
|
|
|
|
|
|
|
|
// }
|
2021-03-03 09:08:34 +00:00
|
|
|
|
2021-03-21 11:01:09 +00:00
|
|
|
|
|
|
|
|
2021-03-21 12:23:18 +00:00
|
|
|
bool isUnitTest;
|
|
|
|
|
2021-03-03 09:08:34 +00:00
|
|
|
public final class Parser
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Tokens management
|
|
|
|
*/
|
2021-03-03 11:30:56 +00:00
|
|
|
private Token[] tokens;
|
|
|
|
private Token currentToken;
|
2021-03-03 09:08:34 +00:00
|
|
|
private ulong tokenPtr;
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
/**
|
|
|
|
* Crashes the program if the given token is not a symbol
|
|
|
|
* the same as the givne expected one
|
|
|
|
*/
|
2021-03-03 12:04:26 +00:00
|
|
|
public static void expect(SymbolType symbol, Token token)
|
2021-03-03 09:08:34 +00:00
|
|
|
{
|
|
|
|
/* TODO: Do checking here to see if token is a type of given symbol */
|
2021-03-03 11:30:56 +00:00
|
|
|
SymbolType actualType = getSymbolType(token);
|
2021-03-03 12:04:26 +00:00
|
|
|
bool isFine = actualType == symbol;
|
2021-03-03 09:08:34 +00:00
|
|
|
|
|
|
|
/* TODO: Crash program if not */
|
2021-03-03 12:42:57 +00:00
|
|
|
if (!isFine)
|
2021-03-03 09:08:34 +00:00
|
|
|
{
|
2021-03-05 10:01:57 +00:00
|
|
|
expect("Expected symbol of type " ~ to!(string)(symbol) ~ " but got " ~ to!(
|
|
|
|
string)(actualType) ~ " with " ~ token.toString());
|
2021-03-03 09:08:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
/**
|
|
|
|
* Crashes the parser with the given message
|
|
|
|
*/
|
2021-03-05 10:01:57 +00:00
|
|
|
public static void expect(string message)
|
|
|
|
{
|
2021-03-17 08:33:22 +00:00
|
|
|
//throw new TError(message);
|
|
|
|
gprintln(message, DebugType.ERROR);
|
2021-03-21 12:23:18 +00:00
|
|
|
|
|
|
|
if(isUnitTest)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exit(0); /* TODO: Exit code */ /* TODO: Version that returns or asserts for unit tests */
|
|
|
|
}
|
2021-03-05 10:01:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
/**
|
|
|
|
* Costructs a new parser with the given set of Tokens
|
|
|
|
*/
|
2021-03-03 11:30:56 +00:00
|
|
|
this(Token[] tokens)
|
2021-03-03 09:08:34 +00:00
|
|
|
{
|
|
|
|
this.tokens = tokens;
|
|
|
|
currentToken = tokens[0];
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:30:56 +00:00
|
|
|
/**
|
|
|
|
* Moves the token pointer to the next token
|
|
|
|
*
|
|
|
|
* Returns true if successful, false otherwise
|
|
|
|
* (if we have exhausted the tokens source)
|
|
|
|
*/
|
2021-03-03 14:30:56 +00:00
|
|
|
private void nextToken()
|
2021-03-03 11:30:56 +00:00
|
|
|
{
|
2021-03-03 14:30:56 +00:00
|
|
|
tokenPtr++;
|
2021-03-03 11:30:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 12:04:26 +00:00
|
|
|
private bool hasTokens()
|
|
|
|
{
|
|
|
|
return tokenPtr < tokens.length;
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:30:56 +00:00
|
|
|
private Token getCurrentToken()
|
|
|
|
{
|
2021-03-03 18:04:54 +00:00
|
|
|
/* TODO: Throw an exception here when we try get more than we can */
|
2021-03-03 12:04:26 +00:00
|
|
|
return tokens[tokenPtr];
|
2021-03-03 11:30:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
/**
|
|
|
|
* Parses if statements
|
|
|
|
*
|
|
|
|
* TODO: Check kanban
|
|
|
|
*/
|
2021-03-03 15:09:08 +00:00
|
|
|
private void parseIf()
|
|
|
|
{
|
2021-03-05 09:58:51 +00:00
|
|
|
gprintln("parseIf(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-18 17:20:17 +00:00
|
|
|
while (hasTokens())
|
2021-03-20 16:36:25 +00:00
|
|
|
{
|
|
|
|
/* This will only be called once (it is what caused a call to parseIf()) */
|
|
|
|
if (getSymbolType(getCurrentToken()) == SymbolType.IF)
|
2021-03-18 17:20:17 +00:00
|
|
|
{
|
|
|
|
/* Pop off the `if` */
|
|
|
|
nextToken();
|
2021-03-03 15:09:08 +00:00
|
|
|
|
2021-03-18 17:20:17 +00:00
|
|
|
/* Expect an opening brace `(` */
|
|
|
|
expect(SymbolType.LBRACE, getCurrentToken());
|
|
|
|
nextToken();
|
2021-03-03 15:35:48 +00:00
|
|
|
|
2021-03-18 17:20:17 +00:00
|
|
|
/* Parse an expression (for the condition) */
|
|
|
|
parseExpression();
|
|
|
|
expect(SymbolType.RBRACE, getCurrentToken());
|
2021-03-03 15:35:48 +00:00
|
|
|
|
2021-03-18 17:20:17 +00:00
|
|
|
/* Opening { */
|
|
|
|
nextToken();
|
|
|
|
expect(SymbolType.OCURLY, getCurrentToken());
|
|
|
|
|
|
|
|
/* Parse the if' statement's body AND expect a closing curly */
|
|
|
|
parseBody();
|
|
|
|
expect(SymbolType.CCURLY, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
}
|
2021-03-20 16:36:25 +00:00
|
|
|
/* If we get an else as the next symbol */
|
2021-03-20 16:31:20 +00:00
|
|
|
else if (getSymbolType(getCurrentToken()) == SymbolType.ELSE)
|
2021-03-18 17:20:17 +00:00
|
|
|
{
|
|
|
|
/* Pop off the `else` */
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-18 19:06:10 +00:00
|
|
|
/* Check if we have an `if` after the `{` (so an "else if" statement) */
|
2021-03-20 16:38:16 +00:00
|
|
|
if (getSymbolType(getCurrentToken()) == SymbolType.IF)
|
2021-03-18 17:20:17 +00:00
|
|
|
{
|
2021-03-18 19:06:10 +00:00
|
|
|
/* Pop off the `if` */
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-18 17:20:17 +00:00
|
|
|
/* Expect an opening brace `(` */
|
|
|
|
expect(SymbolType.LBRACE, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
/* Parse an expression (for the condition) */
|
|
|
|
parseExpression();
|
|
|
|
expect(SymbolType.RBRACE, getCurrentToken());
|
|
|
|
|
|
|
|
/* Opening { */
|
|
|
|
nextToken();
|
|
|
|
expect(SymbolType.OCURLY, getCurrentToken());
|
|
|
|
|
|
|
|
/* Parse the if' statement's body AND expect a closing curly */
|
|
|
|
parseBody();
|
|
|
|
expect(SymbolType.CCURLY, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
}
|
2021-03-18 19:06:10 +00:00
|
|
|
/* Check for opening curly (just an "else" statement) */
|
|
|
|
else if (getSymbolType(getCurrentToken()) == SymbolType.OCURLY)
|
2021-03-18 17:20:17 +00:00
|
|
|
{
|
2021-03-20 16:31:03 +00:00
|
|
|
/* Parse the if' statement's body (starting with `{` AND expect a closing curly */
|
2021-03-18 17:20:17 +00:00
|
|
|
parseBody();
|
|
|
|
expect(SymbolType.CCURLY, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-20 16:36:25 +00:00
|
|
|
/* Exit, this is the end of the if statement as an else is reached */
|
|
|
|
break;
|
2021-03-18 17:20:17 +00:00
|
|
|
}
|
2021-03-20 16:50:48 +00:00
|
|
|
/* Error out if no `{` or `if` */
|
2021-03-18 17:20:17 +00:00
|
|
|
else
|
|
|
|
{
|
2021-03-20 16:36:25 +00:00
|
|
|
expect("Expected either if (for else if) or { for (else)");
|
2021-03-18 17:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 16:41:26 +00:00
|
|
|
/* If we get anything else, then we are done with if statement */
|
2021-03-18 17:20:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 15:35:48 +00:00
|
|
|
|
2021-03-05 09:58:51 +00:00
|
|
|
gprintln("parseIf(): Leave", DebugType.WARNING);
|
2021-03-03 15:09:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 18:50:06 +00:00
|
|
|
private void parseWhile()
|
|
|
|
{
|
2021-03-05 09:58:51 +00:00
|
|
|
gprintln("parseWhile(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-05 09:40:06 +00:00
|
|
|
/* Pop off the `while` */
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-03 18:50:06 +00:00
|
|
|
/* 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();
|
|
|
|
|
2021-03-05 09:58:51 +00:00
|
|
|
gprintln("parseWhile(): Leave", DebugType.WARNING);
|
2021-03-03 18:50:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
private void previousToken()
|
|
|
|
{
|
|
|
|
tokenPtr--;
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:49:02 +01:00
|
|
|
public Assignment parseAssignment()
|
|
|
|
{
|
|
|
|
/* Generated Assignment statement */
|
|
|
|
Assignment assignment;
|
|
|
|
|
|
|
|
/* The identifier being assigned to */
|
|
|
|
string identifier = getCurrentToken().getToken();
|
|
|
|
nextToken();
|
|
|
|
nextToken();
|
2021-04-01 14:59:33 +01:00
|
|
|
gprintln(getCurrentToken());
|
2021-04-01 14:49:02 +01:00
|
|
|
|
|
|
|
/* Expression */
|
|
|
|
Expression assignmentExpression = parseExpression();
|
|
|
|
|
2021-04-01 14:59:33 +01:00
|
|
|
|
2021-04-01 14:49:02 +01:00
|
|
|
assignment = new Assignment(identifier, assignmentExpression);
|
|
|
|
|
|
|
|
/* TODO: Support for (a=1)? */
|
|
|
|
/* Expect a semicolon */
|
|
|
|
expect(SymbolType.SEMICOLON, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
|
|
|
|
return assignment;
|
|
|
|
}
|
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
public Statement parseName()
|
|
|
|
{
|
|
|
|
Statement ret;
|
|
|
|
|
|
|
|
/* Save the name or type */
|
|
|
|
string nameTYpe = getCurrentToken().getToken();
|
|
|
|
|
|
|
|
/* TODO: The problem here is I don't want to progress the token */
|
|
|
|
|
|
|
|
/* Get next token */
|
|
|
|
nextToken();
|
|
|
|
SymbolType type = getSymbolType(getCurrentToken());
|
|
|
|
|
|
|
|
/* If we have `(` then function call */
|
|
|
|
if(type == SymbolType.LBRACE)
|
|
|
|
{
|
|
|
|
/* TODO: Collect and return */
|
|
|
|
previousToken();
|
|
|
|
parseFuncCall();
|
2021-03-28 21:05:58 +01:00
|
|
|
|
|
|
|
/* Expect a semi-colon */
|
|
|
|
expect(SymbolType.SEMICOLON, getCurrentToken());
|
|
|
|
nextToken();
|
2021-03-28 20:54:38 +01:00
|
|
|
}
|
|
|
|
/* If we have an identifier/type then declaration */
|
|
|
|
else if(type == SymbolType.IDENT_TYPE)
|
|
|
|
{
|
|
|
|
previousToken();
|
|
|
|
ret = parseTypedDeclaration();
|
|
|
|
}
|
2021-04-01 14:49:02 +01:00
|
|
|
/* Assignment */
|
|
|
|
else if(type == SymbolType.ASSIGN)
|
|
|
|
{
|
|
|
|
previousToken();
|
|
|
|
ret = parseAssignment();
|
|
|
|
}
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Any other case */
|
|
|
|
else
|
|
|
|
{
|
2021-04-01 14:59:33 +01:00
|
|
|
gprintln(getCurrentToken);
|
2021-03-28 20:54:38 +01:00
|
|
|
expect("Error expected ( for var/func def");
|
2021-04-01 14:59:33 +01:00
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-05-04 17:58:07 +01:00
|
|
|
/* TODO: Implement me, and call me */
|
|
|
|
private Struct parseStruct()
|
|
|
|
{
|
|
|
|
gprintln("parseStruct(): Enter", DebugType.WARNING);
|
|
|
|
|
|
|
|
Struct generatedStruct;
|
|
|
|
|
|
|
|
/* Consume the `struct` that caused `parseStruct` to be called */
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
/* Expect an identifier here (no dot) */
|
|
|
|
string structName = getCurrentToken().getToken();
|
|
|
|
expect(SymbolType.IDENT_TYPE, getCurrentToken());
|
|
|
|
if(!isIdentifier_NoDot(getCurrentToken()))
|
|
|
|
{
|
|
|
|
expect("Identifier (for struct declaration) cannot be dotted");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Consume the name */
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
/* TODO: Here we will do a while loop */
|
2021-05-04 18:07:04 +01:00
|
|
|
expect(SymbolType.OCURLY, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
/* Get current token */
|
|
|
|
SymbolType symbolType = getSymbolType(getCurrentToken());
|
|
|
|
|
|
|
|
/* If closing brace then exit */
|
|
|
|
if(symbolType == SymbolType.CCURLY)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Only allow variables here */
|
|
|
|
/* TODO: Only allowe VariableDeclarations (maybe assignments idk) */
|
|
|
|
/* TODO: Might, do what d does and allow function */
|
|
|
|
/* TODO: Which is just a codegen trick and implicit thing really */
|
|
|
|
/* TODO: I mean isn't OOP too lmao */
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expect closing brace (sanity) */
|
|
|
|
expect(SymbolType.CCURLY, getCurrentToken());
|
2021-05-04 17:58:07 +01:00
|
|
|
|
2021-05-04 18:11:25 +01:00
|
|
|
/* Consume the closing curly brace */
|
|
|
|
nextToken();
|
|
|
|
|
2021-05-04 17:58:07 +01:00
|
|
|
|
|
|
|
gprintln("parseStruct(): Leave", DebugType.WARNING);
|
|
|
|
|
|
|
|
return generatedStruct;
|
|
|
|
}
|
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
private Statement[] parseBody()
|
2021-03-03 14:30:56 +00:00
|
|
|
{
|
2021-03-05 10:00:13 +00:00
|
|
|
gprintln("parseBody(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-03 14:30:56 +00:00
|
|
|
/* TODO: Implement body parsing */
|
2021-03-21 15:57:56 +00:00
|
|
|
Statement[] statements;
|
2021-03-20 19:26:42 +00:00
|
|
|
|
|
|
|
/* Consume the `{` symbol */
|
2021-03-03 14:30:56 +00:00
|
|
|
nextToken();
|
|
|
|
|
2021-03-03 18:16:56 +00:00
|
|
|
/**
|
|
|
|
* If we were able to get a closing token, `}`, then
|
|
|
|
* this will be set to true, else it will be false by
|
|
|
|
* default which implies we ran out of tokens before
|
|
|
|
* we could close te body which is an error we do throw
|
|
|
|
*/
|
2021-03-03 18:04:54 +00:00
|
|
|
bool closedBeforeExit;
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
while (hasTokens())
|
2021-03-03 14:30:56 +00:00
|
|
|
{
|
|
|
|
/* Get the token */
|
|
|
|
Token tok = getCurrentToken();
|
|
|
|
SymbolType symbol = getSymbolType(tok);
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseBody(): SymbolType=" ~ to!(string)(symbol));
|
2021-03-03 15:35:48 +00:00
|
|
|
|
2021-03-03 14:30:56 +00:00
|
|
|
/* If it is a type */
|
2021-03-28 20:54:38 +01:00
|
|
|
if (symbol == SymbolType.IDENT_TYPE)
|
2021-03-03 14:30:56 +00:00
|
|
|
{
|
2021-04-01 14:59:33 +01:00
|
|
|
/* Might be a function, might be a variable, or assignment */
|
2021-03-28 20:54:38 +01:00
|
|
|
statements ~= parseName();
|
2021-03-03 14:30:56 +00:00
|
|
|
}
|
2021-03-21 12:03:13 +00:00
|
|
|
/* If it is an accessor */
|
|
|
|
else if (isAccessor(tok))
|
|
|
|
{
|
2021-03-24 21:17:58 +00:00
|
|
|
statements ~= parseAccessor();
|
2021-03-21 12:03:13 +00:00
|
|
|
}
|
2021-03-03 15:09:08 +00:00
|
|
|
/* If it is a branch */
|
2021-03-05 10:35:58 +00:00
|
|
|
else if (symbol == SymbolType.IF)
|
2021-03-03 15:09:08 +00:00
|
|
|
{
|
|
|
|
parseIf();
|
|
|
|
}
|
2021-03-03 18:50:06 +00:00
|
|
|
/* If it is a while loop */
|
2021-03-05 10:35:58 +00:00
|
|
|
else if (symbol == SymbolType.WHILE)
|
2021-03-03 18:50:06 +00:00
|
|
|
{
|
|
|
|
parseWhile();
|
|
|
|
}
|
2021-03-28 20:54:38 +01:00
|
|
|
/* If it is a function call (further inspection needed) */
|
|
|
|
else if (symbol == SymbolType.IDENT_TYPE)
|
2021-03-03 16:34:38 +00:00
|
|
|
{
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Function calls can have dotted identifiers */
|
2021-03-03 16:34:38 +00:00
|
|
|
parseFuncCall();
|
|
|
|
}
|
|
|
|
/* If it is closing the body `}` */
|
2021-03-05 10:35:58 +00:00
|
|
|
else if (symbol == SymbolType.CCURLY)
|
2021-03-03 14:30:56 +00:00
|
|
|
{
|
|
|
|
// gprintln("Error");
|
2021-03-03 17:53:47 +00:00
|
|
|
// nextToken();
|
|
|
|
gprintln("parseBody(): Exiting body by }", DebugType.WARNING);
|
2021-03-05 10:35:58 +00:00
|
|
|
|
2021-03-03 18:04:54 +00:00
|
|
|
closedBeforeExit = true;
|
2021-03-03 14:30:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-05 09:31:33 +00:00
|
|
|
/* If it is a class definition */
|
2021-03-05 10:35:58 +00:00
|
|
|
else if (symbol == SymbolType.CLASS)
|
2021-03-05 09:31:33 +00:00
|
|
|
{
|
2021-03-24 19:50:06 +00:00
|
|
|
/* Parse the class and add its statements */
|
|
|
|
statements ~= parseClass();
|
2021-03-05 09:31:33 +00:00
|
|
|
}
|
2021-05-04 18:10:59 +01:00
|
|
|
/* If it is a struct definition */
|
|
|
|
else if(symbol == SymbolType.STRUCT)
|
|
|
|
{
|
|
|
|
/* Parse the struct and add it to the statements */
|
|
|
|
statements ~= parseStruct();
|
|
|
|
}
|
2021-03-03 17:53:47 +00:00
|
|
|
/* Error out */
|
|
|
|
else
|
|
|
|
{
|
2021-03-20 16:41:26 +00:00
|
|
|
expect("parseBody(): Unknown symbol: " ~ getCurrentToken()
|
|
|
|
.getToken());
|
2021-03-03 17:53:47 +00:00
|
|
|
}
|
2021-03-03 14:30:56 +00:00
|
|
|
}
|
2021-03-03 17:53:47 +00:00
|
|
|
|
|
|
|
/* TODO: We can sometimes run out of tokens before getting our closing brace, we should fix that here */
|
2021-03-05 10:35:58 +00:00
|
|
|
if (!closedBeforeExit)
|
2021-03-03 18:04:54 +00:00
|
|
|
{
|
2021-03-05 10:03:31 +00:00
|
|
|
expect("Expected closing } but ran out of tokens");
|
2021-03-03 18:04:54 +00:00
|
|
|
}
|
2021-03-05 10:00:13 +00:00
|
|
|
|
|
|
|
gprintln("parseBody(): Leave", DebugType.WARNING);
|
2021-03-21 15:57:56 +00:00
|
|
|
|
|
|
|
return statements;
|
2021-03-03 14:30:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 12:03:13 +00:00
|
|
|
private AccessorType getAccessorType(Token token)
|
|
|
|
{
|
|
|
|
if(getSymbolType(token) == SymbolType.PUBLIC)
|
|
|
|
{
|
|
|
|
return AccessorType.PUBLIC;
|
|
|
|
}
|
|
|
|
else if(getSymbolType(token) == SymbolType.PROTECTED)
|
|
|
|
{
|
|
|
|
return AccessorType.PROTECTED;
|
|
|
|
}
|
|
|
|
else if(getSymbolType(token) == SymbolType.PRIVATE)
|
|
|
|
{
|
|
|
|
return AccessorType.PRIVATE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return AccessorType.UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 10:39:55 +00:00
|
|
|
/* STATUS: Not being used yet */
|
2021-03-21 20:51:42 +00:00
|
|
|
private Entity parseAccessor()
|
2021-03-21 10:39:29 +00:00
|
|
|
{
|
2021-03-21 20:51:42 +00:00
|
|
|
Entity entity;
|
2021-03-21 20:01:54 +00:00
|
|
|
|
2021-03-21 10:39:29 +00:00
|
|
|
/* Save and consume the accessor */
|
2021-03-21 12:03:13 +00:00
|
|
|
AccessorType accessorType = getAccessorType(getCurrentToken());
|
2021-03-21 10:39:29 +00:00
|
|
|
nextToken();
|
|
|
|
|
|
|
|
/* TODO: Only allow, private, public, protected */
|
|
|
|
/* TODO: Pass this to call for class prsewr or whatever comes after the accessor */
|
|
|
|
|
|
|
|
/* Get the current token's symbol type */
|
|
|
|
SymbolType symbolType = getSymbolType(getCurrentToken());
|
|
|
|
|
|
|
|
/* If class */
|
|
|
|
if(symbolType == SymbolType.CLASS)
|
|
|
|
{
|
2021-03-21 20:01:54 +00:00
|
|
|
/* TODO: Set accessor on returned thing */
|
2021-03-21 20:51:42 +00:00
|
|
|
entity = parseClass();
|
2021-03-21 10:39:29 +00:00
|
|
|
}
|
|
|
|
/* If typed-definition (function or variable) */
|
2021-03-28 20:54:38 +01:00
|
|
|
else if(symbolType == SymbolType.IDENT_TYPE)
|
2021-03-21 10:39:29 +00:00
|
|
|
{
|
2021-03-21 20:01:54 +00:00
|
|
|
/* TODO: Set accesor on returned thing */
|
2021-03-28 20:54:38 +01:00
|
|
|
entity = cast(Entity)parseName();
|
|
|
|
|
|
|
|
if(!entity)
|
|
|
|
{
|
|
|
|
expect("Accessor got func call when expecting var/func def");
|
|
|
|
}
|
2021-03-21 10:39:29 +00:00
|
|
|
}
|
|
|
|
/* Error out */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expect("Expected either function definition, variable declaration or class definition");
|
|
|
|
}
|
2021-03-21 20:01:54 +00:00
|
|
|
|
2021-03-21 20:51:42 +00:00
|
|
|
entity.setAccessorType(accessorType);
|
|
|
|
|
|
|
|
return entity;
|
2021-03-21 10:39:29 +00:00
|
|
|
}
|
2021-03-21 10:30:23 +00:00
|
|
|
|
|
|
|
private void parseFunctionArguments()
|
|
|
|
{
|
|
|
|
/* TODO: Use later */
|
|
|
|
/* TODO: Add support for default values for function arguments */
|
|
|
|
}
|
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
private struct funcDefPair
|
|
|
|
{
|
|
|
|
Statement[] bodyStatements;
|
2021-03-22 09:18:56 +00:00
|
|
|
Variable[] args;
|
2021-03-21 15:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private funcDefPair parseFuncDef()
|
2021-03-03 13:17:51 +00:00
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseFuncDef(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
Statement[] statements;
|
2021-03-22 09:18:56 +00:00
|
|
|
Variable[] argumentList;
|
2021-03-21 15:57:56 +00:00
|
|
|
funcDefPair bruh;
|
|
|
|
|
|
|
|
|
2021-03-17 20:25:07 +00:00
|
|
|
/* Consume the `(` token */
|
2021-03-03 14:05:45 +00:00
|
|
|
nextToken();
|
|
|
|
|
2021-03-03 14:46:13 +00:00
|
|
|
/* Count for number of parameters processed */
|
|
|
|
ulong parameterCount;
|
|
|
|
|
2021-03-21 08:02:23 +00:00
|
|
|
/* Expecting more arguments */
|
|
|
|
bool moreArgs;
|
|
|
|
|
2021-03-03 14:05:45 +00:00
|
|
|
/* Get command-line arguments */
|
2021-03-05 10:35:58 +00:00
|
|
|
while (hasTokens())
|
2021-03-03 14:05:45 +00:00
|
|
|
{
|
2021-03-21 08:02:23 +00:00
|
|
|
/* Check if the first thing is a type */
|
2021-03-28 20:54:38 +01:00
|
|
|
if(getSymbolType(getCurrentToken()) == SymbolType.IDENT_TYPE)
|
2021-03-03 14:30:56 +00:00
|
|
|
{
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Get the type (this can be doted) */
|
2021-03-21 08:02:23 +00:00
|
|
|
string type = getCurrentToken().getToken();
|
2021-03-03 14:30:56 +00:00
|
|
|
nextToken();
|
2021-03-05 10:35:58 +00:00
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Get the identifier (This CAN NOT be dotted) */
|
|
|
|
expect(SymbolType.IDENT_TYPE, getCurrentToken());
|
2021-03-28 21:32:44 +01:00
|
|
|
if(!isIdentifier_NoDot(getCurrentToken()))
|
2021-03-28 20:54:38 +01:00
|
|
|
{
|
|
|
|
expect("Identifier can not be path");
|
|
|
|
}
|
2021-03-21 08:02:23 +00:00
|
|
|
string identifier = getCurrentToken().getToken();
|
2021-03-03 17:53:47 +00:00
|
|
|
nextToken();
|
2021-03-16 08:57:04 +00:00
|
|
|
|
2021-03-22 09:18:56 +00:00
|
|
|
|
|
|
|
/* Add the local variable (parameter variable) */
|
|
|
|
argumentList ~= new Variable(type, identifier);
|
|
|
|
|
2021-03-21 08:02:23 +00:00
|
|
|
moreArgs = false;
|
|
|
|
|
|
|
|
parameterCount++;
|
2021-03-03 14:30:56 +00:00
|
|
|
}
|
2021-03-21 08:02:23 +00:00
|
|
|
/* If we get a comma */
|
|
|
|
else if(getSymbolType(getCurrentToken()) == SymbolType.COMMA)
|
2021-03-03 14:30:56 +00:00
|
|
|
{
|
2021-03-21 08:02:23 +00:00
|
|
|
/* Consume the `,` */
|
2021-03-03 14:46:13 +00:00
|
|
|
nextToken();
|
2021-03-21 08:02:23 +00:00
|
|
|
|
|
|
|
moreArgs = true;
|
|
|
|
}
|
|
|
|
/* Check if it is a closing brace */
|
|
|
|
else if(getSymbolType(getCurrentToken()) == SymbolType.RBRACE)
|
|
|
|
{
|
|
|
|
/* Make sure we were not expecting more arguments */
|
|
|
|
if(!moreArgs)
|
|
|
|
{
|
|
|
|
/* Consume the `)` */
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Error out if we were and we prematurely ended */
|
|
|
|
else
|
|
|
|
{
|
2021-03-28 20:54:38 +01:00
|
|
|
expect(SymbolType.IDENT_TYPE, getCurrentToken());
|
2021-03-21 08:02:23 +00:00
|
|
|
}
|
2021-03-03 14:30:56 +00:00
|
|
|
}
|
2021-03-21 08:02:23 +00:00
|
|
|
/* Error out */
|
2021-03-03 14:30:56 +00:00
|
|
|
else
|
|
|
|
{
|
2021-03-21 08:02:23 +00:00
|
|
|
expect("Expected either type or )");
|
2021-03-03 14:30:56 +00:00
|
|
|
}
|
2021-03-03 14:05:45 +00:00
|
|
|
}
|
2021-03-05 10:35:58 +00:00
|
|
|
|
2021-03-21 08:02:23 +00:00
|
|
|
expect(SymbolType.OCURLY, getCurrentToken());
|
|
|
|
|
|
|
|
/* Parse the body (and it leaves ONLY when it gets the correct symbol, no expect needed) */
|
2021-03-21 15:57:56 +00:00
|
|
|
statements = parseBody();
|
2021-03-21 08:02:23 +00:00
|
|
|
nextToken();
|
|
|
|
|
2021-03-16 08:57:04 +00:00
|
|
|
gprintln("ParseFuncDef: Parameter count: " ~ to!(string)(parameterCount));
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseFuncDef(): Leave", DebugType.WARNING);
|
2021-03-21 15:57:56 +00:00
|
|
|
|
|
|
|
bruh.bodyStatements = statements;
|
|
|
|
bruh.args = argumentList;
|
2021-03-28 20:54:38 +01:00
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
return bruh;
|
2021-03-03 13:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 09:46:51 +00:00
|
|
|
/**
|
|
|
|
* Parses an expression
|
|
|
|
*
|
2021-03-05 09:58:51 +00:00
|
|
|
* TODO:
|
|
|
|
*
|
2021-03-05 09:46:51 +00:00
|
|
|
* I think we need a loop here to move till we hit a terminator like `)`
|
|
|
|
* in the case of a condition's/function's argument expression or `;` in
|
|
|
|
* the case of a assignment's expression.
|
|
|
|
*
|
|
|
|
* This means we will be able to get the `+` token and process it
|
|
|
|
* We will also terminate on `;` or `)` and that means our `else` can be
|
|
|
|
* left to error out for unknowns then
|
|
|
|
*/
|
2021-03-21 15:57:56 +00:00
|
|
|
private Expression parseExpression()
|
2021-03-03 13:17:51 +00:00
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseExpression(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
/* The expression to be returned */
|
|
|
|
Expression[] retExpression;
|
2021-03-21 15:57:56 +00:00
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
void addRetExp(Expression e)
|
|
|
|
{
|
|
|
|
retExpression ~= e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expression removeExp()
|
|
|
|
{
|
|
|
|
Expression poppedExp = retExpression[retExpression.length-1];
|
|
|
|
retExpression.length--;
|
|
|
|
|
|
|
|
return poppedExp;
|
|
|
|
}
|
|
|
|
|
2021-03-29 16:31:17 +01:00
|
|
|
bool hasExp()
|
|
|
|
{
|
|
|
|
return retExpression.length != 0;
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
void expressionStackSanityCheck()
|
|
|
|
{
|
|
|
|
/* If we don't have 1 on the stack */
|
|
|
|
if(retExpression.length != 1)
|
|
|
|
{
|
|
|
|
gprintln(retExpression);
|
|
|
|
expect("Expression parsing failed as we had remaining items on the expression parser stack or zero");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Unless I am wrong we can do a check that retExp should always be length 1 */
|
|
|
|
/* TODO: Makes sure that expressions like 1 1 don't wortk */
|
|
|
|
/* TODO: It must always be consumed */
|
2021-03-21 15:57:56 +00:00
|
|
|
|
2021-03-03 13:17:51 +00:00
|
|
|
/* TODO: Implement expression parsing */
|
|
|
|
|
2021-03-15 08:46:00 +00:00
|
|
|
/**
|
|
|
|
* We loop here until we hit something that closes
|
|
|
|
* an expression, in other words an expression
|
|
|
|
* appears in variable assignments which end with a
|
|
|
|
* `;`, they also appear in conditions which end in
|
|
|
|
* a `)`
|
|
|
|
*/
|
|
|
|
while (true)
|
2021-03-03 15:18:19 +00:00
|
|
|
{
|
2021-03-15 08:46:00 +00:00
|
|
|
SymbolType symbol = getSymbolType(getCurrentToken());
|
2021-03-03 15:28:39 +00:00
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
gprintln(retExpression);
|
|
|
|
|
2021-03-15 08:46:00 +00:00
|
|
|
/* If it is a number literal */
|
|
|
|
if (symbol == SymbolType.NUMBER_LITERAL)
|
2021-03-29 13:35:52 +01:00
|
|
|
{
|
|
|
|
/* TODO: Do number checking here to get correct NUmberLiteral */
|
|
|
|
NumberLiteral numberLiteral = new NumberLiteral(getCurrentToken().getToken());
|
|
|
|
|
|
|
|
/* Add expression to stack */
|
|
|
|
addRetExp(numberLiteral);
|
|
|
|
|
2021-03-15 08:46:00 +00:00
|
|
|
/* Get the next token */
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
/* If it is a maths operator */
|
2021-03-18 17:20:17 +00:00
|
|
|
else if (isMathOp(getCurrentToken()))
|
2021-03-03 15:18:19 +00:00
|
|
|
{
|
2021-03-29 16:31:17 +01:00
|
|
|
SymbolType operatorType = getSymbolType(getCurrentToken());
|
2021-03-29 13:35:52 +01:00
|
|
|
|
|
|
|
/* TODO: Save operator, also pass to constructor */
|
2021-03-15 08:46:00 +00:00
|
|
|
/* TODO: Parse expression or pass arithemetic (I think latter) */
|
2021-03-03 15:18:19 +00:00
|
|
|
nextToken();
|
|
|
|
|
2021-03-29 16:31:17 +01:00
|
|
|
OperatorExpression opExp;
|
2021-03-29 13:35:52 +01:00
|
|
|
|
2021-03-29 16:31:17 +01:00
|
|
|
/* Check if unary or not (if so no expressions on stack) */
|
|
|
|
if(!hasExp())
|
|
|
|
{
|
|
|
|
Expression rhs = parseExpression();
|
2021-03-29 13:35:52 +01:00
|
|
|
|
2021-03-29 16:31:17 +01:00
|
|
|
/* Create UnaryExpression */
|
|
|
|
opExp = new UnaryOperatorExpression(operatorType, rhs);
|
|
|
|
}
|
|
|
|
/* If has, then binary */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Pop left-hand side expression */
|
|
|
|
/* TODO: We should have error checking for `removeExp()` */
|
|
|
|
/* TODO: Make it automatically exit if not enough exps */
|
|
|
|
Expression lhs = removeExp();
|
|
|
|
|
|
|
|
/* Parse expression (the right-hand side) */
|
|
|
|
Expression rhs = parseExpression();
|
|
|
|
|
|
|
|
/* Create BinaryOpertaor Expression */
|
|
|
|
opExp = new BinaryOperatorExpression(operatorType, lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add operator expression to stack */
|
|
|
|
addRetExp(opExp);
|
2021-03-03 15:18:19 +00:00
|
|
|
}
|
2021-03-15 08:46:00 +00:00
|
|
|
/* If it is a string literal */
|
|
|
|
else if (symbol == SymbolType.STRING_LITERAL)
|
2021-03-03 15:18:19 +00:00
|
|
|
{
|
2021-03-29 13:35:52 +01:00
|
|
|
/* Add the string to the stack */
|
|
|
|
addRetExp(new StringExpression(getCurrentToken().getToken()));
|
|
|
|
|
2021-03-15 08:46:00 +00:00
|
|
|
/* Get the next token */
|
|
|
|
nextToken();
|
2021-03-03 15:18:19 +00:00
|
|
|
}
|
2021-03-15 08:46:00 +00:00
|
|
|
/* If it is an identifier */
|
2021-03-28 20:54:38 +01:00
|
|
|
else if (symbol == SymbolType.IDENT_TYPE)
|
2021-03-15 08:46:00 +00:00
|
|
|
{
|
|
|
|
string identifier = getCurrentToken().getToken();
|
2021-03-03 21:10:21 +00:00
|
|
|
|
2021-03-15 08:46:00 +00:00
|
|
|
nextToken();
|
2021-03-03 21:10:21 +00:00
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
Expression toAdd;
|
|
|
|
|
2021-03-15 08:46:00 +00:00
|
|
|
/* If the symbol is `(` then function call */
|
|
|
|
if (getSymbolType(getCurrentToken()) == SymbolType.LBRACE)
|
|
|
|
{
|
|
|
|
/* TODO: Implement function call parsing */
|
2021-03-28 20:54:38 +01:00
|
|
|
previousToken();
|
2021-03-29 13:35:52 +01:00
|
|
|
toAdd = parseFuncCall();
|
2021-03-15 08:46:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* TODO: Leave the token here */
|
|
|
|
/* TODO: Just leave it, yeah */
|
2021-03-28 21:05:58 +01:00
|
|
|
// expect("poes");
|
2021-03-29 20:06:37 +01:00
|
|
|
toAdd = new VariableExpression(identifier);
|
2021-03-15 08:46:00 +00:00
|
|
|
}
|
2021-03-29 13:35:52 +01:00
|
|
|
|
|
|
|
/* TODO: Change this later, for now we doing this */
|
|
|
|
addRetExp(toAdd);
|
2021-03-15 08:46:00 +00:00
|
|
|
}
|
2021-03-17 20:25:07 +00:00
|
|
|
/* Detect if this expression is coming to an end, then return */
|
2021-03-29 13:35:52 +01:00
|
|
|
else if (symbol == SymbolType.SEMICOLON || symbol == SymbolType.RBRACE || symbol == SymbolType.COMMA)
|
2021-03-03 21:10:21 +00:00
|
|
|
{
|
2021-03-15 08:46:00 +00:00
|
|
|
break;
|
2021-03-04 21:25:20 +00:00
|
|
|
}
|
2021-03-15 08:50:06 +00:00
|
|
|
/**
|
2021-03-16 08:36:50 +00:00
|
|
|
* For ()
|
2021-03-15 08:50:06 +00:00
|
|
|
*/
|
2021-03-18 17:20:17 +00:00
|
|
|
else if (symbol == SymbolType.LBRACE)
|
2021-03-15 09:29:05 +00:00
|
|
|
{
|
2021-03-16 05:30:18 +00:00
|
|
|
/* Consume the `(` */
|
2021-03-15 09:29:05 +00:00
|
|
|
nextToken();
|
|
|
|
|
2021-03-16 05:30:18 +00:00
|
|
|
/* Parse the inner expression till terminator */
|
2021-03-29 13:48:47 +01:00
|
|
|
addRetExp(parseExpression());
|
2021-03-15 09:29:05 +00:00
|
|
|
|
2021-03-16 05:30:18 +00:00
|
|
|
/* Consume the terminator */
|
2021-03-15 09:29:05 +00:00
|
|
|
nextToken();
|
|
|
|
}
|
2021-03-04 21:25:20 +00:00
|
|
|
else
|
|
|
|
{
|
2021-03-15 08:46:00 +00:00
|
|
|
//gprintln("parseExpression(): NO MATCH", DebugType.ERROR);
|
|
|
|
/* TODO: Something isn't right here */
|
|
|
|
expect("Expected expression terminator ) or ;");
|
2021-03-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-03 15:18:19 +00:00
|
|
|
|
2021-03-29 13:48:47 +01:00
|
|
|
|
|
|
|
gprintln(retExpression);
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseExpression(): Leave", DebugType.WARNING);
|
2021-03-21 15:57:56 +00:00
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
/* TODO: DO check here for retExp.length = 1 */
|
|
|
|
expressionStackSanityCheck();
|
2021-03-21 15:57:56 +00:00
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
return retExpression[0];
|
2021-03-03 13:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
private TypedEntity parseTypedDeclaration()
|
2021-03-03 12:42:57 +00:00
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseTypedDeclaration(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
|
|
|
|
/* Generated object */
|
|
|
|
TypedEntity generated;
|
|
|
|
|
|
|
|
|
2021-03-03 12:42:57 +00:00
|
|
|
/* TODO: Save type */
|
|
|
|
string type = getCurrentToken().getToken();
|
|
|
|
string identifier;
|
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Expect an identifier (CAN NOT be dotted) */
|
2021-03-03 12:42:57 +00:00
|
|
|
nextToken();
|
2021-03-28 20:54:38 +01:00
|
|
|
expect(SymbolType.IDENT_TYPE, getCurrentToken());
|
2021-03-28 21:27:09 +01:00
|
|
|
if(!isIdentifier_NoDot(getCurrentToken()))
|
2021-03-28 20:54:38 +01:00
|
|
|
{
|
|
|
|
expect("Identifier cannot be dotted");
|
|
|
|
}
|
2021-03-03 12:42:57 +00:00
|
|
|
identifier = getCurrentToken().getToken();
|
|
|
|
|
|
|
|
nextToken();
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("ParseTypedDec: DecisionBtwn FuncDef/VarDef: " ~ getCurrentToken().getToken());
|
2021-03-03 13:17:51 +00:00
|
|
|
|
|
|
|
/* Check if it is `(` (func dec) */
|
|
|
|
SymbolType symbolType = getSymbolType(getCurrentToken());
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("ParseTypedDec: SymbolType=" ~ to!(string)(symbolType));
|
|
|
|
if (symbolType == SymbolType.LBRACE)
|
2021-03-03 13:17:51 +00:00
|
|
|
{
|
2021-03-21 15:57:56 +00:00
|
|
|
funcDefPair pair = parseFuncDef();
|
2021-03-05 10:35:58 +00:00
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
generated = new Function(identifier, type, pair.bodyStatements, pair.args);
|
|
|
|
|
|
|
|
import std.stdio;
|
|
|
|
writeln(to!(string)((cast(Function)generated).getVariables()));
|
2021-03-03 13:17:51 +00:00
|
|
|
}
|
|
|
|
/* Check for semi-colon (var dec) */
|
2021-03-05 10:35:58 +00:00
|
|
|
else if (symbolType == SymbolType.SEMICOLON)
|
2021-03-03 13:17:51 +00:00
|
|
|
{
|
|
|
|
nextToken();
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("ParseTypedDec: VariableDeclaration: (Type: " ~ type ~ ", Identifier: " ~ identifier ~ ")",
|
|
|
|
DebugType.WARNING);
|
2021-03-21 15:57:56 +00:00
|
|
|
|
|
|
|
generated = new Variable(type, identifier);
|
2021-03-03 13:17:51 +00:00
|
|
|
}
|
|
|
|
/* Check for `=` (var dec) */
|
2021-03-05 10:35:58 +00:00
|
|
|
else if (symbolType == SymbolType.ASSIGN)
|
2021-03-03 13:17:51 +00:00
|
|
|
{
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
/* Now parse an expression */
|
2021-03-21 15:57:56 +00:00
|
|
|
Expression expression = parseExpression();
|
|
|
|
|
|
|
|
VariableAssignment varAssign = new VariableAssignment(expression);
|
2021-03-03 13:17:51 +00:00
|
|
|
|
2021-03-03 15:28:39 +00:00
|
|
|
/**
|
|
|
|
* The symbol that returned us from `parseExpression` must
|
|
|
|
* be a semi-colon
|
|
|
|
*/
|
2021-03-03 13:17:51 +00:00
|
|
|
expect(SymbolType.SEMICOLON, getCurrentToken());
|
|
|
|
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("ParseTypedDec: VariableDeclarationWithAssingment: (Type: "
|
|
|
|
~ type ~ ", Identifier: " ~ identifier ~ ")", DebugType.WARNING);
|
2021-03-21 15:57:56 +00:00
|
|
|
|
2021-03-23 19:35:13 +00:00
|
|
|
Variable variable = new Variable(type, identifier);
|
2021-03-21 15:57:56 +00:00
|
|
|
variable.addAssignment(varAssign);
|
|
|
|
|
|
|
|
generated = variable;
|
2021-03-03 13:17:51 +00:00
|
|
|
}
|
2021-03-03 13:32:28 +00:00
|
|
|
else
|
|
|
|
{
|
2021-03-05 10:03:31 +00:00
|
|
|
expect("Expected one of the following: (, ; or =");
|
2021-03-03 13:32:28 +00:00
|
|
|
}
|
2021-03-03 12:42:57 +00:00
|
|
|
|
2021-03-03 14:30:56 +00:00
|
|
|
/* TODO: If we outta tokens we should not call this */
|
|
|
|
// gprintln(getCurrentToken());
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseTypedDeclaration(): Leave", DebugType.WARNING);
|
2021-03-21 15:57:56 +00:00
|
|
|
|
|
|
|
return generated;
|
2021-03-03 12:42:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 21:25:20 +00:00
|
|
|
/**
|
|
|
|
* Parses a class definition
|
|
|
|
*
|
|
|
|
* This is called when there is an occurrence of
|
|
|
|
* a token `class`
|
|
|
|
*/
|
2021-03-21 20:51:42 +00:00
|
|
|
private Clazz parseClass()
|
2021-03-04 21:25:20 +00:00
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseClass(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
Clazz generated;
|
|
|
|
|
2021-03-04 21:25:20 +00:00
|
|
|
/* Pop off the `class` */
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Get the class's name (CAN NOT be dotted) */
|
|
|
|
expect(SymbolType.IDENT_TYPE, getCurrentToken());
|
2021-03-28 21:29:33 +01:00
|
|
|
if(!isIdentifier_NoDot(getCurrentToken()))
|
2021-03-28 20:54:38 +01:00
|
|
|
{
|
|
|
|
expect("Class name in declaration cannot be path");
|
|
|
|
}
|
2021-03-04 21:25:20 +00:00
|
|
|
string className = getCurrentToken().getToken();
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseClass(): Class name found '" ~ className ~ "'");
|
2021-03-04 21:25:20 +00:00
|
|
|
nextToken();
|
2021-03-21 07:45:40 +00:00
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
generated = new Clazz(className);
|
|
|
|
|
2021-03-30 19:05:16 +01:00
|
|
|
string[] inheritList;
|
2021-03-21 07:45:40 +00:00
|
|
|
|
|
|
|
/* TODO: If we have the inherit symbol `:` */
|
|
|
|
if(getSymbolType(getCurrentToken()) == SymbolType.INHERIT_OPP)
|
|
|
|
{
|
|
|
|
/* TODO: Loop until `}` */
|
|
|
|
|
|
|
|
/* Consume the inheritance operator `:` */
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Check if it is an identifier (may be dotted) */
|
|
|
|
expect(SymbolType.IDENT_TYPE, getCurrentToken());
|
2021-03-30 19:05:16 +01:00
|
|
|
inheritList ~= getCurrentToken().getToken();
|
2021-03-21 07:45:40 +00:00
|
|
|
nextToken();
|
|
|
|
|
|
|
|
/* Check if we have ended with a `{` */
|
|
|
|
if(getSymbolType(getCurrentToken()) == SymbolType.OCURLY)
|
|
|
|
{
|
|
|
|
/* Exit */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* If we get a comma */
|
|
|
|
else if(getSymbolType(getCurrentToken()) == SymbolType.COMMA)
|
|
|
|
{
|
|
|
|
/* Consume */
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
/* Error out if we get anything else */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expect("Expected either { or ,");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 21:25:20 +00:00
|
|
|
|
2021-03-30 19:05:16 +01:00
|
|
|
/* Add inherit list */
|
|
|
|
generated.addInherit(inheritList);
|
|
|
|
|
2021-03-21 15:57:56 +00:00
|
|
|
/* TODO: Technically we should be more specific, this does too much */
|
2021-03-04 21:25:20 +00:00
|
|
|
/* Parse a body */
|
2021-03-21 20:51:42 +00:00
|
|
|
Statement[] statements = parseBody();
|
|
|
|
generated.addStatements(statements);
|
2021-03-04 21:25:20 +00:00
|
|
|
|
2021-03-29 21:20:47 +01:00
|
|
|
/* Parent each Statement to the container */
|
|
|
|
parentToContainer(generated, statements);
|
|
|
|
|
2021-03-05 09:38:00 +00:00
|
|
|
/* Pop off the ending `}` */
|
2021-03-05 09:21:06 +00:00
|
|
|
nextToken();
|
2021-03-05 10:35:58 +00:00
|
|
|
|
|
|
|
gprintln("parseClass(): Leave", DebugType.WARNING);
|
2021-03-21 20:51:42 +00:00
|
|
|
|
|
|
|
return generated;
|
2021-03-04 21:25:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:20:47 +01:00
|
|
|
private void parentToContainer(Container container, Statement[] statements)
|
|
|
|
{
|
|
|
|
foreach(Statement statement; statements)
|
|
|
|
{
|
|
|
|
if(statement !is null)
|
|
|
|
{
|
|
|
|
statement.parentTo(container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:10:21 +00:00
|
|
|
private void parseStatement()
|
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseStatement(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-03 21:10:21 +00:00
|
|
|
/* TODO: Implement parse statement */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: We should remove the `;` from parseFuncCall
|
|
|
|
* And rather do the following here:
|
|
|
|
*
|
|
|
|
* 1. parseFuncCall()
|
|
|
|
* 2. expect(;)
|
|
|
|
*/
|
2021-03-05 10:35:58 +00:00
|
|
|
|
|
|
|
gprintln("parseStatement(): Leave", DebugType.WARNING);
|
2021-03-03 21:10:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
private Expression parseFuncCall()
|
2021-03-03 16:34:38 +00:00
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseFuncCall(): Enter", DebugType.WARNING);
|
2021-03-03 16:34:38 +00:00
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
/* TODO: Save name */
|
2021-03-29 13:35:52 +01:00
|
|
|
string functionName = getCurrentToken().getToken();
|
|
|
|
|
|
|
|
Expression[] arguments;
|
2021-03-28 20:54:38 +01:00
|
|
|
|
2021-03-03 16:34:38 +00:00
|
|
|
nextToken();
|
|
|
|
|
|
|
|
/* Expect an opening brace `(` */
|
|
|
|
expect(SymbolType.LBRACE, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-29 13:35:52 +01:00
|
|
|
/* If next token is RBRACE we don't expect arguments */
|
|
|
|
if(getSymbolType(getCurrentToken()) == SymbolType.RBRACE)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
/* If not expect arguments */
|
|
|
|
else
|
|
|
|
{
|
2021-03-30 15:25:26 +01:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
/* Get the Expression */
|
|
|
|
Expression exp = parseExpression();
|
|
|
|
|
|
|
|
/* Add it to list */
|
|
|
|
arguments ~= exp;
|
|
|
|
|
|
|
|
/* Check if we exiting */
|
|
|
|
if(getSymbolType(getCurrentToken()) == SymbolType.RBRACE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* If comma expect more */
|
|
|
|
else if(getSymbolType(getCurrentToken()) == SymbolType.COMMA)
|
|
|
|
{
|
|
|
|
nextToken();
|
|
|
|
/* TODO: If rbrace after then error, so save boolean */
|
|
|
|
}
|
|
|
|
/* TODO: Add else, could have exited on `;` which is invalid closing */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expect("Function call closed on ;, invalid");
|
|
|
|
}
|
|
|
|
}
|
2021-03-29 13:35:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-03 16:34:38 +00:00
|
|
|
nextToken();
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parseFuncCall(): Leave", DebugType.WARNING);
|
2021-03-29 13:35:52 +01:00
|
|
|
|
|
|
|
return new FunctionCall(functionName, arguments);
|
2021-03-03 16:34:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:30:56 +00:00
|
|
|
/* Almost like parseBody but has more */
|
2021-03-05 09:27:58 +00:00
|
|
|
/**
|
|
|
|
* TODO: For certain things like `parseClass` we should
|
|
|
|
* keep track of what level we are at as we shouldn't allow
|
|
|
|
* one to define classes within functions
|
|
|
|
*/
|
|
|
|
/* TODO: Variables should be allowed to have letters in them and underscores */
|
2021-03-29 20:06:37 +01:00
|
|
|
public Module parse()
|
2021-03-03 09:08:34 +00:00
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parse(): Enter", DebugType.WARNING);
|
|
|
|
|
2021-03-29 20:06:37 +01:00
|
|
|
Module modulle;
|
2021-03-21 19:43:01 +00:00
|
|
|
|
2021-03-03 09:08:34 +00:00
|
|
|
/* TODO: Do parsing here */
|
2021-03-03 12:04:26 +00:00
|
|
|
|
2021-03-21 11:10:18 +00:00
|
|
|
/* Expect `module` and module name and consume them (and `;`) */
|
|
|
|
expect(SymbolType.MODULE, getCurrentToken());
|
|
|
|
nextToken();
|
2021-03-21 19:43:01 +00:00
|
|
|
|
2021-03-28 20:54:38 +01:00
|
|
|
/* Module name may NOT be dotted (TODO: Maybe it should be yeah) */
|
|
|
|
expect(SymbolType.IDENT_TYPE, getCurrentToken());
|
2021-03-21 19:43:01 +00:00
|
|
|
string programName = getCurrentToken().getToken();
|
2021-03-21 11:10:18 +00:00
|
|
|
nextToken();
|
2021-03-21 19:43:01 +00:00
|
|
|
|
2021-03-21 11:10:18 +00:00
|
|
|
expect(SymbolType.SEMICOLON, getCurrentToken());
|
|
|
|
nextToken();
|
|
|
|
|
2021-03-29 20:06:37 +01:00
|
|
|
/* Initialize Module */
|
|
|
|
modulle = new Module(programName);
|
2021-03-21 19:43:01 +00:00
|
|
|
|
2021-03-21 11:10:18 +00:00
|
|
|
/* TODO: do `hasTokens()` check */
|
|
|
|
/* TODO: We should add `hasTokens()` to the `nextToken()` */
|
|
|
|
/* TODO: And too the `getCurrentTokem()` and throw an error when we have ran out rather */
|
|
|
|
|
2021-03-03 12:04:26 +00:00
|
|
|
/* We can have an import or vardef or funcdef */
|
2021-03-03 12:42:57 +00:00
|
|
|
while (hasTokens())
|
2021-03-03 12:04:26 +00:00
|
|
|
{
|
|
|
|
/* Get the token */
|
|
|
|
Token tok = getCurrentToken();
|
|
|
|
SymbolType symbol = getSymbolType(tok);
|
|
|
|
|
2021-03-05 10:35:58 +00:00
|
|
|
gprintln("parse(): Token: " ~ tok.getToken());
|
2021-03-03 17:53:47 +00:00
|
|
|
|
2021-03-03 12:04:26 +00:00
|
|
|
/* If it is a type */
|
2021-03-28 20:54:38 +01:00
|
|
|
if (symbol == SymbolType.IDENT_TYPE)
|
2021-03-03 12:42:57 +00:00
|
|
|
{
|
2021-04-01 14:59:33 +01:00
|
|
|
/* Might be a function, might be a variable, or assignment */
|
|
|
|
modulle.addStatement(parseName());
|
2021-03-03 12:42:57 +00:00
|
|
|
}
|
2021-03-21 11:32:51 +00:00
|
|
|
/* If it is an accessor */
|
|
|
|
else if (isAccessor(tok))
|
|
|
|
{
|
2021-03-21 20:01:54 +00:00
|
|
|
Statement statement = parseAccessor();
|
|
|
|
|
|
|
|
/* TODO: Tets case has classes which null statement, will crash */
|
2021-03-29 20:06:37 +01:00
|
|
|
modulle.addStatement(statement);
|
2021-03-21 11:32:51 +00:00
|
|
|
}
|
2021-03-05 09:21:06 +00:00
|
|
|
/* If it is a class */
|
2021-03-05 10:35:58 +00:00
|
|
|
else if (symbol == SymbolType.CLASS)
|
2021-03-05 09:21:06 +00:00
|
|
|
{
|
2021-03-21 20:51:42 +00:00
|
|
|
Clazz clazz = parseClass();
|
|
|
|
|
|
|
|
/* Add the class definition to the program */
|
2021-03-29 20:06:37 +01:00
|
|
|
modulle.addStatement(clazz);
|
2021-03-05 09:21:06 +00:00
|
|
|
}
|
2021-05-04 18:13:20 +01:00
|
|
|
/* If it is a struct definition */
|
|
|
|
else if(symbol == SymbolType.STRUCT)
|
|
|
|
{
|
|
|
|
Struct ztruct = parseStruct();
|
|
|
|
|
|
|
|
/* Add the struct definition to the program */
|
|
|
|
modulle.addStatement(ztruct);
|
|
|
|
}
|
2021-03-03 12:42:57 +00:00
|
|
|
else
|
2021-03-03 12:04:26 +00:00
|
|
|
{
|
2021-03-05 10:35:58 +00:00
|
|
|
expect("parse(): Unknown '" ~ tok.getToken() ~ "'");
|
2021-03-03 12:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-05 10:35:58 +00:00
|
|
|
|
|
|
|
gprintln("parse(): Leave", DebugType.WARNING);
|
2021-03-21 19:43:01 +00:00
|
|
|
|
2021-03-29 21:20:47 +01:00
|
|
|
/* Parent each Statement to the container (the module) */
|
|
|
|
parentToContainer(modulle, modulle.getStatements());
|
|
|
|
|
2021-03-29 20:06:37 +01:00
|
|
|
return modulle;
|
2021-03-03 09:08:34 +00:00
|
|
|
}
|
2021-03-03 11:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unittest
|
|
|
|
{
|
2021-03-03 15:12:06 +00:00
|
|
|
/* TODO: Add some unit tests */
|
2021-03-21 12:23:18 +00:00
|
|
|
import std.file;
|
|
|
|
import std.stdio;
|
|
|
|
import compiler.lexer;
|
|
|
|
|
2021-03-21 12:27:31 +00:00
|
|
|
isUnitTest = true;
|
|
|
|
|
2021-03-21 12:23:18 +00:00
|
|
|
string sourceFile = "source/tlang/testing/basic1.t";
|
|
|
|
|
|
|
|
File sourceFileFile;
|
|
|
|
sourceFileFile.open(sourceFile); /* TODO: Error handling with ANY file I/O */
|
|
|
|
ulong fileSize = sourceFileFile.size();
|
|
|
|
byte[] fileBytes;
|
|
|
|
fileBytes.length = fileSize;
|
|
|
|
fileBytes = sourceFileFile.rawRead(fileBytes);
|
|
|
|
sourceFileFile.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: Open source file */
|
|
|
|
string sourceCode = cast(string)fileBytes;
|
|
|
|
// string sourceCode = "hello \"world\"|| ";
|
|
|
|
//string sourceCode = "hello \"world\"||"; /* TODO: Implement this one */
|
|
|
|
// string sourceCode = "hello;";
|
|
|
|
Lexer currentLexer = new Lexer(sourceCode);
|
2021-03-28 19:04:27 +01:00
|
|
|
assert(currentLexer.performLex());
|
2021-03-21 12:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
Parser parser = new Parser(currentLexer.getTokens());
|
|
|
|
parser.parse();
|
2021-03-03 11:30:56 +00:00
|
|
|
}
|
2021-03-21 12:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
unittest
|
|
|
|
{
|
|
|
|
/* TODO: Add some unit tests */
|
|
|
|
import std.file;
|
|
|
|
import std.stdio;
|
|
|
|
import compiler.lexer;
|
|
|
|
|
2021-03-21 12:27:31 +00:00
|
|
|
isUnitTest = true;
|
|
|
|
|
2021-03-29 20:06:37 +01:00
|
|
|
string sourceFile = "source/tlang/testing/basic1.t";
|
2021-03-21 12:23:18 +00:00
|
|
|
|
|
|
|
File sourceFileFile;
|
|
|
|
sourceFileFile.open(sourceFile); /* TODO: Error handling with ANY file I/O */
|
|
|
|
ulong fileSize = sourceFileFile.size();
|
|
|
|
byte[] fileBytes;
|
|
|
|
fileBytes.length = fileSize;
|
|
|
|
fileBytes = sourceFileFile.rawRead(fileBytes);
|
|
|
|
sourceFileFile.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: Open source file */
|
|
|
|
string sourceCode = cast(string)fileBytes;
|
|
|
|
// string sourceCode = "hello \"world\"|| ";
|
|
|
|
//string sourceCode = "hello \"world\"||"; /* TODO: Implement this one */
|
|
|
|
// string sourceCode = "hello;";
|
|
|
|
Lexer currentLexer = new Lexer(sourceCode);
|
2021-03-28 19:04:27 +01:00
|
|
|
assert(currentLexer.performLex());
|
2021-03-21 12:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
Parser parser = new Parser(currentLexer.getTokens());
|
|
|
|
parser.parse();
|
|
|
|
}
|
|
|
|
|