|
@@ -5,21 +5,21 @@
|
|
|
*/
|
|
|
package compiler;
|
|
|
|
|
|
-import common.SyntaxError;
|
|
|
-import common.IvannosysTargetArch;
|
|
|
-import API.Api;
|
|
|
import API.BuildParams;
|
|
|
-import common.Instruction;
|
|
|
-import common.Code;
|
|
|
+import API.Target;
|
|
|
import IntermediaryCode.IRGenInterface;
|
|
|
import ast.AbstractSyntaxTree;
|
|
|
import ast.Semantic;
|
|
|
+import common.Code;
|
|
|
import common.ErrorReport;
|
|
|
+import common.Instruction;
|
|
|
+import common.IvannosysTargetArch;
|
|
|
import common.Log;
|
|
|
-import grammar.IvannosysGrammarLexer;
|
|
|
-import grammar.IvannosysGrammarParser;
|
|
|
+import common.SyntaxError;
|
|
|
import frontend.Ivannosys.IvannosysListener;
|
|
|
import frontend.Ivannosys.IvannosysVisitor;
|
|
|
+import grammar.IvannosysGrammarLexer;
|
|
|
+import grammar.IvannosysGrammarParser;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
@@ -42,91 +42,31 @@ public final class IvannosysCompiler {
|
|
|
|
|
|
public static final int INPUT_FILE = 0;
|
|
|
public static final int INPUT_STREAM = 1;
|
|
|
- protected String inputFile = "";
|
|
|
- protected String inputStream = "";
|
|
|
+ protected String userInput = "";
|
|
|
protected String encType = "UTF-8";
|
|
|
protected int inputType = 0;
|
|
|
public ArrayList<String> path;
|
|
|
public ArrayList<String> pathLibs;
|
|
|
protected ParseTreeWalker walker;
|
|
|
protected IvannosysListener listener;
|
|
|
- protected IvannosysGrammarParser.InitContext tree;
|
|
|
-// protected IvannosysVisitor iVisitor;
|
|
|
- protected HashMap<String, IvannosysTargetArch> targets = new HashMap<>();
|
|
|
-// protected HashMap<String, IvannosysVisitor> visitors;
|
|
|
- protected HashMap<String, AbstractSyntaxTree> asts;
|
|
|
+ protected IRGenInterface IR;
|
|
|
protected AbstractSyntaxTree ast;
|
|
|
- protected HashMap<String, String> settings;
|
|
|
protected String ExtensionLibrary = ".go";
|
|
|
+ protected HashMap<String, AbstractSyntaxTree> asts;
|
|
|
+ protected IvannosysGrammarParser.InitContext tree;
|
|
|
+ protected HashMap<String, IvannosysTargetArch> targets = new HashMap<>();
|
|
|
|
|
|
- protected String EnvPathVarName = "IVANPATH";
|
|
|
- protected String EnvBinVarName = "IVANBIN";
|
|
|
- protected String EnvRootVarName = "IVANROOT";
|
|
|
- protected IRGenInterface IR;
|
|
|
-
|
|
|
- IvannosysCompiler(String srcinputprogram, String enc, int inputType) throws Exception {
|
|
|
- settings = new HashMap<>();
|
|
|
- path = new ArrayList<>();
|
|
|
- pathLibs = new ArrayList<>();
|
|
|
-// visitors = new HashMap<>();
|
|
|
- asts = new HashMap<>();
|
|
|
+ IvannosysCompiler(String enc, int inputType) throws Exception {
|
|
|
ast = AbstractSyntaxTree.getAbstractSyntaxTree();
|
|
|
- if (inputType == INPUT_STREAM) {
|
|
|
- inputStream = srcinputprogram;
|
|
|
- } else {
|
|
|
- inputFile = srcinputprogram;
|
|
|
- }
|
|
|
+ asts = new HashMap<>();
|
|
|
+ path = new ArrayList<>();
|
|
|
encType = enc;
|
|
|
+ pathLibs = new ArrayList<>();
|
|
|
|
|
|
- initSrcPath();
|
|
|
- initOptions();
|
|
|
- }
|
|
|
-
|
|
|
- protected IvannosysCompiler RegisterTarget(String id, IvannosysTargetArch target) {
|
|
|
- targets.put(id, target);
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- protected boolean HasTarget(String id) {
|
|
|
- return targets.containsKey(id);
|
|
|
- }
|
|
|
-
|
|
|
- protected void initSrcPath() throws Exception {
|
|
|
- String[] variavies = new String[]{this.EnvPathVarName, this.EnvRootVarName};
|
|
|
- for (String variavel : variavies) {
|
|
|
- variavel = System.getenv(variavel);
|
|
|
- if (variavel == null) {
|
|
|
- throw new Exception(String.format("Não encontrou a variável de ambeinte %s.", variavel));
|
|
|
- }
|
|
|
- include(variavel);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * StingS.put("clearCode", true);<br>
|
|
|
- * StingS.put("developement",falSe);<br>
|
|
|
- * StingS.put("diSplayASt", falSe);<br>
|
|
|
- * StingS.put("diSplayErroS", falSe);<br>
|
|
|
- * StingS.put("diSplaParSerTree", falSe);
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public IvannosysCompiler initOptions() {
|
|
|
- settings.put("clearCode", "true");
|
|
|
- settings.put("developement", "false");
|
|
|
- settings.put("displayAst", "false");
|
|
|
- settings.put("displayErros", "false");
|
|
|
- settings.put("displaParserTree", "false");
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public IvannosysCompiler SetOption(String prop, String valor) {
|
|
|
- settings.put(prop, valor);
|
|
|
- return this;
|
|
|
}
|
|
|
|
|
|
- public IvannosysCompiler SetClearCode(String lc) {
|
|
|
- settings.put("clearCode", lc);
|
|
|
+ public IvannosysCompiler IRGenerator(IRGenInterface ir) {
|
|
|
+ this.IR = ir;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -137,14 +77,20 @@ public final class IvannosysCompiler {
|
|
|
return listener;
|
|
|
}
|
|
|
|
|
|
- public IvannosysCompiler Compile() throws IOException, Exception {
|
|
|
+ public void Compile(String input) throws Exception {
|
|
|
+ this.userInput = input;
|
|
|
+ Compile();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected IvannosysCompiler Compile() throws IOException, Exception {
|
|
|
try {
|
|
|
// create a CharStream that reads from standard input
|
|
|
ANTLRInputStream input;
|
|
|
+
|
|
|
if (inputType == INPUT_STREAM) {
|
|
|
- input = new ANTLRInputStream(inputStream);
|
|
|
+ input = new ANTLRInputStream(userInput);
|
|
|
} else {
|
|
|
- input = new ANTLRFileStream(inputFile, encType);
|
|
|
+ input = new ANTLRFileStream(userInput, encType);
|
|
|
}
|
|
|
|
|
|
SyntaxError se = new SyntaxError();
|
|
@@ -166,8 +112,6 @@ public final class IvannosysCompiler {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- Api.inicializar(this);
|
|
|
-
|
|
|
listener = getListener();
|
|
|
walker = new ParseTreeWalker();
|
|
|
|
|
@@ -202,7 +146,7 @@ public final class IvannosysCompiler {
|
|
|
|
|
|
Log.PrintInfo("TAC", new Instruction().S("msg", "Gerando código de três endereços"));
|
|
|
|
|
|
- Code code = IR.Create(ast, settings);
|
|
|
+ Code code = IR.Create(ast);
|
|
|
|
|
|
for (String arch : BuildParams.Get("target")) {
|
|
|
Log.PrintInfo("TAC → TARGET", new Instruction().S("msg", "Gerando código " + arch));
|
|
@@ -211,7 +155,7 @@ public final class IvannosysCompiler {
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
System.out.println(e.getMessage());
|
|
|
- if (settings.get("developement").equals("true")) {
|
|
|
+ if (BuildParams.GetFirst("mode").equals("developement")) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
@@ -219,25 +163,22 @@ public final class IvannosysCompiler {
|
|
|
}
|
|
|
|
|
|
public void Generate(String arch, Code code) throws Exception {
|
|
|
- if (!HasTarget(arch)) {
|
|
|
- throw new Exception(String.format("Arch '%s' não definida.", arch));
|
|
|
- }
|
|
|
|
|
|
- this.targets.get(arch)
|
|
|
+ Target.Get(arch)
|
|
|
.SetTAC(code)
|
|
|
.Compile()
|
|
|
.Format()
|
|
|
.Export();
|
|
|
}
|
|
|
|
|
|
- protected void DisplayAST(IvannosysGrammarParser parser) {
|
|
|
- if (settings.get("displaParserTree").equals("true")) {
|
|
|
+ protected void DisplayAST(IvannosysGrammarParser parser) throws Exception {
|
|
|
+ if (BuildParams.GetFirst("display.PARSETREE").equals("true")) {
|
|
|
// ((ParserRuleContext) tree).inspect(parser);
|
|
|
displayTree(parser);//mostra a arvore grafica
|
|
|
System.out.println(tree.toStringTree(parser)); // print LISP-style tree
|
|
|
}
|
|
|
|
|
|
- if (settings.get("displayAst").equals("true")) {
|
|
|
+ if (BuildParams.GetFirst("display.AST").equals("true")) {
|
|
|
System.out.println("...................................Abstract Syntax Tree");
|
|
|
System.out.println(ast.stringfy());
|
|
|
}
|
|
@@ -286,7 +227,7 @@ public final class IvannosysCompiler {
|
|
|
IvannosysGrammarParser parser = new IvannosysGrammarParser((TokenStream) tokens);
|
|
|
IvannosysGrammarParser.InitContext ltree = parser.init(); // begin parsing at init rule
|
|
|
|
|
|
- if (settings.get("displaParserTree").equals("true")) {
|
|
|
+ if (BuildParams.GetFirst("displaParserTree").equals("true")) {
|
|
|
// ((ParserRuleContext) ltree).inspect(parser); //mostra a arvore grafica
|
|
|
System.out.println(ltree.toStringTree(parser)); // print LISP-style tree
|
|
|
displayTree(parser);//mostra a arvore grafica
|
|
@@ -300,16 +241,6 @@ public final class IvannosysCompiler {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- public IvannosysCompiler setDisplayParserTree(boolean b) {
|
|
|
- settings.put("displaParserTree", b ? "true" : "false");
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public IvannosysCompiler setDisplayErros(boolean b) {
|
|
|
- settings.put("displayErros", b ? "true" : "false");
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
public IvannosysCompiler include(String srcinput) {
|
|
|
if (!path.contains(srcinput)) {
|
|
|
path.add(srcinput);
|
|
@@ -343,15 +274,4 @@ public final class IvannosysCompiler {
|
|
|
this.ExtensionLibrary = ExtensionLibrary;
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
- public IvannosysCompiler EnvPathVar(String ivanpath) {
|
|
|
- this.EnvPathVarName = ivanpath;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public IvannosysCompiler TacGen(IRGenInterface tac) {
|
|
|
- this.IR = tac;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
}
|