Added support for unary operators

Currently any math operator is allowed in unary form, of course this must be changed

Updated test cases to test this
This commit is contained in:
Tristan B. Velloza Kildaire 2021-03-29 17:31:17 +02:00
parent 50e84ec06e
commit 38ef3a49c1
3 changed files with 52 additions and 17 deletions

View File

@ -545,6 +545,11 @@ public final class Parser
return poppedExp;
}
bool hasExp()
{
return retExpression.length != 0;
}
void expressionStackSanityCheck()
{
/* If we don't have 1 on the stack */
@ -589,23 +594,39 @@ public final class Parser
/* If it is a maths operator */
else if (isMathOp(getCurrentToken()))
{
/* 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();
SymbolType operatorType = getSymbolType(getCurrentToken());
/* TODO: Save operator, also pass to constructor */
/* TODO: Parse expression or pass arithemetic (I think latter) */
nextToken();
/* Parse expression (the right-hand side) */
Expression rhs = parseExpression();
OperatorExpression opExp;
/* Add to stack BinaryOpertaor Expression */
addRetExp(new BinaryOperatorExpression(lhs, rhs));
/* Check if unary or not (if so no expressions on stack) */
if(!hasExp())
{
Expression rhs = parseExpression();
gprintln("addop");
gprintln(retExpression);
/* 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);
}
/* If it is a string literal */
else if (symbol == SymbolType.STRING_LITERAL)
@ -630,10 +651,7 @@ public final class Parser
{
/* TODO: Implement function call parsing */
previousToken();
gprintln("bruh", DebugType.ERROR);
toAdd = parseFuncCall();
gprintln("Out paraseFUncall");
gprintln(retExpression);
}
else
{

View File

@ -656,7 +656,7 @@ public class Expression : Statement
public class StringExpression : Expression
{
private string ztring;
this(string ztring)
{
this.ztring = ztring;
@ -665,15 +665,32 @@ public class StringExpression : Expression
public class OperatorExpression : Expression
{
/* Operator */
private SymbolType operator;
this(SymbolType operator)
{
this.operator = operator;
}
}
public class UnaryOperatorExpression : OperatorExpression
{
private Expression exp;
this(SymbolType operator, Expression exp)
{
super(operator);
this.exp = exp;
}
}
public class BinaryOperatorExpression : OperatorExpression
{
/* TODO: Take in operator */
this(Expression lhs, Expression rhs)
this(SymbolType operator, Expression lhs, Expression rhs)
{
super(operator);
}
}

View File

@ -164,7 +164,7 @@ public void main(int hello, byte d)
}
ubyte kak;
ubyte kak2;
ubyte kak2 = -1;
ubyte thing = "Hello";
print("Hello world");
print(1+1);