summary refs log tree commit diff
path: root/source/mod_student/parser.mly
blob: ab095ef83176f306e0ec005230bc1c7b76eb9b51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
%start <Xi_lib.Ast.module_definition> file

%{
open Xi_lib
open Ast
open Parser_utils

let mkTag =
    let i = ref 0 in
    fun () ->
        let tag = !i in
        incr i;
        NodeTag tag


%}


%token EOF
%token COMMA
%token SEMICOLON
%token LPAREN
%token RPAREN
%token LBRACKET
%token RBRACKET
%token LSBRACKET
%token RSBRACKET
%token COLON
%token OP_PLUS
%token OP_MINUS
%token OP_MULT
%token OP_DIV
%token OP_REM
%token OP_AND
%token OP_OR
%token OP_EQ
%token OP_NEQ
%token OP_LE
%token OP_GE
%token OP_LT
%token OP_GT
%token OP_NOT
%token ASSIGN
%token IF
%token ELSE
%token WHILE
%token RETURN
%token LENGTH
%token <int>INT
%token <string>IDENTIFIER
%token <string>STRING
%token <char>CHAR
%token <bool>BOOL
%token UNDERSCORE
%token T_BOOL
%token T_INT


%%

file:
    | declarations= list(func) EOF
    { ModuleDefinition {global_declarations=declarations } }

func:
    | id=identifier LPAREN parameters=separated_list(COMMA,var_decl) RPAREN return=return_type body=option(statement_block)
    { GDECL_Function { loc=mkLocation $startpos;
    id=id;formal_parameters=parameters; return_types=return;body=body} }
var_decl:
    | id = identifier COLON t=typ 
    { VarDecl {loc=mkLocation $startpos;id=id;tp=t} }
option_var_decl:
    | decl=var_decl { Some decl }
    | UNDERSCORE { None }
return_type:
    | return = option(preceded(COLON,separated_list(COMMA,typ) ) )
      { match return with
        | None -> []
        | Some l -> l
      } 
typ:
    | t=base_type { t }
    | t=typ LSBRACKET dim=option(expression) RSBRACKET 
      { TEXPR_Array { loc=mkLocation $startpos;
        sub=t;dim=dim } }
base_type:
    | T_INT  {TEXPR_Int {loc=mkLocation $startpos} }
    | T_BOOL {TEXPR_Bool {loc=mkLocation $startpos} }
op_unary:
    | OP_MINUS { UNOP_Neg }
    | OP_NOT   { UNOP_Not }
op_mult_prec:
    | OP_MULT {BINOP_Mult}
    | OP_DIV  {BINOP_Div}
    | OP_REM  {BINOP_Rem}
op_add_prec:
    | OP_PLUS   {BINOP_Add}
    | OP_MINUS  {BINOP_Sub}
op_cmp_prec:
    | OP_LE  {RELOP_Le}
    | OP_LT  {RELOP_Lt}
    | OP_GE  {RELOP_Ge}
    | OP_GT  {RELOP_Gt}
op_eq_prec:
    | OP_EQ   {RELOP_Eq}
    | OP_NEQ  {RELOP_Ne}
expression:
    | node=expression_or {node}
expression_or:
    | node=expression_and {node}
    | left=expression_or OP_OR right=expression_and
       {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
        op=BINOP_Or;lhs=left;rhs=right } }
expression_and:
    | node=expression_eq {node}
    | left=expression_and OP_AND right=expression_eq
       {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
        op=BINOP_And;lhs=left;rhs=right } }
expression_eq:
    | node=expression_cmp {node}
    | left=expression_eq op=op_eq_prec right=expression_cmp
       {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
        op=op;lhs=left;rhs=right } }
expression_cmp:
    | node=expression_add {node}
    | left=expression_cmp op=op_cmp_prec right=expression_add
       {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos;
        op=op;lhs=left;rhs=right } }
expression_add:
    | node=expression_mult { node }
    | left=expression_add op=op_add_prec right=expression_mult
       {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
        op=op;lhs=left;rhs=right } }
expression_mult:
    | node=expression_unary { node }
    | left=expression_mult op=op_mult_prec right=expression_unary
       {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos;
        op=op;lhs=left;rhs=right } }
expression_unary:
    | node=expression_highest_prec { node }
    |  op=op_unary right=expression_unary
       {EXPR_Unop {tag=mkTag ();loc=mkLocation $startpos;
        op=op;sub=right } }
expression_highest_prec:
    | node=expression_index { node }
    | node=expression_call { node }
    | node=expression_length { node }
expression_index:
    | sub=expression_base index=option( delimited(LSBRACKET,expression,RSBRACKET) )
      {
        match index with
        | None -> sub
        | Some index -> EXPR_Index {tag=mkTag (); loc=mkLocation $startpos; expr=sub; index=index} 
      }
expression_base:
    | node=expression_identifier { node }
    | node=expression_integer { node }
    | node=expression_char    { node }
    | node=expression_string  { node }
    | node=expression_bool  { node }
    | node=expression_struct  { node }
    | LPAREN node=expression RPAREN { node }
expression_identifier:
    | id=identifier  { EXPR_Id {tag= mkTag ();loc=mkLocation $startpos;
                              id=id} }
expression_integer:
    | value=INT  { EXPR_Int {tag= mkTag ();loc=mkLocation $startpos;
                              value=Int32.of_int value} }
expression_char:
    | value=CHAR  { EXPR_Char {tag= mkTag ();loc=mkLocation $startpos;
                              value=value} }
expression_string:
    | value=STRING  { EXPR_String {tag= mkTag ();loc=mkLocation $startpos;
                              value=value} }
expression_bool:
    | value=BOOL  { EXPR_Bool {tag= mkTag ();loc=mkLocation $startpos;
                              value=value} }
expression_call:
    | value=call  { EXPR_Call value }
expression_struct:
    | LBRACKET elements=separated_list(COMMA,expression) RBRACKET
      { EXPR_Struct { tag=mkTag (); loc=mkLocation $startpos; elements=elements} }
expression_length:
    |  LENGTH LPAREN argument=expression RPAREN
      { EXPR_Length { tag=mkTag (); loc=mkLocation $startpos;
               arg=argument } }
statement_block:
    | LBRACKET body=append(terminated(separated_list(semicolons,statement),semicolons),loption(terminated(return_stmt,semicolons))) RBRACKET 
        { STMTBlock { loc=mkLocation $startpos;body=body } }
statement:
    | left=lvalue ASSIGN right=expression  { STMT_Assign {loc=mkLocation $startpos;
                              lhs=left;rhs=right} }
    | block=statement_block {STMT_Block block }
    | call=call {STMT_Call call }
    | var=var_decl init=option(preceded(ASSIGN,expression))
        {STMT_VarDecl { var=var;init=init } }
    | WHILE cond=expression body=statement 
        { STMT_While {loc=mkLocation $startpos;cond=cond;body=body } }
    | IF cond=expression then_branch=statement else_branch=option(preceded(ELSE,statement))
        { STMT_If {loc=mkLocation $startpos;cond=cond;then_branch=then_branch;
          else_branch=else_branch } }
    | head=option_var_decl COMMA tail=separated_nonempty_list(COMMA, option_var_decl) ASSIGN call=call
        { STMT_MultiVarDecl {loc=mkLocation $startpos; vars= head::tail; init=call } }
return_stmt:
    | RETURN values=separated_list(COMMA,expression)
        { [STMT_Return { loc=mkLocation $startpos; values=values }] }
call:
    | callee=identifier LPAREN arguments=separated_list(COMMA,expression) RPAREN
      { Call { tag=mkTag (); loc=mkLocation $startpos;
               callee=callee; arguments=arguments } }
    (* TODO Add other statements *)
lvalue:
    | id=identifier {LVALUE_Id {loc=mkLocation $startpos;id=id} }
    | sub=lvalue_index LSBRACKET index=expression RSBRACKET 
      {LVALUE_Index {loc=mkLocation $startpos;sub=sub;index=index} }
lvalue_index:
    | node=expression_identifier {node}
    | node=expression_call {node}
    | sub=lvalue_index LSBRACKET index=expression RSBRACKET
      {EXPR_Index {tag=mkTag (); loc=mkLocation $startpos; expr=sub; index=index} }
semicolons:
    | list(SEMICOLON) {}
identifier:
    |  IDENTIFIER
    { Identifier $1 }


(* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   ----------------------------------------------------------------------------- *)