while imp en cours, todo : if else then

This commit is contained in:
trochas
2025-04-10 13:07:52 +02:00
8 changed files with 53 additions and 34 deletions

View File

@@ -103,9 +103,7 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
String str = indent + "IF ";
str +=(instr.e().accept(this, ""));
str +=" THEN ";
for(int i=0; i<instr.i1().size();i++){
str+= instr.i1().get(i).accept(this, "");
}
str+= instr.i1().accept(this, "");
return str;
}
@@ -114,13 +112,9 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
String str = indent + "IF ";
str +=(instr.e().accept(this, ""));
str +=" THEN ";
for(int i=0; i<instr.i1().size();i++){
str+= instr.i1().get(i).accept(this, "");
}
str+= instr.i1().accept(this, "");
str +=" ELSE ";
for(int i=0; i<instr.i2().size();i++){
str+= instr.i2().get(i).accept(this, "");
}
str+= instr.i2().accept(this, "");
return str;
}
@@ -139,9 +133,7 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
String str = indent+"WHILE ";
str += (instr.e().accept(this, ""));
str+= " DO ";
for(int i=0; i<instr.i1().size();i++){
str+= instr.i1().get(i).accept(this, "");
}
str+= instr.i1().accept(this, "");
str+= " DONE";
return str;
}

View File

@@ -102,21 +102,21 @@ public class Program{
}
}
public static record IfThenImp(Expression e, ArrayList<Instruction> i1) implements Instruction {
public static record IfThenImp(Expression e, Instruction i1) implements Instruction {
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitIfThen(this, h);
}
}
public static record IfThenElseImp(Expression e, ArrayList<Instruction> i1, ArrayList<Instruction> i2) implements Instruction {
public static record IfThenElseImp(Expression e, Instruction i1, Instruction i2) implements Instruction {
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitIfThenElse(this, h);
}
}
public static record WhileImp(Expression e, ArrayList<Instruction> i1) implements Instruction {
public static record WhileImp(Expression e, Instruction i1) implements Instruction {
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitWhile(this, h);

View File

@@ -27,6 +27,7 @@ public class SymTable {
private PStack<PMap<String,ValueTable>> 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;

View File

@@ -156,6 +156,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
ArrayList<InstructionLLVM> 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;
}

View File

@@ -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)

View File

@@ -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 <H, S> S accept(InstructionLLVMVisitor<H, S> 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 <H, S> S accept(InstructionLLVMVisitor<H, S> 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 <H, S> S accept(InstructionLLVMVisitor<H, S> 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 <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'accept'");
return v.visitWhileLLVM(this, h);
}
}