summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Dybiec <pdybiec@stud.cs.uni.wroc.pl>2018-11-08 18:31:37 +0100
committerPaweł Dybiec <pdybiec@stud.cs.uni.wroc.pl>2018-11-08 18:31:37 +0100
commitd2aa4a19080f602bd8b94d2a4698b73e0e852e84 (patch)
tree4b4d750bf66716cceb39e713bd1f6c2304a93fac
parentAdd semicolons, multi variable declaration. Fix returns (diff)
Added structs and array indexing
-rw-r--r--source/mod_student/parser.mly26
1 files changed, 21 insertions, 5 deletions
diff --git a/source/mod_student/parser.mly b/source/mod_student/parser.mly
index b55622b..f8a71eb 100644
--- a/source/mod_student/parser.mly
+++ b/source/mod_student/parser.mly
@@ -144,16 +144,23 @@ expression_unary:
{EXPR_Unop {tag=mkTag ();loc=mkLocation $startpos;
op=op;sub=right } }
expression_highest_prec:
- | node=expression_base { node }
+ | 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_char { node }
+ | node=expression_string { node }
+ | node=expression_struct { node }
| LPAREN node=expression RPAREN { node }
- (* TODO Add calls and arrays and other constants *)
expression_identifier:
| id=identifier { EXPR_Id {tag= mkTag ();loc=mkLocation $startpos;
id=id} }
@@ -171,6 +178,9 @@ expression_bool:
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;
@@ -202,7 +212,13 @@ call:
(* TODO Add other statements *)
lvalue:
| id=identifier {LVALUE_Id {loc=mkLocation $startpos;id=id} }
- (* TODO lvalue index *)
+ | 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: