From 95fee3f1ae645295ad9c5c5545d0b9e24a4547fc Mon Sep 17 00:00:00 2001 From: Vu Tuan Minh Date: Mon, 31 Mar 2025 03:35:30 +0200 Subject: [PATCH] =?UTF-8?q?fragment=5F0=20manque=20code=20interm=C3=A9diai?= =?UTF-8?q?re?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 11 +++++ .idea/.name | 1 + .vscode/settings.json | 9 ---- src/main/Visitor/ASDVisitor.java | 9 ++++ src/main/antlr/VSLLexer.g | 14 ++++++ src/main/antlr/VSLParser.g | 73 +++++++++++++++++++++++++--- src/main/java/TP2/asd/Interface.java | 9 ++++ src/main/java/TP2/asd/Program.java | 38 ++++++++++++++- 8 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name delete mode 100644 .vscode/settings.json create mode 100644 src/main/Visitor/ASDVisitor.java create mode 100644 src/main/java/TP2/asd/Interface.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..478ba71 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,11 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +*.xml +.idea/ +.vscode/ \ No newline at end of file diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..b113f14 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +TP2 \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index b97bdc5..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "java.server.launchMode": "Standard", - "java.gradle.buildServer.enabled": "on", - "antlr4.generation": { - "mode": "none" // Don't generate source file with the antlr4-vscode extension - }, - "java.configuration.updateBuildConfiguration": "automatic", - "java.inlayHints.parameterNames.enabled": "none" -} diff --git a/src/main/Visitor/ASDVisitor.java b/src/main/Visitor/ASDVisitor.java new file mode 100644 index 0000000..527fa7f --- /dev/null +++ b/src/main/Visitor/ASDVisitor.java @@ -0,0 +1,9 @@ +package TP2.Visitor; + +import java.util.ArrayList; + + +public interface ASDVisitor { + public ArrayList visit(Ftion function); + public Instruction visit(Instruction instr); +} diff --git a/src/main/antlr/VSLLexer.g b/src/main/antlr/VSLLexer.g index a80ca2f..b279cba 100644 --- a/src/main/antlr/VSLLexer.g +++ b/src/main/antlr/VSLLexer.g @@ -42,10 +42,24 @@ READ : 'READ' ; PRINT : 'PRINT' ; +RETURN: 'RETURN' + ; TEXT : '"' (ASCII)+ '"' {getText().substring(1,getText().length()-1)} ; NUMBER : (DIGIT)+ ; +ParO: '(' + ; +ParF: ')' + ; +PLUS: '+' + ; +MINUS: '-' + ; +DIV:'/' + ; +TIMES: '*' + ; WS : (' '|'\n'|'\t') { skip(); } ; diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index 7234e46..e43744c 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -9,7 +9,7 @@ options { @header { package TP2; - import TP2.asd.*; + import TP2.Program.Program; } // On syntax error, raise exception rather than silently recovery @@ -20,17 +20,76 @@ options { } } - -program returns [Program p] : +program returns [Program.Program p] : func=function EOF - {$p = new Program($func.p);} + {$p = new Program.Program($func.p);} ; -function returns [List out]: - {$out = new ArrayList();} +function returns [Program.List out] + @init{ + SymTable sym_table = new SymTable(); + }: + FUNCTION type ident ParO ParF list_instr[sym_table] ; -prototype returns [Program p]: +prototype returns [Program.Program p]: +; + +list_instr [SymTable table] returns [Program.List out] + @init{ + List instructions = new List(); + }: + instruction [table]{ + instructions.add($instruction.out); + $out=instructions; + } +; + +instruction [SymTable table] returns [List out]: + RETURN expression[SymTable table] ; + +expression [SymTable table] returns [Type return_Type, Expression out]: + //Binop(op,exp,exp) et this.return_type est meme que left ou right + left_exp=expression[table] op=(PLUS| MINUS | DIV | TIMES) right_exp=expression[table]{ + switch($op.getType()){ + case PLUS: + $out = new Program.BinopExpression("PLUS",$left_exp,$right_exp); + break; + case MINUS: + $out = new Program.BinopExpression("MINUS", $l.out, $r.out); + break; + case TIMES: + $out = new Program.BinopExpression("TIMES", $l.out, $r.out); + break; + case DIV: + $out = new Program.BinopExpression("DIV", $l.out, $r.out); + break; + } + $return_Type = $right_exp.return_Type + } + | + //Const(lit) et peut-etre var aussi ( dans le futur )? + lit{ + $out=lit.out; + $return_Type= $lit.return_Type + } +; + +lit returns [Expression out, return_Type retType]: + NUMBER { + $return_Type = new Program.IntType(); + $out = new Program.IntergerNum($NUMBER.int); + } + ; + +ident returns [String ident]: + TEXT { $ident = $TEXT.getText(); } +; + +type returns [Type return_type]: + TYPE_INT { $return_type = new Program.Type.Type_int(); } + |TYPE_VOID { $return_type = new Program.Type.Type_void(); } +; \ No newline at end of file diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java new file mode 100644 index 0000000..ad0cc1c --- /dev/null +++ b/src/main/java/TP2/asd/Interface.java @@ -0,0 +1,9 @@ +package TP2.asd; + +interface Instruction { + public String prettyprinter(); +} + +interface Type{ + public String prettyprinter(); +} \ No newline at end of file diff --git a/src/main/java/TP2/asd/Program.java b/src/main/java/TP2/asd/Program.java index 4582f6f..b132c8b 100644 --- a/src/main/java/TP2/asd/Program.java +++ b/src/main/java/TP2/asd/Program.java @@ -1,4 +1,40 @@ package TP2.asd; -public record Program() { +import java.util.ArrayList; +import java.util.stream.Collectors; + +public record Program(ArrayList functions) { + + } + +record Function(Type type, String nom, ArrayList instructions){ + public String prettyprinter(){ + return "FUNC " +type.prettyprinter() +" "+ nom + + instructions.stream().map(Instruction::prettyprinter).collect(Collectors.joining("\n")); + // Map: appel prettyprinter pour isntruction, combiner dans 1 paragraph avec \n au milieu + } +} +record Return_instr(Expression e) implements Instruction{ + public String prettyprinter(){ + return "RETURN" +e.prettyprinter(); + } +} + +record Expression(){ + public String prettyprinter(){ + return ""; + } +} + +record Type_void() implements Type{ + public String prettyprinter() { + return "VOID"; + } +} + +record Type_int() implements Type{ + public String prettyprinter() { + return "INT"; + } +} \ No newline at end of file