From d2aa4a19080f602bd8b94d2a4698b73e0e852e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Dybiec?= Date: Thu, 8 Nov 2018 18:31:37 +0100 Subject: Added structs and array indexing --- source/mod_student/parser.mly | 26 +++++++++++++++++++++----- 1 file 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: -- cgit v1.2.3