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) {
String str = indent + "PRINT ";
for(int i = 0; i<instr.t().size(); i++){
String g = "";
Object o = instr.t().get(i);
if(o instanceof String){
str += "\"" + instr.t().get(i) +"\"";
}
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;
}
@Override
public String visitRead(ReadImp instr, String h) {
return "Tibo, tu peux implenter le truc stp";
public String visitRead(ReadImp instr, String indent) {
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

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
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitPrint(this, h);
}
}
public static record ReadImp(ArrayList t) implements Instruction{
public static record ReadImp(ArrayList<String> t) implements Instruction{
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitRead(this, h);

View File

@@ -4,24 +4,41 @@ import java.util.Stack;
import org.pcollections.*;
import TP2.asd.Interface.Type;
import TP2.asd.Program.Type_intImp;
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;
public SymTable(){
this.stackMap= ConsPStack.empty();
}
public int getNewId(){
int a = this.id;
this.id++;
return a;
}
public String addNewTempVar(/*Type type*/){
//TODO
String newVar = "temp"+this.id;
//this.addVar(newVar,new Type_intImp()); //TODO
id++;
this.addVar(newVar,new Type_intImp()); //TODO
return newVar;
}
public void next_layer(){
stackMap.plus(HashTreePMap.empty());
public PStack next_layer(){
return stackMap.plus(HashTreePMap.empty());
}
public void quit_layer() throws Exception{
@@ -30,14 +47,19 @@ public class SymTable {
}
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();
}
public void addVar(String s, Type t){
//Save temporary if not PMap wont save
PMap<String, Type> pmap = this.peppapeek();
pmap= pmap.plus(s/*+"_"+this.id*/,t);
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()));
@@ -56,11 +78,11 @@ public class SymTable {
return false;
}
public Stack<PMap<String,Type>> stackmap(){
public Stack<PMap<String,ValueTable>> 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--){
if(stackMap.get(i).containsKey(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
*/
public static class InstrAndVal{
public ArrayList<AssignLVMImp> instr = null;
public ArrayList<AssignLVMImpl> instr = null;
public ValLLVM val = null;
public InstrAndVal(ArrayList<AssignLVMImp> instr, ValLLVM val){
public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
this.instr = instr;
this.val = val;
}
@@ -60,7 +60,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
InstrAndVal res = instr.e().accept(this,h);
ValLLVM var = res.val;
InstructionLLVM r = new ReturnLLVMImp(var.getType(),var);
InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var);
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr);
result.add(r);
@@ -75,7 +75,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
ArrayList<InstructionLLVM> result = new ArrayList<>();
result.addAll(res.instr);
//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);
return result;
}
@@ -86,7 +86,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
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;
}
@@ -94,14 +94,15 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
ArrayList<InstructionLLVM> l = new ArrayList<>();
l.add(new PrintLLVMImp(new ArrayList())); //TODO
l.add(new PrintLLVMImpl(new ArrayList())); //TODO
return l;
}
@Override
public ArrayList<InstructionLLVM> visitRead(ReadImp instr, SymTable h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitRead'");
ArrayList<InstructionLLVM> l = new ArrayList<>();
l.add(new ReadLLVMImpl(new ArrayList())); //TODO
return l;
}
//EXPRESSION
@@ -120,7 +121,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override
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 res2 = e.e2().accept(this, h);
@@ -138,7 +139,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
}
String temp = h.addNewTempVar();
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);
}

View File

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

View File

@@ -51,17 +51,17 @@ TypeLLVMVisitor<String,String>
}
@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);
}
@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);
}
@Override
public String visitBinOpLLVM(BinOpLLVMImp e, String h) {
public String visitBinOpLLVM(BinOpLLVMImpl e, String h) {
String str = "";
switch(e.op()){
case PLUS:
@@ -91,24 +91,25 @@ TypeLLVMVisitor<String,String>
}
@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,"");
}
@Override
public String visitLoadLLVM(loadLLVMImpl e, String h) {
return "load" + " i" + e.nbBits() + ", i"+ e.nbBits2() + "* %" + e.val().accept(this, h);
}
@Override
public String visitPrintLLVM(PrintLLVMImp instr, String h) {
return INDENT+"print";
public String visitPrintLLVM(PrintLLVMImpl instr, String h) {
return INDENT+"call " + "...TODO..." +" printf " + "...TODO...";
}
@Override
public String visitReadLLVM(ReadLLVMImp instr, String h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitReadLLVM'");
public String visitReadLLVM(ReadLLVMImpl instr, String h) {
return INDENT+"call " + "...TODO..." +" scanf " + "...TODO...";
}
@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
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H 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
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H 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
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H 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
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H 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
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
@@ -78,7 +78,7 @@ public class ProgramLLVM {
}
//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
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitBinOpLLVM(this, h);

View File

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