From fc33ab6b1289e7bb0c0a730ca323aa9225218423 Mon Sep 17 00:00:00 2001 From: nitrix Date: Mon, 3 Feb 2020 23:42:07 +0100 Subject: [PATCH 1/4] add method to get the raw value representation --- src/main/java/de/hsrm/compiler/Klang/Value.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/de/hsrm/compiler/Klang/Value.java b/src/main/java/de/hsrm/compiler/Klang/Value.java index 81c05ea..af12fb3 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Value.java +++ b/src/main/java/de/hsrm/compiler/Klang/Value.java @@ -1,5 +1,7 @@ package de.hsrm.compiler.Klang; +import de.hsrm.compiler.Klang.types.Type; + public class Value { private Object value; @@ -7,6 +9,10 @@ public class Value { this.value = value; } + public Object asObject() { + return this.value; + } + public int asInteger() { return (int) this.value; } From 56463c3e3044ac6f136cb5055e9b962ea940aad6 Mon Sep 17 00:00:00 2001 From: nitrix Date: Mon, 3 Feb 2020 23:42:49 +0100 Subject: [PATCH 2/4] the the result of eval as an object and call toString\(\) on it --- src/main/java/de/hsrm/compiler/Klang/Klang.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/Klang.java b/src/main/java/de/hsrm/compiler/Klang/Klang.java index 389e8bf..555302d 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Klang.java +++ b/src/main/java/de/hsrm/compiler/Klang/Klang.java @@ -112,11 +112,7 @@ public class Klang { System.out.println("\nEvaluating the source code:"); EvalVisitor evalVisitor = new EvalVisitor(); Value result = root.welcome(evalVisitor); - if (result != null) { - generateOutput(out, "Result was: TODO"); - } else { - System.out.println("result was null"); - } + generateOutput(out, "Result was: " + result.asObject().toString()); return; } From 26b14060134475f39e61c9f850f615440dcf9274 Mon Sep 17 00:00:00 2001 From: nitrix Date: Tue, 4 Feb 2020 00:07:22 +0100 Subject: [PATCH 3/4] make boolean and comparison expression evaluate to bool, user asBoolean when expecting a boolean, re-welcome the conditions of loops everytime another step has to be evaluated --- .../compiler/Klang/visitors/EvalVisitor.java | 45 +++++-------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/visitors/EvalVisitor.java b/src/main/java/de/hsrm/compiler/Klang/visitors/EvalVisitor.java index 29649ee..5147648 100644 --- a/src/main/java/de/hsrm/compiler/Klang/visitors/EvalVisitor.java +++ b/src/main/java/de/hsrm/compiler/Klang/visitors/EvalVisitor.java @@ -33,60 +33,42 @@ public class EvalVisitor implements Visitor { public Value visit(EqualityExpression e) { Value lhs = e.lhs.welcome(this); Value rhs = e.rhs.welcome(this); - if (lhs.asInteger() == rhs.asInteger()) { - return new Value(1); - } - return new Value(0); + return new Value(lhs.asObject() == rhs.asObject()); } @Override public Value visit(NotEqualityExpression e) { Value lhs = e.lhs.welcome(this); Value rhs = e.rhs.welcome(this); - if (lhs.asInteger() != rhs.asInteger()) { - return new Value(1); - } - return new Value(0); + return new Value(lhs.asObject() != rhs.asObject()); } @Override public Value visit(GTExpression e) { Value lhs = e.lhs.welcome(this); Value rhs = e.rhs.welcome(this); - if (lhs.asInteger() > rhs.asInteger()) { - return new Value(1); - } - return new Value(0); + return new Value(lhs.asInteger() > rhs.asInteger()); } @Override public Value visit(GTEExpression e) { Value lhs = e.lhs.welcome(this); Value rhs = e.rhs.welcome(this); - if (lhs.asInteger() >= rhs.asInteger()) { - return new Value(1); - } - return new Value(0); + return new Value(lhs.asInteger() >= rhs.asInteger()); } @Override public Value visit(LTExpression e) { Value lhs = e.lhs.welcome(this); Value rhs = e.rhs.welcome(this); - if (lhs.asInteger() < rhs.asInteger()) { - return new Value(1); - } - return new Value(0); + return new Value(lhs.asInteger() < rhs.asInteger()); } @Override public Value visit(LTEExpression e) { Value lhs = e.lhs.welcome(this); Value rhs = e.rhs.welcome(this); - if (lhs.asInteger() <= rhs.asInteger()) { - return new Value(1); - } - return new Value(0); + return new Value(lhs.asInteger() <= rhs.asInteger()); } @Override @@ -163,7 +145,7 @@ public class EvalVisitor implements Visitor { Value result = null; - if (condition.asInteger() != 0) { + if (condition.asBoolean()) { result = e.then.welcome(this); } else if (e.alt != null) { result = e.alt.welcome(this); @@ -176,9 +158,8 @@ public class EvalVisitor implements Visitor { @Override public Value visit(WhileLoop e) { - Value condition = e.cond.welcome(this); Value result = null; - while (condition.asInteger() != 0) { + while (e.cond.welcome(this).asBoolean()) { result = e.block.welcome(this); } @@ -187,11 +168,10 @@ public class EvalVisitor implements Visitor { @Override public Value visit(DoWhileLoop e) { - Value condition = e.cond.welcome(this); Value result = null; do { result = e.block.welcome(this); - } while (condition.asInteger() != 0); + } while (e.cond.welcome(this).asBoolean()); return result; } @@ -201,7 +181,7 @@ public class EvalVisitor implements Visitor { e.init.welcome(this); Value cond = e.condition.welcome(this); Value result = null; - while (cond.asInteger() != 0) { + while (cond.asBoolean()) { result = e.block.welcome(this); e.step.welcome(this); cond = e.condition.welcome(this); @@ -213,10 +193,7 @@ public class EvalVisitor implements Visitor { @Override public Value visit(PrintStatement e) { Value value = e.expression.welcome(this); - - // In the future we have to determine of which type the value is - // before calling an "asX()" method - System.out.println(value.asInteger()); + System.out.println(value.asObject()); return null; } From e978b4d52c8d617938f16970eaf67e6e09c8fa7b Mon Sep 17 00:00:00 2001 From: nitrix Date: Tue, 4 Feb 2020 00:07:56 +0100 Subject: [PATCH 4/4] remove unused import --- src/main/java/de/hsrm/compiler/Klang/Value.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/Value.java b/src/main/java/de/hsrm/compiler/Klang/Value.java index af12fb3..1b2eeb9 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Value.java +++ b/src/main/java/de/hsrm/compiler/Klang/Value.java @@ -1,7 +1,5 @@ package de.hsrm.compiler.Klang; -import de.hsrm.compiler.Klang.types.Type; - public class Value { private Object value;