maj SymTable (id global) + DeclGlobal

This commit is contained in:
trochas
2025-04-28 17:20:15 +02:00
parent 1b5dd97226
commit 79920ed4d4
6 changed files with 108 additions and 71 deletions

View File

@@ -1,7 +1,10 @@
package TP2.asd;
import java.util.ArrayList;
import org.pcollections.*;
import TP2.asd.Interface.Type;
import TP2.asd.Program.Type_intImp;
import TP2.llvm.ProgramLLVM.DeclarGlobalLLVMImp;
import TP2.llvm.ProgramLLVM.DefineLLVMImp;
@@ -9,18 +12,22 @@ public class SymTable {
private PStack<PMap<String,ValueVarMap>> varMap;
private PMap<String,ValueFunMap> functionsMap;
private int id=1;
private int idLabel = 1;
private int[] id ; //id partagé entre toute les symTable, [0] : idVar, [1] : id Label, [2] : global
private ArrayList<DeclarGlobalLLVMImp> declarationsGlobal;
public SymTable(){
public SymTable(){
this.id = new int[3];
this.varMap= ConsPStack.singleton(HashTreePMap.empty());
this.functionsMap = HashTreePMap.empty();
this.declarationsGlobal = new ArrayList<>();
this.id[0] = 1;
this.id[1] = 1;
}
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap, int id, int idLabel){
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap, int[] id,ArrayList<DeclarGlobalLLVMImp> declarationsGlobal){
this.varMap= varMap;
this.id = id;
this.functionsMap = functionsMap;
this.idLabel = idLabel;
this.declarationsGlobal = declarationsGlobal;
}
public static class ValueFunMap{
@@ -34,14 +41,13 @@ public class SymTable {
}
public SymTable newBlock() {
return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id, idLabel);
return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id,declarationsGlobal);
}
public SymTable outBlock() {
if (varMap.size() > 1) {
return new SymTable(varMap.minus(0), functionsMap, id, idLabel);
return new SymTable(varMap.minus(0), functionsMap, id,declarationsGlobal);
} else {
System.err.println("Vide");
return this;
}
}
@@ -57,9 +63,14 @@ public class SymTable {
}
}
public void updateId(SymTable symTable2){
/*public void update(SymTable symTable2){
this.id = symTable2.getId();
this.idLabel = symTable2.getIdLabel();
this.declarationsGlobal = symTable2.getDeclarationGlobal();
}*/
public ArrayList<DeclarGlobalLLVMImp> getDeclarationGlobal(){
return this.declarationsGlobal;
}
public static class Result{
@@ -75,7 +86,7 @@ public class SymTable {
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
ValueFunMap value = this.functionsMap.get(function.name());
if(value == null || (value!=null && value.isProto && !isProto)){
return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel);
return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id,this.declarationsGlobal);
}
else{
if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà");
@@ -90,46 +101,56 @@ public class SymTable {
}
public int getId(){
return this.id;
return this.id[0];
}
public int getIdLabel(){
return this.idLabel;
return this.id[1];
}
public int getNewId(){
int a = this.id;
this.id++;
int a = this.id[0];
this.id[0]++;
return a;
}
public int getNewIdLabel(){
int a = this.idLabel;
this.idLabel++;
int a = this.id[1];
this.id[1]++;
return a;
}
public Result addNewTempVar(){
String newVar = "temp"+id;
String newVar = "temp"+id[0];
SymTable newSymTab = this.addVarInTab(newVar,new Type_intImp(),false);
return new Result(newSymTab,newVar);
}
public Result addParam(String nomParam){
String newParam = "param_"+nomParam+id;
String newParam = "param_"+nomParam;
SymTable newSymTab = this.addVarInTab(nomParam,new Type_intImp(),true);
return new Result(newSymTab,newParam);
}
public String getGlobalDeclName(){;
return "fmt"+id[2];
}
public void addGlobalDecl(DeclarGlobalLLVMImp decl){
id[2]++;
this.declarationsGlobal.add(decl);
}
public Result addVar(String nomVar) {
PMap<String, ValueVarMap> top = varMap.get(0);
if (top.containsKey(nomVar)) {
System.err.println("[VSL compile error] :" + "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), functionsMap, id+1, idLabel);
String realName = nomVar + id[0];
top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id[0], false));
id[0]++;
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id,this.declarationsGlobal);
return new Result(newSym, realName);
}
@@ -186,13 +207,14 @@ public class SymTable {
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), functionsMap, id+1, idLabel);
top = top.plus(nomVar, new ValueVarMap(type, id[0], isParam));
if(!isParam) this.id[0]++;
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id, this.declarationsGlobal);
}
public String print_all(){
StringBuilder str = new StringBuilder();
str.append("Id = ").append(id).append("\n");
str.append("Id = ").append(id[0]).append("\n");
str.append("VARIABLES:\n");
int scopeLevel = varMap.size();