correction TypeCheck
This commit is contained in:
@@ -1,34 +1,34 @@
|
|||||||
//package TP2.Error;
|
package TP2.Error;
|
||||||
//import TP2.asd.Interface.*;
|
import TP2.asd.Interface.*;
|
||||||
//public class TypeCheckExprDiag {
|
public class TypeCheckExprDiag {
|
||||||
// private Type t;
|
private Type t;
|
||||||
// private String err;
|
private String err;
|
||||||
// private boolean check;
|
private boolean check;
|
||||||
//
|
|
||||||
// public TypeCheckExprDiag(Type type) {
|
public TypeCheckExprDiag(Type type) {
|
||||||
// this.t = type;
|
this.t = type;
|
||||||
// this.check = true;
|
this.check = true;
|
||||||
// this.err = null;
|
this.err = null;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public TypeCheckExprDiag(String error){
|
public TypeCheckExprDiag(String error){
|
||||||
// this.err=error;
|
this.err=error;
|
||||||
// this.check=false;
|
this.check=false;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public boolean get_check(){
|
public boolean get_check(){
|
||||||
// return this.check;
|
return this.check;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public Type get_type(){
|
public Type get_type(){
|
||||||
// return this.t;
|
return this.t;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public static TypeCheckExprDiag error(String err){
|
public static TypeCheckExprDiag error(String err){
|
||||||
// return new TypeCheckExprDiag(err);
|
return new TypeCheckExprDiag(err);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public static TypeCheckExprDiag checked(Type type){
|
public static TypeCheckExprDiag checked(Type type){
|
||||||
// return new TypeCheckExprDiag(type);
|
return new TypeCheckExprDiag(type);
|
||||||
// }
|
}
|
||||||
//}
|
}
|
||||||
|
|||||||
@@ -1,155 +1,166 @@
|
|||||||
//package TP2.Error;
|
package TP2.Error;
|
||||||
//
|
|
||||||
//import TP2.asd.SymTable;
|
import TP2.asd.SymTable;
|
||||||
//import TP2.asd.Program.*;
|
import TP2.asd.Program.*;
|
||||||
//import TP2.asd.Interface.*;
|
import TP2.asd.Interface.*;
|
||||||
//
|
|
||||||
//public class TypeChecking {
|
public class TypeChecking {
|
||||||
//
|
|
||||||
// public class TypeCheckProg implements ProgramVisitor<SymTable, TypeCheckExprDiag>{
|
public class TypeCheckProg implements ProgramVisitor<SymTable, TypeCheckExprDiag>{
|
||||||
// private TypeCheckFunction func_check;
|
private TypeCheckFunction func_check;
|
||||||
// @Override
|
@Override
|
||||||
// public TypeCheckExprDiag visitProgram(ProgramImp prog, SymTable h) {
|
public TypeCheckExprDiag visitProgram(ProgramImp prog, SymTable h) {
|
||||||
// SymTable st= new SymTable();
|
SymTable st= new SymTable();
|
||||||
// for (Function f : prog.fonctions()) {
|
for (Function f : prog.fonctions()) {
|
||||||
// TypeCheckExprDiag diag = f.accept(func_check, h);
|
TypeCheckExprDiag diag = f.accept(func_check, h);
|
||||||
// if (!diag.get_check()) return diag;
|
if (!diag.get_check()) return diag;
|
||||||
// }
|
}
|
||||||
// return TypeCheckExprDiag.checked(null);
|
return TypeCheckExprDiag.checked(null);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public class TypeCheckFunction implements FunctionVisitor<SymTable, TypeCheckExprDiag>{
|
public class TypeCheckFunction implements FunctionVisitor<SymTable, TypeCheckExprDiag>{
|
||||||
// private TypeCheckInstr instr_check;
|
private TypeCheckInstr instr_check;
|
||||||
// @Override
|
@Override
|
||||||
// public TypeCheckExprDiag visitFunction(FunctionImp f, SymTable h) {
|
public TypeCheckExprDiag visitFunction(FunctionImp f, SymTable h) {
|
||||||
// return f.instruction().accept(instr_check,h);
|
return f.instruction().accept(instr_check,h);
|
||||||
// }
|
}
|
||||||
// }
|
@Override
|
||||||
//
|
public TypeCheckExprDiag visitPrototype(PrototypeImp proto, SymTable h) {
|
||||||
// public class TypeCheckInstr implements InstrVisitor<SymTable, TypeCheckExprDiag> {
|
// TODO Auto-generated method stub
|
||||||
// private TypeCheckExpr expr_check;
|
throw new UnsupportedOperationException("Unimplemented method 'visitPrototype'");
|
||||||
// @Override
|
}
|
||||||
// public TypeCheckExprDiag visitReturn(Return_instrImp instr, SymTable h) {
|
}
|
||||||
// return instr.e().accept(expr_check, h);
|
|
||||||
// }
|
public class TypeCheckInstr implements InstrVisitor<SymTable, TypeCheckExprDiag> {
|
||||||
//
|
private TypeCheckExpr expr_check;
|
||||||
// @Override
|
@Override
|
||||||
// public TypeCheckExprDiag visitBloc(BlocImp instr, SymTable h) {
|
public TypeCheckExprDiag visitReturn(Return_instrImp instr, SymTable h) {
|
||||||
// for(Instruction i: instr.instrs()){
|
return instr.e().accept(expr_check, h);
|
||||||
// TypeCheckExprDiag diag= i.accept(this, h);
|
}
|
||||||
// if(!diag.get_check()) return diag;
|
|
||||||
// }
|
@Override
|
||||||
// return TypeCheckExprDiag.checked(null);
|
public TypeCheckExprDiag visitBloc(BlocImp instr, SymTable h) {
|
||||||
// }
|
for(Instruction i: instr.instrs()){
|
||||||
//
|
TypeCheckExprDiag diag= i.accept(this, h);
|
||||||
// @Override
|
if(!diag.get_check()) return diag;
|
||||||
// public TypeCheckExprDiag visitBlocDec(BlocDecImp instr, SymTable h) {
|
}
|
||||||
// // TODO Auto-generated method stub
|
return TypeCheckExprDiag.checked(null);
|
||||||
// throw new UnsupportedOperationException("Unimplemented method 'visitBlocDec'");
|
}
|
||||||
// }
|
|
||||||
//
|
@Override
|
||||||
// @Override
|
public TypeCheckExprDiag visitBlocDec(BlocDecImp instr, SymTable h) {
|
||||||
// public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) {
|
// TODO Auto-generated method stub
|
||||||
// if(!h.searchVar(instr.t())){
|
throw new UnsupportedOperationException("Unimplemented method 'visitBlocDec'");
|
||||||
// return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas");
|
}
|
||||||
// }
|
|
||||||
// Type t_type=h.getvar_Type(instr.t());
|
@Override
|
||||||
// TypeCheckExprDiag expr= instr.e().accept(expr_check, h);
|
public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) {
|
||||||
//
|
if(!h.searchVar(instr.t())){
|
||||||
// if (!expr.get_check()) return expr;
|
return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas");
|
||||||
// //Verify type t = Type expr
|
}
|
||||||
// if(!t_type.getClass().equals(expr.get_type().getClass())){
|
Type t_type=h.getvar_Type(instr.t());
|
||||||
// return TypeCheckExprDiag.error("Type d'expression est different que le type de variable");
|
TypeCheckExprDiag expr= instr.e().accept(expr_check, h);
|
||||||
// }
|
|
||||||
// return TypeCheckExprDiag.checked(t_type);
|
if (!expr.get_check()) return expr;
|
||||||
// }
|
//Verify type t = Type expr
|
||||||
//
|
if(!t_type.getClass().equals(expr.get_type().getClass())){
|
||||||
// @Override
|
return TypeCheckExprDiag.error("Type d'expression est different que le type de variable");
|
||||||
// public TypeCheckExprDiag visitPrint(PrintImp instr, SymTable h) {
|
}
|
||||||
// for(Object o :instr.t()){
|
return TypeCheckExprDiag.checked(t_type);
|
||||||
// //We have string and expression
|
}
|
||||||
// if(o instanceof Expression e){
|
|
||||||
// TypeCheckExprDiag result = e.accept(expr_check, h);
|
@Override
|
||||||
// if (!result.get_check()) return result;
|
public TypeCheckExprDiag visitPrint(PrintImp instr, SymTable h) {
|
||||||
// }
|
for(Object o :instr.t()){
|
||||||
// }
|
//We have string and expression
|
||||||
// return TypeCheckExprDiag.checked(null);
|
if(o instanceof Expression e){
|
||||||
// }
|
TypeCheckExprDiag result = e.accept(expr_check, h);
|
||||||
//
|
if (!result.get_check()) return result;
|
||||||
// @Override
|
}
|
||||||
// public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) {
|
}
|
||||||
// for(VarImp v: instr.t()){
|
return TypeCheckExprDiag.checked(null);
|
||||||
// if(!h.searchVar(v.name())){
|
}
|
||||||
// return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas");
|
|
||||||
// }
|
@Override
|
||||||
// }
|
public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) {
|
||||||
// return TypeCheckExprDiag.checked(null);
|
for(VarImp v: instr.t()){
|
||||||
// }
|
if(!h.searchVar(v.name())){
|
||||||
//
|
return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas");
|
||||||
// @Override
|
}
|
||||||
// public TypeCheckExprDiag visitIfThen(IfThenImp instr, SymTable h) {
|
}
|
||||||
// TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
return TypeCheckExprDiag.checked(null);
|
||||||
// if(!cond.get_check()) return cond;
|
}
|
||||||
// if (!(cond.get_type() instanceof Type_intImp)){
|
|
||||||
// return TypeCheckExprDiag.error("Condition n'est pas un int");
|
@Override
|
||||||
// }
|
public TypeCheckExprDiag visitIfThen(IfThenImp instr, SymTable h) {
|
||||||
// return instr.i1().accept(this, h);
|
TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
||||||
// }
|
if(!cond.get_check()) return cond;
|
||||||
//
|
if (!(cond.get_type() instanceof Type_intImp)){
|
||||||
// @Override
|
return TypeCheckExprDiag.error("Condition n'est pas un int");
|
||||||
// public TypeCheckExprDiag visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
}
|
||||||
// TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
return instr.i1().accept(this, h);
|
||||||
// if(!cond.get_check()) return cond;
|
}
|
||||||
// if (!(cond.get_type() instanceof Type_intImp)){
|
|
||||||
// return TypeCheckExprDiag.error("Condition n'est pas un int");
|
@Override
|
||||||
// }
|
public TypeCheckExprDiag visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
||||||
//
|
TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
||||||
// TypeCheckExprDiag then= instr.i1().accept(this, h);
|
if(!cond.get_check()) return cond;
|
||||||
// if (!then.get_check()) return then;
|
if (!(cond.get_type() instanceof Type_intImp)){
|
||||||
// return instr.i2().accept(this, h);
|
return TypeCheckExprDiag.error("Condition n'est pas un int");
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Override
|
TypeCheckExprDiag then= instr.i1().accept(this, h);
|
||||||
// public TypeCheckExprDiag visitWhile(WhileImp instr, SymTable h) {
|
if (!then.get_check()) return then;
|
||||||
// TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
return instr.i2().accept(this, h);
|
||||||
// if(!cond.get_check()) return cond;
|
}
|
||||||
// if (!(cond.get_type() instanceof Type_intImp)){
|
|
||||||
// return TypeCheckExprDiag.error("Condition n'est pas un int");
|
@Override
|
||||||
// }
|
public TypeCheckExprDiag visitWhile(WhileImp instr, SymTable h) {
|
||||||
// return instr.i1().accept(this, 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");
|
||||||
// public class TypeCheckExpr implements ExprVisitor<SymTable ,TypeCheckExprDiag>{
|
}
|
||||||
// @Override
|
return instr.i1().accept(this, h);
|
||||||
// public TypeCheckExprDiag visitConst(ConstImp e, SymTable h) {
|
}
|
||||||
// return TypeCheckExprDiag.checked(new Type_intImp());
|
}
|
||||||
// }
|
|
||||||
//
|
public class TypeCheckExpr implements ExprVisitor<SymTable ,TypeCheckExprDiag>{
|
||||||
// @Override
|
@Override
|
||||||
// public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
|
public TypeCheckExprDiag visitConst(ConstImp e, SymTable h) {
|
||||||
// if(!h.searchVar(e.name())){
|
return TypeCheckExprDiag.checked(new Type_intImp());
|
||||||
// return TypeCheckExprDiag.error("Ce variable n'existe pas");
|
}
|
||||||
// }
|
|
||||||
// Type e_type= h.getvar_Type(e.name());
|
@Override
|
||||||
// return TypeCheckExprDiag.checked(e_type);
|
public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
|
||||||
// }
|
if(!h.searchVar(e.name())){
|
||||||
//
|
return TypeCheckExprDiag.error("Ce variable n'existe pas");
|
||||||
// @Override
|
}
|
||||||
// public TypeCheckExprDiag visitBinOp(BinopExpressionImp e, SymTable h) {
|
Type e_type= h.getvar_Type(e.name());
|
||||||
// TypeCheckExprDiag tce1 = e.e1().accept(this, h);
|
return TypeCheckExprDiag.checked(e_type);
|
||||||
// TypeCheckExprDiag tce2 = e.e2().accept(this, h);
|
}
|
||||||
//
|
|
||||||
// // Check if not ok then return its error
|
@Override
|
||||||
// if(!tce1.get_check()) return tce1;
|
public TypeCheckExprDiag visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||||
// if(!tce2.get_check()) return tce2;
|
TypeCheckExprDiag tce1 = e.e1().accept(this, h);
|
||||||
//
|
TypeCheckExprDiag tce2 = e.e2().accept(this, h);
|
||||||
// // Check int + int
|
|
||||||
// if (!(tce1.get_type() instanceof Type_intImp) || !(tce2.get_type() instanceof Type_intImp) ){
|
// Check if not ok then return its error
|
||||||
// return TypeCheckExprDiag.error("Ses types sont different");
|
if(!tce1.get_check()) return tce1;
|
||||||
// }
|
if(!tce2.get_check()) return tce2;
|
||||||
// return TypeCheckExprDiag.checked(tce1.get_type());
|
|
||||||
// }
|
// 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'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -98,19 +98,6 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
//INSTRUCTION
|
//INSTRUCTION
|
||||||
|
|
||||||
@Override
|
|
||||||
public ArrayList<InstructionLLVM> 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<InstructionLLVM> result = new ArrayList<>();
|
|
||||||
result.addAll(res.instrs);
|
|
||||||
result.add(r);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
|
||||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||||
@@ -134,6 +121,20 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
return instrLLVM;
|
return instrLLVM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ArrayList<InstructionLLVM> 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<InstructionLLVM> result = new ArrayList<>();
|
||||||
|
result.addAll(res.instrs);
|
||||||
|
result.add(r);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
|
||||||
InstrAndVal res = instr.e().accept(this,h);
|
InstrAndVal res = instr.e().accept(this,h);
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
PROTO INT add(x,y)
|
PROTO INT add(x,y)
|
||||||
|
|
||||||
|
FUNC INT add(x,y) {
|
||||||
|
INT z
|
||||||
|
y := x+y
|
||||||
|
RETURN z
|
||||||
|
}
|
||||||
|
|
||||||
FUNC INT main(x,y) {
|
FUNC INT main(x,y) {
|
||||||
INT a,b,c,minh
|
INT a,b,c,minh
|
||||||
x := 5
|
x := 5
|
||||||
|
|||||||
Reference in New Issue
Block a user