This commit is contained in:
Vu Tuan Minh
2025-04-28 05:43:08 +02:00
parent 38e3f992b9
commit 85a693acbd
2 changed files with 105 additions and 39 deletions

View File

@@ -1,5 +1,4 @@
package TP2.asd; package TP2.asd;
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;
@@ -8,16 +7,16 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImp;
public class SymTable { public class SymTable {
private PMap<String,ValueVarMap> varMap; private PStack<PMap<String,ValueVarMap>> varMap;
private PMap<String,ValueFunMap> fuctionsMap; private PMap<String,ValueFunMap> fuctionsMap;
private int id=1; private int id=1;
private int idLabel = 1; private int idLabel = 1;
public SymTable(){ public SymTable(){
this.varMap= HashTreePMap.empty(); this.varMap= ConsPStack.singleton(HashTreePMap.empty());
this.fuctionsMap = HashTreePMap.empty(); this.fuctionsMap = HashTreePMap.empty();
} }
public SymTable(PMap<String,ValueVarMap> varMap, PMap<String,ValueFunMap> fuctionsMap, int id, int idLabel){ public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> fuctionsMap, int id, int idLabel){
this.varMap= varMap; this.varMap= varMap;
this.id = id; this.id = id;
this.fuctionsMap = fuctionsMap; 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 static class ValueVarMap{
public Type type; public Type type;
public int id; public int id;
@@ -60,7 +72,6 @@ public class SymTable {
} }
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){ public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
ValueFunMap value = this.fuctionsMap.get(function.name()); ValueFunMap value = this.fuctionsMap.get(function.name());
if(value == null || (value!=null && value.isProto && !isProto)){ if(value == null || (value!=null && value.isProto && !isProto)){
@@ -110,58 +121,100 @@ public class SymTable {
return new Result(newSymTab,newParam); return new Result(newSymTab,newParam);
} }
public Result addVar(String nomVar){ public Result addVar(String nomVar) {
String newVar = nomVar+id; PMap<String, ValueVarMap> top = varMap.get(0);
SymTable newSymTab = this.addVar(nomVar,new Type_intImp(),false); if (top.containsKey(nomVar)) {
return new Result(newSymTab,newVar); 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 //retourne le nom de la var déjà déclaré avec son id
public String getVar(String nomVar){ public String getVar(String nomVar){
for(PMap<String,ValueVarMap> scope : varMap) {
if (scope.containsKey(nomVar)) {
ValueVarMap value = scope.get(nomVar);
String prefix = ""; String prefix = "";
ValueVarMap value = this.varMap.get(nomVar);
if(value.isParam){ if(value.isParam){
prefix = "param_"; 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){ public Boolean isPresentVar(String nomVar){
return this.varMap.containsKey("nomVar"); boolean x= false;
while(!x){
for (PMap<String,ValueVarMap> 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){ public Type getvar_Type(String s){
if(this.varMap.containsKey(s)){ for (PMap<String,ValueVarMap> scope : varMap) {
return this.varMap.get(s).type; if (scope.containsKey(s)) {
return scope.get(s).type;
}
} }
return null; return null;
} }
//retourne le type de la var //retourne le type de la var
public Type getType(String nomVar){ public Type getType(String nomVar){
return this.varMap.get(nomVar).type; for (PMap<String,ValueVarMap> scope : varMap) {
if (scope.containsKey(nomVar)) {
return scope.get(nomVar).type;
}
}
return null;
} }
public SymTable addVar(String s, Type t,Boolean isParam){
PMap<String, ValueVarMap> 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<String,ValueVarMap> 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(){ public String print_all(){
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
str.append("Id = " + id+"\n"); str.append("Id = ").append(id).append("\n");
str.append("VAR :\n"); str.append("VARIABLES:\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"); int scopeLevel = varMap.size();
for (PMap<String, ValueVarMap> 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");
} }
str.append("FUNCTIONS:\n");
for (String funcName : fuctionsMap.keySet()) {
str.append(" Name: ").append(funcName).append("\n");
}
return str.toString(); return str.toString();
} }
} }

View File

@@ -106,16 +106,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
@Override @Override
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) { public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
SymTable prev = h;
h= h.newBlock();
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>(); ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.instrs().size(); i++){ for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h)); instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
} }
h= h.outBlock();
prev.updateId(h);
return instrLLVM; return instrLLVM;
} }
@Override @Override
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) { public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
SymTable prevSymTable = h; SymTable prev = h;
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>(); ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
for(int i = 0; i<instr.decls().size(); i++){ for(int i = 0; i<instr.decls().size(); i++){
InstrAndSymTable temp = instr.decls().get(i).accept(this, h); InstrAndSymTable temp = instr.decls().get(i).accept(this, h);
@@ -125,7 +131,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
for(int i = 0; i<instr.instrs().size(); i++){ for(int i = 0; i<instr.instrs().size(); i++){
instrLLVM.addAll(instr.instrs().get(i).accept(this, h)); instrLLVM.addAll(instr.instrs().get(i).accept(this, h));
} }
prevSymTable.updateId(h);
prev.updateId(h);
return instrLLVM; return instrLLVM;
} }
@@ -198,7 +205,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
l.add(new BrCondLLVMImp(varCond,labelThen,labelFin)); l.add(new BrCondLLVMImp(varCond,labelThen,labelFin));
l.add(new LabelLLVMImp(labelThen)); l.add(new LabelLLVMImp(labelThen));
h = h.newBlock();
l.addAll(instr.i1().accept(this,h)); l.addAll(instr.i1().accept(this,h));
h=h.outBlock();
l.add(new LabelLLVMImp(labelFin)); l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h); prevSymTable.updateId(h);
@@ -226,16 +236,16 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
l.add(new BrCondLLVMImp(varCond,labelThen,labelElse)); l.add(new BrCondLLVMImp(varCond,labelThen,labelElse));
l.add(new LabelLLVMImp(labelThen)); 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 BrLLVMImp(labelFin));
l.add(new LabelLLVMImp(labelElse)); 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 BrLLVMImp(labelFin));
l.add(new LabelLLVMImp(labelFin)); l.add(new LabelLLVMImp(labelFin));
prevSymTable.updateId(h);
return l; return l;
} }
@@ -260,7 +270,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
l.add(new BrCondLLVMImp(varCond,labelDo,labelDone)); l.add(new BrCondLLVMImp(varCond,labelDo,labelDone));
l.add(new LabelLLVMImp(labelDo)); l.add(new LabelLLVMImp(labelDo));
h=h.outBlock();
l.addAll(instr.i1().accept(this,h)); l.addAll(instr.i1().accept(this,h));
h = h.outBlock();
l.add(new BrLLVMImp(labelWhile)); l.add(new BrLLVMImp(labelWhile));