From 85a693acbdc08310cfebb96a2b2e96f9f31bc0a2 Mon Sep 17 00:00:00 2001 From: Vu Tuan Minh Date: Mon, 28 Apr 2025 05:43:08 +0200 Subject: [PATCH] symtable --- src/main/java/TP2/asd/SymTable.java | 117 ++++++++++++++++------ src/main/java/TP2/asd/toLLVM_Visitor.java | 27 +++-- 2 files changed, 105 insertions(+), 39 deletions(-) diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 8d4260e..35b856f 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -1,5 +1,4 @@ package TP2.asd; -import java.util.Stack; import org.pcollections.*; import TP2.asd.Interface.Type; import TP2.asd.Program.Type_intImp; @@ -8,16 +7,16 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImp; public class SymTable { - private PMap varMap; + private PStack> varMap; private PMap fuctionsMap; private int id=1; private int idLabel = 1; public SymTable(){ - this.varMap= HashTreePMap.empty(); + this.varMap= ConsPStack.singleton(HashTreePMap.empty()); this.fuctionsMap = HashTreePMap.empty(); } - public SymTable(PMap varMap, PMap fuctionsMap, int id, int idLabel){ + public SymTable(PStack> varMap, PMap fuctionsMap, int id, int idLabel){ this.varMap= varMap; this.id = id; this.fuctionsMap = fuctionsMap; @@ -34,6 +33,19 @@ public class SymTable { } } + public SymTable newBlock() { + return new SymTable(varMap.plus(HashTreePMap.empty()), fuctionsMap, id, idLabel); + } + + public SymTable outBlock() { + if (varMap.size() > 1) { + return new SymTable(varMap.minus(0), fuctionsMap, id, idLabel); + } else { + System.err.println("Vide"); + return this; + } + } + public static class ValueVarMap{ public Type type; public int id; @@ -60,7 +72,6 @@ public class SymTable { } - public SymTable addFunction(DefineLLVMImp function, Boolean isProto){ ValueFunMap value = this.fuctionsMap.get(function.name()); if(value == null || (value!=null && value.isProto && !isProto)){ @@ -110,58 +121,100 @@ public class SymTable { return new Result(newSymTab,newParam); } - public Result addVar(String nomVar){ - String newVar = nomVar+id; - SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false); - return new Result(newSymTab,newVar); + public Result addVar(String nomVar) { + PMap top = varMap.get(0); + if (top.containsKey(nomVar)) { + System.err.println("Erreur"); + return new Result(this, null); + } + String realName = nomVar + id; + top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id, false)); + SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), fuctionsMap, id+1, idLabel); + return new Result(newSym, realName); } //retourne le nom de la var déjà déclaré avec son id public String getVar(String nomVar){ - String prefix = ""; - ValueVarMap value = this.varMap.get(nomVar); - if(value.isParam){ - prefix = "param_"; + for(PMap scope : varMap) { + if (scope.containsKey(nomVar)) { + ValueVarMap value = scope.get(nomVar); + String prefix = ""; + if(value.isParam){ + prefix = "param_"; + } + return prefix + nomVar + value.id; + } } - return prefix + nomVar + value.id; + System.err.println(nomVar+" n'est pas trovué"); + return null; } public Boolean isPresentVar(String nomVar){ - return this.varMap.containsKey("nomVar"); + boolean x= false; + while(!x){ + for (PMap scope : varMap){ + x=scope.containsKey(nomVar); + } + if (x) return true; + } + System.err.println(nomVar+" n'est pas trouvé"); + return false; } public Type getvar_Type(String s){ - if(this.varMap.containsKey(s)){ - return this.varMap.get(s).type; + for (PMap scope : varMap) { + if (scope.containsKey(s)) { + return scope.get(s).type; + } } return null; } //retourne le type de la var public Type getType(String nomVar){ - return this.varMap.get(nomVar).type; + for (PMap scope : varMap) { + if (scope.containsKey(nomVar)) { + return scope.get(nomVar).type; + } + } + return null; } - public SymTable addVar(String s, Type t,Boolean isParam){ - PMap pmap = this.varMap; - pmap= pmap.plus(s,new ValueVarMap(t, /*getNewId()*/ id,isParam)); - return new SymTable(pmap,this.fuctionsMap,this.id+1,this.idLabel); + public SymTable addVar(String nomVar, Interface.Type type, boolean isParam) { + PMap top = varMap.get(0); + if (top.containsKey(nomVar)) { + System.err.println(nomVar+ " déjà déclaré."); + return this; + } + top = top.plus(nomVar, new ValueVarMap(type, id, isParam)); + return new SymTable(varMap.minus(0).plus(0, top), fuctionsMap, id+1, idLabel); } - public String print_all(){ StringBuilder str = new StringBuilder(); - str.append("Id = " + id+"\n"); - str.append("VAR :\n"); - for(String s: this.varMap.keySet()){ - str.append(s).append(" id :").append(varMap.get(s).id).append(" type :").append(varMap.get(s).type).append(" isParam :").append(varMap.get(s).isParam).append("\n"); + str.append("Id = ").append(id).append("\n"); + str.append("VARIABLES:\n"); + + int scopeLevel = varMap.size(); + for (PMap scope : varMap) { + str.append("Scope Level ").append(scopeLevel--).append(":\n"); + for (String varName : scope.keySet()) { + ValueVarMap value = scope.get(varName); + str.append("Name: ").append(varName) + .append(", Id: ").append(value.id) + .append(", Type: ").append(value.type) + .append(", IsParam: ").append(value.isParam) + .append("\n"); } - str.append("FUNCTION :\n"); - for(String f: this.fuctionsMap.keySet()){ - str.append(f).append("\n"); - } - return str.toString(); + } + + str.append("FUNCTIONS:\n"); + for (String funcName : fuctionsMap.keySet()) { + str.append(" Name: ").append(funcName).append("\n"); + } + + return str.toString(); } } diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 5344722..feca761 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -106,16 +106,22 @@ public class toLLVM_Visitor implements ProgramVisitor, @Override public ArrayList visitBloc(BlocImp instr, SymTable h) { + SymTable prev = h; + h= h.newBlock(); ArrayList instrLLVM = new ArrayList<>(); for(int i = 0; i visitBlocDec(BlocDecImp instr, SymTable h) { - SymTable prevSymTable = h; + SymTable prev = h; + ArrayList instrLLVM = new ArrayList<>(); for(int i = 0; i, for(int i = 0; i, l.add(new BrCondLLVMImp(varCond,labelThen,labelFin)); l.add(new LabelLLVMImp(labelThen)); + + h = h.newBlock(); l.addAll(instr.i1().accept(this,h)); + h=h.outBlock(); l.add(new LabelLLVMImp(labelFin)); prevSymTable.updateId(h); @@ -226,16 +236,16 @@ public class toLLVM_Visitor implements ProgramVisitor, l.add(new BrCondLLVMImp(varCond,labelThen,labelElse)); l.add(new LabelLLVMImp(labelThen)); - l.addAll(instr.i1().accept(this,h)); + SymTable h0 = h.newBlock(); + l.addAll(instr.i1().accept(this,h0)); l.add(new BrLLVMImp(labelFin)); l.add(new LabelLLVMImp(labelElse)); - l.addAll(instr.i2().accept(this,h)); + SymTable h1 = h.newBlock(); + l.addAll(instr.i2().accept(this,h1)); l.add(new BrLLVMImp(labelFin)); l.add(new LabelLLVMImp(labelFin)); - - prevSymTable.updateId(h); return l; } @@ -260,8 +270,11 @@ public class toLLVM_Visitor implements ProgramVisitor, l.add(new BrCondLLVMImp(varCond,labelDo,labelDone)); l.add(new LabelLLVMImp(labelDo)); + + h=h.outBlock(); l.addAll(instr.i1().accept(this,h)); - + h = h.outBlock(); + l.add(new BrLLVMImp(labelWhile)); l.add(new LabelLLVMImp(labelDone));