Evaluate: Implement evaluation for enums.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package de.hsrm.compiler.Klang.types;
|
package de.hsrm.compiler.Klang.types;
|
||||||
|
|
||||||
|
import de.hsrm.compiler.Klang.Value;
|
||||||
|
|
||||||
public class BooleanType extends PrimitiveType {
|
public class BooleanType extends PrimitiveType {
|
||||||
|
|
||||||
private static BooleanType instance = null;
|
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());
|
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;
|
package de.hsrm.compiler.Klang.types;
|
||||||
|
|
||||||
|
import de.hsrm.compiler.Klang.Value;
|
||||||
|
|
||||||
public class FloatType extends NumericType {
|
public class FloatType extends NumericType {
|
||||||
|
|
||||||
private static FloatType instance = null;
|
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());
|
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;
|
package de.hsrm.compiler.Klang.types;
|
||||||
|
|
||||||
|
import de.hsrm.compiler.Klang.Value;
|
||||||
|
|
||||||
public class IntegerType extends NumericType {
|
public class IntegerType extends NumericType {
|
||||||
|
|
||||||
private static IntegerType instance = null;
|
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());
|
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;
|
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 class NamedType extends Type {
|
||||||
public String name;
|
public String name;
|
||||||
|
|
||||||
@@ -21,6 +28,11 @@ public class NamedType extends Type {
|
|||||||
throw new RuntimeException("Type mismatch: cannot combine " + getName() + " and " + that.getName());
|
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
|
@Override
|
||||||
public boolean isPrimitiveType() {
|
public boolean isPrimitiveType() {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package de.hsrm.compiler.Klang.types;
|
package de.hsrm.compiler.Klang.types;
|
||||||
|
|
||||||
|
import de.hsrm.compiler.Klang.Value;
|
||||||
|
|
||||||
public class NullType extends Type {
|
public class NullType extends Type {
|
||||||
|
|
||||||
private static NullType instance = null;
|
private static NullType instance = null;
|
||||||
@@ -28,6 +30,11 @@ public class NullType extends Type {
|
|||||||
return that;
|
return that;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean valuesEqual(Value a, Value b) {
|
||||||
|
return a.asObject() == b.asObject();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPrimitiveType() {
|
public boolean isPrimitiveType() {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
package de.hsrm.compiler.Klang.types;
|
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 {
|
public abstract class Type {
|
||||||
|
|
||||||
@@ -34,6 +39,7 @@ public abstract class Type {
|
|||||||
|
|
||||||
public abstract String getName();
|
public abstract String getName();
|
||||||
public abstract Type combine(Type that);
|
public abstract Type combine(Type that);
|
||||||
|
public abstract boolean valuesEqual(Value a, Value b);
|
||||||
public abstract boolean isPrimitiveType();
|
public abstract boolean isPrimitiveType();
|
||||||
public abstract boolean isNumericType();
|
public abstract boolean isNumericType();
|
||||||
}
|
}
|
||||||
@@ -46,25 +46,11 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value visit(EqualityExpression e) {
|
public Value visit(EqualityExpression e) {
|
||||||
Value lhs = e.lhs.welcome(this);
|
var lhs = e.lhs.welcome(this);
|
||||||
Value rhs = e.rhs.welcome(this);
|
var rhs = e.rhs.welcome(this);
|
||||||
Type resultType = Type.getBooleanType();
|
var combinedType = lhs.type.combine(rhs.type);
|
||||||
Type combineType = lhs.type.combine(rhs.type);
|
|
||||||
|
|
||||||
switch(combineType.getName()) {
|
return new Value(combinedType.valuesEqual(lhs, rhs), Type.getBooleanType());
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -498,7 +484,7 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value visit(EnumAccessExpression e) {
|
public Value visit(EnumAccessExpression e) {
|
||||||
return null;
|
return new Value(e.enumValueName, e.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user