From 9385618252188f2cd264b7369a5d4afaba5a1e55 Mon Sep 17 00:00:00 2001 From: nitrix Date: Tue, 12 Nov 2019 12:03:59 +0100 Subject: [PATCH] print and evaluate the source code --- .../java/de/hsrm/compiler/Klang/Klang.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/Klang.java b/src/main/java/de/hsrm/compiler/Klang/Klang.java index a57f8c7..f7d850d 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Klang.java +++ b/src/main/java/de/hsrm/compiler/Klang/Klang.java @@ -25,11 +25,23 @@ public class Klang { ParseTree tree = parser.parse(); // begin parsing at init rule ContextAnalysis ctxAnal = new ContextAnalysis(); Node node = ctxAnal.visit(tree); // this gets us the DAST - //EvalVisitor visitor = new EvalVisitor(); + + // Pretty Print the sourcecode + System.out.println("\nPrinting the sourcecode:"); StringWriter w = new StringWriter(); PrettyPrintVisitor.ExWriter ex = new PrettyPrintVisitor.ExWriter(w); - PrettyPrintVisitor visitor = new PrettyPrintVisitor(ex); - node.welcome(visitor); + PrettyPrintVisitor printVisitor = new PrettyPrintVisitor(ex); + node.welcome(printVisitor); System.out.println(w.toString()); + + // Evaluate the sourcecode and print the result + System.out.println("\nEvaluating the source code:"); + EvalVisitor evalVisitor = new EvalVisitor(); + Value result = node.welcome(evalVisitor); + if (result != null) { + System.out.println("result: " + result.asInteger()); + } else { + System.out.println("result was null"); + } } }