prettyprinter en Visitor

This commit is contained in:
Rochas
2025-04-05 19:15:38 +02:00
parent 0a48bf22bf
commit 3f0ca04b19
3 changed files with 125 additions and 62 deletions

View File

@@ -16,24 +16,24 @@ public interface Interface{
} }
public interface ProgramVisitor<H,S> { public interface ProgramVisitor<H,S> {
public S visitProgram(ProgramImp programImp, H h); public S visitProgram(ProgramImp prog, H h);
} }
//////////Function //////////Function
public interface Function { public interface Function {
public <H,S> S accept(FunctionVisitor<H,S> v, H h); public <H,S> S accept(FunctionVisitor<H,S> v, H h);
public String prettyprinter(String indent);
public DefineLLVM toLLVM(); public DefineLLVM toLLVM();
} }
public interface FunctionVisitor<H,S> { public interface FunctionVisitor<H,S> {
public S visitFunction(FunctionImp e, H h); public S visitFunction(FunctionImp fun, H h);
} }
//////////Instruction //////////Instruction
public interface Instruction { public interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h); public <H,S> S accept(InstrVisitor<H,S> v, H h);
public String prettyprinter(String indent);
public ArrayList<InstructionLLVM> toLLVM(); public ArrayList<InstructionLLVM> toLLVM();
} }
@@ -43,12 +43,12 @@ public interface Interface{
public S visitDeclaration(DeclarationImp e,H h); public S visitDeclaration(DeclarationImp e,H h);
} }
//////////Expression //////////Expression
//We put prettyprinter here beause each expr will have to implement it like accept visitor //We put prettyprinter here beause each expr will have to implement it like accept visitor
//but each implement will be different for prettyprinter //but each implement will be different for prettyprinter
public interface Expression { public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h); public <H,S> S accept(ExprVisitor<H,S> v, H h);
public String prettyprinter();
public ArrayList<AssignLVMImp> toLLVM(); public ArrayList<AssignLVMImp> toLLVM();
} }
@@ -56,6 +56,7 @@ public interface Interface{
public ValLLVM getValLLVM(); public ValLLVM getValLLVM();
} }
public interface ExprVisitor<H,S> { public interface ExprVisitor<H,S> {
public S visitConst(ConstImp e,H h); public S visitConst(ConstImp e,H h);
public S visitBinOp(BinopExpressionImp e, H h); public S visitBinOp(BinopExpressionImp e, H h);
@@ -63,9 +64,17 @@ public interface Interface{
} }
public interface Type{ public interface Type{
public String prettyprinter(); public <H,S> S accept(TypeVisitor<H,S> v, H h);
public TypeLLVM toLLVM(); public TypeLLVM toLLVM();
} }
public interface TypeVisitor<H,S>{
public S visitInt(Type_intImp t, H h);
public S visitVoid(Type_voidImp t, H h);
}
public enum Op {PLUS, MINUS, TIMES, DIV, MOD} public enum Op {PLUS, MINUS, TIMES, DIV, MOD}
} }

View File

@@ -0,0 +1,98 @@
package TP2.asd;
import TP2.asd.Interface.TypeVisitor;
import TP2.asd.Interface.ExprVisitor;
import TP2.asd.Interface.FunctionVisitor;
import TP2.asd.Interface.InstrVisitor;
import TP2.asd.Interface.ProgramVisitor;
import TP2.asd.Program.AssignImp;
import TP2.asd.Program.BinopExpressionImp;
import TP2.asd.Program.ConstImp;
import TP2.asd.Program.DeclarationImp;
import TP2.asd.Program.FunctionImp;
import TP2.asd.Program.ProgramImp;
import TP2.asd.Program.Return_instrImp;
import TP2.asd.Program.Type_intImp;
import TP2.asd.Program.Type_voidImp;
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
FunctionVisitor<String,String>,
InstrVisitor<String,String>,
ExprVisitor<String,String>,
TypeVisitor<String,String>
{
static String INDENT = " ";
@Override
public String visitProgram(ProgramImp prog, String indent) {
String str ="";
for(int i = 0; i<prog.fonctions().size(); i++){
str += prog.fonctions().get(i).accept(this,INDENT);
if(i<prog.fonctions().size()-1) str += "\n";
}
return str;
}
@Override
public String visitFunction(FunctionImp fun, String indent) {
String str = indent+"FUNC " + fun.type().accept(this,"")+ " " + fun.nom() +"(){\n";
for(int i = 0; i<fun.instructions().size(); i++){
str += fun.instructions().get(i).accept(this,indent+INDENT)+"\n";
}
str+= indent+"}";
return str;
}
@Override
public String visitReturn(Return_instrImp e, String indent) {
return indent+"RETURN " + e.e().accept(this,"");
}
@Override
public String visitAssign(AssignImp e, String indent) {
return e.t()+ " :=" + e.e().accept(this,"");
}
@Override
public String visitDeclaration(DeclarationImp e, String indent) {
String str = indent + "declare "+e.t().accept(this,"") + " ";
for(int i = 0; i<e.s().size();i++){
str += e.s().get(i) + ",";
}
return str;
}
@Override
public String visitConst(ConstImp e, String indent) {
return e.c()+"";
}
@Override
public String visitBinOp(BinopExpressionImp e, String indent) {
String opStr = "?";
switch(e.op()){
case PLUS: opStr = "+"; break;
case DIV: opStr = "/"; break;
case MINUS: opStr = "-"; break;
case MOD: opStr = "%"; break;
case TIMES: opStr = "*"; break;
default:break;
}
return "(" + e.e1().accept(this,"") +" "+ opStr +" " + e.e2().accept(this,"") + ")";
}
@Override
public String visitInt(Type_intImp t, String h) {
return "INT";
}
@Override
public String visitVoid(Type_voidImp t, String h) {
return "VOID";
}
}

View File

@@ -17,12 +17,9 @@ public class Program{
} }
public String prettyprinter(){ public String prettyprinter(){
String str =""; PrettyprinterVisitor ppVisitor = new PrettyprinterVisitor();
for(int i = 0; i<fonctions.size(); i++){ return this.accept(ppVisitor, "");
str += fonctions.get(i).prettyprinter(INDENT);
if(i<fonctions.size()-1) str += "\n";
}
return str;
} }
@Override @Override
@@ -47,15 +44,6 @@ public class Program{
return v.visitFunction(this, h); return v.visitFunction(this, h);
} }
public String prettyprinter(String indent){
String str = indent+"FUNC " + type.prettyprinter()+ " " + nom +"(){\n";
for(int i = 0; i<instructions.size(); i++){
str += instructions.get(i).prettyprinter(indent+INDENT)+"\n";
}
str+= indent+"}";
return str;
}
@Override @Override
public DefineLLVM toLLVM() { public DefineLLVM toLLVM() {
ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>(); ArrayList<InstructionLLVM> instrLLVM = new ArrayList<>();
@@ -72,10 +60,6 @@ public class Program{
return v.visitConst(this, h); return v.visitConst(this, h);
} }
public String prettyprinter(){
return c+"";
}
public ValLLVM getValLLVM(){ public ValLLVM getValLLVM(){
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),c); ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),c);
return val; return val;
@@ -97,19 +81,6 @@ public class Program{
return v.visitBinOp(this, h); return v.visitBinOp(this, h);
} }
public String prettyprinter(){
String opStr = "?";
switch(op){
case PLUS: opStr = "+"; break;
case DIV: opStr = "/"; break;
case MINUS: opStr = "-"; break;
case MOD: opStr = "%"; break;
case TIMES: opStr = "*"; break;
default:break;
}
return "(" + e1.prettyprinter() +" "+ opStr +" " + e2.prettyprinter() + ")";
}
@Override @Override
public ArrayList<AssignLVMImp> toLLVM() { //TODO si e1 ou e2 est une constante, elle doit pouvoir être mise directement dans l'expression public ArrayList<AssignLVMImp> toLLVM() { //TODO si e1 ou e2 est une constante, elle doit pouvoir être mise directement dans l'expression
ArrayList<AssignLVMImp> list = new ArrayList<>(); ArrayList<AssignLVMImp> list = new ArrayList<>();
@@ -145,10 +116,6 @@ public class Program{
return v.visitReturn(this,h); return v.visitReturn(this,h);
} }
public String prettyprinter(String indent){
return indent+"RETURN " + e.prettyprinter();
}
@Override @Override
public ArrayList<InstructionLLVM> toLLVM() { public ArrayList<InstructionLLVM> toLLVM() {
ArrayList<AssignLVMImp> list = e.toLLVM(); ArrayList<AssignLVMImp> list = e.toLLVM();
@@ -167,11 +134,6 @@ public class Program{
return v.visitAssign(this, h); return v.visitAssign(this, h);
} }
@Override
public String prettyprinter(String indent) {
return t+ " :=" + e.toString();
}
@Override @Override
public ArrayList<InstructionLLVM> toLLVM() { public ArrayList<InstructionLLVM> toLLVM() {
ArrayList<AssignLVMImp> list = e.toLLVM(); ArrayList<AssignLVMImp> list = e.toLLVM();
@@ -190,15 +152,6 @@ public class Program{
return v.visitDeclaration(this, h); return v.visitDeclaration(this, h);
} }
@Override
public String prettyprinter(String indent) {
String str = indent + "declare "+t.prettyprinter() + " ";
for(int i = 0; i<s.size();i++){
str += s.get(i) + ", ";
}
return str;
}
@Override @Override
public ArrayList<InstructionLLVM> toLLVM() { public ArrayList<InstructionLLVM> toLLVM() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@@ -208,20 +161,23 @@ public class Program{
//Type //Type
public static record Type_voidImp() implements Type{ public static record Type_voidImp() implements Type{
public String prettyprinter() { @Override
return "VOID"; public <H, S> S accept(TypeVisitor<H, S> v, H h) {
return v.visitVoid(this, h);
} }
@Override @Override
public TypeLLVM toLLVM() { public TypeLLVM toLLVM() {
return new IntLLVMImpl(); return new IntLLVMImpl();
} }
} }
public static record Type_intImp() implements Type{ public static record Type_intImp() implements Type{
public String prettyprinter() { @Override
return "INT"; public <H, S> S accept(TypeVisitor<H, S> v, H h) {
return v.visitInt(this, h);
} }
@Override @Override