print read et SymTable

This commit is contained in:
trochas
2025-04-09 13:01:11 +02:00
parent 3232a9f9ec
commit f4a438df01
8 changed files with 77 additions and 47 deletions

View File

@@ -62,21 +62,26 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
public String visitPrint(PrintImp instr, String indent) { public String visitPrint(PrintImp instr, String indent) {
String str = indent + "PRINT "; String str = indent + "PRINT ";
for(int i = 0; i<instr.t().size(); i++){ for(int i = 0; i<instr.t().size(); i++){
String g = "";
Object o = instr.t().get(i); Object o = instr.t().get(i);
if(o instanceof String){ if(o instanceof String){
str += "\"" + instr.t().get(i) +"\""; str += "\"" + instr.t().get(i) +"\"";
} }
else if(o instanceof Expression){ else if(o instanceof Expression){
str += g + ((Expression)instr.t().get(i)).accept(this,""); str += ((Expression)instr.t().get(i)).accept(this,"");
} }
if(i<instr.t().size()-1) str += ", ";
} }
return str; return str;
} }
@Override @Override
public String visitRead(ReadImp instr, String h) { public String visitRead(ReadImp instr, String indent) {
return "Tibo, tu peux implenter le truc stp"; String str = indent+"READ ";
for(int i = 0; i<instr.t().size(); i++){
str += instr.t().get(i);//.accept(this,h);
if(i<instr.t().size()-1) str += ", ";
}
return str;
} }
//EXPRESSION //EXPRESSION

View File

@@ -74,14 +74,14 @@ public class Program{
} }
} }
public static record PrintImp(ArrayList t) implements Instruction{ public static record PrintImp(ArrayList t) implements Instruction{ //TODO
@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 t) implements Instruction{ public static record ReadImp(ArrayList<String> 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);

View File

@@ -4,24 +4,41 @@ import java.util.Stack;
import org.pcollections.*; import org.pcollections.*;
import TP2.asd.Interface.Type; import TP2.asd.Interface.Type;
import TP2.asd.Program.Type_intImp;
public class SymTable { public class SymTable {
private PStack<PMap<String,Type>> stackMap;
public static class ValueTable{
public Type type;
public int id;
public ValueTable(Type type,int id){
this.type = type;
this.id = id;
}
}
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 int getNewId(){
int a = this.id;
this.id++;
return a;
}
public String addNewTempVar(/*Type type*/){ public String addNewTempVar(/*Type type*/){
//TODO //TODO
String newVar = "temp"+this.id; String newVar = "temp"+this.id;
//this.addVar(newVar,new Type_intImp()); //TODO this.addVar(newVar,new Type_intImp()); //TODO
id++;
return newVar; return newVar;
} }
public void next_layer(){ public PStack next_layer(){
stackMap.plus(HashTreePMap.empty()); return stackMap.plus(HashTreePMap.empty());
} }
public void quit_layer() throws Exception{ public void quit_layer() throws Exception{
@@ -30,14 +47,19 @@ public class SymTable {
} }
stackMap.minus(stackMap.indexOf(stackMap.getLast())); stackMap.minus(stackMap.indexOf(stackMap.getLast()));
} }
public PMap<String,Type> peppapeek(){
public PMap<String,ValueTable> peppapeek(){
if(stackMap.isEmpty()){
System.out.println("TEST...............................");
this.next_layer();
}
return stackMap.getLast(); return stackMap.getLast();
} }
public void addVar(String s, Type t){ public void addVar(String s, Type t){
//Save temporary if not PMap wont save //Save temporary if not PMap wont save
PMap<String, Type> pmap = this.peppapeek(); PMap<String, ValueTable> pmap = this.peppapeek();
pmap= pmap.plus(s/*+"_"+this.id*/,t); 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())); stackMap.minus(stackMap.indexOf(stackMap.getLast()));
@@ -56,11 +78,11 @@ public class SymTable {
return false; return false;
} }
public Stack<PMap<String,Type>> stackmap(){ public Stack<PMap<String,ValueTable>> stackmap(){
return this.stackmap(); return this.stackmap();
} }
public Type getvar_Type(String s){ public ValueTable getvar_Type(String s){
for(int i= stackMap.size()-1; i>=0; i--){ for(int i= stackMap.size()-1; i>=0; i--){
if(stackMap.get(i).containsKey(s)){ if(stackMap.get(i).containsKey(s)){
return stackMap.get(i).get(s); return stackMap.get(i).get(s);

View File

@@ -22,10 +22,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
une simplement un val (var ou const) ou un binop une simplement un val (var ou const) ou un binop
*/ */
public static class InstrAndVal{ public static class InstrAndVal{
public ArrayList<AssignLVMImp> instr = null; public ArrayList<AssignLVMImpl> instr = null;
public ValLLVM val = null; public ValLLVM val = null;
public InstrAndVal(ArrayList<AssignLVMImp> instr, ValLLVM val){ public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
this.instr = instr; this.instr = instr;
this.val = val; this.val = val;
} }
@@ -60,7 +60,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
InstrAndVal res = instr.e().accept(this,h); InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val; ValLLVM var = res.val;
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var); InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var);
ArrayList<InstructionLLVM> result = new ArrayList<>(); ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr); result.addAll(res.instr);
result.add(r); result.add(r);
@@ -75,7 +75,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ArrayList<InstructionLLVM> result = new ArrayList<>(); ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr); result.addAll(res.instr);
//InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var); //InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
InstructionLLVM r = new StoreLLVMImp(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()/*"h.getVar(instr.t())*/)); InstructionLLVM r = new StoreLLVMImpl(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()/*"h.getVar(instr.t())*/));
result.add(r); result.add(r);
return result; return result;
} }
@@ -86,7 +86,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
for(int i = 0; i<instr.s().size();i++){ for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h); TypeLLVM t2 = instr.t().accept(this,h);
String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i)); String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i));
list.add(new AssignLVMImp(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2))); list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
} }
return list; return list;
} }
@@ -94,14 +94,15 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override @Override
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) { public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>(); ArrayList<InstructionLLVM> l = new ArrayList<>();
l.add(new PrintLLVMImp(new ArrayList())); //TODO l.add(new PrintLLVMImpl(new ArrayList())); //TODO
return l; return l;
} }
@Override @Override
public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) { public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) {
// TODO Auto-generated method stub ArrayList<InstructionLLVM> l = new ArrayList<>();
throw new UnsupportedOperationException("Unimplemented method 'visitRead'"); l.add(new ReadLLVMImpl(new ArrayList())); //TODO
return l;
} }
//EXPRESSION //EXPRESSION
@@ -120,7 +121,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override @Override
public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) { public InstrAndVal visitBinOp(BinopExpressionImp e, SymTable h) {
ArrayList<AssignLVMImp> list = new ArrayList<>(); ArrayList<AssignLVMImpl> list = new ArrayList<>();
InstrAndVal res1 = e.e1().accept(this, h); InstrAndVal res1 = e.e1().accept(this, h);
InstrAndVal res2 = e.e2().accept(this, h); InstrAndVal res2 = e.e2().accept(this, h);
@@ -138,7 +139,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
} }
String temp = h.addNewTempVar(); String temp = h.addNewTempVar();
VarLLVMImpl var = new VarLLVMImpl(type,temp); VarLLVMImpl var = new VarLLVMImpl(type,temp);
list.add(new AssignLVMImp(var, new BinOpLLVMImp(type,e.op(),val1,val2))); list.add(new AssignLVMImpl(var, new BinOpLLVMImpl(type,e.op(),val1,val2)));
return new InstrAndVal(list, var); return new InstrAndVal(list, var);
} }

View File

@@ -26,11 +26,11 @@ public interface Interface {
} }
public interface InstructionLLVMVisitor<H,S> { public interface InstructionLLVMVisitor<H,S> {
public S visitReturnLLVM(ReturnLLVMImp instr, H h); public S visitReturnLLVM(ReturnLLVMImpl instr, H h);
public S visitAssignLLVM(AssignLVMImp instr, H h); public S visitAssignLLVM(AssignLVMImpl instr, H h);
public S visitStoreLLVM(StoreLLVMImp instr, H h); public S visitStoreLLVM(StoreLLVMImpl instr, H h);
public S visitPrintLLVM(PrintLLVMImp instr, H h); public S visitPrintLLVM(PrintLLVMImpl instr, H h);
public S visitReadLLVM(ReadLLVMImp instr, H h); public S visitReadLLVM(ReadLLVMImpl instr, H h);
} }
//////////ExpressionLLVM (expression) //////////ExpressionLLVM (expression)
@@ -43,7 +43,7 @@ public interface Interface {
} }
public interface ExpressionLLVMVisitor<H,S> { public interface ExpressionLLVMVisitor<H,S> {
public S visitBinOpLLVM(BinOpLLVMImp e, H h); public S visitBinOpLLVM(BinOpLLVMImpl e, H h);
public S visitAllocaLLVM(allocaLLVMImpl e,H h); public S visitAllocaLLVM(allocaLLVMImpl e,H h);
public S visitLoadLLVM(loadLLVMImpl e,H h); public S visitLoadLLVM(loadLLVMImpl e,H h);
public S visitValLLVM(ValLLVMImpl e,H h); public S visitValLLVM(ValLLVMImpl e,H h);

View File

@@ -51,17 +51,17 @@ TypeLLVMVisitor<String,String>
} }
@Override @Override
public String visitReturnLLVM(ReturnLLVMImp instr, String h) { public String visitReturnLLVM(ReturnLLVMImpl instr, String h) {
return INDENT+"ret " + instr.type().accept(this, h) + " " + instr.e().accept(this, h); return INDENT+"ret " + instr.type().accept(this, h) + " " + instr.e().accept(this, h);
} }
@Override @Override
public String visitAssignLLVM(AssignLVMImp instr, String h) { public String visitAssignLLVM(AssignLVMImpl instr, String h) {
return INDENT+instr.var().accept(this, h) + " = " + instr.e().accept(this, h); return INDENT+instr.var().accept(this, h) + " = " + instr.e().accept(this, h);
} }
@Override @Override
public String visitBinOpLLVM(BinOpLLVMImp e, String h) { public String visitBinOpLLVM(BinOpLLVMImpl e, String h) {
String str = ""; String str = "";
switch(e.op()){ switch(e.op()){
case PLUS: case PLUS:
@@ -91,24 +91,25 @@ TypeLLVMVisitor<String,String>
} }
@Override @Override
public String visitStoreLLVM(StoreLLVMImp instr, String h) { public String visitStoreLLVM(StoreLLVMImpl instr, String h) {
return INDENT+"store " + instr.valType().accept(this, "") + " " + instr.e().accept(this, "") + ", " + instr.varType().accept(this, "") + "* " + instr.var().accept(this,""); return INDENT+"store " + instr.valType().accept(this, "") + " " + instr.e().accept(this, "") + ", " + instr.varType().accept(this, "") + "* " + instr.var().accept(this,"");
} }
@Override @Override
public String visitLoadLLVM(loadLLVMImpl e, String h) { public String visitLoadLLVM(loadLLVMImpl e, String h) {
return "load" + " i" + e.nbBits() + ", i"+ e.nbBits2() + "* %" + e.val().accept(this, h); return "load" + " i" + e.nbBits() + ", i"+ e.nbBits2() + "* %" + e.val().accept(this, h);
} }
@Override @Override
public String visitPrintLLVM(PrintLLVMImp instr, String h) { public String visitPrintLLVM(PrintLLVMImpl instr, String h) {
return INDENT+"print"; return INDENT+"call " + "...TODO..." +" printf " + "...TODO...";
} }
@Override @Override
public String visitReadLLVM(ReadLLVMImp instr, String h) { public String visitReadLLVM(ReadLLVMImpl instr, String h) {
// TODO Auto-generated method stub return INDENT+"call " + "...TODO..." +" scanf " + "...TODO...";
throw new UnsupportedOperationException("Unimplemented method 'visitReadLLVM'");
} }
@Override @Override

View File

@@ -40,35 +40,35 @@ public class ProgramLLVM {
*/ */
public static record AssignLVMImp(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{ public static record AssignLVMImpl(VarLLVMImpl var, ExpressionLLVM e) implements InstructionLLVM{
@Override @Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitAssignLLVM(this, h); return v.visitAssignLLVM(this, h);
} }
} }
public static record ReturnLLVMImp(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{ public static record ReturnLLVMImpl(TypeLLVM type, ExpressionLLVM e) implements InstructionLLVM{
@Override @Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitReturnLLVM(this, h); return v.visitReturnLLVM(this, h);
} }
} }
public static record StoreLLVMImp(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{ public static record StoreLLVMImpl(TypeLLVM valType, ExpressionLLVM e,TypeLLVM varType, ValLLVM var) implements InstructionLLVM{
@Override @Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitStoreLLVM(this, h); return v.visitStoreLLVM(this, h);
} }
} }
public static record PrintLLVMImp(ArrayList<String> l) implements InstructionLLVM{ public static record PrintLLVMImpl(ArrayList<Object> l) implements InstructionLLVM{
@Override @Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitPrintLLVM(this, h); return v.visitPrintLLVM(this, h);
} }
} }
public static record ReadLLVMImp(ArrayList<String> l) implements InstructionLLVM{ public static record ReadLLVMImpl(ArrayList<VarLLVMImpl> l) implements InstructionLLVM{
@Override @Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
@@ -78,7 +78,7 @@ public class ProgramLLVM {
} }
//Expression : //Expression :
public static record BinOpLLVMImp(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{ public static record BinOpLLVMImpl(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
@Override @Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitBinOpLLVM(this, h); return v.visitBinOpLLVM(this, h);

View File

@@ -3,4 +3,5 @@ FUNC INT main() {
b:=3 b:=3
c:=1 c:=1
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu" PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu"
READ a, b
RETURN 4 + 6 * 5 + 2 } RETURN 4 + 6 * 5 + 2 }