Evaluate: Implement evaluation for enums.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package de.hsrm.compiler.Klang.types;
|
||||
|
||||
import de.hsrm.compiler.Klang.Value;
|
||||
|
||||
public class BooleanType extends PrimitiveType {
|
||||
|
||||
private static BooleanType instance = null;
|
||||
@@ -33,4 +35,8 @@ public class BooleanType extends PrimitiveType {
|
||||
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean valuesEqual(Value a, Value b) {
|
||||
return a.asBoolean() == b.asBoolean();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.hsrm.compiler.Klang.types;
|
||||
|
||||
import de.hsrm.compiler.Klang.Value;
|
||||
|
||||
public class FloatType extends NumericType {
|
||||
|
||||
private static FloatType instance = null;
|
||||
@@ -37,4 +39,8 @@ public class FloatType extends NumericType {
|
||||
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean valuesEqual(Value a, Value b) {
|
||||
return a.asFloat() == b.asFloat();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.hsrm.compiler.Klang.types;
|
||||
|
||||
import de.hsrm.compiler.Klang.Value;
|
||||
|
||||
public class IntegerType extends NumericType {
|
||||
|
||||
private static IntegerType instance = null;
|
||||
@@ -37,4 +39,9 @@ public class IntegerType extends NumericType {
|
||||
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean valuesEqual(Value a, Value b) {
|
||||
return a.asInteger() == b.asInteger();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
package de.hsrm.compiler.Klang.types;
|
||||
|
||||
import de.hsrm.compiler.Klang.Value;
|
||||
import de.hsrm.compiler.Klang.nodes.EnumDefinition;
|
||||
import de.hsrm.compiler.Klang.nodes.FunctionDefinition;
|
||||
import de.hsrm.compiler.Klang.nodes.StructDefinition;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class NamedType extends Type {
|
||||
public String name;
|
||||
|
||||
@@ -21,6 +28,11 @@ public class NamedType extends Type {
|
||||
throw new RuntimeException("Type mismatch: cannot combine " + getName() + " and " + that.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean valuesEqual(Value a, Value b) {
|
||||
return a.asObject().equals(b.asObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return false;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.hsrm.compiler.Klang.types;
|
||||
|
||||
import de.hsrm.compiler.Klang.Value;
|
||||
|
||||
public class NullType extends Type {
|
||||
|
||||
private static NullType instance = null;
|
||||
@@ -28,6 +30,11 @@ public class NullType extends Type {
|
||||
return that;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean valuesEqual(Value a, Value b) {
|
||||
return a.asObject() == b.asObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return false;
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package de.hsrm.compiler.Klang.types;
|
||||
|
||||
import java.util.Set;
|
||||
import de.hsrm.compiler.Klang.Value;
|
||||
import de.hsrm.compiler.Klang.nodes.EnumDefinition;
|
||||
import de.hsrm.compiler.Klang.nodes.FunctionDefinition;
|
||||
import de.hsrm.compiler.Klang.nodes.StructDefinition;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Type {
|
||||
|
||||
@@ -34,6 +39,7 @@ public abstract class Type {
|
||||
|
||||
public abstract String getName();
|
||||
public abstract Type combine(Type that);
|
||||
public abstract boolean valuesEqual(Value a, Value b);
|
||||
public abstract boolean isPrimitiveType();
|
||||
public abstract boolean isNumericType();
|
||||
}
|
||||
@@ -46,25 +46,11 @@ public class EvalVisitor implements Visitor<Value> {
|
||||
|
||||
@Override
|
||||
public Value visit(EqualityExpression e) {
|
||||
Value lhs = e.lhs.welcome(this);
|
||||
Value rhs = e.rhs.welcome(this);
|
||||
Type resultType = Type.getBooleanType();
|
||||
Type combineType = lhs.type.combine(rhs.type);
|
||||
var lhs = e.lhs.welcome(this);
|
||||
var rhs = e.rhs.welcome(this);
|
||||
var combinedType = lhs.type.combine(rhs.type);
|
||||
|
||||
switch(combineType.getName()) {
|
||||
case "bool": {
|
||||
return new Value(lhs.asBoolean() == rhs.asBoolean(), resultType);
|
||||
}
|
||||
case "int": {
|
||||
return new Value(lhs.asInteger() == rhs.asInteger(), resultType);
|
||||
}
|
||||
case "float": {
|
||||
return new Value(lhs.asFloat() == rhs.asFloat(), resultType);
|
||||
}
|
||||
default: {
|
||||
return new Value(lhs.asObject() == rhs.asObject(), resultType);
|
||||
}
|
||||
}
|
||||
return new Value(combinedType.valuesEqual(lhs, rhs), Type.getBooleanType());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -498,7 +484,7 @@ public class EvalVisitor implements Visitor<Value> {
|
||||
|
||||
@Override
|
||||
public Value visit(EnumAccessExpression e) {
|
||||
return null;
|
||||
return new Value(e.enumValueName, e.type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user