Commit 8fa0c06f authored by Sven Eric Panitz's avatar Sven Eric Panitz
Browse files

Negation was missing (and still missing in java version)

parent 038f2033
......@@ -50,8 +50,9 @@ class BuildTree extends SubatoScriptBaseVisitor<STree> {
}
return OneOf(es);
} else {
} else if (ctx.NOT() != null) {
return Negation(visit(ctx.expression(0)!)!);
}else {
var result = visit(ctx.expression(0)!)!;
if (ctx.relop() != null) {
result = OpEx(result, (visit(ctx.relop()!)! as TOp).op, visit(ctx.expression(1)!)!);
......
......@@ -72,6 +72,16 @@ class Eval implements Visitor<Val> {
return r;
}
@override
Val visitNegation(Negation il){
var l = il.expression.welcome(this);
if (l is BoolVal){
return BoolVal(!(l as BoolVal).b);
}else{
throw Exception("logical operator needs boolean values");
}
}
@override
Val visitUnaryOpEx(UnaryOpEx il) {
var r = il.expression.welcome(this);
......@@ -201,9 +211,10 @@ class Eval implements Visitor<Val> {
var left = (l as BoolVal).b;
var right = (r as BoolVal).b;
return BoolVal(il.op == Op.AND?left && right:left||right);
}else{
throw Exception("logical operator needs boolean values");
}
}
if (!(l.isNum() && r.isNum())) {
throw Exception("arithmetic Operator needs numeric values");
}
......
......@@ -3,6 +3,7 @@ abstract class Visitor<R> {
R visitIntNum(IntNum il);
R visitDoubleNum(DoubleNum il);
R visitVar(Var il);
R visitNegation(Negation il);
R visitUnaryOpEx(UnaryOpEx il);
R visitOpEx(OpEx il);
R visitOneOf(OneOf il);
......@@ -56,6 +57,16 @@ enum Op {
EQEQ, NEQ, LT, LE, GT, GE, ADD, SUB, MUL, DIV, MOD, POW, AND, OR, NOT
}
class Negation implements STree{
STree expression;
Negation(this.expression);
@override
R welcome<R>(Visitor<R> v){
return v.visitNegation(this);
}
}
class UnaryOpEx implements STree{
STree expression;
bool isAdd = false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment