Added possibility to select pretty printing / evaluating via argument
This commit is contained in:
@@ -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,14 +57,16 @@ 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (compile) {
|
||||||
// Generate assembler code
|
// Generate assembler code
|
||||||
// System.out.println("\nPrinting the assembler code");
|
// System.out.println("\nPrinting the assembler code");
|
||||||
StringWriter wAsm = new StringWriter();
|
StringWriter wAsm = new StringWriter();
|
||||||
@@ -41,15 +74,18 @@ public class Klang {
|
|||||||
GenASM genasm = new GenASM(exAsm);
|
GenASM genasm = new GenASM(exAsm);
|
||||||
node.welcome(genasm);
|
node.welcome(genasm);
|
||||||
System.out.println(wAsm.toString());
|
System.out.println(wAsm.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (evaluate) {
|
||||||
// Evaluate the sourcecode and print the result
|
// Evaluate the sourcecode and print the result
|
||||||
// System.out.println("\nEvaluating the source code:");
|
System.out.println("\nEvaluating the source code:");
|
||||||
// EvalVisitor evalVisitor = new EvalVisitor();
|
EvalVisitor evalVisitor = new EvalVisitor();
|
||||||
// Value result = node.welcome(evalVisitor);
|
Value result = node.welcome(evalVisitor);
|
||||||
// if (result != null) {
|
if (result != null) {
|
||||||
// System.out.println("result: " + result.asInteger());
|
System.out.println("result: " + result.asInteger());
|
||||||
// } else {
|
} else {
|
||||||
// System.out.println("result was null");
|
System.out.println("result was null");
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user