prettyprinter
This commit is contained in:
@@ -29,9 +29,6 @@ public class Main {
|
||||
VSLLexer lexer = new VSLLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
|
||||
for (Token token : tokens.getTokens()) {
|
||||
System.out.println("Token: " + token.getText() + " Type: " + token.getType());
|
||||
}
|
||||
|
||||
// Instantiate Parser
|
||||
VSLParser parser = new VSLParser(tokens);
|
||||
@@ -42,6 +39,8 @@ public class Main {
|
||||
// Pretty-print the program (to debug parsing)
|
||||
System.err.println("todo " + ast);
|
||||
|
||||
System.out.println("\n\n PRETTYPRINTER : \n--------------\n" + ast.prettyprinter() + "\n--------------\nFIN PRETTYPRINTER");
|
||||
|
||||
// Verify the program semantic
|
||||
|
||||
// Generate the intermediate representation
|
||||
|
||||
@@ -8,16 +8,20 @@ import TP2.asd.Program.*;
|
||||
public interface Interface{
|
||||
public interface ProgramI {
|
||||
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
|
||||
public String prettyprinter();
|
||||
}
|
||||
public interface Function {
|
||||
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
|
||||
public String prettyprinter(String indent);
|
||||
}
|
||||
public interface Instruction {
|
||||
public <H,S> S accept(InstrVisitor<H,S> v, H h);
|
||||
public String prettyprinter(String indent);
|
||||
}
|
||||
|
||||
public interface Expression {
|
||||
public <H,S> S accept(ExprVisitor<H,S> v, H h);
|
||||
public String prettyprinter();
|
||||
}
|
||||
|
||||
public interface ProgramVisitor<H,S> {
|
||||
|
||||
@@ -1,17 +1,40 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import TP2.asd.Interface.*;
|
||||
import TP2.asd.Eval.*;
|
||||
import org.antlr.grammar.v3.ANTLRParser.defaultNodeOption_return;
|
||||
|
||||
import TP2.asd.Interface.ExprVisitor;
|
||||
import TP2.asd.Interface.Expression;
|
||||
import TP2.asd.Interface.Function;
|
||||
import TP2.asd.Interface.FunctionVisitor;
|
||||
import TP2.asd.Interface.InstrVisitor;
|
||||
import TP2.asd.Interface.Instruction;
|
||||
import TP2.asd.Interface.Op;
|
||||
import TP2.asd.Interface.ProgramI;
|
||||
import TP2.asd.Interface.ProgramVisitor;
|
||||
import TP2.asd.Interface.Type;
|
||||
|
||||
|
||||
|
||||
public class Program{
|
||||
public static record ProgramImp(ArrayList<Function> instructions) implements ProgramI{
|
||||
|
||||
static String INDENT = " ";
|
||||
|
||||
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{
|
||||
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
|
||||
return v.visitProgram(this, h);
|
||||
}
|
||||
|
||||
public String prettyprinter(){
|
||||
String str ="";
|
||||
for(int i = 0; i<fonctions.size(); i++){
|
||||
str += fonctions.get(i).prettyprinter(INDENT);
|
||||
if(i<fonctions.size()-1) str += "\n";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function {
|
||||
@@ -22,18 +45,44 @@ public class Program{
|
||||
public <H, S> S accept(FunctionVisitor<H, S> v, H h) {
|
||||
return v.visitFunction(this, h);
|
||||
}
|
||||
|
||||
public String prettyprinter(String indent){
|
||||
String str = indent+"FUNC " + type.prettyprinter()+ " " + nom +"(){\n";
|
||||
for(int i = 0; i<instructions.size(); i++){
|
||||
str += instructions.get(i).prettyprinter(indent+INDENT)+"\n";
|
||||
}
|
||||
str+= indent+"}";
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
public static record ConstImp(int c) implements Expression{
|
||||
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||
return v.visitConst(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
public String prettyprinter(){
|
||||
return c+"";
|
||||
}
|
||||
}
|
||||
|
||||
public static record BinopExpressionImp(Op op,Expression e1, Expression e2) implements Expression{
|
||||
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||
return v.visitBinOp(this, h);
|
||||
}
|
||||
|
||||
public String prettyprinter(){
|
||||
String opStr = "?";
|
||||
switch(op){
|
||||
case PLUS: opStr = "+"; break;
|
||||
case DIV: opStr = "/"; break;
|
||||
case MINUS: opStr = "-"; break;
|
||||
case MOD: opStr = "%"; break;
|
||||
case TIMES: opStr = "*"; break;
|
||||
default:break;
|
||||
}
|
||||
return "(" + e1.prettyprinter() +" "+ opStr +" " + e2.prettyprinter() + ")";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,6 +90,10 @@ public class Program{
|
||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||
return v.visitReturn(this,h);
|
||||
}
|
||||
|
||||
public String prettyprinter(String indent){
|
||||
return indent+"RETURN " + e.prettyprinter();
|
||||
}
|
||||
}
|
||||
|
||||
public static record Type_voidImp() implements Type{
|
||||
@@ -61,7 +114,7 @@ public class Program{
|
||||
public Integer visitProgram(ProgramImp e, Map<String, Integer> h) {
|
||||
Integer result = null;
|
||||
FunctionEval functionEval = new FunctionEval();
|
||||
for (Function function : e.instructions()) {
|
||||
for (Function function : e.fonctions()) {
|
||||
result = function.accept(functionEval, h);
|
||||
}
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user