pretty printer
This commit is contained in:
132
src/main/java/TP2/LLVM.java
Normal file
132
src/main/java/TP2/LLVM.java
Normal file
@@ -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<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(){
|
||||||
|
String str = "";
|
||||||
|
for(int i = 0; i<listDecls.size(); i++){
|
||||||
|
str += "\n" + listDecls.get(i).tostring();
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Aff(String var, Expression expression) implements Aff{
|
||||||
|
public String toString(){
|
||||||
|
return var + ":=" + expression.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Print(Item item) implements Print{
|
||||||
|
public String toString(){
|
||||||
|
return "PRINT" + item.tostring();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
record IfThenElse(Cond cond, List<Instr> instrs1, List<Instr> 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<Instr> 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<String> vars) implements ListDecl{
|
||||||
|
public String toString(){
|
||||||
|
String str = type.toString + " ";
|
||||||
|
for(int i = 0, i<vars.size(), i++){
|
||||||
|
if(i<vars.size()-1){
|
||||||
|
str += var+",";
|
||||||
|
}
|
||||||
|
else str += var;
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
record Expression() implements Expression{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user