|
|
|
|
@@ -8,9 +8,7 @@ options {
|
|
|
|
|
|
|
|
|
|
@header {
|
|
|
|
|
package TP2;
|
|
|
|
|
|
|
|
|
|
import TP2.Program.Program;
|
|
|
|
|
import TP2.SymTable.SymTable;
|
|
|
|
|
import TP2.asd.*;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// On syntax error, raise exception rather than silently recovery
|
|
|
|
|
@@ -21,41 +19,42 @@ options {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
program returns [Program.Program p] :
|
|
|
|
|
program returns [Program p] :
|
|
|
|
|
func=function
|
|
|
|
|
EOF
|
|
|
|
|
{$p = new Program.Program($func.out);}
|
|
|
|
|
{$p = new Program($func.out);}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function returns [List<Program.Function> out]
|
|
|
|
|
function returns [List<Function> out]
|
|
|
|
|
@init{
|
|
|
|
|
SymTable.SymTable sym_table = new SymTable.SymTable();
|
|
|
|
|
SymTable sym_table = new SymTable();
|
|
|
|
|
}:
|
|
|
|
|
FUNCTION t=type i=ident ParO ParF instrs=list_instr[sym_table]
|
|
|
|
|
{
|
|
|
|
|
$out.add(new Program.Function($t.return_type, $i.out, $instrs.out));
|
|
|
|
|
$out.add(new Function($t.return_type, $i.out, $instrs.out));
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
prototype returns [Program.Program p]:
|
|
|
|
|
prototype returns [Program p]:
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
list_instr [SymTable.SymTable table] returns [List<Program.Instruction> out]
|
|
|
|
|
list_instr [SymTable table] returns [ArrayList<Instruction> out]
|
|
|
|
|
@init{
|
|
|
|
|
List<Program.Instruction> instructions = new List<Program.Instruction>();
|
|
|
|
|
$out = new ArrayList<>();
|
|
|
|
|
}:
|
|
|
|
|
instruction [table]{
|
|
|
|
|
instructions.add($instruction.out);
|
|
|
|
|
$out=instructions;
|
|
|
|
|
instruction [table] {
|
|
|
|
|
$out.addAll($instruction.out);
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
instruction [SymTable.SymTable table] returns [List<Program.Instruction> out]:
|
|
|
|
|
RETURN expression[SymTable table]
|
|
|
|
|
instruction [SymTable table] returns [ArrayList<Instruction> out]:
|
|
|
|
|
RETURN e=expression [table]
|
|
|
|
|
{$out = new ArrayList<>();
|
|
|
|
|
$out.add(new Return_instr($e.out));}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
|
|
|
|
expression [SymTable table] returns [Type return_Type, Expression out]:
|
|
|
|
|
//Fix LL1 and compare add and sub first
|
|
|
|
|
left=td_expression[table] {
|
|
|
|
|
$out = $left.out;
|
|
|
|
|
@@ -64,10 +63,10 @@ expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
|
|
|
|
//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(
|
|
|
|
|
$out = new BinopExpression(
|
|
|
|
|
switch($op.getType()) {
|
|
|
|
|
case PLUS -> Op.PLUS;
|
|
|
|
|
case MINUS -> Op.MINUS;
|
|
|
|
|
case PLUS -> PLUS;
|
|
|
|
|
case MINUS -> MINUS;
|
|
|
|
|
default -> throw new IllegalArgumentException("Unknown operator");
|
|
|
|
|
},
|
|
|
|
|
$left.out,
|
|
|
|
|
@@ -77,7 +76,7 @@ expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
|
|
|
|
})*
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
td_expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
|
|
|
|
td_expression [SymTable table] returns [Type return_Type, Expression out]:
|
|
|
|
|
left=lit {
|
|
|
|
|
$out = $left.out;
|
|
|
|
|
$return_Type = $left.return_Type;
|
|
|
|
|
@@ -85,8 +84,8 @@ td_expression [SymTable.SymTable table] returns [Type return_Type, Expression ou
|
|
|
|
|
(op=(TIMES | DIV) right=lit {
|
|
|
|
|
$out = new Program.BinopExpression(
|
|
|
|
|
switch($op.getType()) {
|
|
|
|
|
case TIMES -> Op.TIMES;
|
|
|
|
|
case DIV -> Op.DIV;
|
|
|
|
|
case TIMES -> TIMES;
|
|
|
|
|
case DIV -> DIV;
|
|
|
|
|
default -> throw new IllegalArgumentException("Unknown operator");
|
|
|
|
|
},
|
|
|
|
|
$left.out,
|
|
|
|
|
@@ -98,8 +97,8 @@ td_expression [SymTable.SymTable table] returns [Type return_Type, Expression ou
|
|
|
|
|
|
|
|
|
|
lit returns [Expression out, Type return_Type]:
|
|
|
|
|
NUMBER {
|
|
|
|
|
$return_Type = new Program.Type_int();
|
|
|
|
|
$out = new Program.Const($NUMBER.int);
|
|
|
|
|
$return_Type = new Type_int();
|
|
|
|
|
$out = new Const($NUMBER.int);
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
@@ -108,6 +107,6 @@ ident returns [String out]:
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
type returns [Type return_type]:
|
|
|
|
|
TYPE_INT { $return_type = new Program.Type.Type_int(); }
|
|
|
|
|
|TYPE_VOID { $return_type = new Program.Type.Type_void(); }
|
|
|
|
|
TYPE_INT { $return_type = new Type_int(); }
|
|
|
|
|
|TYPE_VOID { $return_type = new Type_void(); }
|
|
|
|
|
;
|