package TP2; import java.util.ArrayList; import java.util.List; public class LLVM { static String INDENT = " "; sealed interface Prog{} 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{ Type getType(); } sealed static interface Op{} sealed interface Var extends Expression{ }; sealed interface Val extends Expression{ }; public static void main(String[] args) { Add ADD = new Add(); Modulo MODULO = new Modulo(); Expression exp1 = new BinOp(ADD,new BinOp(ADD,new ValInt(0),new ValInt(1)),new ValInt(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 VarInt("a"); Var b = new VarInt("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 BinOp(MODULO,b,new ValInt(2))) )), new Return(new BinOp(ADD,a,b)) )) )); System.out.println(p2.toString()); } record ProgImpl(List fonctions) implements Prog{ public String toString(){ 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 items) implements Instr{ //TODO pas bon public String toString(String indent){ String str = indent + "READ"; for(int i = 0; i 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 += 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 VOID() implements Type{ public String toString(){ return "VOID"; } } record ListDeclImpl(Type type, List vars) implements ListDecl{ //erreur 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