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

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