summaryrefslogtreecommitdiff
path: root/src/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Main.java')
-rw-r--r--src/Main.java128
1 files changed, 88 insertions, 40 deletions
diff --git a/src/Main.java b/src/Main.java
index 15d472d..5970b3e 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,6 +1,7 @@
import java.util.ArrayList;
import java.util.Arrays;
+
import javax.swing.*;
import org.antlr.v4.gui.TreeViewer;
import org.antlr.v4.runtime.*;
@@ -11,15 +12,18 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import ast.*;
import ast.nodes.*;
-import parser.Python3Lexer;
-import parser.Python3Parser;
+import parser.*;
import semanticanalysis.*;
import svm.*;
public class Main {
+ private static boolean execute = false;
+ private static boolean treeFlag = false;
+ private static boolean optimize = false;
+
public static void main(String[] args) {
- if (args.length != 1) {
+ if (args.length < 1) {
System.err
.println(
"You must execute this program with a file parameter.\nUsage: java -cp lib/antlr-4.13.1-complete.jar:out Main <file.py>");
@@ -27,36 +31,58 @@ public class Main {
}
try {
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("--exec")) {
+ execute = true;
+ }
+
+ if (args[i].equals("--tree")) {
+ treeFlag = true;
+ }
+
+ if (args[i].equals("--optimize")) {
+ optimize = true;
+ }
+ }
+
String fileStr = args[0];
- System.out.println(fileStr);
- System.out.println(Share.readFile(fileStr));
CharStream cs = CharStreams.fromFileName(fileStr);
Python3Lexer lexer = new Python3Lexer(cs);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Python3Parser parser = new Python3Parser(tokens);
Python3Parser.RootContext tree = parser.root();
- JFrame frame = new JFrame("Parse Tree");
- JPanel panel = new JPanel();
- TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()),
- tree);
- viewer.setScale(1); // Zoom factor
- panel.add(viewer);
- frame.add(panel);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(800, 600);
- frame.setVisible(true);
if (tree == null) {
System.err.println("The tree is null.");
return;
}
+
if (parser.getNumberOfSyntaxErrors() > 0) {
System.err.println("Error on program parsing.");
return;
}
- Python3VisitorImpl visitor = new Python3VisitorImpl();
- SymbolTable ST = new SymbolTable();
+
+ Python3VisitorImpl visitor = new Python3VisitorImpl(tokens, optimize);
Node ast = visitor.visit(tree);
+ SymbolTable ST = new SymbolTable();
+
+ if (optimize) {
+ // first visit to optimize
+ cs = CharStreams.fromString(visitor.getRewriter());
+ System.out.println(cs);
+ lexer = new Python3Lexer(cs);
+ tokens = new CommonTokenStream(lexer);
+ parser = new Python3Parser(tokens);
+ tree = parser.root();
+ ST = new SymbolTable();
+
+ ast = visitor.visit(tree);
+
+ System.out.println("Saving optimized file...");
+ String astToPrint = ast.toPrint("");
+ Share.saveFile("optimized.py", astToPrint);
+ }
+
ArrayList<SemanticError> errorsWithDup = ast.checkSemantics(ST, 0, null);
ArrayList<SemanticError> errors = Share.removeDuplicates(errorsWithDup);
if (!errors.isEmpty()) {
@@ -65,35 +91,57 @@ public class Main {
System.out.println("\t" + e);
}
} else {
- System.out.println("Visualizing AST...");
- System.out.println(ast.toPrint(""));
- System.out.println("Creating VM code...");
- String prog = ast.codeGeneration();
- String asmFile = "code.asm";
- Path file = Paths.get(asmFile);
- if (!Files.exists(file)) {
- Files.createFile(file);
+ if (treeFlag) {
+ JFrame frame = new JFrame("Parse Tree");
+ JPanel panel = new JPanel();
+ TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()),
+ tree);
+ viewer.setScale(1); // Zoom factor
+ panel.add(viewer);
+ frame.add(panel);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(800, 600);
+ frame.setVisible(true);
+
+ System.out.println(ast.printAST(""));
+ }
+
+ if (execute) {
+ System.out.println("Creating VM code...");
+ String asmFile = "code.asm";
+ String prog = ast.codeGeneration();
+ Share.saveFile(asmFile, prog);
+ executeVMFile(asmFile);
}
- Files.write(file, prog.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
- System.out.println("Done!");
- CharStream inputASM = CharStreams.fromFileName(asmFile);
- SVMLexer lexerASM = new SVMLexer(inputASM);
- CommonTokenStream tokensASM = new CommonTokenStream(lexerASM);
- SVMParser parserASM = new SVMParser(tokensASM);
+ System.out.println("Everything is OK!");
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
- SVMVisitorImpl visitorSVM = new SVMVisitorImpl();
- visitorSVM.visit(parserASM.assembly());
+ private static void executeVMFile(String fileName) {
+ try {
+ CharStream inputASM = CharStreams.fromFileName(fileName);
+ SVMLexer lexerASM = new SVMLexer(inputASM);
+ CommonTokenStream tokensASM = new CommonTokenStream(lexerASM);
+ SVMParser parserASM = new SVMParser(tokensASM);
- System.out.println("You had: " + lexerASM.lexicalErrors + " lexical errors and "
- + parserASM.getNumberOfSyntaxErrors() + " syntax errors.");
- if (lexerASM.lexicalErrors > 0 || parserASM.getNumberOfSyntaxErrors() > 0)
- System.exit(1);
+ SVMVisitorImpl visitorSVM = new SVMVisitorImpl();
+ visitorSVM.visit(parserASM.assembly());
- System.out.println("Starting Virtual Machine...");
- ExecuteVM vm = new ExecuteVM(visitorSVM.code);
- vm.cpu();
+ int nLexicalErrors = lexerASM.lexicalErrors;
+ int nSyntaxErrors = parserASM.getNumberOfSyntaxErrors();
+ if (nLexicalErrors > 0 || nSyntaxErrors > 0) {
+ System.out.println("You had " + nLexicalErrors + " lexical errors and "
+ + nSyntaxErrors + " syntax errors in the asm file.");
+ System.exit(1);
}
+
+ System.out.println("Executing assemply code...");
+ ExecuteVM vm = new ExecuteVM(visitorSVM.code);
+ vm.cpu();
} catch (Exception e) {
e.printStackTrace();
}