toLLVM, manque table des symboles, et expression pas correct

This commit is contained in:
Rochas
2025-04-05 00:08:50 +02:00
parent 59ba13fe18
commit 7dfb07d59f
5 changed files with 124 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
package TP2.asd;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import TP2.asd.Interface.*;
import TP2.llvm.ProgramLLVM.*;
@@ -36,6 +37,7 @@ public class Program{
}
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function {
public FunctionImp(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }});
@@ -58,13 +60,13 @@ public class Program{
public DefineLLVM toLLVM() {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instructions.size(); i++){
instrLLVM.add(instructions.get(i).toLLVM());
instrLLVM.addAll(instructions.get(i).toLLVM());
}
//return new DefineLLVMImpl(nom, "int", instrLLVM);
return null;
return new DefineLLVMImpl(nom, type.toLLVM(), instrLLVM);
}
}
public static record ConstImp(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h);
@@ -75,12 +77,16 @@ public class Program{
}
@Override
public DefineLLVM toLLVM() {
public ArrayList<AssignLVMImp> toLLVM() { //TODO
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
ArrayList<AssignLVMImp> list = new ArrayList<>();
ConstLLVMImp cLLVM = new ConstLLVMImp(new IntLLVMImpl(),c);
list.add(new AssignLVMImp(new VarLLVMImpl("todo"), cLLVM));
return list;
}
}
public static record BinopExpressionImp(Op op,Expression e1, Expression e2) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitBinOp(this, h);
@@ -100,13 +106,22 @@ public class Program{
}
@Override
public DefineLLVM toLLVM() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
public ArrayList<AssignLVMImp> toLLVM() { //TODO si e1 ou e2 est une constante, elle doit pouvoir être mise directement dans l'expression
ArrayList<AssignLVMImp> list = new ArrayList<>();
ArrayList<AssignLVMImp> eLLVM1 = e1.toLLVM();
ArrayList<AssignLVMImp> eLLVM2 = e2.toLLVM();
list.addAll(eLLVM1);
list.addAll(eLLVM2);
VarLLVMImpl var1 = eLLVM1.getLast().getVar();
VarLLVMImpl var2 = eLLVM2.getLast().getVar();
list.add(new AssignLVMImp(new VarLLVMImpl("todo"), new BinOpLLVMImp(new IntLLVMImpl(),op,var1,var2)));
return list;
}
}
public static record Return_instrImp(Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitReturn(this,h);
@@ -117,11 +132,18 @@ public class Program{
}
@Override
public InstructionLLVM toLLVM() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
public ArrayList<InstructionLLVM> toLLVM() {
ArrayList<AssignLVMImp> list = e.toLLVM();
InstructionLLVM r = new ReturnLLVMImp(new IntLLVMImpl(),list.getLast().getVar());
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(list);
result.add(r);
return result;
}
}
public static record AssignImp(String t, Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitAssign(this, h);
@@ -134,24 +156,41 @@ public class Program{
}
@Override
public InstructionLLVM toLLVM() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toLLVM'");
public ArrayList<InstructionLLVM> toLLVM() {
ArrayList<AssignLVMImp> list = e.toLLVM();
InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(t),list.getLast().getVar());
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(list);
result.add(r);
return result;
}
}
public static record Type_voidImp() implements Type{
public String prettyprinter() {
return "VOID";
}
@Override
public TypeLLVM toLLVM() {
return new IntLLVMImpl();
}
}
public static record Type_intImp() implements Type{
public String prettyprinter() {
return "INT";
}
@Override
public TypeLLVM toLLVM() {
return new VoidLLVMImpl();
}
}
//Eval
public static class ProgramEval implements ProgramVisitor<Map<String, Integer>, Integer> {
@Override
@@ -165,6 +204,7 @@ public class Program{
}
}
public static class FunctionEval implements FunctionVisitor<Map<String, Integer>, Integer> {
@Override
@@ -178,6 +218,7 @@ public class Program{
}
}
public static class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
@Override
@@ -193,6 +234,7 @@ public class Program{
}
}
public static class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
public Integer visitConst(ConstImp c, Map<String,Integer> h){
return c.c();