diff --git a/src/main/antlr/.antlr/VSLLexer.java b/src/main/antlr/.antlr/VSLLexer.java index b3fe90f..9cabdf7 100644 --- a/src/main/antlr/.antlr/VSLLexer.java +++ b/src/main/antlr/.antlr/VSLLexer.java @@ -1,4 +1,4 @@ -// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLLexer.g by ANTLR 4.13.1 +// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLLexer.g by ANTLR 4.13.1 package TP2; diff --git a/src/main/antlr/.antlr/VSLParser.java b/src/main/antlr/.antlr/VSLParser.java index 2217beb..11450d9 100644 --- a/src/main/antlr/.antlr/VSLParser.java +++ b/src/main/antlr/.antlr/VSLParser.java @@ -1,4 +1,4 @@ -// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1 +// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1 package TP2; import TP2.asd.Program.*; diff --git a/src/main/antlr/.antlr/VSLParserBaseListener.java b/src/main/antlr/.antlr/VSLParserBaseListener.java new file mode 100644 index 0000000..1b05a5b --- /dev/null +++ b/src/main/antlr/.antlr/VSLParserBaseListener.java @@ -0,0 +1,166 @@ +// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1 + + package TP2; + import TP2.asd.Program.*; + import TP2.asd.*; + import TP2.asd.Interface.*; + import TP2.asd.Eval.*; + + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link VSLParserListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +@SuppressWarnings("CheckReturnValue") +public class VSLParserBaseListener implements VSLParserListener { + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterProgram(VSLParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitProgram(VSLParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterFunction(VSLParser.FunctionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitFunction(VSLParser.FunctionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterPrototype(VSLParser.PrototypeContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitPrototype(VSLParser.PrototypeContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterList_instr(VSLParser.List_instrContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitList_instr(VSLParser.List_instrContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterInstruction(VSLParser.InstructionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitInstruction(VSLParser.InstructionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterExpression(VSLParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitExpression(VSLParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterTd_expression(VSLParser.Td_expressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitTd_expression(VSLParser.Td_expressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterLit(VSLParser.LitContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitLit(VSLParser.LitContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterIdent(VSLParser.IdentContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitIdent(VSLParser.IdentContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterType(VSLParser.TypeContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitType(VSLParser.TypeContext ctx) { } + + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitTerminal(TerminalNode node) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitErrorNode(ErrorNode node) { } +} \ No newline at end of file diff --git a/src/main/antlr/.antlr/VSLParserListener.java b/src/main/antlr/.antlr/VSLParserListener.java new file mode 100644 index 0000000..ab73cf1 --- /dev/null +++ b/src/main/antlr/.antlr/VSLParserListener.java @@ -0,0 +1,116 @@ +// Generated from c:/Users/Thibaut/Documents/Git/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1 + + package TP2; + import TP2.asd.Program.*; + import TP2.asd.*; + import TP2.asd.Interface.*; + import TP2.asd.Eval.*; + +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link VSLParser}. + */ +public interface VSLParserListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link VSLParser#program}. + * @param ctx the parse tree + */ + void enterProgram(VSLParser.ProgramContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#program}. + * @param ctx the parse tree + */ + void exitProgram(VSLParser.ProgramContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#function}. + * @param ctx the parse tree + */ + void enterFunction(VSLParser.FunctionContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#function}. + * @param ctx the parse tree + */ + void exitFunction(VSLParser.FunctionContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#prototype}. + * @param ctx the parse tree + */ + void enterPrototype(VSLParser.PrototypeContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#prototype}. + * @param ctx the parse tree + */ + void exitPrototype(VSLParser.PrototypeContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#list_instr}. + * @param ctx the parse tree + */ + void enterList_instr(VSLParser.List_instrContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#list_instr}. + * @param ctx the parse tree + */ + void exitList_instr(VSLParser.List_instrContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#instruction}. + * @param ctx the parse tree + */ + void enterInstruction(VSLParser.InstructionContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#instruction}. + * @param ctx the parse tree + */ + void exitInstruction(VSLParser.InstructionContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#expression}. + * @param ctx the parse tree + */ + void enterExpression(VSLParser.ExpressionContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#expression}. + * @param ctx the parse tree + */ + void exitExpression(VSLParser.ExpressionContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#td_expression}. + * @param ctx the parse tree + */ + void enterTd_expression(VSLParser.Td_expressionContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#td_expression}. + * @param ctx the parse tree + */ + void exitTd_expression(VSLParser.Td_expressionContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#lit}. + * @param ctx the parse tree + */ + void enterLit(VSLParser.LitContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#lit}. + * @param ctx the parse tree + */ + void exitLit(VSLParser.LitContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#ident}. + * @param ctx the parse tree + */ + void enterIdent(VSLParser.IdentContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#ident}. + * @param ctx the parse tree + */ + void exitIdent(VSLParser.IdentContext ctx); + /** + * Enter a parse tree produced by {@link VSLParser#type}. + * @param ctx the parse tree + */ + void enterType(VSLParser.TypeContext ctx); + /** + * Exit a parse tree produced by {@link VSLParser#type}. + * @param ctx the parse tree + */ + void exitType(VSLParser.TypeContext ctx); +} \ No newline at end of file diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index 6c5436d..7c1a7b7 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -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 diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index d615027..37118c9 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -8,16 +8,20 @@ import TP2.asd.Program.*; public interface Interface{ public interface ProgramI { public S accept(ProgramVisitor v, H h); + public String prettyprinter(); } public interface Function { public S accept(FunctionVisitor v, H h); + public String prettyprinter(String indent); } public interface Instruction { public S accept(InstrVisitor v, H h); + public String prettyprinter(String indent); } public interface Expression { public S accept(ExprVisitor v, H h); + public String prettyprinter(); } public interface ProgramVisitor { diff --git a/src/main/java/TP2/asd/Program.java b/src/main/java/TP2/asd/Program.java index 198a4f0..e3afe9a 100644 --- a/src/main/java/TP2/asd/Program.java +++ b/src/main/java/TP2/asd/Program.java @@ -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 instructions) implements ProgramI{ + + static String INDENT = " "; + + public static record ProgramImp(ArrayList fonctions) implements ProgramI{ public S accept(ProgramVisitor v, H h) { return v.visitProgram(this, h); } + + public String prettyprinter(){ + String str =""; + for(int i = 0; i instructions)implements Function { @@ -22,18 +45,44 @@ public class Program{ public S accept(FunctionVisitor 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 S accept(ExprVisitor 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 S accept(ExprVisitor 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 S accept(InstrVisitor 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 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;