diff options
author | Paweł Dybiec <pdybiec@stud.cs.uni.wroc.pl> | 2018-11-04 20:12:52 +0100 |
---|---|---|
committer | Paweł Dybiec <pdybiec@stud.cs.uni.wroc.pl> | 2018-11-04 20:12:52 +0100 |
commit | 790fc88c8635abd5ce10353051016ec2853ecc39 (patch) | |
tree | 321b24d61d08a785378b3163544866d8eada76de | |
parent | Parse array dimensions (diff) |
Parse basic expressions and statements
-rw-r--r-- | source/mod_student/lexer.mll | 4 | ||||
-rw-r--r-- | source/mod_student/parser.mly | 83 |
2 files changed, 82 insertions, 5 deletions
diff --git a/source/mod_student/lexer.mll b/source/mod_student/lexer.mll index 806cfb8..ae11472 100644 --- a/source/mod_student/lexer.mll +++ b/source/mod_student/lexer.mll @@ -55,10 +55,11 @@ (* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv * Miejsce na twoje reguły *) + | "," { COMMA } | "(" { LPAREN } | ")" { RPAREN } | "{" { LBRACKET } - | "{" { RBRACKET } + | "}" { RBRACKET } | "[" { LSBRACKET } | "]" { RSBRACKET } | ":" { COLON } @@ -75,6 +76,7 @@ | ">=" { OP_GE } | "<" { OP_LT } | ">" { OP_GT } + | "!" { OP_NOT } | "=" { ASSIGN } | "if" { IF } | "else" { ELSE } diff --git a/source/mod_student/parser.mly b/source/mod_student/parser.mly index c031383..7a6c303 100644 --- a/source/mod_student/parser.mly +++ b/source/mod_student/parser.mly @@ -30,6 +30,7 @@ let mkTag = *) %token EOF +%token COMMA %token LPAREN %token RPAREN %token LBRACKET @@ -50,6 +51,7 @@ let mkTag = %token OP_GE %token OP_LT %token OP_GT +%token OP_NOT %token ASSIGN %token IF %token ELSE @@ -81,9 +83,9 @@ file: { ModuleDefinition {global_declarations=[] } } func: - | id=identifier LPAREN parameters=list(argument) RPAREN + | id=identifier LPAREN parameters=separated_list(COMMA,argument) RPAREN return=separated_list(COMMA,typ) body=option(statement_block) { GDECL_Function { loc=mkLocation $startpos; - id=id;formal_parameters=parameters; return_types=[];body=None} } + id=id;formal_parameters=parameters; return_types=return;body=body} } argument: | id = identifier COLON t=typ { VarDecl {loc=mkLocation $startpos;id=id;tp=t} } @@ -93,11 +95,84 @@ typ: { 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}} + | T_INT {TEXPR_Int {loc=mkLocation $startpos} } + | T_BOOL {TEXPR_Bool {loc=mkLocation $startpos} } 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_rel {node} + | left=expression_eq OP_EQ right=expression_rel + {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos; + op=RELOP_Eq;lhs=left;rhs=right } } + | left=expression_eq OP_NEQ right=expression_rel + {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos; + op=RELOP_Ne;lhs=left;rhs=right } } +expression_rel: + | node=expression_add {node} + | left=expression_rel OP_LE right=expression_add + {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos; + op=RELOP_Le;lhs=left;rhs=right } } + | left=expression_rel OP_GE right=expression_add + {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos; + op=RELOP_Ge;lhs=left;rhs=right } } + | left=expression_rel OP_LT right=expression_add + {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos; + op=RELOP_Lt;lhs=left;rhs=right } } + | left=expression_rel OP_GT right=expression_add + {EXPR_Relation {tag=mkTag ();loc=mkLocation $startpos; + op=RELOP_Gt;lhs=left;rhs=right } } +expression_add: + | node=expression_mult { node } + | left=expression_add OP_PLUS right=expression_mult + {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos; + op=BINOP_Add;lhs=left;rhs=right } } + | left=expression_add OP_MINUS right=expression_mult + {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos; + op=BINOP_Sub;lhs=left;rhs=right } } +expression_mult: + | node=expression_unary { node } + | left=expression_mult OP_MULT right=expression_unary + {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos; + op=BINOP_Mult;lhs=left;rhs=right } } + | left=expression_mult OP_DIV right=expression_unary + {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos; + op=BINOP_Div;lhs=left;rhs=right } } + | left=expression_mult OP_REM right=expression_unary + {EXPR_Binop {tag=mkTag ();loc=mkLocation $startpos; + op=BINOP_Rem;lhs=left;rhs=right } } +expression_unary: + | node=expression_highest { node } + | OP_MINUS right=expression_unary + {EXPR_Unop {tag=mkTag ();loc=mkLocation $startpos; + op=UNOP_Neg;sub=right } } + | OP_NOT right=expression_unary + {EXPR_Unop {tag=mkTag ();loc=mkLocation $startpos; + op=UNOP_Not;sub=right } } +expression_highest: + | node=expression_integer { node } + | node=expression_identifier { node } + (* TODO Add calls and arrays and other constants *) +expression_integer: | value=INT { EXPR_Int {tag= mkTag ();loc=mkLocation $startpos; value=Int32.of_int value} } +expression_identifier: + | id=identifier { EXPR_Id {tag= mkTag ();loc=mkLocation $startpos; + id=id} } +statement_block: + | LBRACKET body=list(statement) RBRACKET { STMTBlock { + loc=mkLocation $startpos;body=body } } +statement: + | block=statement_block {STMT_Block block } identifier: | IDENTIFIER { Identifier $1 } |