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);}
| // PRINT
PRINT t1=TEXT
{
String text= $t1.getText();
ArrayList<String> printer= new ArrayList<String>();
printer.add(text.substring(1,text.length()-1));
}(VIRGULE t2=TEXT
PRINT
{ArrayList<String> printer= new ArrayList<String>();}
(t1=TEXT
{
String text= $t1.getText();
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();
printer.add(text2.substring(1,text2.length()-1));
}
)*
| e2=expression
{
String text_e2= $e2.out.toString();
printer.add(text_e2);
}
))*
{$out = new PrintImp(printer);}
| //READ
READ i1=ident

View File

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

View File

@@ -5,7 +5,7 @@ import TP2.asd.Interface.*;
import TP2.llvm.ProgramLLVM.*;
public class Program{
//Prog
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{
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
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitPrint(this, h);
}
}
public static record ReadImp(ArrayList<String> t) implements Instruction{
public static record ReadImp(ArrayList t) implements Instruction{
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitRead(this, h);
}
}
}
//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 int id=1;
public SymTable(){
this.stackMap= ConsPStack.empty();
}
public SymTable(PStack<PMap<String,ValueTable>> stackMap, int id){
this.stackMap= stackMap;
this.id = id;
}
public int getNewId(){
int a = this.id;
@@ -30,14 +44,14 @@ public class SymTable {
return a;
}
public String addNewTempVar(/*Type type*/){
public Result addNewTempVar(/*Type type*/){
//TODO
String newVar = "temp"+this.id;
this.addVar(newVar,new Type_intImp()); //TODO
return newVar;
String newVar = "temp"+getNewId();
SymTable newSymTab = this.addVar(newVar,new Type_intImp()); //TODO
return new Result(newSymTab,newVar);
}
public PStack next_layer(){
public PStack<PMap<String,ValueTable>> next_layer(){
return stackMap.plus(HashTreePMap.empty());
}
@@ -51,20 +65,27 @@ public class SymTable {
public PMap<String,ValueTable> peppapeek(){
if(stackMap.isEmpty()){
System.out.println("TEST...............................");
this.next_layer();
return this.next_layer().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
PMap<String, ValueTable> pmap = this.peppapeek();
pmap= pmap.plus(s/*+"_"+this.id*/,new ValueTable(t, getNewId()));
//this.id++;
//Delete old ones
stackMap.minus(stackMap.indexOf(stackMap.getLast()));
newpstack = newpstack.minus(stackMap.indexOf(stackMap.getLast()));
//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.

View File

@@ -137,7 +137,9 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
if(val1.getType().getClass() != val1.getType().getClass()){
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);
list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2)));