modification de l'implémentation de prototype, il implement Function
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,155 @@
|
|||||||
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);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public class TypeCheckInstr implements InstrVisitor<SymTable, TypeCheckExprDiag> {
|
// public class TypeCheckInstr implements InstrVisitor<SymTable, TypeCheckExprDiag> {
|
||||||
private TypeCheckExpr expr_check;
|
// private TypeCheckExpr expr_check;
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitReturn(Return_instrImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitReturn(Return_instrImp instr, SymTable h) {
|
||||||
return instr.e().accept(expr_check, h);
|
// return instr.e().accept(expr_check, h);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitBloc(BlocImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitBloc(BlocImp instr, SymTable h) {
|
||||||
for(Instruction i: instr.instrs()){
|
// for(Instruction i: instr.instrs()){
|
||||||
TypeCheckExprDiag diag= i.accept(this, h);
|
// TypeCheckExprDiag diag= i.accept(this, h);
|
||||||
if(!diag.get_check()) return diag;
|
// if(!diag.get_check()) return diag;
|
||||||
}
|
// }
|
||||||
return TypeCheckExprDiag.checked(null);
|
// return TypeCheckExprDiag.checked(null);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitBlocDec(BlocDecImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitBlocDec(BlocDecImp instr, SymTable h) {
|
||||||
// TODO Auto-generated method stub
|
// // TODO Auto-generated method stub
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitBlocDec'");
|
// throw new UnsupportedOperationException("Unimplemented method 'visitBlocDec'");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) {
|
||||||
if(!h.searchVar(instr.t())){
|
// if(!h.searchVar(instr.t())){
|
||||||
return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas");
|
// return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas");
|
||||||
}
|
// }
|
||||||
Type t_type=h.getvar_Type(instr.t());
|
// Type t_type=h.getvar_Type(instr.t());
|
||||||
TypeCheckExprDiag expr= instr.e().accept(expr_check, h);
|
// TypeCheckExprDiag expr= instr.e().accept(expr_check, h);
|
||||||
|
//
|
||||||
if (!expr.get_check()) return expr;
|
// if (!expr.get_check()) return expr;
|
||||||
//Verify type t = Type expr
|
// //Verify type t = Type expr
|
||||||
if(!t_type.getClass().equals(expr.get_type().getClass())){
|
// if(!t_type.getClass().equals(expr.get_type().getClass())){
|
||||||
return TypeCheckExprDiag.error("Type d'expression est different que le type de variable");
|
// return TypeCheckExprDiag.error("Type d'expression est different que le type de variable");
|
||||||
}
|
// }
|
||||||
return TypeCheckExprDiag.checked(t_type);
|
// return TypeCheckExprDiag.checked(t_type);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitPrint(PrintImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitPrint(PrintImp instr, SymTable h) {
|
||||||
for(Object o :instr.t()){
|
// for(Object o :instr.t()){
|
||||||
//We have string and expression
|
// //We have string and expression
|
||||||
if(o instanceof Expression e){
|
// if(o instanceof Expression e){
|
||||||
TypeCheckExprDiag result = e.accept(expr_check, h);
|
// TypeCheckExprDiag result = e.accept(expr_check, h);
|
||||||
if (!result.get_check()) return result;
|
// if (!result.get_check()) return result;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return TypeCheckExprDiag.checked(null);
|
// return TypeCheckExprDiag.checked(null);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) {
|
||||||
for(VarImp v: instr.t()){
|
// for(VarImp v: instr.t()){
|
||||||
if(!h.searchVar(v.name())){
|
// if(!h.searchVar(v.name())){
|
||||||
return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas");
|
// return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return TypeCheckExprDiag.checked(null);
|
// return TypeCheckExprDiag.checked(null);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitIfThen(IfThenImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitIfThen(IfThenImp instr, SymTable h) {
|
||||||
TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
// TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
||||||
if(!cond.get_check()) return cond;
|
// if(!cond.get_check()) return cond;
|
||||||
if (!(cond.get_type() instanceof Type_intImp)){
|
// if (!(cond.get_type() instanceof Type_intImp)){
|
||||||
return TypeCheckExprDiag.error("Condition n'est pas un int");
|
// return TypeCheckExprDiag.error("Condition n'est pas un int");
|
||||||
}
|
// }
|
||||||
return instr.i1().accept(this, h);
|
// return instr.i1().accept(this, h);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
||||||
TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
// TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
||||||
if(!cond.get_check()) return cond;
|
// if(!cond.get_check()) return cond;
|
||||||
if (!(cond.get_type() instanceof Type_intImp)){
|
// if (!(cond.get_type() instanceof Type_intImp)){
|
||||||
return TypeCheckExprDiag.error("Condition n'est pas un int");
|
// return TypeCheckExprDiag.error("Condition n'est pas un int");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
TypeCheckExprDiag then= instr.i1().accept(this, h);
|
// TypeCheckExprDiag then= instr.i1().accept(this, h);
|
||||||
if (!then.get_check()) return then;
|
// if (!then.get_check()) return then;
|
||||||
return instr.i2().accept(this, h);
|
// return instr.i2().accept(this, h);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitWhile(WhileImp instr, SymTable h) {
|
// public TypeCheckExprDiag visitWhile(WhileImp instr, SymTable h) {
|
||||||
TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
// TypeCheckExprDiag cond =instr.e().accept(expr_check, h);
|
||||||
if(!cond.get_check()) return cond;
|
// if(!cond.get_check()) return cond;
|
||||||
if (!(cond.get_type() instanceof Type_intImp)){
|
// if (!(cond.get_type() instanceof Type_intImp)){
|
||||||
return TypeCheckExprDiag.error("Condition n'est pas un int");
|
// return TypeCheckExprDiag.error("Condition n'est pas un int");
|
||||||
}
|
// }
|
||||||
return instr.i1().accept(this, h);
|
// return instr.i1().accept(this, h);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public class TypeCheckExpr implements ExprVisitor<SymTable ,TypeCheckExprDiag>{
|
// public class TypeCheckExpr implements ExprVisitor<SymTable ,TypeCheckExprDiag>{
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitConst(ConstImp e, SymTable h) {
|
// public TypeCheckExprDiag visitConst(ConstImp e, SymTable h) {
|
||||||
return TypeCheckExprDiag.checked(new Type_intImp());
|
// return TypeCheckExprDiag.checked(new Type_intImp());
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
|
// public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
|
||||||
if(!h.searchVar(e.name())){
|
// if(!h.searchVar(e.name())){
|
||||||
return TypeCheckExprDiag.error("Ce variable n'existe pas");
|
// return TypeCheckExprDiag.error("Ce variable n'existe pas");
|
||||||
}
|
// }
|
||||||
Type e_type= h.getvar_Type(e.name());
|
// Type e_type= h.getvar_Type(e.name());
|
||||||
return TypeCheckExprDiag.checked(e_type);
|
// return TypeCheckExprDiag.checked(e_type);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public TypeCheckExprDiag visitBinOp(BinopExpressionImp e, SymTable h) {
|
// public TypeCheckExprDiag visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||||
TypeCheckExprDiag tce1 = e.e1().accept(this, h);
|
// TypeCheckExprDiag tce1 = e.e1().accept(this, h);
|
||||||
TypeCheckExprDiag tce2 = e.e2().accept(this, h);
|
// TypeCheckExprDiag tce2 = e.e2().accept(this, h);
|
||||||
|
//
|
||||||
// Check if not ok then return its error
|
// // Check if not ok then return its error
|
||||||
if(!tce1.get_check()) return tce1;
|
// if(!tce1.get_check()) return tce1;
|
||||||
if(!tce2.get_check()) return tce2;
|
// if(!tce2.get_check()) return tce2;
|
||||||
|
//
|
||||||
// Check int + int
|
// // Check int + int
|
||||||
if (!(tce1.get_type() instanceof Type_intImp) || !(tce2.get_type() instanceof Type_intImp) ){
|
// if (!(tce1.get_type() instanceof Type_intImp) || !(tce2.get_type() instanceof Type_intImp) ){
|
||||||
return TypeCheckExprDiag.error("Ses types sont different");
|
// return TypeCheckExprDiag.error("Ses types sont different");
|
||||||
}
|
// }
|
||||||
return TypeCheckExprDiag.checked(tce1.get_type());
|
// return TypeCheckExprDiag.checked(tce1.get_type());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import java.util.*;
|
|||||||
/*
|
/*
|
||||||
./gradlew build
|
./gradlew build
|
||||||
java -jar build/libs/TP2.jar tests/fragment0/priority2.vsl
|
java -jar build/libs/TP2.jar tests/fragment0/priority2.vsl
|
||||||
|
java -jar build/libs/TP2.jar tests/aLaMain.vsl
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -15,14 +15,6 @@ public interface Interface{
|
|||||||
public S visitProgram(ProgramImp prog, H h);
|
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
|
//FUNCTION
|
||||||
public interface Function {
|
public interface Function {
|
||||||
@@ -31,6 +23,7 @@ public interface Interface{
|
|||||||
|
|
||||||
public interface FunctionVisitor<H,S> {
|
public interface FunctionVisitor<H,S> {
|
||||||
public S visitFunction(FunctionImp fun, H h);
|
public S visitFunction(FunctionImp fun, H h);
|
||||||
|
public S visitPrototype(PrototypeImp proto, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
//DECLARATION
|
//DECLARATION
|
||||||
@@ -73,6 +66,7 @@ public interface Interface{
|
|||||||
public S visitConst(ConstImp e,H h);
|
public S visitConst(ConstImp e,H h);
|
||||||
public S visitBinOp(BinopExpressionImp e, H h);
|
public S visitBinOp(BinopExpressionImp e, H h);
|
||||||
public S visitVar(VarImp e,H h);
|
public S visitVar(VarImp e,H h);
|
||||||
|
public S visitAppeal(Appeal instr, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Type{
|
public interface Type{
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import TP2.asd.Interface.*;
|
|||||||
import TP2.asd.Program.*;
|
import TP2.asd.Program.*;
|
||||||
|
|
||||||
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||||
PrototypeVisitor<String,String>,
|
|
||||||
FunctionVisitor<String,String>,
|
FunctionVisitor<String,String>,
|
||||||
DeclVisitor<String,String>,
|
DeclVisitor<String,String>,
|
||||||
InstrVisitor<String,String>,
|
InstrVisitor<String,String>,
|
||||||
@@ -19,11 +18,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
@Override
|
@Override
|
||||||
public String visitProgram(ProgramImp prog, String indent) {
|
public String visitProgram(ProgramImp prog, String indent) {
|
||||||
String str ="";
|
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++){
|
for(int i = 0; i<prog.fonctions().size(); i++){
|
||||||
str += prog.fonctions().get(i).accept(this,INDENT);
|
str += prog.fonctions().get(i).accept(this,INDENT);
|
||||||
if(i<prog.fonctions().size()-1) str += "\n";
|
if(i<prog.fonctions().size()-1) str += "\n";
|
||||||
@@ -163,6 +157,16 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
return str;
|
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
|
//EXPRESSION
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import TP2.llvm.ProgramLLVM.*;
|
|||||||
public class Program{
|
public class Program{
|
||||||
|
|
||||||
//Prog
|
//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) {
|
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
|
||||||
return v.visitProgram(this, 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
|
//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) {
|
//public FunctionImp(Type type, String name, Instruction instruction) {
|
||||||
// this(type, name, new ArrayList<>() {{ add(instruction); }}); C KOI ?
|
// this(type, name, new ArrayList<>() {{ add(instruction); }}); C KOI ?
|
||||||
//}
|
//}
|
||||||
@@ -43,6 +35,13 @@ public class Program{
|
|||||||
return v.visitFunction(this, h);
|
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
|
//Expression
|
||||||
public static record ConstImp(int c) implements 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
|
//Declaration
|
||||||
|
|
||||||
public static record DeclarationImp(Type t, ArrayList<String> s) implements Declaration{
|
public static record DeclarationImp(Type t, ArrayList<String> s) implements Declaration{
|
||||||
|
|||||||
@@ -3,10 +3,16 @@ import java.util.Stack;
|
|||||||
import org.pcollections.*;
|
import org.pcollections.*;
|
||||||
import TP2.asd.Interface.Type;
|
import TP2.asd.Interface.Type;
|
||||||
import TP2.asd.Program.Type_intImp;
|
import TP2.asd.Program.Type_intImp;
|
||||||
|
import TP2.llvm.ProgramLLVM.DefineLLVMImpl;
|
||||||
|
|
||||||
|
|
||||||
public class SymTable {
|
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 static class ValueTable{
|
||||||
public Type type;
|
public Type type;
|
||||||
public int id;
|
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(){
|
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){
|
public SymTable(PStack<PMap<String,ValueTable>> stackMap, int id){
|
||||||
this.stackMap= stackMap;
|
this.stackMap= stackMap;
|
||||||
this.id = id;
|
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(){
|
public int getNewId(){
|
||||||
int a = this.id;
|
int a = this.id;
|
||||||
this.id++;
|
this.id++;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import TP2.llvm.Interface.*;
|
|||||||
import TP2.llvm.ProgramLLVM.*;
|
import TP2.llvm.ProgramLLVM.*;
|
||||||
|
|
||||||
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
||||||
FunctionVisitor<SymTable,DefineLLVM>,
|
FunctionVisitor<SymTable,DefineLLVMImpl>,
|
||||||
DeclVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndSymTable>,
|
DeclVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndSymTable>,
|
||||||
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
||||||
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
|
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
|
une simplement un val (var ou const) ou un binop
|
||||||
*/
|
*/
|
||||||
public static class InstrAndVal{
|
public static class InstrAndVal{
|
||||||
public ArrayList<AssignLVMImpl> instrs;
|
public ArrayList<InstructionLLVM> instrs;
|
||||||
public ValLLVM val;
|
public ValLLVM val;
|
||||||
|
|
||||||
public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
|
public InstrAndVal(ArrayList<InstructionLLVM> instr, ValLLVM val){
|
||||||
this.instrs = instr;
|
this.instrs = instr;
|
||||||
this.val = val;
|
this.val = val;
|
||||||
}
|
}
|
||||||
@@ -46,15 +46,20 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) {
|
public ProgramLLVMImpl visitProgram(ProgramImp prog, SymTable h) {
|
||||||
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
|
||||||
for(int i = 0; i<prog.fonctions().size(); i++){
|
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);
|
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//FUNCTION
|
//FUNCTION
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefineLLVM visitFunction(FunctionImp fun, SymTable h) {
|
public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) {
|
||||||
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
|
||||||
ArrayList<VarLLVMImpl> paramsLLVM = 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);
|
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
|
//DECLARATION
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -245,6 +255,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//EXPRESSION
|
//EXPRESSION
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -255,7 +266,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InstrAndVal visitVar(VarImp e, SymTable h) {
|
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()));
|
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);
|
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
|
@Override
|
||||||
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
|
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 res1 = e.e1().accept(this, h);
|
||||||
InstrAndVal res2 = e.e2().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);
|
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
|
@Override
|
||||||
public TypeLLVM visitInt(Type_intImp t, SymTable h) {
|
public TypeLLVM visitInt(Type_intImp t, SymTable h) {
|
||||||
return new IntLLVMImpl();
|
return new IntLLVMImpl();
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package TP2.llvm;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import TP2.asd.Program.IfThenElseImp;
|
|
||||||
import TP2.llvm.Interface.*;
|
import TP2.llvm.Interface.*;
|
||||||
import TP2.llvm.ProgramLLVM.*;
|
import TP2.llvm.ProgramLLVM.*;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
PROTO INT add(x,y)
|
PROTO INT add(x,y)
|
||||||
FUNC INT main(x,y) {
|
FUNC INT main(x,y) {
|
||||||
INT a,b,c,minh
|
INT a,b,c,minh
|
||||||
|
x = 5
|
||||||
minh := x * y
|
minh := x * y
|
||||||
b:=3
|
b:=3
|
||||||
c:=1
|
c:=1
|
||||||
|
|||||||
Reference in New Issue
Block a user