Added possibility to select pretty printing / evaluating via argument

This commit is contained in:
Marvin Kaiser
2019-12-17 15:52:06 +01:00
parent a01a27bd77
commit 086e9dae76

View File

@@ -5,11 +5,42 @@ import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*; import org.antlr.v4.runtime.tree.*;
import java.io.*; import java.io.*;
import java.util.Arrays;
import java.util.List;
import de.hsrm.compiler.Klang.nodes.Node; import de.hsrm.compiler.Klang.nodes.Node;
import de.hsrm.compiler.Klang.visitors.*; import de.hsrm.compiler.Klang.visitors.*;
public class Klang { public class Klang {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
boolean evaluate = false;
boolean prettyPrint = false;
boolean compile = true;
List<String> arguments = Arrays.asList(args);
if (arguments.contains("-h") || arguments.contains("--help") || arguments.contains("?")) {
System.out.println("\nKaiser Lang Compiler");
System.out.println("Authors: Dennis Kaiser and Marvin Kaiser");
System.out.println("");
System.out.println("Pass source code via stdin");
System.out.println("");
System.out.println("Arguments:");
System.out.println("--evaluate:\t Evaluates the given source code");
System.out.println("--pretty:\t Pretty print the given source code");
System.out.println("--no-compile:\t Do not compile the source code");
return;
}
if (arguments.contains("--evaluate")) {
evaluate = true;
}
if (arguments.contains("--pretty")) {
prettyPrint = true;
}
if (arguments.contains("--no-compile")) {
compile = false;
}
// create a CharStream that reads from standard input // create a CharStream that reads from standard input
CharStream input = CharStreams.fromStream(System.in); CharStream input = CharStreams.fromStream(System.in);
@@ -26,30 +57,35 @@ public class Klang {
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
// // Pretty Print the sourcecode if (prettyPrint) {
// System.out.println("\nPrinting the sourcecode:"); // Pretty Print 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 printVisitor = new PrettyPrintVisitor(ex); PrettyPrintVisitor printVisitor = new PrettyPrintVisitor(ex);
// node.welcome(printVisitor); node.welcome(printVisitor);
// System.out.println(w.toString()); System.out.println(w.toString());
}
// Generate assembler code if (compile) {
// System.out.println("\nPrinting the assembler code"); // Generate assembler code
StringWriter wAsm = new StringWriter(); // System.out.println("\nPrinting the assembler code");
GenASM.ExWriter exAsm = new GenASM.ExWriter(wAsm); StringWriter wAsm = new StringWriter();
GenASM genasm = new GenASM(exAsm); GenASM.ExWriter exAsm = new GenASM.ExWriter(wAsm);
node.welcome(genasm); GenASM genasm = new GenASM(exAsm);
System.out.println(wAsm.toString()); node.welcome(genasm);
System.out.println(wAsm.toString());
}
// Evaluate the sourcecode and print the result if (evaluate) {
// System.out.println("\nEvaluating the source code:"); // Evaluate the sourcecode and print the result
// EvalVisitor evalVisitor = new EvalVisitor(); System.out.println("\nEvaluating the source code:");
// Value result = node.welcome(evalVisitor); EvalVisitor evalVisitor = new EvalVisitor();
// if (result != null) { Value result = node.welcome(evalVisitor);
// System.out.println("result: " + result.asInteger()); if (result != null) {
// } else { System.out.println("result: " + result.asInteger());
// System.out.println("result was null"); } else {
// } System.out.println("result was null");
}
}
} }
} }