diff --git a/src/main/antlr/.antlr/VSLLexer.java b/src/main/antlr/.antlr/VSLLexer.java index 191f1cb..7700158 100644 --- a/src/main/antlr/.antlr/VSLLexer.java +++ b/src/main/antlr/.antlr/VSLLexer.java @@ -1,4 +1,4 @@ -// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLLexer.g by ANTLR 4.13.1 +// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLLexer.g by ANTLR 4.13.1 package TP2; diff --git a/src/main/antlr/.antlr/VSLParser.java b/src/main/antlr/.antlr/VSLParser.java index da45987..3b19aa0 100644 --- a/src/main/antlr/.antlr/VSLParser.java +++ b/src/main/antlr/.antlr/VSLParser.java @@ -1,10 +1,9 @@ -// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1 +// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1 package TP2; import TP2.asd.Program.*; import TP2.asd.*; import TP2.asd.Interface.*; - import TP2.asd.Eval.*; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index 2915b00..cd90e99 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -11,7 +11,6 @@ options { import TP2.asd.Program.*; import TP2.asd.*; import TP2.asd.Interface.*; - import TP2.asd.Eval.*; } // On syntax error, raise exception rather than silently recovery diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index 96d430d..44f7df0 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -47,16 +47,16 @@ public class Main { // Pretty-print the program (to debug parsing) System.err.println("todo " + ast); - System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); + //System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); // Verify the program semantic // Generate the intermediate representation System.out.println("todo"); - //ProgramLLVMImpl astLLVM = ast.toLLVM(); + ProgramLLVMImpl astLLVM = ast.toLLVM(); - //System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); + System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + astLLVM.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER"); diff --git a/src/main/java/TP2/asd/Eval.java b/src/main/java/TP2/asd/Eval.java deleted file mode 100644 index d1e248c..0000000 --- a/src/main/java/TP2/asd/Eval.java +++ /dev/null @@ -1,42 +0,0 @@ -package TP2.asd; - -import java.util.Map; - -import TP2.asd.Interface.*; -import TP2.asd.Program.*; - -public class Eval { - public class ExprEval implements ExprVisitor,Integer>{ - public Integer visitConst(ConstImp c, Map h){ - return c.c(); - } - - public Integer visitBinOp(BinopExpressionImp e, Map h) { - switch(e.op()) { - case Op.PLUS: return e.e1().accept(this, h)+e.e2().accept(this, h); - case Op.MINUS: return e.e1().accept(this, h)-e.e2().accept(this, h); - case Op.TIMES: return e.e1().accept(this, h)*e.e2().accept(this, h); - case Op.DIV: return e.e1().accept(this, h)/e.e2().accept(this, h); - case Op.MOD: return e.e1().accept(this, h)%e.e2().accept(this, h); - default: throw new IllegalArgumentException(); - } - } - } - - public class InstructionEval implements InstrVisitor,Integer>{ - - @Override - public Integer visitReturn(Return_instrImp e, Map h) { - ExprEval exprEval = new ExprEval(); - return e.e().accept(exprEval, h); - } - - @Override - public Integer visitAssign(AssignImp e, Map h) { - //h.put(e.i(), e.t()); - //TODO - return 1; - } - - } -} diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index af353f8..325b480 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -1,46 +1,52 @@ package TP2.asd; -import java.util.ArrayList; -import java.util.Map; import TP2.asd.Program.*; import TP2.llvm.Interface.*; import TP2.llvm.ProgramLLVM.*; public interface Interface{ + //////////Program public interface ProgramI { public S accept(ProgramVisitor v, H h); public String prettyprinter(); public ProgramLLVMImpl toLLVM(); } - public interface Function { - public S accept(FunctionVisitor v, H h); - public String prettyprinter(String indent); - public DefineLLVM toLLVM(); - } - public interface Instruction { - public S accept(InstrVisitor v, H h); - public String prettyprinter(String indent); - public InstructionLLVM toLLVM(); - } - - public interface Expression { - public S accept(ExprVisitor v, H h); - public String prettyprinter(); - } public interface ProgramVisitor { public S visitProgram(ProgramImp programImp, H h); } + //////////Function + public interface Function { + public S accept(FunctionVisitor v, H h); + public String prettyprinter(String indent); + public DefineLLVM toLLVM(); + } + public interface FunctionVisitor { public S visitFunction(FunctionImp e, H h); } + //////////Instruction + public interface Instruction { + public S accept(InstrVisitor v, H h); + public String prettyprinter(String indent); + public InstructionLLVM toLLVM(); + } + public interface InstrVisitor{ public S visitReturn(Return_instrImp e, H h); public S visitAssign(AssignImp e, H h); } + //////////Expression + //We put prettyprinter here beause each expr will have to implement it like accept visitor + //but each implement will be different for prettyprinter + public interface Expression { + public S accept(ExprVisitor v, H h); + public String prettyprinter(); + } + public interface ExprVisitor { public S visitConst(ConstImp e,H h); public S visitBinOp(BinopExpressionImp e, H h); diff --git a/src/main/java/TP2/asd/Program.java b/src/main/java/TP2/asd/Program.java index fec6b40..aa26c15 100644 --- a/src/main/java/TP2/asd/Program.java +++ b/src/main/java/TP2/asd/Program.java @@ -2,15 +2,10 @@ package TP2.asd; import java.util.ArrayList; import java.util.Map; - -import org.antlr.grammar.v3.ANTLRParser.defaultNodeOption_return; - import TP2.asd.Interface.*; import TP2.llvm.ProgramLLVM.*; import TP2.llvm.Interface.*; - - public class Program{ static String INDENT = " "; @@ -35,10 +30,10 @@ public class Program{ for(int i = 0; i(),fonctionLLVM); + return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM); } - + } public static record FunctionImp(Type type, String nom, ArrayList instructions)implements Function { @@ -65,7 +60,8 @@ public class Program{ for(int i = 0; i> stackMap; + public SymTable(){ - + this.stackMap= new Stack<>(); + } + + public void next_layer(){ + stackMap.push(HashTreePMap.empty()); + } + + public void quit_layer() throws Exception{ + if(stackMap.isEmpty()){ + throw new Exception(); + } + stackMap.pop(); + } + public PMap peppapeek(){ + return stackMap.peek(); + } + + public void addVar(String s, Type t){ + //Save temporary if not PMap wont save + PMap pmap = this.peppapeek(); + pmap= pmap.plus(s,t); + //Delete old ones + stackMap.pop(); + //Push the new one + stackMap.push(pmap); + } + + //Usually look for var in highest level , if not found research. + public boolean searchVar(String s){ + for(int i= stackMap.size()-1; i>=0; i--){ + if(stackMap.get(i).containsKey(s)){ + return true; + } + } + return false; } } diff --git a/src/main/java/TP2/llvm/Interface.java b/src/main/java/TP2/llvm/Interface.java index 8282a23..72e0662 100644 --- a/src/main/java/TP2/llvm/Interface.java +++ b/src/main/java/TP2/llvm/Interface.java @@ -1,15 +1,29 @@ package TP2.llvm; +import TP2.asd.Program.ProgramImp; import TP2.llvm.ProgramLLVM.*; public interface Interface { + //////////ProgramLLVM public interface ProgLLVM{ + public S accept(ProgramLLVMVisitor v, H h); public String prettyprinter(); } + public interface ProgramLLVMVisitor { + public S visitProgramLLVM(ProgramLLVMImpl programImp, H h); + } + + //////////DefineLLVM public interface DefineLLVM{ + public S accept(DefineLLVMVisitor v, H h); public String prettyprinter(); } + public interface DefineLLVMVisitor { + public S visitProgramLLVM(DefineLLVMImpl defineLLVMImp, H h); + } + + public interface IdentifierLLVM{ //globaux @ et local % public String prettyprinter(); } diff --git a/src/main/java/TP2/llvm/ProgramLLVM.java b/src/main/java/TP2/llvm/ProgramLLVM.java index eb74dfa..ab01564 100644 --- a/src/main/java/TP2/llvm/ProgramLLVM.java +++ b/src/main/java/TP2/llvm/ProgramLLVM.java @@ -2,7 +2,8 @@ package TP2.llvm; import java.util.ArrayList; -import TP2.asd.Program.ProgramImp; +import TP2.asd.Interface.*; +import TP2.asd.Program.*; import TP2.llvm.Interface.*; @@ -10,60 +11,141 @@ public class ProgramLLVM { static String INDENT = " "; - //TODO //TODO - public static record ProgramLLVMImpl(int target ,ArrayList declration ,ArrayList fonctions) implements ProgLLVM{ + + //Program + public static record ProgramLLVMImpl(ArrayList declration ,ArrayList fonctions) implements ProgLLVM{ + public S accept(ProgramLLVMVisitor v, H h) { + return v.visitProgramLLVM(this, h); + } public String prettyprinter(){ - String str = ""; - str += target + "\n"; //TODO - for(int i = 0; i instrs) implements DefineLLVM{ - public String prettyprinter(){ - String str = "define i"+ nbBits + "@"+nom + "() {\n"; - for(int i = 0; i < instrs.size(); i++){ - str += instrs.get(i).prettyprinter() + "\n"; + //Define + public static record DefineLLVMImpl(String nom, Type t, ArrayList instrs) implements DefineLLVM{ + public S accept(DefineLLVMVisitor v, H h) { + return v.visitProgramLLVM(this, h); + } + + public String type_toString(){ + switch(t.toString()){ + case "INT": + return "i32"; + case "VOID": + return "void"; + default: + throw new AssertionError(); } - str += "}"; - return str; + } + + public String prettyprinter(){ + StringBuilder str = new StringBuilder("define "); + str.append(type_toString()).append(" @").append(nom).append("("); + + //TODO param + + str.append("){\n"); + for(InstructionLLVM instr : instrs){ + str.append(instr.prettyprinter()).append("\n"); + } + str.append("}"); + return str.toString(); } } //Instructon : - + /* public static record LabelLLVMImpl(String nom) implements InstructionLLVM{ public String prettyprinter(){ - String str = "" + nom + ":"; - return str; + StringBuilder str = new StringBuilder(); + return str.toString(); } } + */ - public static record AffectLLVM(Var var, ExpressionLLVM e) implements InstructionLLVM{ + public static record AssignLVMImp(Var var, ExpressionLLVM e) implements InstructionLLVM{ + @Override + public S accept(InstructionLLVMVisitor v, H h) { + return v.visitAssignLLVM(this, h); + } public String prettyprinter(){ return "%" + var.prettyprinter() + " = " + e.prettyprinter(); } } - //Expression : + public static record ReturnLLVMImp(ExpressionLLVM e) implements InstructionLLVM{ + @Override + public S accept(InstructionLLVMVisitor v, H h) { + return v.visitReturnLLVM(this, h); + } - public static record AddLLVMImpl(int nbBits, Val val1,Val val2) implements ExpressionLLVM{ public String prettyprinter(){ - return "add" + " i" + nbBits + " " + val1.prettyprinter() + ", " + val2.prettyprinter(); + StringBuilder str = new StringBuilder(); + return str.toString(); + } + + + } + + //Expression : + public static record BinOpLLVMImp(Op op, Val val1,Val val2) implements ExpressionLLVM{ + public S accept(ExpressionLLVMVisitor v, H h) { + return v.visitBinOpLLVM(this, h); + } + public String op_toString(){ + StringBuilder str = new StringBuilder(); + switch(op.toString()){ + case "PLUS": + str.append("add"); + break; + case "MINUS": + str.append("minus"); + break; + default: + throw new AssertionError(); + } + return str.append("i32").toString(); + } + + public String prettyprinter(){ + return op_toString() + val1.prettyprinter() + ", " + val2.prettyprinter(); } } - public static record SubLLVMImpl(int nbBits, Val val1,Val val2) implements ExpressionLLVM{ - public String prettyprinter(){ - return "sub" + " i" + nbBits + " " + val1.prettyprinter() + ", " + val2.prettyprinter(); + public static record ConstLLVMImp() implements ExpressionLLVM{ + public S accept(ExpressionLLVMVisitor v, H h) { + return v.visitConstLLVM(this, h); } + + @Override + public String prettyprinter() { + StringBuilder str= new StringBuilder(); + return str.toString(); + } + } public static record allocaLLVMImpl(int nbBits) implements ExpressionLLVM{ @@ -91,10 +173,4 @@ public class ProgramLLVM { return "%"+nom; } } - - - - - - }