From ae7275e7ab7ea2b4599b613454d4c1149855bced Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 31 Jul 2016 01:25:21 +0300 Subject: Add 'eq' builtin and comparison functionality for datatypes --- .../core/parser/datatype/BooleanDatatype.java | 11 ++++++++ .../core/parser/datatype/CallableDatatype.java | 9 ++++++ .../tunkki/core/parser/datatype/Datatype.java | 1 + .../core/parser/datatype/DoubleDatatype.java | 15 ++++++++++ .../core/parser/datatype/IntegerDatatype.java | 15 ++++++++++ .../tunkki/core/parser/datatype/ListDatatype.java | 18 ++++++++++++ .../tunkki/core/parser/datatype/NadaDatatype.java | 8 ++++++ .../core/parser/datatype/StringDatatype.java | 11 ++++++++ .../tunkki/core/parser/datatype/VoidDatatype.java | 5 ++++ .../core/runtime/builtins/BuiltinManager.java | 1 + .../runtime/builtins/ContainsBuiltinFunction.java | 2 +- .../runtime/builtins/EqualsBuiltinFunction.java | 33 ++++++++++++++++++++++ 12 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/main/com/jantuomi/tunkki/core/runtime/builtins/EqualsBuiltinFunction.java (limited to 'src') diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/BooleanDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/BooleanDatatype.java index 9d8fc69..9a42897 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/BooleanDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/BooleanDatatype.java @@ -41,4 +41,15 @@ public class BooleanDatatype extends Datatype { public Datatype divide(Datatype other) throws TunkkiError { return null; } + + @Override + public BooleanDatatype equals(Datatype other) throws TunkkiError { + switch (other.getType()) { + case Boolean: + return new BooleanDatatype(getData().equals(other.getData())); + case Nada: + return new BooleanDatatype(false); + } + return null; + } } diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/CallableDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/CallableDatatype.java index 3480b5d..d4b93d6 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/CallableDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/CallableDatatype.java @@ -43,4 +43,13 @@ public class CallableDatatype extends Datatype { public Datatype divide(Datatype other) throws TunkkiError { return null; } + + @Override + public BooleanDatatype equals(Datatype other) throws TunkkiError { + switch (other.getType()) { + case Nada: + return new BooleanDatatype(false); + } + return null; + } } diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/Datatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/Datatype.java index 887b2f4..dcbd66e 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/Datatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/Datatype.java @@ -40,6 +40,7 @@ abstract public class Datatype { public abstract Datatype subtract(Datatype other) throws TunkkiError; public abstract Datatype multiply(Datatype other) throws TunkkiError; public abstract Datatype divide(Datatype other) throws TunkkiError; + public abstract BooleanDatatype equals(Datatype other) throws TunkkiError; public static String toString(List variables) { String result = "["; diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/DoubleDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/DoubleDatatype.java index 38ec4ec..4483ff6 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/DoubleDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/DoubleDatatype.java @@ -10,6 +10,8 @@ public class DoubleDatatype extends Datatype { setData(value); } + private final static double EPSILON = 0.00001; + @Override public Type getType() { return Type.Double; @@ -20,6 +22,19 @@ public class DoubleDatatype extends Datatype { return getData().toString(); } + @Override + public BooleanDatatype equals(Datatype other) throws TunkkiError { + switch (other.getType()) { + case Double: + return new BooleanDatatype( + Math.abs(getData() - other.getData()) < EPSILON + ); + case Nada: + return new BooleanDatatype(false); + } + return null; + } + @Override public Datatype add(Datatype other) throws TunkkiError { switch (other.getType()) { diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/IntegerDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/IntegerDatatype.java index 8d91e3a..1891326 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/IntegerDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/IntegerDatatype.java @@ -21,6 +21,21 @@ public class IntegerDatatype extends Datatype { return Integer.toString(getData()); } + @Override + public BooleanDatatype equals(Datatype other) throws TunkkiError { + switch (other.getType()) { + case Integer: + return new BooleanDatatype(getData().equals(other.getData())); + case Double: + return new BooleanDatatype( + new DoubleDatatype(getData()).equals( + ((DoubleDatatype) other).getData())); + case Nada: + return new BooleanDatatype(false); + } + return null; + } + @Override public Datatype add(Datatype other) { switch (other.getType()) { diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/ListDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/ListDatatype.java index 2ff9b4a..8b4d44c 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/ListDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/ListDatatype.java @@ -24,6 +24,24 @@ public class ListDatatype extends Datatype> { return Datatype.toString(getData()); } + @Override + public BooleanDatatype equals(Datatype> other) throws TunkkiError { + switch (other.getType()) { + case List: + boolean found = false; + for (int i = 0; i < getData().size(); i++) { + if (!getData().get(i).equals(other.getData().get(i)).getData()) { + found = true; + break; + } + return new BooleanDatatype(found); + } + case Nada: + return new BooleanDatatype(false); + } + return null; + } + @Override public Datatype add(Datatype other) throws TunkkiError { switch (other.getType()) { diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/NadaDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/NadaDatatype.java index e2827f8..56c91b6 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/NadaDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/NadaDatatype.java @@ -40,6 +40,14 @@ public class NadaDatatype extends Datatype { return null; } + @Override + public BooleanDatatype equals(Datatype other) throws TunkkiError { + if (other.getType() == Type.Nada) { + return new BooleanDatatype(true); + } + return new BooleanDatatype(false); + } + private void doOperation() throws TunkkiError { throw new TunkkiError(TunkkiError.ExceptionType.NadaError, -1); } diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/StringDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/StringDatatype.java index 8a744a4..80dd527 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/StringDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/StringDatatype.java @@ -43,4 +43,15 @@ public class StringDatatype extends Datatype { public Datatype divide(Datatype other) throws TunkkiError { return null; } + + @Override + public BooleanDatatype equals(Datatype other) throws TunkkiError { + switch (other.getType()) { + case String: + return new BooleanDatatype(getData().equals(other.getData())); + case Nada: + return new BooleanDatatype(false); + } + return null; + } } diff --git a/src/main/com/jantuomi/tunkki/core/parser/datatype/VoidDatatype.java b/src/main/com/jantuomi/tunkki/core/parser/datatype/VoidDatatype.java index 4a25c64..a65f6aa 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/VoidDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/VoidDatatype.java @@ -36,4 +36,9 @@ public class VoidDatatype extends Datatype { public Datatype divide(Datatype other) throws TunkkiError { return null; } + + @Override + public BooleanDatatype equals(Datatype other) throws TunkkiError { + return new BooleanDatatype(false); + } } diff --git a/src/main/com/jantuomi/tunkki/core/runtime/builtins/BuiltinManager.java b/src/main/com/jantuomi/tunkki/core/runtime/builtins/BuiltinManager.java index 298fe55..c902f44 100644 --- a/src/main/com/jantuomi/tunkki/core/runtime/builtins/BuiltinManager.java +++ b/src/main/com/jantuomi/tunkki/core/runtime/builtins/BuiltinManager.java @@ -28,6 +28,7 @@ public class BuiltinManager { new ListBuiltinFunction(); new ContainsBuiltinFunction(); new GetBuiltinFunction(); + new EqualsBuiltinFunction(); } public void addBuiltin(BuiltinFunction func) { diff --git a/src/main/com/jantuomi/tunkki/core/runtime/builtins/ContainsBuiltinFunction.java b/src/main/com/jantuomi/tunkki/core/runtime/builtins/ContainsBuiltinFunction.java index 2be4780..256340a 100644 --- a/src/main/com/jantuomi/tunkki/core/runtime/builtins/ContainsBuiltinFunction.java +++ b/src/main/com/jantuomi/tunkki/core/runtime/builtins/ContainsBuiltinFunction.java @@ -30,7 +30,7 @@ public class ContainsBuiltinFunction extends BuiltinFunction { Datatype comp = params.get(1); for (Datatype elem : list.getData()) { - if (elem.equals(comp)) { + if (elem.equals(comp).getData()) { return new BooleanDatatype(true); } } diff --git a/src/main/com/jantuomi/tunkki/core/runtime/builtins/EqualsBuiltinFunction.java b/src/main/com/jantuomi/tunkki/core/runtime/builtins/EqualsBuiltinFunction.java new file mode 100644 index 0000000..6b1ed1a --- /dev/null +++ b/src/main/com/jantuomi/tunkki/core/runtime/builtins/EqualsBuiltinFunction.java @@ -0,0 +1,33 @@ +package com.jantuomi.tunkki.core.runtime.builtins; + +import com.jantuomi.tunkki.core.parser.datatype.BooleanDatatype; +import com.jantuomi.tunkki.core.parser.datatype.Datatype; +import com.jantuomi.tunkki.exception.TunkkiError; + +import java.util.Arrays; +import java.util.List; + +/** + * Created by jan on 31.7.2016. + */ +public class EqualsBuiltinFunction extends BuiltinFunction { + public EqualsBuiltinFunction() { + super(Arrays.asList("lhs", "rhs")); + } + + @Override + public Datatype evaluate(List params) throws TunkkiError { + if (params.size() != 2) { + throw new TunkkiError(TunkkiError.ExceptionType.FunctionArgumentError, -1, getName(), Datatype.toString(params)); + } + + return new BooleanDatatype( + params.get(0).equals(params.get(1)).getData() + ); + } + + @Override + public String getName() { + return "eq"; + } +} -- cgit v1.3