From a34802db55ce7b2cad7858b3ae928145b6cbc365 Mon Sep 17 00:00:00 2001 From: Rochas Date: Sat, 26 Apr 2025 21:48:41 +0200 Subject: [PATCH] =?UTF-8?q?modification=20de=20l'impl=C3=A9mentation=20de?= =?UTF-8?q?=20prototype,=20il=20implement=20Function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/TP2/Error/TypeCheckExprDiag.java | 68 ++-- src/main/java/TP2/Error/TypeChecking.java | 310 +++++++++--------- src/main/java/TP2/Main.java | 1 + src/main/java/TP2/asd/Interface.java | 10 +- .../java/TP2/asd/PrettyprinterVisitor.java | 16 +- src/main/java/TP2/asd/Program.java | 26 +- src/main/java/TP2/asd/SymTable.java | 23 +- src/main/java/TP2/asd/toLLVM_Visitor.java | 41 ++- .../TP2/llvm/PrettyprinterLLVM_Visitor.java | 1 - tests/aLaMain.vsl | 1 + 10 files changed, 272 insertions(+), 225 deletions(-) diff --git a/src/main/java/TP2/Error/TypeCheckExprDiag.java b/src/main/java/TP2/Error/TypeCheckExprDiag.java index 2625026..484a729 100644 --- a/src/main/java/TP2/Error/TypeCheckExprDiag.java +++ b/src/main/java/TP2/Error/TypeCheckExprDiag.java @@ -1,34 +1,34 @@ -package TP2.Error; -import TP2.asd.Interface.*; -public class TypeCheckExprDiag { - private Type t; - private String err; - private boolean check; - - public TypeCheckExprDiag(Type type) { - this.t = type; - this.check = true; - this.err = null; - } - - public TypeCheckExprDiag(String error){ - this.err=error; - this.check=false; - } - - public boolean get_check(){ - return this.check; - } - - public Type get_type(){ - return this.t; - } - - public static TypeCheckExprDiag error(String err){ - return new TypeCheckExprDiag(err); - } - - public static TypeCheckExprDiag checked(Type type){ - return new TypeCheckExprDiag(type); - } -} +//package TP2.Error; +//import TP2.asd.Interface.*; +//public class TypeCheckExprDiag { +// private Type t; +// private String err; +// private boolean check; +// +// public TypeCheckExprDiag(Type type) { +// this.t = type; +// this.check = true; +// this.err = null; +// } +// +// public TypeCheckExprDiag(String error){ +// this.err=error; +// this.check=false; +// } +// +// public boolean get_check(){ +// return this.check; +// } +// +// public Type get_type(){ +// return this.t; +// } +// +// public static TypeCheckExprDiag error(String err){ +// return new TypeCheckExprDiag(err); +// } +// +// public static TypeCheckExprDiag checked(Type type){ +// return new TypeCheckExprDiag(type); +// } +//} diff --git a/src/main/java/TP2/Error/TypeChecking.java b/src/main/java/TP2/Error/TypeChecking.java index d7c8ee7..d6f004e 100644 --- a/src/main/java/TP2/Error/TypeChecking.java +++ b/src/main/java/TP2/Error/TypeChecking.java @@ -1,155 +1,155 @@ -package TP2.Error; - -import TP2.asd.SymTable; -import TP2.asd.Program.*; -import TP2.asd.Interface.*; - -public class TypeChecking { - - public class TypeCheckProg implements ProgramVisitor{ - private TypeCheckFunction func_check; - @Override - public TypeCheckExprDiag visitProgram(ProgramImp prog, SymTable h) { - SymTable st= new SymTable(); - for (Function f : prog.fonctions()) { - TypeCheckExprDiag diag = f.accept(func_check, h); - if (!diag.get_check()) return diag; - } - return TypeCheckExprDiag.checked(null); - } - } - - public class TypeCheckFunction implements FunctionVisitor{ - private TypeCheckInstr instr_check; - @Override - public TypeCheckExprDiag visitFunction(FunctionImp f, SymTable h) { - return f.instruction().accept(instr_check,h); - } - } - - public class TypeCheckInstr implements InstrVisitor { - private TypeCheckExpr expr_check; - @Override - public TypeCheckExprDiag visitReturn(Return_instrImp instr, SymTable h) { - return instr.e().accept(expr_check, h); - } - - @Override - public TypeCheckExprDiag visitBloc(BlocImp instr, SymTable h) { - for(Instruction i: instr.instrs()){ - TypeCheckExprDiag diag= i.accept(this, h); - if(!diag.get_check()) return diag; - } - return TypeCheckExprDiag.checked(null); - } - - @Override - public TypeCheckExprDiag visitBlocDec(BlocDecImp instr, SymTable h) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'visitBlocDec'"); - } - - @Override - public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) { - if(!h.searchVar(instr.t())){ - return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas"); - } - Type t_type=h.getvar_Type(instr.t()); - TypeCheckExprDiag expr= instr.e().accept(expr_check, h); - - if (!expr.get_check()) return expr; - //Verify type t = Type expr - if(!t_type.getClass().equals(expr.get_type().getClass())){ - return TypeCheckExprDiag.error("Type d'expression est different que le type de variable"); - } - return TypeCheckExprDiag.checked(t_type); - } - - @Override - public TypeCheckExprDiag visitPrint(PrintImp instr, SymTable h) { - for(Object o :instr.t()){ - //We have string and expression - if(o instanceof Expression e){ - TypeCheckExprDiag result = e.accept(expr_check, h); - if (!result.get_check()) return result; - } - } - return TypeCheckExprDiag.checked(null); - } - - @Override - public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) { - for(VarImp v: instr.t()){ - if(!h.searchVar(v.name())){ - return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas"); - } - } - return TypeCheckExprDiag.checked(null); - } - - @Override - public TypeCheckExprDiag visitIfThen(IfThenImp instr, SymTable h) { - TypeCheckExprDiag cond =instr.e().accept(expr_check, h); - if(!cond.get_check()) return cond; - if (!(cond.get_type() instanceof Type_intImp)){ - return TypeCheckExprDiag.error("Condition n'est pas un int"); - } - return instr.i1().accept(this, h); - } - - @Override - public TypeCheckExprDiag visitIfThenElse(IfThenElseImp instr, SymTable h) { - TypeCheckExprDiag cond =instr.e().accept(expr_check, h); - if(!cond.get_check()) return cond; - if (!(cond.get_type() instanceof Type_intImp)){ - return TypeCheckExprDiag.error("Condition n'est pas un int"); - } - - TypeCheckExprDiag then= instr.i1().accept(this, h); - if (!then.get_check()) return then; - return instr.i2().accept(this, h); - } - - @Override - public TypeCheckExprDiag visitWhile(WhileImp instr, SymTable h) { - TypeCheckExprDiag cond =instr.e().accept(expr_check, h); - if(!cond.get_check()) return cond; - if (!(cond.get_type() instanceof Type_intImp)){ - return TypeCheckExprDiag.error("Condition n'est pas un int"); - } - return instr.i1().accept(this, h); - } - } - - public class TypeCheckExpr implements ExprVisitor{ - @Override - public TypeCheckExprDiag visitConst(ConstImp e, SymTable h) { - return TypeCheckExprDiag.checked(new Type_intImp()); - } - - @Override - public TypeCheckExprDiag visitVar(VarImp e, SymTable h) { - if(!h.searchVar(e.name())){ - return TypeCheckExprDiag.error("Ce variable n'existe pas"); - } - Type e_type= h.getvar_Type(e.name()); - return TypeCheckExprDiag.checked(e_type); - } - - @Override - public TypeCheckExprDiag visitBinOp(BinopExpressionImp e, SymTable h) { - TypeCheckExprDiag tce1 = e.e1().accept(this, h); - TypeCheckExprDiag tce2 = e.e2().accept(this, h); - - // Check if not ok then return its error - if(!tce1.get_check()) return tce1; - if(!tce2.get_check()) return tce2; - - // Check int + int - if (!(tce1.get_type() instanceof Type_intImp) || !(tce2.get_type() instanceof Type_intImp) ){ - return TypeCheckExprDiag.error("Ses types sont different"); - } - return TypeCheckExprDiag.checked(tce1.get_type()); - } - } -} +//package TP2.Error; +// +//import TP2.asd.SymTable; +//import TP2.asd.Program.*; +//import TP2.asd.Interface.*; +// +//public class TypeChecking { +// +// public class TypeCheckProg implements ProgramVisitor{ +// private TypeCheckFunction func_check; +// @Override +// public TypeCheckExprDiag visitProgram(ProgramImp prog, SymTable h) { +// SymTable st= new SymTable(); +// for (Function f : prog.fonctions()) { +// TypeCheckExprDiag diag = f.accept(func_check, h); +// if (!diag.get_check()) return diag; +// } +// return TypeCheckExprDiag.checked(null); +// } +// } +// +// public class TypeCheckFunction implements FunctionVisitor{ +// private TypeCheckInstr instr_check; +// @Override +// public TypeCheckExprDiag visitFunction(FunctionImp f, SymTable h) { +// return f.instruction().accept(instr_check,h); +// } +// } +// +// public class TypeCheckInstr implements InstrVisitor { +// private TypeCheckExpr expr_check; +// @Override +// public TypeCheckExprDiag visitReturn(Return_instrImp instr, SymTable h) { +// return instr.e().accept(expr_check, h); +// } +// +// @Override +// public TypeCheckExprDiag visitBloc(BlocImp instr, SymTable h) { +// for(Instruction i: instr.instrs()){ +// TypeCheckExprDiag diag= i.accept(this, h); +// if(!diag.get_check()) return diag; +// } +// return TypeCheckExprDiag.checked(null); +// } +// +// @Override +// public TypeCheckExprDiag visitBlocDec(BlocDecImp instr, SymTable h) { +// // TODO Auto-generated method stub +// throw new UnsupportedOperationException("Unimplemented method 'visitBlocDec'"); +// } +// +// @Override +// public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) { +// if(!h.searchVar(instr.t())){ +// return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas"); +// } +// Type t_type=h.getvar_Type(instr.t()); +// TypeCheckExprDiag expr= instr.e().accept(expr_check, h); +// +// if (!expr.get_check()) return expr; +// //Verify type t = Type expr +// if(!t_type.getClass().equals(expr.get_type().getClass())){ +// return TypeCheckExprDiag.error("Type d'expression est different que le type de variable"); +// } +// return TypeCheckExprDiag.checked(t_type); +// } +// +// @Override +// public TypeCheckExprDiag visitPrint(PrintImp instr, SymTable h) { +// for(Object o :instr.t()){ +// //We have string and expression +// if(o instanceof Expression e){ +// TypeCheckExprDiag result = e.accept(expr_check, h); +// if (!result.get_check()) return result; +// } +// } +// return TypeCheckExprDiag.checked(null); +// } +// +// @Override +// public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) { +// for(VarImp v: instr.t()){ +// if(!h.searchVar(v.name())){ +// return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas"); +// } +// } +// return TypeCheckExprDiag.checked(null); +// } +// +// @Override +// public TypeCheckExprDiag visitIfThen(IfThenImp instr, SymTable h) { +// TypeCheckExprDiag cond =instr.e().accept(expr_check, h); +// if(!cond.get_check()) return cond; +// if (!(cond.get_type() instanceof Type_intImp)){ +// return TypeCheckExprDiag.error("Condition n'est pas un int"); +// } +// return instr.i1().accept(this, h); +// } +// +// @Override +// public TypeCheckExprDiag visitIfThenElse(IfThenElseImp instr, SymTable h) { +// TypeCheckExprDiag cond =instr.e().accept(expr_check, h); +// if(!cond.get_check()) return cond; +// if (!(cond.get_type() instanceof Type_intImp)){ +// return TypeCheckExprDiag.error("Condition n'est pas un int"); +// } +// +// TypeCheckExprDiag then= instr.i1().accept(this, h); +// if (!then.get_check()) return then; +// return instr.i2().accept(this, h); +// } +// +// @Override +// public TypeCheckExprDiag visitWhile(WhileImp instr, SymTable h) { +// TypeCheckExprDiag cond =instr.e().accept(expr_check, h); +// if(!cond.get_check()) return cond; +// if (!(cond.get_type() instanceof Type_intImp)){ +// return TypeCheckExprDiag.error("Condition n'est pas un int"); +// } +// return instr.i1().accept(this, h); +// } +// } +// +// public class TypeCheckExpr implements ExprVisitor{ +// @Override +// public TypeCheckExprDiag visitConst(ConstImp e, SymTable h) { +// return TypeCheckExprDiag.checked(new Type_intImp()); +// } +// +// @Override +// public TypeCheckExprDiag visitVar(VarImp e, SymTable h) { +// if(!h.searchVar(e.name())){ +// return TypeCheckExprDiag.error("Ce variable n'existe pas"); +// } +// Type e_type= h.getvar_Type(e.name()); +// return TypeCheckExprDiag.checked(e_type); +// } +// +// @Override +// public TypeCheckExprDiag visitBinOp(BinopExpressionImp e, SymTable h) { +// TypeCheckExprDiag tce1 = e.e1().accept(this, h); +// TypeCheckExprDiag tce2 = e.e2().accept(this, h); +// +// // Check if not ok then return its error +// if(!tce1.get_check()) return tce1; +// if(!tce2.get_check()) return tce2; +// +// // Check int + int +// if (!(tce1.get_type() instanceof Type_intImp) || !(tce2.get_type() instanceof Type_intImp) ){ +// return TypeCheckExprDiag.error("Ses types sont different"); +// } +// return TypeCheckExprDiag.checked(tce1.get_type()); +// } +// } +//} diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index fa416a3..181b173 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -19,6 +19,7 @@ import java.util.*; /* ./gradlew build java -jar build/libs/TP2.jar tests/fragment0/priority2.vsl +java -jar build/libs/TP2.jar tests/aLaMain.vsl */ public class Main { public static void main(String[] args) { diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index 3e1f290..bb2feb2 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -15,14 +15,6 @@ public interface Interface{ public S visitProgram(ProgramImp prog, H h); } - //PROTOTYPE - public interface Prototype{ - public S accept(PrototypeVisitor v, H h); - } - - public interface PrototypeVisitor { - public S visitPrototype(PrototypeImp proto, H h); - } //FUNCTION public interface Function { @@ -31,6 +23,7 @@ public interface Interface{ public interface FunctionVisitor { 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{ diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java index 7dee6f4..57fab59 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -4,7 +4,6 @@ import TP2.asd.Interface.*; import TP2.asd.Program.*; public class PrettyprinterVisitor implements ProgramVisitor, - PrototypeVisitor, FunctionVisitor, DeclVisitor, InstrVisitor, @@ -19,11 +18,6 @@ public class PrettyprinterVisitor implements ProgramVisitor, @Override public String visitProgram(ProgramImp prog, String indent) { String str =""; - for(int i= 0; i, return str; } + @Override + public String visitAppeal(Appeal instr,String indent){ + String str = indent + instr.fName() + "("; + for(int i=0; i fonctions,ArrayList protos) implements ProgramI{ + public static record ProgramImp(ArrayList fonctions) implements ProgramI{ public S accept(ProgramVisitor 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 params) implements Prototype{ - @Override - public S accept(PrototypeVisitor v, H h) { - return v.visitPrototype(this,h); - } - - } //Fonction - public static record FunctionImp(Type type, String nom, ArrayList params,Instruction instruction)implements Function { + public static record FunctionImp(Type type, String nom, ArrayList 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 params) implements Function{ + @Override + public S accept(FunctionVisitor 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 params) implements Expression{ + @Override + public S accept(ExprVisitor v, H h) { + return v.visitAppeal(this, h); + } + } + //Declaration public static record DeclarationImp(Type t, ArrayList s) implements Declaration{ diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index c6f5fbd..51ea6b0 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -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> stackMap; + private PMap 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> 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> 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++; diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 7b45a3f..33843b2 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -9,7 +9,7 @@ import TP2.llvm.Interface.*; import TP2.llvm.ProgramLLVM.*; public class toLLVM_Visitor implements ProgramVisitor, - FunctionVisitor, + FunctionVisitor, DeclVisitor, InstrVisitor>, ExprVisitor, @@ -23,10 +23,10 @@ public class toLLVM_Visitor implements ProgramVisitor une simplement un val (var ou const) ou un binop */ public static class InstrAndVal{ - public ArrayList instrs; + public ArrayList instrs; public ValLLVM val; - public InstrAndVal(ArrayList instr, ValLLVM val){ + public InstrAndVal(ArrayList instr, ValLLVM val){ this.instrs = instr; this.val = val; } @@ -46,15 +46,20 @@ public class toLLVM_Visitor implements ProgramVisitor public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) { ArrayList fonctionLLVM = new ArrayList<>(); for(int i = 0; i(),fonctionLLVM); } + //FUNCTION @Override - public DefineLLVM visitFunction(FunctionImp fun, SymTable h) { + public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) { ArrayList instrLLVM = new ArrayList<>(); ArrayList paramsLLVM = new ArrayList<>(); @@ -70,6 +75,11 @@ public class toLLVM_Visitor implements ProgramVisitor 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 return l; } + //EXPRESSION @Override @@ -255,7 +266,7 @@ public class toLLVM_Visitor implements ProgramVisitor @Override public InstrAndVal visitVar(VarImp e, SymTable h) { - ArrayList l =new ArrayList<>(); + ArrayList 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 @Override public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) { - ArrayList list = new ArrayList<>(); + ArrayList 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 return new InstrAndVal(list, var); } + public InstrAndVal visitAppeal(Appeal instr,SymTable h){ + ArrayList l = new ArrayList<>(); + ArrayList 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(); diff --git a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java index e030e3a..4b7cad9 100644 --- a/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java +++ b/src/main/java/TP2/llvm/PrettyprinterLLVM_Visitor.java @@ -3,7 +3,6 @@ package TP2.llvm; import java.util.ArrayList; import java.util.List; -import TP2.asd.Program.IfThenElseImp; import TP2.llvm.Interface.*; import TP2.llvm.ProgramLLVM.*; diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index 9cf8966..fa984d7 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -1,6 +1,7 @@ PROTO INT add(x,y) FUNC INT main(x,y) { INT a,b,c,minh + x = 5 minh := x * y b:=3 c:=1