prettyprinter en Visitor
This commit is contained in:
98
src/main/java/TP2/asd/PrettyprinterVisitor.java
Normal file
98
src/main/java/TP2/asd/PrettyprinterVisitor.java
Normal file
@@ -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<String,String>,
|
||||
FunctionVisitor<String,String>,
|
||||
InstrVisitor<String,String>,
|
||||
ExprVisitor<String,String>,
|
||||
TypeVisitor<String,String>
|
||||
{
|
||||
|
||||
static String INDENT = " ";
|
||||
|
||||
@Override
|
||||
public String visitProgram(ProgramImp prog, String indent) {
|
||||
String str ="";
|
||||
for(int i = 0; i<prog.fonctions().size(); i++){
|
||||
str += prog.fonctions().get(i).accept(this,INDENT);
|
||||
if(i<prog.fonctions().size()-1) str += "\n";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitFunction(FunctionImp fun, String indent) {
|
||||
String str = indent+"FUNC " + fun.type().accept(this,"")+ " " + fun.nom() +"(){\n";
|
||||
for(int i = 0; i<fun.instructions().size(); i++){
|
||||
str += fun.instructions().get(i).accept(this,indent+INDENT)+"\n";
|
||||
}
|
||||
str+= indent+"}";
|
||||
return str;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitReturn(Return_instrImp e, String indent) {
|
||||
return indent+"RETURN " + e.e().accept(this,"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitAssign(AssignImp e, String indent) {
|
||||
return e.t()+ " :=" + e.e().accept(this,"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitDeclaration(DeclarationImp e, String indent) {
|
||||
String str = indent + "declare "+e.t().accept(this,"") + " ";
|
||||
for(int i = 0; i<e.s().size();i++){
|
||||
str += e.s().get(i) + ",";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitConst(ConstImp e, String indent) {
|
||||
return e.c()+"";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitBinOp(BinopExpressionImp e, String indent) {
|
||||
String opStr = "?";
|
||||
switch(e.op()){
|
||||
case PLUS: opStr = "+"; break;
|
||||
case DIV: opStr = "/"; break;
|
||||
case MINUS: opStr = "-"; break;
|
||||
case MOD: opStr = "%"; break;
|
||||
case TIMES: opStr = "*"; break;
|
||||
default:break;
|
||||
}
|
||||
return "(" + e.e1().accept(this,"") +" "+ opStr +" " + e.e2().accept(this,"") + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitInt(Type_intImp t, String h) {
|
||||
return "INT";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitVoid(Type_voidImp t, String h) {
|
||||
return "VOID";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user