print and evaluate the source code

This commit is contained in:
2019-11-12 12:03:59 +01:00
parent 38d34a7cbc
commit 9385618252

View File

@@ -25,11 +25,23 @@ public class Klang {
ParseTree tree = parser.parse(); // begin parsing at init rule ParseTree tree = parser.parse(); // begin parsing at init rule
ContextAnalysis ctxAnal = new ContextAnalysis(); ContextAnalysis ctxAnal = new ContextAnalysis();
Node node = ctxAnal.visit(tree); // this gets us the DAST 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(); StringWriter w = new StringWriter();
PrettyPrintVisitor.ExWriter ex = new PrettyPrintVisitor.ExWriter(w); PrettyPrintVisitor.ExWriter ex = new PrettyPrintVisitor.ExWriter(w);
PrettyPrintVisitor visitor = new PrettyPrintVisitor(ex); PrettyPrintVisitor printVisitor = new PrettyPrintVisitor(ex);
node.welcome(visitor); node.welcome(printVisitor);
System.out.println(w.toString()); 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");
}
} }
} }