todo declaration c pt

This commit is contained in:
trochas
2025-04-10 11:13:09 +02:00
parent ecdb8ac687
commit 9905831b24
5 changed files with 72 additions and 45 deletions

View File

@@ -4,7 +4,7 @@ import TP2.asd.Program.*;
import TP2.llvm.ProgramLLVM.*;
public interface Interface{
//////////Program
//PROGRAM
public interface ProgramI {
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
public String prettyprinter();
@@ -16,7 +16,7 @@ public interface Interface{
}
//////////Function
//FUNCTION
public interface Function {
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
}
@@ -25,16 +25,26 @@ public interface Interface{
public S visitFunction(FunctionImp fun, H h);
}
//DECLARATION
public interface Declaration{
public <H,S> S accept(DeclVisitor<H,S> v, H h);
}
public interface DeclVisitor<H,S>{
public S visitDeclaration(DeclarationImp instr,H h);
}
//INSTRUCTION
//////////Instruction
public interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h);
}
public interface InstrVisitor<H,S>{
public S visitReturn(Return_instrImp instr, H h);
public S visitAssign(AssignImp instr, H h);
public S visitDeclaration(DeclarationImp instr,H h);
public S visitPrint(PrintImp instr, H h);
public S visitRead(ReadImp instr,H h);
public S visitIfThen(IfThenImp instr, H h);
@@ -43,7 +53,7 @@ public interface Interface{
}
//////////Expression
//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 {

View File

@@ -5,6 +5,7 @@ import TP2.asd.Program.*;
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
FunctionVisitor<String,String>,
DeclVisitor<String,String>,
InstrVisitor<String,String>,
ExprVisitor<String,String>,
TypeVisitor<String,String>
@@ -36,6 +37,18 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
return str;
}
//DELCARATION
@Override
public String visitDeclaration(DeclarationImp instr, String indent) {
String str = indent +instr.t().accept(this,"") + " ";
for(int i = 0; i<instr.s().size();i++){
str += instr.s().get(i);
if(i<instr.s().size()-1) str += ",";
}
return str;
}
//INSTRUCTION
@Override
@@ -48,16 +61,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
return indent + instr.t()+ " := " + instr.e().accept(this,"");
}
@Override
public String visitDeclaration(DeclarationImp instr, String indent) {
String str = indent +instr.t().accept(this,"") + " ";
for(int i = 0; i<instr.s().size();i++){
str += instr.s().get(i);
if(i<instr.s().size()-1) str += ",";
}
return str;
}
@Override
public String visitPrint(PrintImp instr, String indent) {
String str = indent + "PRINT ";

View File

@@ -54,25 +54,27 @@ public class Program{
}
}
//Declaration
public static record DeclarationImp(Type t, ArrayList<String> s) implements Declaration{
@Override
public <H, S> S accept(DeclVisitor<H, S> v, H h) {
return v.visitDeclaration(this, h);
}
}
//Instructions
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);
}
}
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);
}
}
public static record DeclarationImp(Type t, ArrayList<String> s) implements Instruction{
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitDeclaration(this, h);
}
}
public static record PrintImp(ArrayList<Object> t) implements Instruction{
@Override

View File

@@ -10,6 +10,7 @@ import TP2.llvm.ProgramLLVM.*;
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
FunctionVisitor<SymTable,DefineLLVM>,
DeclVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndSymTable>,
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
TypeVisitor<SymTable,TypeLLVM>
@@ -22,15 +23,23 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
une simplement un val (var ou const) ou un binop
*/
public static class InstrAndVal{
public ArrayList<AssignLVMImpl> instr = null;
public ValLLVM val = null;
public ArrayList<AssignLVMImpl> instrs;
public ValLLVM val;
public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
this.instr = instr;
this.instrs = instr;
this.val = val;
}
}
public static class InstrAndSymTable{
public ArrayList<InstructionLLVM> instrs;
public SymTable symTable;
public InstrAndSymTable(ArrayList<InstructionLLVM> instrs, SymTable symTable){
this.instrs = instrs;
this.symTable = symTable;
}
}
//PROGRAM
@Override
@@ -53,6 +62,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM);
}
//DECLARATION
@Override
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
ArrayList<InstructionLLVM> list = new ArrayList<>();
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
//String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i));
Result r = h.addVar(instr.s().get(i));
String name = r.var;
h = r.symTable;
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
}
return new InstrAndSymTable(list,h);
}
//INSTRUCTION
@Override
@@ -62,7 +87,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var);
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr);
result.addAll(res.instrs);
result.add(r);
return result;
@@ -73,26 +98,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val;
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr);
result.addAll(res.instrs);
//InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
InstructionLLVM r = new StoreLLVMImpl(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()/*"h.getVar(instr.t())*/));
InstructionLLVM r = new StoreLLVMImpl(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),h.getVar(instr.t())));
result.add(r);
return result;
}
@Override
public ArrayList<InstructionLLVM> visitDeclaration(DeclarationImp instr, SymTable h) {
ArrayList<InstructionLLVM> list = new ArrayList<>();
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
//String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i));
Result r = h.addVar(instr.s().get(i));
String name = r.var;
h = r.symTable;
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
}
return list;
}
@Override
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
@@ -151,8 +163,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ValLLVM val1 = res1.val;
ValLLVM val2 = res2.val;
list.addAll(res1.instr);
list.addAll(res2.instr);
list.addAll(res1.instrs);
list.addAll(res2.instrs);
TypeLLVM type = val1.getType();