parser + asd non testé non builder
This commit is contained in:
@@ -10,6 +10,7 @@ options {
|
||||
package TP2;
|
||||
|
||||
import TP2.Program.Program;
|
||||
import TP2.SymTable.SymTable;
|
||||
}
|
||||
|
||||
// On syntax error, raise exception rather than silently recovery
|
||||
@@ -23,21 +24,24 @@ options {
|
||||
program returns [Program.Program p] :
|
||||
func=function
|
||||
EOF
|
||||
{$p = new Program.Program($func.p);}
|
||||
{$p = new Program.Program($func.out);}
|
||||
;
|
||||
|
||||
|
||||
function returns [Program.List<Function> out]
|
||||
function returns [List<Program.Function> out]
|
||||
@init{
|
||||
SymTable sym_table = new SymTable();
|
||||
SymTable.SymTable sym_table = new SymTable.SymTable();
|
||||
}:
|
||||
FUNCTION type ident ParO ParF list_instr[sym_table]
|
||||
FUNCTION t=type i=ident ParO ParF instrs=list_instr[sym_table]
|
||||
{
|
||||
$out.add(new Program.Function($t.return_type, $i.out, $instrs.out));
|
||||
}
|
||||
;
|
||||
|
||||
prototype returns [Program.Program p]:
|
||||
;
|
||||
|
||||
list_instr [SymTable table] returns [Program.List<Instruction> out]
|
||||
list_instr [SymTable.SymTable table] returns [List<Program.Instruction> out]
|
||||
@init{
|
||||
List<Program.Instruction> instructions = new List<Program.Instruction>();
|
||||
}:
|
||||
@@ -47,46 +51,60 @@ list_instr [SymTable table] returns [Program.List<Instruction> out]
|
||||
}
|
||||
;
|
||||
|
||||
instruction [SymTable table] returns [List<Program.Instruction> out]:
|
||||
instruction [SymTable.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
|
||||
expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
||||
//Fix LL1 and compare add and sub first
|
||||
left=td_expression[table] {
|
||||
$out = $left.out;
|
||||
$return_Type = $left.return_Type;
|
||||
}
|
||||
//maybe it will be binop(s) with add/sub here
|
||||
//if it is mul or div, they are all gonna be left=td_expression[table] to handle
|
||||
(op=(PLUS | MINUS) right=td_expression[table] {
|
||||
$out = new Program.BinopExpression(
|
||||
switch($op.getType()) {
|
||||
case PLUS -> Op.PLUS;
|
||||
case MINUS -> Op.MINUS;
|
||||
default -> throw new IllegalArgumentException("Unknown operator");
|
||||
},
|
||||
$left.out,
|
||||
$right.out
|
||||
);
|
||||
$return_Type = $right.return_Type;
|
||||
})*
|
||||
;
|
||||
|
||||
lit returns [Expression out, return_Type retType]:
|
||||
td_expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
||||
left=lit {
|
||||
$out = $left.out;
|
||||
$return_Type = $left.return_Type;
|
||||
}
|
||||
(op=(TIMES | DIV) right=lit {
|
||||
$out = new Program.BinopExpression(
|
||||
switch($op.getType()) {
|
||||
case TIMES -> Op.TIMES;
|
||||
case DIV -> Op.DIV;
|
||||
default -> throw new IllegalArgumentException("Unknown operator");
|
||||
},
|
||||
$left.out,
|
||||
$right.out
|
||||
);
|
||||
$return_Type = $right.return_Type;
|
||||
})*
|
||||
;
|
||||
|
||||
lit returns [Expression out, Type return_Type]:
|
||||
NUMBER {
|
||||
$return_Type = new Program.IntType();
|
||||
$out = new Program.IntergerNum($NUMBER.int);
|
||||
$return_Type = new Program.Type_int();
|
||||
$out = new Program.Const($NUMBER.int);
|
||||
}
|
||||
;
|
||||
|
||||
ident returns [String ident]:
|
||||
TEXT { $ident = $TEXT.getText(); }
|
||||
ident returns [String out]:
|
||||
TEXT { $out = $TEXT.getText(); }
|
||||
;
|
||||
|
||||
type returns [Type return_type]:
|
||||
|
||||
Reference in New Issue
Block a user