fragment_0 manque code intermédiaire

This commit is contained in:
Vu Tuan Minh
2025-03-31 03:35:30 +02:00
parent 6643190a3f
commit 95fee3f1ae
8 changed files with 147 additions and 17 deletions

11
.idea/.gitignore generated vendored Normal file
View File

@@ -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/

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
TP2

View File

@@ -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"
}

View File

@@ -0,0 +1,9 @@
package TP2.Visitor;
import java.util.ArrayList;
public interface ASDVisitor {
public ArrayList<Function> visit(Ftion function);
public Instruction visit(Instruction instr);
}

View File

@@ -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(); }
;

View File

@@ -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<ASD.Function> out]:
{$out = new ArrayList<ASD.Function>();}
function returns [Program.List<Function> 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<Instruction> out]
@init{
List<Program.Instruction> instructions = new List<Program.Instruction>();
}:
instruction [table]{
instructions.add($instruction.out);
$out=instructions;
}
;
instruction [SymTable table] returns [List<Program.Instruction> 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(); }
;

View File

@@ -0,0 +1,9 @@
package TP2.asd;
interface Instruction {
public String prettyprinter();
}
interface Type{
public String prettyprinter();
}

View File

@@ -1,4 +1,40 @@
package TP2.asd;
public record Program() {
import java.util.ArrayList;
import java.util.stream.Collectors;
public record Program(ArrayList<Function> functions) {
}
record Function(Type type, String nom, ArrayList<Instruction> 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";
}
}