pretty printer fini (pas entièrement testé)
This commit is contained in:
@@ -1,126 +1,280 @@
|
|||||||
package TP2;
|
package TP2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class LLVM {
|
public class LLVM {
|
||||||
|
|
||||||
|
static String INDENT = " ";
|
||||||
|
|
||||||
sealed interface Prog{}
|
sealed interface Prog{}
|
||||||
sealed interface Fonction{}
|
sealed interface Fonction{
|
||||||
sealed interface Instr{}
|
String toString(String string);}
|
||||||
sealed interface Bloc implements Instr{}
|
sealed interface Instr permits Return,Bloc,Aff,Print,Read,IfThen,IfThenElse,While {
|
||||||
sealed interface Aff implements Instr{}
|
String toString(String string);}
|
||||||
sealed interface Print implements Instr{}
|
sealed interface Type permits INT,VOID {}
|
||||||
sealed interface Read implements Instr{}
|
sealed interface ListDecl{
|
||||||
sealed interface IfThen implements Instr{}
|
String toString(String string);}
|
||||||
sealed interface IfThenElse implements Instr{}
|
sealed interface ListPar{}
|
||||||
sealed interface While implements Instr{}
|
sealed interface ListInstr{
|
||||||
sealed interface Type{}
|
String toString(String string);}
|
||||||
sealed interface INT implements Type{}
|
sealed interface Par{}
|
||||||
sealed interface ListDecl{}
|
|
||||||
sealed interface ListVar{}
|
|
||||||
sealed interface Expression{}
|
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<Instr> 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<Fonction> fonctions) implements Prog{
|
record ProgImpl(List<Fonction> fonctions) implements Prog{
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
record FonctionImpl(Type type, String nom, List<Instr> instrs) implements Fonction{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
record Bloc(List<ListDecl> listDecls) implements Bloc{
|
|
||||||
public String toString(){
|
public String toString(){
|
||||||
String str = "";
|
String str ="";
|
||||||
|
for(int i = 0; i<fonctions.size(); i++){
|
||||||
|
str += fonctions.get(i).toString(INDENT)+"\n";
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record FonctionImpl(Type type, String nom,ListPar param, List<Instr> instrs) implements Fonction{
|
||||||
|
public String toString(String indent){
|
||||||
|
String str = indent+"FUNC " + type.toString()+ " " + nom +" ("+param.toString()+"){\n";
|
||||||
|
for(int i = 0; i<instrs.size(); i++){
|
||||||
|
str += instrs.get(i).toString(indent+INDENT)+"\n";
|
||||||
|
}
|
||||||
|
str+= indent+"}";
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Return(Expression exp) implements Instr{
|
||||||
|
public String toString(String indent){
|
||||||
|
return indent+"RETURN " + exp.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Bloc(List<ListDecl> listDecls, ListInstr instrs) implements Instr{
|
||||||
|
public String toString(String indent){
|
||||||
|
String str = indent +"{\n";
|
||||||
for(int i = 0; i<listDecls.size(); i++){
|
for(int i = 0; i<listDecls.size(); i++){
|
||||||
str += "\n" + listDecls.get(i).tostring();
|
str += listDecls.get(i).toString(indent+INDENT) +"\n";
|
||||||
|
}
|
||||||
|
str += instrs.toString(indent+INDENT);
|
||||||
|
str += indent +"}";
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Aff(Var var, Expression expression) implements Instr{
|
||||||
|
public String toString(String indent){
|
||||||
|
return indent + var.toString() + ":=" + expression.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Print(List<Object> items) implements Instr{
|
||||||
|
public String toString(String indent){
|
||||||
|
String str = indent+"PRINT ";
|
||||||
|
for(int i = 0; i<items.size(); i++){
|
||||||
|
if(items.get(i) instanceof Expression){
|
||||||
|
str += items.get(i).toString();
|
||||||
|
}
|
||||||
|
else if(items.get(i) instanceof String){
|
||||||
|
str += '"'+items.get(i).toString()+'"';
|
||||||
|
}
|
||||||
|
else System.err.println("Item de mauvais type dans PRINT");
|
||||||
|
if(i<items.size()-1) str += ", ";
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
record Aff(String var, Expression expression) implements Aff{
|
record Read(List<Object> items) implements Instr{ //TODO pas bon
|
||||||
public String toString(){
|
public String toString(String indent){
|
||||||
return var + ":=" + expression.toString();
|
String str = indent + "READ";
|
||||||
}
|
for(int i = 0; i<items.size(); i++){
|
||||||
}
|
if(items.get(i) instanceof Exception || items.get(i) instanceof String){
|
||||||
|
str += items.get(i).toString();
|
||||||
record Print(Item item) implements Print{
|
if(i<items.size()-1) str += ", ";
|
||||||
public String toString(){
|
}
|
||||||
return "PRINT" + item.tostring();
|
else System.err.println("Item de mauvais type, READ");
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
record Read(Item item) implements Read{
|
|
||||||
public String toString(){
|
|
||||||
return "READ " + item.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
record IfThen(Cond cond, List<Instr> 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;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
record IfThenElse(Cond cond, List<Instr> instrs1, List<Instr> instrs1) implements IfThenElse{
|
record IfThen(Expression cond, List<Instr> instrs) implements Instr{
|
||||||
public String toString(){
|
public String toString(String indent){
|
||||||
String str = "IF " + cond.toString() + "THEN";
|
String str = indent + "IF " + cond.toString() + "\n" + indent + "THEN\n";
|
||||||
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<Instr> instrs) implements While{
|
|
||||||
public String toString(){
|
|
||||||
String str = "WHILE " + cond.toString() +"\nDO {";
|
|
||||||
for(int i = 0; i< instrs.size(); i++){
|
for(int i = 0; i< instrs.size(); i++){
|
||||||
str += "\n" + instrs.get(i).toString();
|
str += instrs.get(i).toString(indent+INDENT) + "\n";
|
||||||
|
if(i<instrs.size()-1) str += "\n";
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
record IfThenElse(Expression cond, List<Instr> instrs1, List<Instr> 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<instrs2.size()-1) str += "\n";
|
||||||
}
|
}
|
||||||
str += "\n}";
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
record INT() implements INT{
|
record While(Expression cond, List<Instr> 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(){
|
public String toString(){
|
||||||
return "INT";
|
return "INT";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
record ListDecl(Type type, List<String> vars) implements ListDecl{
|
record VOID() implements Type{
|
||||||
public String toString(){
|
public String toString(){
|
||||||
String str = type.toString + " ";
|
return "VOID";
|
||||||
for(int i = 0, i<vars.size(), i++){
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record ListDeclImpl(Type type, List<String> vars) implements ListDecl{
|
||||||
|
public String toString(String indent){
|
||||||
|
String str = indent + type.toString() + " ";
|
||||||
|
for(int i = 0; i<vars.size(); i++){
|
||||||
if(i<vars.size()-1){
|
if(i<vars.size()-1){
|
||||||
str += var+",";
|
str += vars.get(i)+",";
|
||||||
}
|
}
|
||||||
else str += var;
|
else str += vars.get(i);
|
||||||
}
|
}
|
||||||
return str
|
return str;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
record ListParImpl(List<Par> pars) implements ListPar{
|
||||||
record Expression() implements Expression{
|
public String toString(){
|
||||||
|
String str = "";
|
||||||
|
for(int i = 0; i<pars.size();i++){
|
||||||
|
str += pars.get(i).toString();
|
||||||
|
if(i<pars.size()-1) str += ", ";
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
record ListInstrImpl(List<Instr> instrs) implements ListInstr{
|
||||||
|
public String toString(String indent){
|
||||||
|
String str = "";
|
||||||
|
for(int i = 0; i<instrs.size();i++){
|
||||||
|
str += indent + instrs.get(i).toString() + "\n";
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record ParImpl(Type type, Var var) implements Par{
|
||||||
|
public String toString(){
|
||||||
|
return type.toString() + " " + var.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
record Add(Expression e1, Expression e2) implements Expression{
|
||||||
|
public String toString(){
|
||||||
|
return "("+ e1 + " + " + e2 + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Sub(Expression e1, Expression e2) implements Expression{
|
||||||
|
public String toString(){
|
||||||
|
return "("+ e1 + " - " + e2 + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Modulo(Expression e1, Expression e2) implements Expression{
|
||||||
|
public String toString(){
|
||||||
|
return "("+ e1 + " % " + e2 + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Mult(Expression e1, Expression e2) implements Expression{
|
||||||
|
public String toString(){
|
||||||
|
return "("+ e1 + " * " + e2 + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Div(Expression e1, Expression e2) implements Expression{
|
||||||
|
public String toString(){
|
||||||
|
return "("+ e1 + " / " + e2 + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Val(int val) implements Expression{
|
||||||
|
public String toString(){
|
||||||
|
return val+"";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Var(String nom) implements Expression{
|
||||||
|
public String toString(){
|
||||||
|
return nom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user