modification de l'implémentation de prototype, il implement Function

This commit is contained in:
Rochas
2025-04-26 21:48:41 +02:00
parent 29ab19fd7a
commit a34802db55
10 changed files with 272 additions and 225 deletions

View File

@@ -15,14 +15,6 @@ public interface Interface{
public S visitProgram(ProgramImp prog, H h);
}
//PROTOTYPE
public interface Prototype{
public <H,S> S accept(PrototypeVisitor<H,S> v, H h);
}
public interface PrototypeVisitor<H,S> {
public S visitPrototype(PrototypeImp proto, H h);
}
//FUNCTION
public interface Function {
@@ -31,6 +23,7 @@ public interface Interface{
public interface FunctionVisitor<H,S> {
public S visitFunction(FunctionImp fun, H h);
public S visitPrototype(PrototypeImp proto, H h);
}
//DECLARATION
@@ -73,6 +66,7 @@ public interface Interface{
public S visitConst(ConstImp e,H h);
public S visitBinOp(BinopExpressionImp e, H h);
public S visitVar(VarImp e,H h);
public S visitAppeal(Appeal instr, H h);
}
public interface Type{

View File

@@ -4,7 +4,6 @@ import TP2.asd.Interface.*;
import TP2.asd.Program.*;
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
PrototypeVisitor<String,String>,
FunctionVisitor<String,String>,
DeclVisitor<String,String>,
InstrVisitor<String,String>,
@@ -19,11 +18,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override
public String visitProgram(ProgramImp prog, String indent) {
String str ="";
for(int i= 0; i<prog.protos().size();i++){
str+=prog.protos().get(i).accept(this, INDENT);
if(i<prog.protos().size()-1) str += "\n";
}
str+="\n";
for(int i = 0; i<prog.fonctions().size(); i++){
str += prog.fonctions().get(i).accept(this,INDENT);
if(i<prog.fonctions().size()-1) str += "\n";
@@ -163,6 +157,16 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
return str;
}
@Override
public String visitAppeal(Appeal instr,String indent){
String str = indent + instr.fName() + "(";
for(int i=0; i<instr.params().size();i++){
str += instr.params().get(i).accept(this, "");
if(i<instr.params().size()-1) str += ",";
}
return str+")";
}
//EXPRESSION
@Override

View File

@@ -7,7 +7,7 @@ import TP2.llvm.ProgramLLVM.*;
public class Program{
//Prog
public static record ProgramImp(ArrayList<Function> fonctions,ArrayList<Prototype> protos) implements ProgramI{
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
return v.visitProgram(this, h);
}
@@ -24,17 +24,9 @@ public class Program{
}
}
//Prototype
public static record PrototypeImp(Type type, String nom, ArrayList<VarImp> params) implements Prototype{
@Override
public <H, S> S accept(PrototypeVisitor<H, S> v, H h) {
return v.visitPrototype(this,h);
}
}
//Fonction
public static record FunctionImp(Type type, String nom, ArrayList<VarImp> params,Instruction instruction)implements Function {
public static record FunctionImp(Type type, String nom, ArrayList<VarImp> params,Instruction instruction) implements Function {
//public FunctionImp(Type type, String name, Instruction instruction) {
// this(type, name, new ArrayList<>() {{ add(instruction); }}); C KOI ?
//}
@@ -43,6 +35,13 @@ public class Program{
return v.visitFunction(this, h);
}
}
public static record PrototypeImp(Type type, String nom, ArrayList<VarImp> params) implements Function{
@Override
public <H, S> S accept(FunctionVisitor<H, S> v, H h) {
return v.visitPrototype(this,h);
}
}
//Expression
public static record ConstImp(int c) implements Expression{
@@ -63,6 +62,13 @@ public class Program{
}
}
public static record Appeal(String fName, ArrayList<Expression> params) implements Expression{
@Override
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitAppeal(this, h);
}
}
//Declaration
public static record DeclarationImp(Type t, ArrayList<String> s) implements Declaration{

View File

@@ -3,10 +3,16 @@ import java.util.Stack;
import org.pcollections.*;
import TP2.asd.Interface.Type;
import TP2.asd.Program.Type_intImp;
import TP2.llvm.ProgramLLVM.DefineLLVMImpl;
public class SymTable {
private PStack<PMap<String,ValueTable>> stackMap;
private PMap<String,DefineLLVMImpl> fuctionsMap;
private int id=1;
private int idLabel = 1;
public static class ValueTable{
public Type type;
public int id;
@@ -27,17 +33,26 @@ public class SymTable {
}
}
private PStack<PMap<String,ValueTable>> stackMap;
private int id=1;
public int idLabel = 1;
public SymTable(){
this.stackMap= ConsPStack.empty();
this.stackMap= ConsPStack.empty(); //todo : HashTreePMap.empty() stack sers à rien
this.fuctionsMap = HashTreePMap.empty();
}
public SymTable(PStack<PMap<String,ValueTable>> stackMap, int id){
this.stackMap= stackMap;
this.id = id;
}
public void addFunction(DefineLLVMImpl function){
if(!this.fuctionsMap.containsKey(function.name())){
this.fuctionsMap.plus(function.name(),function);
}
}
public DefineLLVMImpl getFunction(String name){
return this.fuctionsMap.get(name);
}
public int getNewId(){
int a = this.id;
this.id++;

View File

@@ -9,7 +9,7 @@ import TP2.llvm.Interface.*;
import TP2.llvm.ProgramLLVM.*;
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
FunctionVisitor<SymTable,DefineLLVM>,
FunctionVisitor<SymTable,DefineLLVMImpl>,
DeclVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndSymTable>,
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
@@ -23,10 +23,10 @@ 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> instrs;
public ArrayList<InstructionLLVM> instrs;
public ValLLVM val;
public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
public InstrAndVal(ArrayList<InstructionLLVM> instr, ValLLVM val){
this.instrs = instr;
this.val = val;
}
@@ -46,15 +46,20 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) {
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
for(int i = 0; i<prog.fonctions().size(); i++){
fonctionLLVM.add(prog.fonctions().get(i).accept(this, h));
DefineLLVMImpl function = prog.fonctions().get(i).accept(this, h);
h.addFunction(function);
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){
fonctionLLVM.add(function);
}
}
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);
}
//FUNCTION
@Override
public DefineLLVM visitFunction(FunctionImp fun, SymTable h) {
public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
ArrayList<VarLLVMImpl> paramsLLVM = new ArrayList<>();
@@ -70,6 +75,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
}
@Override
public DefineLLVMImpl visitPrototype(PrototypeImp fun, SymTable h) {
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), null,null);
}
//DECLARATION
@Override
@@ -245,6 +255,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return l;
}
//EXPRESSION
@Override
@@ -255,7 +266,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public InstrAndVal visitVar(VarImp e, SymTable h) {
ArrayList<AssignLVMImpl> l =new ArrayList<>();
ArrayList<InstructionLLVM> l =new ArrayList<>();
ValLLVM val = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.getVar(e.name()));
VarLLVMImpl varTemp = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),h.addNewTempVar().var);
@@ -265,7 +276,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
ArrayList<AssignLVMImpl> list = new ArrayList<>();
ArrayList<InstructionLLVM> list = new ArrayList<>();
InstrAndVal res1 = e.e1().accept(this, h);
InstrAndVal res2 = e.e2().accept(this, h);
@@ -290,6 +301,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return new InstrAndVal(list, var);
}
public InstrAndVal visitAppeal(Appeal instr,SymTable h){
ArrayList<InstructionLLVM> l = new ArrayList<>();
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
for(Expression param : instr.params()){
InstrAndVal result = param.accept(this,h);
l.add((InstructionLLVM) result.instrs);
paramsLLVM.add(result.val);
}
DefineLLVMImpl fLLVM = h.getFunction(instr.fName()); //on récupère la fonction LLVM dans la table des Symboles
if(fLLVM == null){
System.err.println("[VSL compile error] : la fonction n'existe pas, veuillez vous assurer de l'avoir déclarée avant l'appel");
}
l.add(new CallLLVMImpl(fLLVM,paramsLLVM,""));
return new InstrAndVal(l, null);
}
@Override
public TypeLLVM visitInt(Type_intImp t, SymTable h) {
return new IntLLVMImpl();