From 3f0ca04b19bb56b9cf3395f3424098f5d7ac2b3b Mon Sep 17 00:00:00 2001 From: Rochas Date: Sat, 5 Apr 2025 19:15:38 +0200 Subject: [PATCH] prettyprinter en Visitor --- src/main/java/TP2/asd/Interface.java | 25 +++-- .../java/TP2/asd/PrettyprinterVisitor.java | 98 +++++++++++++++++++ src/main/java/TP2/asd/Program.java | 64 ++---------- 3 files changed, 125 insertions(+), 62 deletions(-) create mode 100644 src/main/java/TP2/asd/PrettyprinterVisitor.java diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index f2e28dd..8dea2b0 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -16,24 +16,24 @@ public interface Interface{ } public interface ProgramVisitor { - public S visitProgram(ProgramImp programImp, H h); + public S visitProgram(ProgramImp prog, H h); } + //////////Function public interface Function { public S accept(FunctionVisitor v, H h); - public String prettyprinter(String indent); public DefineLLVM toLLVM(); } public interface FunctionVisitor { - public S visitFunction(FunctionImp e, H h); + public S visitFunction(FunctionImp fun, H h); } + //////////Instruction public interface Instruction { public S accept(InstrVisitor v, H h); - public String prettyprinter(String indent); public ArrayList toLLVM(); } @@ -43,18 +43,19 @@ public interface Interface{ public S visitDeclaration(DeclarationImp e,H h); } + //////////Expression //We put prettyprinter here beause each expr will have to implement it like accept visitor //but each implement will be different for prettyprinter public interface Expression { public S accept(ExprVisitor v, H h); - public String prettyprinter(); public ArrayList toLLVM(); } public interface Val extends Expression{ - public ValLLVM getValLLVM(); - } + public ValLLVM getValLLVM(); + } + public interface ExprVisitor { public S visitConst(ConstImp e,H h); @@ -63,9 +64,17 @@ public interface Interface{ } public interface Type{ - public String prettyprinter(); + public S accept(TypeVisitor v, H h); public TypeLLVM toLLVM(); } + public interface TypeVisitor{ + public S visitInt(Type_intImp t, H h); + public S visitVoid(Type_voidImp t, H h); + } + + + + public enum Op {PLUS, MINUS, TIMES, DIV, MOD} } \ No newline at end of file diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java new file mode 100644 index 0000000..a0c70b6 --- /dev/null +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -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, + FunctionVisitor, + InstrVisitor, + ExprVisitor, + TypeVisitor + { + + static String INDENT = " "; + + @Override + public String visitProgram(ProgramImp prog, String indent) { + String str =""; + for(int i = 0; i instrLLVM = new ArrayList<>(); @@ -72,10 +60,6 @@ public class Program{ return v.visitConst(this, h); } - public String prettyprinter(){ - return c+""; - } - public ValLLVM getValLLVM(){ ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),c); return val; @@ -97,19 +81,6 @@ public class Program{ 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 public ArrayList toLLVM() { //TODO si e1 ou e2 est une constante, elle doit pouvoir ĂȘtre mise directement dans l'expression ArrayList list = new ArrayList<>(); @@ -145,10 +116,6 @@ public class Program{ return v.visitReturn(this,h); } - public String prettyprinter(String indent){ - return indent+"RETURN " + e.prettyprinter(); - } - @Override public ArrayList toLLVM() { ArrayList list = e.toLLVM(); @@ -167,11 +134,6 @@ public class Program{ return v.visitAssign(this, h); } - @Override - public String prettyprinter(String indent) { - return t+ " :=" + e.toString(); - } - @Override public ArrayList toLLVM() { ArrayList list = e.toLLVM(); @@ -190,15 +152,6 @@ public class Program{ return v.visitDeclaration(this, h); } - @Override - public String prettyprinter(String indent) { - String str = indent + "declare "+t.prettyprinter() + " "; - for(int i = 0; i toLLVM() { // TODO Auto-generated method stub @@ -208,20 +161,23 @@ public class Program{ //Type public static record Type_voidImp() implements Type{ - public String prettyprinter() { - return "VOID"; + @Override + public S accept(TypeVisitor v, H h) { + return v.visitVoid(this, h); } @Override public TypeLLVM toLLVM() { return new IntLLVMImpl(); } + } public static record Type_intImp() implements Type{ - public String prettyprinter() { - return "INT"; + @Override + public S accept(TypeVisitor v, H h) { + return v.visitInt(this, h); } @Override