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

@@ -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);
// }
//}

View File

@@ -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<SymTable, TypeCheckExprDiag>{
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<SymTable, TypeCheckExprDiag>{
private TypeCheckInstr instr_check;
@Override
public TypeCheckExprDiag visitFunction(FunctionImp f, SymTable h) {
return f.instruction().accept(instr_check,h);
}
}
public class TypeCheckInstr implements InstrVisitor<SymTable, TypeCheckExprDiag> {
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<SymTable ,TypeCheckExprDiag>{
@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<SymTable, TypeCheckExprDiag>{
// 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<SymTable, TypeCheckExprDiag>{
// private TypeCheckInstr instr_check;
// @Override
// public TypeCheckExprDiag visitFunction(FunctionImp f, SymTable h) {
// return f.instruction().accept(instr_check,h);
// }
// }
//
// public class TypeCheckInstr implements InstrVisitor<SymTable, TypeCheckExprDiag> {
// 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<SymTable ,TypeCheckExprDiag>{
// @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());
// }
// }
//}

View File

@@ -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) {

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();

View File

@@ -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.*;

View File

@@ -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