From af2e42ab892b5cfe1975451652d315dec833fc5d Mon Sep 17 00:00:00 2001 From: Rochas Date: Sat, 26 Apr 2025 23:23:16 +0200 Subject: [PATCH] correction TypeCheck --- .../java/TP2/Error/TypeCheckExprDiag.java | 68 ++-- src/main/java/TP2/Error/TypeChecking.java | 321 +++++++++--------- src/main/java/TP2/asd/toLLVM_Visitor.java | 27 +- tests/aLaMain.vsl | 7 + 4 files changed, 221 insertions(+), 202 deletions(-) diff --git a/src/main/java/TP2/Error/TypeCheckExprDiag.java b/src/main/java/TP2/Error/TypeCheckExprDiag.java index 484a729..2625026 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 d6f004e..7d604b7 100644 --- a/src/main/java/TP2/Error/TypeChecking.java +++ b/src/main/java/TP2/Error/TypeChecking.java @@ -1,155 +1,166 @@ -//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); + } + @Override + public TypeCheckExprDiag visitPrototype(PrototypeImp proto, SymTable h) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'visitPrototype'"); + } + } + + 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()); + } + + @Override + public TypeCheckExprDiag visitAppeal(Appeal instr, SymTable h) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'visitAppeal'"); + } + } +} diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 33843b2..7b6f7c1 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -98,19 +98,6 @@ public class toLLVM_Visitor implements ProgramVisitor //INSTRUCTION - @Override - public ArrayList visitReturn(Return_instrImp instr, SymTable h) { - InstrAndVal res = instr.e().accept(this,h); - ValLLVM var = res.val; - - InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var); - ArrayList result = new ArrayList<>(); - result.addAll(res.instrs); - result.add(r); - - return result; - } - @Override public ArrayList visitBloc(BlocImp instr, SymTable h) { ArrayList instrLLVM = new ArrayList<>(); @@ -134,6 +121,20 @@ public class toLLVM_Visitor implements ProgramVisitor return instrLLVM; } + @Override + public ArrayList visitReturn(Return_instrImp instr, SymTable h) { + InstrAndVal res = instr.e().accept(this,h); + ValLLVM var = res.val; + + InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var); + ArrayList result = new ArrayList<>(); + result.addAll(res.instrs); + result.add(r); + + return result; + } + + @Override public ArrayList visitAssign(AssignImp instr, SymTable h) { InstrAndVal res = instr.e().accept(this,h); diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index 47bb4a7..31b6d4f 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -1,4 +1,11 @@ PROTO INT add(x,y) + +FUNC INT add(x,y) { + INT z + y := x+y + RETURN z +} + FUNC INT main(x,y) { INT a,b,c,minh x := 5