From e39279bc6aa53e611bf5bd11726e7a5f6dc1112c Mon Sep 17 00:00:00 2001 From: jantuomi Date: Mon, 1 Aug 2016 16:27:52 +0300 Subject: Fix error in DoubleDatatype caused by floating point problems --- .../jantuomi/tunkki/core/parser/datatype/DoubleDatatype.java | 6 ++++-- .../com/jantuomi/tunkki/core/parser/tokenizer/Tokenizer.java | 4 ++-- src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java | 3 ++- .../core/runtime/builtins/globals/EqualsBuiltinFunction.java | 12 +++++++++--- 4 files changed, 17 insertions(+), 8 deletions(-) (limited to 'src/main/com') 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 4483ff6..31a7f96 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/datatype/DoubleDatatype.java +++ b/src/main/com/jantuomi/tunkki/core/parser/datatype/DoubleDatatype.java @@ -23,11 +23,13 @@ public class DoubleDatatype extends Datatype { } @Override - public BooleanDatatype equals(Datatype other) throws TunkkiError { + public BooleanDatatype equals(Datatype other) throws TunkkiError { switch (other.getType()) { + case Integer: + return new DoubleDatatype(((IntegerDatatype) other).getData()).equals(this); case Double: return new BooleanDatatype( - Math.abs(getData() - other.getData()) < EPSILON + Math.abs(getData() - ((DoubleDatatype) other).getData()) < EPSILON ); case Nada: return new BooleanDatatype(false); diff --git a/src/main/com/jantuomi/tunkki/core/parser/tokenizer/Tokenizer.java b/src/main/com/jantuomi/tunkki/core/parser/tokenizer/Tokenizer.java index 3b5eaf2..50936b5 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/tokenizer/Tokenizer.java +++ b/src/main/com/jantuomi/tunkki/core/parser/tokenizer/Tokenizer.java @@ -35,8 +35,8 @@ public class Tokenizer { private Tokenizer() { tokenRegexes.put(Token.Type.CommentToken, "^\\/\\*(.*?)\\*\\/"); - tokenRegexes.put(Token.Type.DoubleLiteralToken, "^([-+]?[0-9]*\\.?[0-9]+)f\\b"); - tokenRegexes.put(Token.Type.IntegerLiteralToken, "^([-+]?\\d+)\\b"); + tokenRegexes.put(Token.Type.DoubleLiteralToken, "^([-+]?[0-9]*\\.?[0-9]?)f\\b"); + tokenRegexes.put(Token.Type.IntegerLiteralToken, "^([-+]?[0-9]+)\\b"); tokenRegexes.put(Token.Type.BooleanLiteralToken, "^(true|false)\\b"); tokenRegexes.put(Token.Type.StringLiteralToken, "^\"(.*?)\""); diff --git a/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java b/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java index 0c986c3..7160b04 100644 --- a/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java +++ b/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java @@ -27,7 +27,8 @@ public class Interpreter { data = node.evaluate(); } catch (NullPointerException ex) { - throw new TunkkiError(TunkkiError.ExceptionType.GeneralError, -1, "Malformed input."); + continue; + //throw new TunkkiError(TunkkiError.ExceptionType.GeneralError, -1, "Malformed input."); } if (data != null) { diff --git a/src/main/com/jantuomi/tunkki/core/runtime/builtins/globals/EqualsBuiltinFunction.java b/src/main/com/jantuomi/tunkki/core/runtime/builtins/globals/EqualsBuiltinFunction.java index 3a8f525..b9a62cb 100644 --- a/src/main/com/jantuomi/tunkki/core/runtime/builtins/globals/EqualsBuiltinFunction.java +++ b/src/main/com/jantuomi/tunkki/core/runtime/builtins/globals/EqualsBuiltinFunction.java @@ -21,9 +21,15 @@ public class EqualsBuiltinFunction extends BuiltinFunction { throw new TunkkiError(TunkkiError.ExceptionType.FunctionArgumentError, -1, getName(), Datatype.toString(params)); } - return new BooleanDatatype( - params.get(0).equals(params.get(1)).getData() - ); + Datatype lhs = params.get(0); + Datatype rhs = params.get(1); + + BooleanDatatype isEqual = lhs.equals(rhs); + if (isEqual != null) { + return isEqual; + } else { + throw new TunkkiError(TunkkiError.ExceptionType.TypeError, -1, lhs.getType().toString(), rhs.getType().toString()); + } } @Override -- cgit v1.3