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

@@ -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();
}