diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index 168924a..ea2700a 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -81,9 +81,9 @@ instruction returns [Instruction out]: $out =new Return_instrImp($e.out);} | //BLOC BacO - (dec=list_decls ins2=list_instr + (dec=list_decls instr2=list_instr { - $out=new BlocDecImp($dec.out,$ins2.out); + $out=new BlocDecImp($dec.out,$instr2.out); } | instr3=list_instr { $out= new BlocImp($instr3.out); @@ -128,13 +128,13 @@ instruction returns [Instruction out]: })* {$out = new ReadImp(read);} | //IF THEN ELSE FIN - IF ex1=expression THEN ins1=list_instr + IF ex1=expression THEN ins1=instruction (FIN {$out= new IfThenImp($ex1.out, $ins1.out);} - | ELSE ins2=list_instr FIN + | ELSE ins2=instruction FIN {$out= new IfThenElseImp($ex1.out, $ins1.out,$ins2.out); } ) - | WHILE exp1=expression DO ins3=list_instr DONE + | WHILE exp1=expression DO ins3=instruction DONE {$out = new WhileImp($exp1.out,$ins3.out);} ; diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java index 543a991..7acd021 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -103,9 +103,7 @@ public class PrettyprinterVisitor implements ProgramVisitor, String str = indent + "IF "; str +=(instr.e().accept(this, "")); str +=" THEN "; - for(int i=0; i, String str = indent + "IF "; str +=(instr.e().accept(this, "")); str +=" THEN "; - for(int i=0; i, String str = indent+"WHILE "; str += (instr.e().accept(this, "")); str+= " DO "; - for(int i=0; i i1) implements Instruction { + public static record IfThenImp(Expression e, Instruction i1) implements Instruction { @Override public S accept(InstrVisitor v, H h) { return v.visitIfThen(this, h); } } - public static record IfThenElseImp(Expression e, ArrayList i1, ArrayList i2) implements Instruction { + public static record IfThenElseImp(Expression e, Instruction i1, Instruction i2) implements Instruction { @Override public S accept(InstrVisitor v, H h) { return v.visitIfThenElse(this, h); } } - public static record WhileImp(Expression e, ArrayList i1) implements Instruction { + public static record WhileImp(Expression e, Instruction i1) implements Instruction { @Override public S accept(InstrVisitor v, H h) { return v.visitWhile(this, h); diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 7938724..88a57ab 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -27,6 +27,7 @@ public class SymTable { private PStack> stackMap; private int id=1; + public int idLabel = 1; public SymTable(){ this.stackMap= ConsPStack.empty(); } @@ -41,6 +42,12 @@ public class SymTable { return a; } + public int getNewIdLabel(){ + int a = this.idLabel; + this.id++; + return a; + } + public Result addNewTempVar(/*Type type*/){ //TODO String newVar = "temp"+id; diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 96c0657..f38f522 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -156,6 +156,22 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public ArrayList visitWhile(WhileImp instr, SymTable h) { ArrayList l = new ArrayList<>(); + + String labelWhile = "while"+h.getNewIdLabel(); + String labelDo = "do"+h.getNewIdLabel(); + String labelDone = "done"+h.getNewIdLabel(); + + l.add(new LabelLLVMImp(labelWhile)); + InstrAndVal temp = instr.e().accept(this,h); //retourne les instruction pour optenir le résultat ainsi que la variable contenant le résultat final + l.addAll(temp.instrs); + ValLLVM val = temp.val; //temp6 + //TODO : icmp et br + + l.add(new LabelLLVMImp(labelDo)); + l.addAll(instr.i1().accept(this,h)); + //TODO br vers label while + l.add(new LabelLLVMImp(labelDone)); + return l; } diff --git a/src/main/java/TP2/llvm/Interface.java b/src/main/java/TP2/llvm/Interface.java index 86a2dbe..421a3d0 100644 --- a/src/main/java/TP2/llvm/Interface.java +++ b/src/main/java/TP2/llvm/Interface.java @@ -35,6 +35,7 @@ public interface Interface { public S visitIfThenElseLLVM(IfThenElseLLVMImp instr, H h); public S visitIfThenLLVM(IfThenLLVMImp instr, H h); public S visitWhileLLVM(WhileLLVMImp instr, H h); + public S visitLabelLLVM(LabelLLVMImp instr, H h); } //////////ExpressionLLVM (expression) diff --git a/src/main/java/TP2/llvm/ProgramLLVM.java b/src/main/java/TP2/llvm/ProgramLLVM.java index 947e14b..085bd96 100644 --- a/src/main/java/TP2/llvm/ProgramLLVM.java +++ b/src/main/java/TP2/llvm/ProgramLLVM.java @@ -30,14 +30,18 @@ public class ProgramLLVM { //Instructon : - /* - public static record LabelLLVMImpl(String nom) implements InstructionLLVM{ + + public static record LabelLLVMImp(String nom) implements InstructionLLVM{ public String prettyprinter(){ StringBuilder str = new StringBuilder(); return str.toString(); } + + @Override + public S accept(InstructionLLVMVisitor v, H h) { + return v.visitLabelLLVM(this, h); + } } - */ public static record AssignLVMImpl(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{ @@ -76,30 +80,27 @@ public class ProgramLLVM { } } - public static record IfThenLLVMImp() implements InstructionLLVM{ + public static record IfThenLLVMImp(ExpressionLLVM cond, InstructionLLVM instr) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); + return v.visitIfThenLLVM(this, h); } } - public static record IfThenElseLLVMImp() implements InstructionLLVM{ + public static record IfThenElseLLVMImp(ExpressionLLVM cond, InstructionLLVM intr1, InstructionLLVM instr2) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); + return v.visitIfThenElseLLVM(this, h); } } - public static record WhileLLVMImp() implements InstructionLLVM{ + public static record WhileLLVMImp(ExpressionLLVM cond, InstructionLLVM instr) implements InstructionLLVM{ @Override public S accept(InstructionLLVMVisitor v, H h) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); + return v.visitWhileLLVM(this, h); } } diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index 48f0eed..a061105 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -3,6 +3,8 @@ FUNC INT main() { b:=3 c:=1 PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b - b:=c+1 - READ a, b, c, d + IF 1 THEN a:=a+1 FI + IF 2 THEN b:=c+1 ELSE + b:=c+1 + FI RETURN 4 + 6 * 5 + 2 } \ No newline at end of file