symTable en cours

This commit is contained in:
trochas
2025-04-09 16:08:54 +02:00
5 changed files with 55 additions and 22 deletions

View File

@@ -81,17 +81,28 @@ instruction returns [Instruction out]:
})* })*
{$out = new DeclarationImp($t.return_type, declare);} {$out = new DeclarationImp($t.return_type, declare);}
| // PRINT | // PRINT
PRINT t1=TEXT PRINT
{ {ArrayList<String> printer= new ArrayList<String>();}
String text= $t1.getText(); (t1=TEXT
ArrayList<String> printer= new ArrayList<String>(); {
printer.add(text.substring(1,text.length()-1)); String text= $t1.getText();
}(VIRGULE t2=TEXT printer.add(text.substring(1,text.length()-1));
}
|e1=expression
{ String text_e= $e1.out.toString();
printer.add(text_e);
})
(VIRGULE (t2=TEXT
{ {
String text2= $t2.getText(); String text2= $t2.getText();
printer.add(text2.substring(1,text2.length()-1)); printer.add(text2.substring(1,text2.length()-1));
} }
)* | e2=expression
{
String text_e2= $e2.out.toString();
printer.add(text_e2);
}
))*
{$out = new PrintImp(printer);} {$out = new PrintImp(printer);}
| //READ | //READ
READ i1=ident READ i1=ident

View File

@@ -37,7 +37,6 @@ public class Main {
VSLLexer lexer = new VSLLexer(input); VSLLexer lexer = new VSLLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
// Instantiate Parser // Instantiate Parser
VSLParser parser = new VSLParser(tokens); VSLParser parser = new VSLParser(tokens);

View File

@@ -5,7 +5,7 @@ import TP2.asd.Interface.*;
import TP2.llvm.ProgramLLVM.*; import TP2.llvm.ProgramLLVM.*;
public class Program{ public class Program{
//Prog //Prog
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{ public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{
public <H, S> S accept(ProgramVisitor<H, S> v, H h) { public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
@@ -74,18 +74,18 @@ public class Program{
} }
} }
public static record PrintImp(ArrayList t) implements Instruction{ //TODO public static record PrintImp(ArrayList t) implements Instruction{
@Override @Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) { public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitPrint(this, h); return v.visitPrint(this, h);
} }
} }
public static record ReadImp(ArrayList<String> t) implements Instruction{ public static record ReadImp(ArrayList t) implements Instruction{
@Override @Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) { public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitRead(this, h); return v.visitRead(this, h);
} }
} }
//Type //Type

View File

@@ -18,11 +18,25 @@ public class SymTable {
} }
} }
public static class Result{
public SymTable symTable;
public String var;
public Result(SymTable symTable, String var){
this.symTable = symTable;
this.var = var;
}
}
private PStack<PMap<String,ValueTable>> stackMap; private PStack<PMap<String,ValueTable>> stackMap;
private int id=1; private int id=1;
public SymTable(){ public SymTable(){
this.stackMap= ConsPStack.empty(); this.stackMap= ConsPStack.empty();
} }
public SymTable(PStack<PMap<String,ValueTable>> stackMap, int id){
this.stackMap= stackMap;
this.id = id;
}
public int getNewId(){ public int getNewId(){
int a = this.id; int a = this.id;
@@ -30,14 +44,14 @@ public class SymTable {
return a; return a;
} }
public String addNewTempVar(/*Type type*/){ public Result addNewTempVar(/*Type type*/){
//TODO //TODO
String newVar = "temp"+this.id; String newVar = "temp"+getNewId();
this.addVar(newVar,new Type_intImp()); //TODO SymTable newSymTab = this.addVar(newVar,new Type_intImp()); //TODO
return newVar; return new Result(newSymTab,newVar);
} }
public PStack next_layer(){ public PStack<PMap<String,ValueTable>> next_layer(){
return stackMap.plus(HashTreePMap.empty()); return stackMap.plus(HashTreePMap.empty());
} }
@@ -51,20 +65,27 @@ public class SymTable {
public PMap<String,ValueTable> peppapeek(){ public PMap<String,ValueTable> peppapeek(){
if(stackMap.isEmpty()){ if(stackMap.isEmpty()){
System.out.println("TEST..............................."); System.out.println("TEST...............................");
this.next_layer(); return this.next_layer().getLast();
} }
return stackMap.getLast(); return stackMap.getLast();
} }
public void addVar(String s, Type t){ public SymTable addVar(String s, Type t){
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 //Save temporary if not PMap wont save
PMap<String, ValueTable> pmap = this.peppapeek(); PMap<String, ValueTable> pmap = this.peppapeek();
pmap= pmap.plus(s/*+"_"+this.id*/,new ValueTable(t, getNewId())); pmap= pmap.plus(s/*+"_"+this.id*/,new ValueTable(t, getNewId()));
//this.id++; //this.id++;
//Delete old ones //Delete old ones
stackMap.minus(stackMap.indexOf(stackMap.getLast())); newpstack = newpstack.minus(stackMap.indexOf(stackMap.getLast()));
//Push the new one //Push the new one
stackMap.plus(pmap); newpstack = newpstack.plus(pmap);
return new SymTable(newpstack,this.id);
} }
//Usually look for var in highest level , if not found research. //Usually look for var in highest level , if not found research.

View File

@@ -137,7 +137,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
if(val1.getType().getClass() != val1.getType().getClass()){ if(val1.getType().getClass() != val1.getType().getClass()){
throw new UnsupportedOperationException("Type error in VSL file"); throw new UnsupportedOperationException("Type error in VSL file");
} }
String temp = h.addNewTempVar(); Result r = h.addNewTempVar();
String temp = r.var;
h = r.symTable;
VarLLVMImpl var = new VarLLVMImpl(type,temp); VarLLVMImpl var = new VarLLVMImpl(type,temp);
list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2))); list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2)));