Merge remote-tracking branch 'origin/main'

# Conflicts:
#	src/main/java/TP2/asd/Program.java
This commit is contained in:
Vu Tuan Minh
2025-04-09 12:33:25 +02:00
6 changed files with 57 additions and 19 deletions

View File

@@ -61,8 +61,15 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override
public String visitPrint(PrintImp instr, String indent) {
String str = indent + "PRINT ";
for (String elem: instr.t()){
str += "\"" +elem +"\"";
for(int i = 0; i<instr.t().size(); i++){
String g = "";
Object o = instr.t().get(i);
if(o instanceof String){
str += "\"" + instr.t().get(i) +"\"";
}
else if(o instanceof Expression){
str += g + ((Expression)instr.t().get(i)).accept(this,"");
}
}
return str;
}

View File

@@ -5,6 +5,7 @@ import TP2.asd.Interface.*;
import TP2.llvm.ProgramLLVM.*;
public class Program{
//Prog
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
@@ -84,7 +85,7 @@ public class Program{
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitRead(this, h);
}
}
}
//Type

View File

@@ -11,7 +11,7 @@ import TP2.llvm.ProgramLLVM.*;
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
FunctionVisitor<SymTable,DefineLLVM>,
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrOrVal>,
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
TypeVisitor<SymTable,TypeLLVM>
{
@@ -21,13 +21,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
toLLVM ne renvoit pas la même chose si l'expression est
une simplement un val (var ou const) ou un binop
*/
public static class InstrOrVal{
public static class InstrAndVal{
public ArrayList<AssignLVMImp> instr = null;
public ValLLVM val = null;
public InstrOrVal(ArrayList<AssignLVMImp> instr, ValLLVM val){
public InstrAndVal(ArrayList<AssignLVMImp> instr, ValLLVM val){
this.instr = instr;
this.val = instr.getLast().var();
this.val = val;
}
}
@@ -57,7 +57,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitReturn(Return_instrImp instr, SymTable h) {
InstrOrVal res = instr.e().accept(this,h);
InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val;
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
@@ -70,7 +70,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
InstrOrVal res = instr.e().accept(this,h);
InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val;
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr);
@@ -93,8 +93,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitPrint'");
ArrayList<InstructionLLVM> l = new ArrayList<>();
l.add(new PrintLLVMImp(new ArrayList())); //TODO
return l;
}
@Override
@@ -106,23 +107,23 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
//EXPRESSION
@Override
public InstrOrVal visitConst(ConstImp e, SymTable h) {
public InstrAndVal visitConst(ConstImp e, SymTable h) {
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c());
return new InstrOrVal(new ArrayList<>(), val);
return new InstrAndVal(new ArrayList<>(), val);
}
@Override
public InstrOrVal visitVar(VarImp e, SymTable h) {
public InstrAndVal visitVar(VarImp e, SymTable h) {
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
return new InstrOrVal(new ArrayList<>(), val);
return new InstrAndVal(new ArrayList<>(), val);
}
@Override
public InstrOrVal visitBinOp(BinopExpressionImp e, SymTable h) {
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
ArrayList<AssignLVMImp> list = new ArrayList<>();
InstrOrVal res1 = e.e1().accept(this, h);
InstrOrVal res2 = e.e2().accept(this, h);
InstrAndVal res1 = e.e1().accept(this, h);
InstrAndVal res2 = e.e2().accept(this, h);
ValLLVM val1 = res1.val;
ValLLVM val2 = res2.val;
@@ -139,7 +140,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
VarLLVMImpl var = new VarLLVMImpl(type,temp);
list.add(new AssignLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2)));
return new InstrOrVal(list, var);
return new InstrAndVal(list, var);
}
@Override

View File

@@ -29,6 +29,8 @@ public interface Interface {
public S visitReturnLLVM(ReturnLLVMImp instr, H h);
public S visitAssignLLVM(AssignLVMImp instr, H h);
public S visitStoreLLVM(StoreLLVMImp instr, H h);
public S visitPrintLLVM(PrintLLVMImp instr, H h);
public S visitReadLLVM(ReadLLVMImp instr, H h);
}
//////////ExpressionLLVM (expression)

View File

@@ -100,6 +100,17 @@ TypeLLVMVisitor<String,String>
return "load" + " i" + e.nbBits() + ", i"+ e.nbBits2() + "* %" + e.val().accept(this, h);
}
@Override
public String visitPrintLLVM(PrintLLVMImp instr, String h) {
return INDENT+"print";
}
@Override
public String visitReadLLVM(ReadLLVMImp instr, String h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitReadLLVM'");
}
@Override
public String visitValLLVM(ValLLVMImpl e, String h) {
return e.val() + "";
@@ -121,4 +132,5 @@ TypeLLVMVisitor<String,String>
}
}

View File

@@ -61,6 +61,21 @@ public class ProgramLLVM {
}
}
public static record PrintLLVMImp(ArrayList<String> l) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitPrintLLVM(this, h);
}
}
public static record ReadLLVMImp(ArrayList<String> l) implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitReadLLVM(this, h);
}
}
//Expression :
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{