correction SymTable

This commit is contained in:
Rochas
2025-04-27 16:55:47 +02:00
parent af2e42ab89
commit 0105a3f59e
5 changed files with 157 additions and 98 deletions

View File

@@ -56,7 +56,7 @@ public class TypeChecking {
@Override
public TypeCheckExprDiag visitAssign(AssignImp instr, SymTable h) {
if(!h.searchVar(instr.t())){
if(!h.isPresentVar(instr.t())){
return TypeCheckExprDiag.error("Variable "+instr.t()+" n'existe pas");
}
Type t_type=h.getvar_Type(instr.t());
@@ -85,7 +85,7 @@ public class TypeChecking {
@Override
public TypeCheckExprDiag visitRead(ReadImp instr, SymTable h) {
for(VarImp v: instr.t()){
if(!h.searchVar(v.name())){
if(!h.isPresentVar(v.name())){
return TypeCheckExprDiag.error("Variable "+v.name()+" n'existe pas");
}
}
@@ -134,8 +134,8 @@ public class TypeChecking {
@Override
public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
if(!h.searchVar(e.name())){
return TypeCheckExprDiag.error("Ce variable n'existe pas");
if(!h.isPresentVar(e.name())){
return TypeCheckExprDiag.error("Cette variable n'existe pas");
}
Type e_type= h.getvar_Type(e.name());
return TypeCheckExprDiag.checked(e_type);

View File

@@ -8,11 +8,23 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImpl;
public class SymTable {
private PStack<PMap<String,ValueTable>> stackMap;
private PMap<String,ValueTable> varMap;
private PMap<String,DefineLLVMImpl> fuctionsMap;
private int id=1;
private int id=1;
private int idLabel = 1;
public SymTable(){
this.varMap= HashTreePMap.empty();
this.fuctionsMap = HashTreePMap.empty();
}
public SymTable(PMap<String,ValueTable> varMap,PMap<String,DefineLLVMImpl> fuctionsMap, int id, int idLabel){
this.varMap= varMap;
this.id = id;
this.fuctionsMap = fuctionsMap;
this.idLabel = idLabel;
}
public static class ValueTable{
public Type type;
public int id;
@@ -23,6 +35,11 @@ public class SymTable {
this.isParam = isParam;
}
}
public void updateId(SymTable symTable2){
this.id = symTable2.getId();
this.idLabel = symTable2.getIdLabel();
}
public static class Result{
public SymTable symTable;
@@ -33,26 +50,27 @@ public class SymTable {
}
}
public SymTable(){
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){
public SymTable addFunction(DefineLLVMImpl function){
if(!this.fuctionsMap.containsKey(function.name())){
this.fuctionsMap.plus(function.name(),function);
return new SymTable(this.varMap,this.fuctionsMap.plus(function.name(),function),this.id,this.idLabel);
}
return this;
}
public DefineLLVMImpl getFunction(String name){
return this.fuctionsMap.get(name);
}
public int getId(){
return this.id;
}
public int getIdLabel(){
return this.idLabel;
}
public int getNewId(){
int a = this.id;
this.id++;
@@ -65,10 +83,9 @@ public class SymTable {
return a;
}
public Result addNewTempVar(/*Type type*/){
//TODO
public Result addNewTempVar(){
String newVar = "temp"+id;
SymTable newSymTab = this.addVar(newVar,new Type_intImp(),false); //TODO
SymTable newSymTab = this.addVar(newVar,new Type_intImp(),false);
return new Result(newSymTab,newVar);
}
@@ -80,85 +97,55 @@ public class SymTable {
public Result addVar(String nomVar){
String newVar = nomVar+id;
SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false); //TODO
SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false);
return new Result(newSymTab,newVar);
}
//retourne le nom de la var déjà déclaré avec son id
public String getVar(String nomVar){
String prefix = "";
ValueTable value = this.stackMap.getLast().get(nomVar);
ValueTable value = this.varMap.get(nomVar);
if(value.isParam){
prefix = "param_";
}
return prefix + nomVar + value.id;
}
//retourne le type de la var
public Type getType(String nomVar){
return this.stackMap.getLast().get(nomVar).type;
}
public PStack<PMap<String,ValueTable>> next_layer(){
return stackMap.plus(HashTreePMap.empty());
}
public PMap<String,ValueTable> peppapeek(){
if(stackMap.isEmpty()){
return this.next_layer().getLast();
}
return stackMap.getLast();
}
public SymTable addVar(String s, Type t,Boolean isParam){
PStack<PMap<String,ValueTable>> newpstack = null;
if(this.stackMap.isEmpty()){
newpstack = this.next_layer();
}
else newpstack = this.stackMap;
//Save temporary if not PMap wont save
PMap<String, ValueTable> pmap = this.peppapeek();
pmap= pmap.plus(s/*+"_"+this.id*/,new ValueTable(t, getNewId(),isParam));
//this.id++;
//Delete old ones
newpstack = newpstack.minus(newpstack.indexOf(newpstack.getLast()));
//Push the new one
newpstack = newpstack.plus(pmap);
return new SymTable(newpstack,this.id);
}
//Usually look for var in highest level , if not found research.
public boolean searchVar(String s){
//TOTO
for(int i= stackMap.size()-1; i>=0; i--){
if(stackMap.get(i).containsKey(s)){
return true;
}
}
return false;
}
public Stack<PMap<String,ValueTable>> stackmap(){
return this.stackmap();
public Boolean isPresentVar(String nomVar){
return this.varMap.containsKey("nomVar");
}
public Type getvar_Type(String s){
for(int i= stackMap.size()-1; i>=0; i--){
if(stackMap.get(i).containsKey(s)){
return stackMap.get(i).get(s).type;
}
if(this.varMap.containsKey(s)){
return this.varMap.get(s).type;
}
return null;
}
//retourne le type de la var
public Type getType(String nomVar){
return this.varMap.get(nomVar).type;
}
public SymTable addVar(String s, Type t,Boolean isParam){
PMap<String, ValueTable> pmap = this.varMap;
pmap= pmap.plus(s,new ValueTable(t, /*getNewId()*/ id,isParam));
return new SymTable(pmap,this.fuctionsMap,this.id+1,this.idLabel);
}
public String print_all(){
StringBuilder str = new StringBuilder();
for(int i= stackMap.size()-1; i>=0; i--){
str.append("level ").append(i).append("\n");
for(String s: stackMap.get(i).keySet()){
str.append(s).append(" ").append(stackMap.get(i).get(s)).append("\n");
}
str.append("Id = " + id+"\n");
str.append("VAR :\n");
for(String s: this.varMap.keySet()){
str.append(s).append(" ").append(varMap.get(s)).append("\n");
}
str.append("FUNCTION :\n");
for(String f: this.fuctionsMap.keySet()){
str.append(f).append(" ").append(fuctionsMap.get(f)).append("\n");
}
return str.toString();
}

View File

@@ -40,6 +40,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
this.symTable = symTable;
}
}
//PROGRAM
@Override
@@ -47,8 +48,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ArrayList<DefineLLVM> fonctionLLVM = new ArrayList<>();
for(int i = 0; i<prog.fonctions().size(); i++){
DefineLLVMImpl function = prog.fonctions().get(i).accept(this, h);
h.addFunction(function);
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){
//h = h.addFunction(function);
if(!(prog.fonctions().get(i) instanceof PrototypeImp)){ //les prototypes n'existent pas dans LLVM
fonctionLLVM.add(function);
}
}
@@ -60,9 +61,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public DefineLLVMImpl visitFunction(FunctionImp fun, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
ArrayList<VarLLVMImpl> paramsLLVM = new ArrayList<>();
for(VarImp param: fun.params()){
Result r = h.addParam(param.name());
String name = r.var;
@@ -70,9 +71,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
h = r.symTable;
paramsLLVM.add(var);
}
instrLLVM.addAll(fun.instruction().accept(this, h));
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
DefineLLVMImpl define = new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), paramsLLVM, instrLLVM);
prevSymTable.updateId(h);
return define;
}
@Override
@@ -84,6 +86,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> list = new ArrayList<>();
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
@@ -93,6 +96,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
h = r.symTable;
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
}
prevSymTable.updateId(h);
return new InstrAndSymTable(list,h);
}
@@ -109,6 +113,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.decls().size(); i++){
InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
@@ -118,6 +123,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
}
prevSymTable.updateId(h);
return instrLLVM;
}
@@ -172,6 +178,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelIf= "if"+h.getNewIdLabel()+":";
String labelThen= "then"+h.getNewIdLabel()+":";
@@ -182,9 +189,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(temp.instrs);
ValLLVM val = temp.val;
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
Result temp2 = h.addNewTempVar();
h = temp2.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
Result r = h.addNewTempVar();
h = r.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
l.add(new AssignLVMImpl(varCond,exTemp));
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
@@ -192,12 +199,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(instr.i1().accept(this,h));
l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h);
return l;
}
@Override
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelIf= "if"+h.getNewIdLabel()+":";
String labelThen= "then"+h.getNewIdLabel()+":";
@@ -209,9 +217,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(temp.instrs);
ValLLVM val = temp.val;
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
Result temp2 = h.addNewTempVar();
h = temp2.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
Result r = h.addNewTempVar();
h = r.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
l.add(new AssignLVMImpl(varCond,exTemp));
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
@@ -223,13 +231,15 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(instr.i2().accept(this,h));
l.add(new BrLLVMImp(labelFin));
l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h);
return l;
}
@Override
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> l = new ArrayList<>();
String labelWhile = "while"+h.getNewIdLabel()+":";
@@ -241,8 +251,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.addAll(temp.instrs); //instructions
ValLLVM val = temp.val; //temp6
ExpressionLLVM exTemp = new IcmpLLVMImp(val,new ValLLVMImpl(new IntLLVMImpl(), 0));
Result temp2 = h.addNewTempVar();
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), temp2.var);
Result r = h.addNewTempVar();
h = r.symTable;
VarLLVMImpl varCond = new VarLLVMImpl(exTemp.getType(), r.var);
l.add(new AssignLVMImpl(varCond,exTemp));
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
@@ -252,7 +263,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
l.add(new BrLLVMImp(labelWhile));
l.add(new LabelLLVMImp(labelDone));
prevSymTable.updateId(h);
return l;
}
@@ -267,18 +278,21 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public InstrAndVal visitVar(VarImp e, SymTable h) {
SymTable prevSymTable = h;
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);
Result r = h.addNewTempVar();
h = r.symTable;
VarLLVMImpl varTemp = new VarLLVMImpl(h.getvar_Type(e.name()).accept(this,h),r.var);
l.add(new AssignLVMImpl(varTemp,((ExpressionLLVM)(new LoadLLVMImpl(val)))));
prevSymTable.updateId(h);
return new InstrAndVal(l, varTemp);
}
@Override
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
SymTable prevSymTable = h;
ArrayList<InstructionLLVM> list = new ArrayList<>();
InstrAndVal res1 = e.e1().accept(this, h);
InstrAndVal res2 = e.e2().accept(this, h);
@@ -298,7 +312,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
h = r.symTable;
VarLLVMImpl var = new VarLLVMImpl(type,temp);
list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2)));
prevSymTable.updateId(h);
return new InstrAndVal(list, var);
}

View File

@@ -101,7 +101,7 @@ TypeLLVMVisitor<String,String>
@Override
public String visitAllocaLLVM(allocaLLVMImpl e, String h) {
return "alloca" + e.type().accept(this, h);
return "alloca " + e.type().accept(this, h);
}
@Override