Commit 097ab47e authored by Sarah Grebing's avatar Sarah Grebing
Browse files

change to existing classes for evaluating expressions

parent f3d39151
Pipeline #10511 failed with stage
in 2 minutes and 15 seconds
...@@ -26,11 +26,11 @@ package edu.kit.formal.proofscriptparser.ast; ...@@ -26,11 +26,11 @@ package edu.kit.formal.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.NotWelldefinedException; import edu.kit.formal.proofscriptparser.NotWelldefinedException;
import edu.kit.formal.proofscriptparser.Visitor; import edu.kit.formal.proofscriptparser.Visitor;
import lombok.*; import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import java.util.Optional;
/** /**
* Represents a boolean literal (ie. {@link #FALSE} or {@link #TRUE}). * Represents a boolean literal (ie. {@link #FALSE} or {@link #TRUE}).
* <p> * <p>
......
...@@ -23,8 +23,14 @@ package edu.kit.formal.proofscriptparser.ast; ...@@ -23,8 +23,14 @@ package edu.kit.formal.proofscriptparser.ast;
*/ */
import edu.kit.formal.interpreter.Value;
import static edu.kit.formal.proofscriptparser.ast.Type.*; import java.math.BigInteger;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import static edu.kit.formal.proofscriptparser.ast.Type.BOOL;
import static edu.kit.formal.proofscriptparser.ast.Type.INT;
/** /**
* An enum which contains meta-data to all operators. * An enum which contains meta-data to all operators.
...@@ -36,46 +42,139 @@ import static edu.kit.formal.proofscriptparser.ast.Type.*; ...@@ -36,46 +42,139 @@ import static edu.kit.formal.proofscriptparser.ast.Type.*;
* @version 1 (28.04.17) * @version 1 (28.04.17)
*/ */
public enum Operator { public enum Operator {
/** /**
* special entry for marking match as an atomic expression. * special entry for marking match as an atomic expression.
*/ */
MATCH("match", 1000, BOOL), MATCH("match", 1000, BOOL) {
@Override
public Value evaluate(Value... v) {
//return Value.from(v[0].getData().equals(v[1]));
return null;
}
},
/** /**
* *
*/ */
NOT("not", "¬", 10, BOOL, BOOL), NOT("not", "¬", 10, BOOL, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from(!(Boolean) v[0].getData());
}
},
/** */ /** */
NEGATE("-", "-", 10, INT, INT), NEGATE("-", "-", 10, INT, INT) {
@Override
public Value evaluate(Value... v) {
return evaluate(BigInteger::negate, v);
}
},
/** */ /** */
MULTIPLY("*", "×", 20, INT, INT, INT), MULTIPLY("*", "×", 20, INT, INT, INT) {
@Override
public Value evaluate(Value... v) {
return evaluate(BigInteger::multiply, v);
}
},
/** */ /** */
DIVISION("/", "÷", 20, INT, INT, INT), DIVISION("/", "÷", 20, INT, INT, INT) {
@Override
public Value evaluate(Value... v) {
return evaluate(BigInteger::divide, v);
}
},
/** */ /** */
PLUS("+", 30, INT, INT, INT), PLUS("+", 30, INT, INT, INT) {
@Override
public Value evaluate(Value... v) {
return evaluate(BigInteger::add, v);
}
},
/** */ /** */
MINUS("-", 30, INT, INT, INT), MINUS("-", 30, INT, INT, INT) {
@Override
public Value evaluate(Value... v) {
return evaluate(BigInteger::subtract, v);
}
},
/** */ /** */
LE("<", 40, INT, INT, BOOL), LE("<", 40, INT, INT, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((((BigInteger) v[0].getData()).compareTo((BigInteger) v[1].getData()) < 0));
}
},
/** */ /** */
GE(">", 40, INT, INT, BOOL), GE(">", 40, INT, INT, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((((BigInteger) v[0].getData()).compareTo((BigInteger) v[1].getData()) > 0));
}
},
/** */ /** */
LEQ("<=", "≤", 40, INT, INT, BOOL), LEQ("<=", "≤", 40, INT, INT, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((((BigInteger) v[0].getData()).compareTo((BigInteger) v[1].getData()) <= 0));
}
},
/** */ /** */
GEQ(">=", "≥", 40, INT, INT, BOOL), GEQ(">=", "≥", 40, INT, INT, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((((BigInteger) v[0].getData()).compareTo((BigInteger) v[1].getData()) >= 0));
}
},
/** */ /** */
EQ("=", "≡", 50, INT, INT, BOOL), EQ("=", "≡", 50, INT, INT, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((((BigInteger) v[0].getData()).compareTo((BigInteger) v[1].getData()) == 0));
}
},
/** */ /** */
NEQ("<>", "≢", 50, INT, INT, BOOL), NEQ("<>", "≢", 50, INT, INT, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((((BigInteger) v[0].getData()).compareTo((BigInteger) v[1].getData()) != 0));
}
},
/** */ /** */
AND("&", "∧", 60, BOOL, BOOL, BOOL), AND("&", "∧", 60, BOOL, BOOL, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((Boolean) v[0].getData() & (Boolean) v[1].getData());
}
},
/** */ /** */
OR("|", "∨", 70, BOOL, BOOL, BOOL), OR("|", "∨", 70, BOOL, BOOL, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((Boolean) v[0].getData() | (Boolean) v[1].getData());
}
},
/** */ /** */
IMPLICATION("==>", "⇒", 80, BOOL, BOOL, BOOL), IMPLICATION("==>", "⇒", 80, BOOL, BOOL, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from(!(Boolean) v[0].getData() | (Boolean) v[1].getData());
}
},
/** /**
* *
* */ * */
EQUIVALENCE("<=>", "⇔", 90, BOOL, BOOL, BOOL); EQUIVALENCE("<=>", "⇔", 90, BOOL, BOOL, BOOL) {
@Override
public Value evaluate(Value... v) {
return Value.from((!(Boolean) v[0].getData() | (Boolean) v[1].getData()) & ((Boolean) v[0].getData() | !(Boolean) v[1].getData()));
}
};
private final String symbol; private final String symbol;
private final String unicode; private final String unicode;
...@@ -93,6 +192,17 @@ public enum Operator { ...@@ -93,6 +192,17 @@ public enum Operator {
this.type = type; this.type = type;
} }
public static Value<BigInteger> evaluate(
BiFunction<BigInteger, BigInteger, BigInteger> func, Value<BigInteger>[] v) {
return Value.from(func.apply(v[0].getData(), v[1].getData()));
}
public static Value<BigInteger> evaluate(UnaryOperator<BigInteger> func, Value<BigInteger>[] v) {
return Value.from(func.apply(v[0].getData()));
}
/** /**
* unicode symbol of this operator * unicode symbol of this operator
* *
...@@ -144,4 +254,13 @@ public enum Operator { ...@@ -144,4 +254,13 @@ public enum Operator {
public int arity() { public int arity() {
return type.length - 1; return type.length - 1;
} }
/**
* Evaluate the collection of values using the operator
*
* @param v
* @return
*/
public abstract Value evaluate(Value... v);
} }
...@@ -27,14 +27,15 @@ package edu.kit.formal.proofscriptparser.ast; ...@@ -27,14 +27,15 @@ package edu.kit.formal.proofscriptparser.ast;
import java.util.Arrays; import java.util.Arrays;
/** /**
* Represents the possible types. * Represents the possible types (including scriptVarTypes).
* *
* Created at 30.04.2017 * Created at 30.04.2017
* @author Sarah Grebing * @author Sarah Grebing
*/ */
public enum Type { public enum Type {
STRING("string"), TERM("term"), ANY("any"), STRING("string"), TERM("term"), ANY("any"),
INT("int"), BOOL("bool"); INT("int"), BOOL("bool"), INT_ARRAY("int[]"), OBJECT("object"),
HEAP("heap"), FIELD("field"), LOCSET("locset"), NULL("null"), FORMULA("formula");
private final String symbol; private final String symbol;
...@@ -42,11 +43,6 @@ public enum Type { ...@@ -42,11 +43,6 @@ public enum Type {
this.symbol = symbol; this.symbol = symbol;
} }
public String symbol() {
return symbol;
}
public static Type findType(String n) { public static Type findType(String n) {
for (Type t : Type.values()) { for (Type t : Type.values()) {
if (t.symbol().equals(n)) if (t.symbol().equals(n))
...@@ -54,4 +50,8 @@ public enum Type { ...@@ -54,4 +50,8 @@ public enum Type {
} }
throw new IllegalStateException("Type " + n + " is not defined. Valid types are: " + Arrays.toString(values())); throw new IllegalStateException("Type " + n + " is not defined. Valid types are: " + Arrays.toString(values()));
} }
public String symbol() {
return symbol;
}
} }
Markdown is supported
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