From 37dccf91da85f69a346253e2b8508f5475acec34 Mon Sep 17 00:00:00 2001 From: trochas Date: Tue, 25 Mar 2025 18:19:57 +0100 Subject: [PATCH 1/2] pretty printer --- src/main/java/TP2/LLVM.java | 132 ++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/main/java/TP2/LLVM.java diff --git a/src/main/java/TP2/LLVM.java b/src/main/java/TP2/LLVM.java new file mode 100644 index 0000000..9375698 --- /dev/null +++ b/src/main/java/TP2/LLVM.java @@ -0,0 +1,132 @@ +package TP2; + +import java.util.List; + + +public class LLVM { + sealed interface Prog{} + sealed interface Fonction{} + sealed interface Instr{} + sealed interface Bloc implements Instr{} + sealed interface Aff implements Instr{} + sealed interface Print implements Instr{} + sealed interface Read implements Instr{} + sealed interface IfThen implements Instr{} + sealed interface IfThenElse implements Instr{} + sealed interface While implements Instr{} + sealed interface Type{} + sealed interface INT implements Type{} + sealed interface ListDecl{} + sealed interface ListVar{} + sealed interface Expression{} + + + + record ProgImpl(List fonctions) implements Prog{ + + } + + record FonctionImpl(Type type, String nom, List instrs) implements Fonction{ + + } + + record Bloc(List listDecls) implements Bloc{ + public String toString(){ + String str = ""; + for(int i = 0; i instrs) implements IfThen{ + public String toString(){ + String str = "IF " + cond.toString() + "THEN"; + for(int i = 0; i< instrs1.size(); i++){ + str += "\n" + instrs1.get(i).toString(); + } + return str; + } + + } + + record IfThenElse(Cond cond, List instrs1, List instrs1) implements IfThenElse{ + public String toString(){ + String str = "IF " + cond.toString() + "THEN"; + for(int i = 0; i< instrs1.size(); i++){ + str += "\n" + instrs1.get(i).toString(); + } + str += "\nELSE"; + for(int i = 0; i< instrs2.size(); i++){ + str += "\n" + instrs2.get(i).toString(); + } + return str; + } + } + + record While(Cond cond, List instrs) implements While{ + public String toString(){ + String str = "WHILE " + cond.toString() +"\nDO {"; + for(int i = 0; i< instrs.size(); i++){ + str += "\n" + instrs.get(i).toString(); + } + str += "\n}"; + return str; + } + } + + record INT() implements INT{ + + public String toString(){ + return "INT"; + } + } + + record ListDecl(Type type, List vars) implements ListDecl{ + public String toString(){ + String str = type.toString + " "; + for(int i = 0, i Date: Sat, 29 Mar 2025 19:22:30 +0100 Subject: [PATCH 2/2] =?UTF-8?q?pretty=20printer=20fini=20(pas=20enti=C3=A8?= =?UTF-8?q?rement=20test=C3=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/TP2/LLVM.java | 306 +++++++++++++++++++++++++++--------- 1 file changed, 230 insertions(+), 76 deletions(-) diff --git a/src/main/java/TP2/LLVM.java b/src/main/java/TP2/LLVM.java index 9375698..70209e9 100644 --- a/src/main/java/TP2/LLVM.java +++ b/src/main/java/TP2/LLVM.java @@ -1,126 +1,280 @@ package TP2; +import java.util.ArrayList; import java.util.List; + + public class LLVM { + + static String INDENT = " "; + sealed interface Prog{} - sealed interface Fonction{} - sealed interface Instr{} - sealed interface Bloc implements Instr{} - sealed interface Aff implements Instr{} - sealed interface Print implements Instr{} - sealed interface Read implements Instr{} - sealed interface IfThen implements Instr{} - sealed interface IfThenElse implements Instr{} - sealed interface While implements Instr{} - sealed interface Type{} - sealed interface INT implements Type{} - sealed interface ListDecl{} - sealed interface ListVar{} + sealed interface Fonction{ + String toString(String string);} + sealed interface Instr permits Return,Bloc,Aff,Print,Read,IfThen,IfThenElse,While { + String toString(String string);} + sealed interface Type permits INT,VOID {} + sealed interface ListDecl{ + String toString(String string);} + sealed interface ListPar{} + sealed interface ListInstr{ + String toString(String string);} + sealed interface Par{} sealed interface Expression{} + public static void main(String[] args) { + + Expression exp1 = new Add(new Add(new Val(0),new Val(1)),new Val(2)); + ListPar param = new ListParImpl(new ArrayList<>()); + ArrayList l = new ArrayList<>(); + l.add(new Return(exp1)); + Fonction f = new FonctionImpl(new VOID(),"main",param,l); + + Prog p1 = new ProgImpl(List.of(f)); + + System.out.println(p1.toString()); + + System.out.println("\n"); + + Var a = new Var("a"); + Var b = new Var("b"); + Prog p2 = new ProgImpl(List.of( + f, + new FonctionImpl(new INT(),"maFonction",new ListParImpl(List.of(new ParImpl(new INT(),a),new ParImpl(new INT(),b) )),List.of( + new IfThenElse(a,List.of( + new Print(List.of("a = ",a)) + ),List.of( + new Print(List.of("b = ",b)) + )), + new While(b, List.of( + new Aff(b, new Modulo(b,new Val(2))) + )), + new Return(new Add(a,b)) + )) + + + + )); + + System.out.println(p2.toString()); + } + record ProgImpl(List fonctions) implements Prog{ - - } - - record FonctionImpl(Type type, String nom, List instrs) implements Fonction{ - - } - - record Bloc(List listDecls) implements Bloc{ public String toString(){ - String str = ""; + String str =""; + for(int i = 0; i instrs) implements Fonction{ + public String toString(String indent){ + String str = indent+"FUNC " + type.toString()+ " " + nom +" ("+param.toString()+"){\n"; + for(int i = 0; i listDecls, ListInstr instrs) implements Instr{ + public String toString(String indent){ + String str = indent +"{\n"; for(int i = 0; i instrs) implements IfThen{ - public String toString(){ - String str = "IF " + cond.toString() + "THEN"; - for(int i = 0; i< instrs1.size(); i++){ - str += "\n" + instrs1.get(i).toString(); + record Read(List items) implements Instr{ //TODO pas bon + public String toString(String indent){ + String str = indent + "READ"; + for(int i = 0; i instrs1, List instrs1) implements IfThenElse{ - public String toString(){ - String str = "IF " + cond.toString() + "THEN"; - for(int i = 0; i< instrs1.size(); i++){ - str += "\n" + instrs1.get(i).toString(); - } - str += "\nELSE"; - for(int i = 0; i< instrs2.size(); i++){ - str += "\n" + instrs2.get(i).toString(); - } - return str; - } - } - - record While(Cond cond, List instrs) implements While{ - public String toString(){ - String str = "WHILE " + cond.toString() +"\nDO {"; + record IfThen(Expression cond, List instrs) implements Instr{ + public String toString(String indent){ + String str = indent + "IF " + cond.toString() + "\n" + indent + "THEN\n"; for(int i = 0; i< instrs.size(); i++){ - str += "\n" + instrs.get(i).toString(); + str += instrs.get(i).toString(indent+INDENT) + "\n"; + if(i instrs1, List instrs2) implements Instr{ + public String toString(String indent){ + String str = indent + "IF " + cond.toString() + "\n" + indent + "THEN\n"; + for(int i = 0; i< instrs1.size(); i++){ + str += instrs1.get(i).toString(indent+INDENT) + "\n"; + } + str += indent + "ELSE\n"; + for(int i = 0; i< instrs2.size(); i++){ + str += instrs2.get(i).toString(indent+INDENT); + if(i instrs) implements Instr{ + public String toString(String indent){ + String str = indent + "WHILE " + cond.toString() +"\n" + indent +"DO{\n"; + for(int i = 0; i< instrs.size(); i++){ + str += instrs.get(i).toString(indent+INDENT) + "\n"; + } + str += indent + "}"; + return str; + } + } + record INT() implements Type{ public String toString(){ return "INT"; } } - record ListDecl(Type type, List vars) implements ListDecl{ + record VOID() implements Type{ public String toString(){ - String str = type.toString + " "; - for(int i = 0, i vars) implements ListDecl{ + public String toString(String indent){ + String str = indent + type.toString() + " "; + for(int i = 0; i pars) implements ListPar{ + public String toString(){ + String str = ""; + for(int i = 0; i instrs) implements ListInstr{ + public String toString(String indent){ + String str = ""; + for(int i = 0; i