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;
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<String,ValueVarMap> varMap;
private PStack<PMap<String,ValueVarMap>> varMap;
private PMap<String,ValueFunMap> 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<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.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<String, ValueVarMap> 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<String,ValueVarMap> 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<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){
if(this.varMap.containsKey(s)){
return this.varMap.get(s).type;
for (PMap<String,ValueVarMap> 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<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(){
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<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");
}
return str.toString();
}
str.append("FUNCTIONS:\n");
for (String funcName : fuctionsMap.keySet()) {
str.append(" Name: ").append(funcName).append("\n");
}
return str.toString();
}
}