From c0800a64ef3c59b7927557a92292ef2425cdcdee Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Tue, 26 Mar 2019 12:00:09 +0100 Subject: [PATCH] Set membership is now a full-fledged expression. It used to be tied to the test expression of a conditional. --- AST.ml | 29 ++++++++++++----------------- AST.mli | 7 ++----- Parser.mly | 35 ++++++++++++++++------------------- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/AST.ml b/AST.ml index 66acbc3cf..71eb036ee 100644 --- a/AST.ml +++ b/AST.ml @@ -407,7 +407,7 @@ and fail_instr = { and conditional = { kwd_if : kwd_if; - test : test_expr; + test : expr; kwd_then : kwd_then; ifso : if_clause; terminator : semi option; @@ -419,10 +419,6 @@ and if_clause = ClauseInstr of instruction | ClauseBlock of (instructions * semi option) braces reg -and test_expr = - GenExpr of expr -| SetMem of set_membership reg - and set_membership = { set : expr; kwd_contains : kwd_contains; @@ -519,6 +515,7 @@ and expr = and set_expr = SetInj of set_injection reg +| SetMem of set_membership reg and set_injection = { opening : opening; @@ -705,7 +702,8 @@ and map_expr_to_region = function | MapInj {region; _} -> region and set_expr_to_region = function - SetInj {region; _} -> region + SetInj {region; _} +| SetMem {region; _} -> region and logic_expr_to_region = function BoolExpr e -> bool_expr_to_region e @@ -1093,7 +1091,7 @@ and print_conditional node = let {kwd_if; test; kwd_then; ifso; terminator; kwd_else; ifnot} = node in print_token kwd_if "if"; - print_test_expr test; + print_expr test; print_token kwd_then "then"; print_if_clause ifso; print_terminator terminator; @@ -1110,16 +1108,6 @@ and print_if_clause = function print_terminator terminator; print_token rbrace "}" -and print_test_expr = function - GenExpr e -> print_expr e -| SetMem m -> print_set_membership m - -and print_set_membership {value; _} = - let {set; kwd_contains; element} = value in - print_expr set; - print_token kwd_contains "contains"; - print_expr element - and print_case_instr (node : case_instr) = let {kwd_case; expr; kwd_of; lead_vbar; cases; kwd_end} = node in @@ -1235,6 +1223,13 @@ and print_map_expr = function and print_set_expr = function SetInj inj -> print_set_injection inj +| SetMem mem -> print_set_membership mem + +and print_set_membership {value; _} = + let {set; kwd_contains; element} = value in + print_expr set; + print_token kwd_contains "contains"; + print_expr element and print_map_lookup {path; index} = let {lbracket; inside; rbracket} = index.value in diff --git a/AST.mli b/AST.mli index 3811bc968..56fd67639 100644 --- a/AST.mli +++ b/AST.mli @@ -391,7 +391,7 @@ and fail_instr = { and conditional = { kwd_if : kwd_if; - test : test_expr; + test : expr; kwd_then : kwd_then; ifso : if_clause; terminator : semi option; @@ -403,10 +403,6 @@ and if_clause = ClauseInstr of instruction | ClauseBlock of (instructions * semi option) braces reg -and test_expr = - GenExpr of expr -| SetMem of set_membership reg - and set_membership = { set : expr; kwd_contains : kwd_contains; @@ -503,6 +499,7 @@ and expr = and set_expr = SetInj of set_injection reg +| SetMem of set_membership reg and set_injection = { opening : opening; diff --git a/Parser.mly b/Parser.mly index 42ab41617..80daa0229 100644 --- a/Parser.mly +++ b/Parser.mly @@ -641,7 +641,7 @@ proc_call: fun_call { $1 } conditional: - If test_expr Then if_clause option(SEMI) Else if_clause { + If expr Then if_clause option(SEMI) Else if_clause { let region = cover $1 (if_clause_to_region $7) in let value = { kwd_if = $1; @@ -668,22 +668,6 @@ if_clause: ClauseBlock {value; region} } -test_expr: - expr { GenExpr $1 } -| set_membership { SetMem $1 } - -set_membership: - expr Contains expr { - let start = expr_to_region $1 - and stop = expr_to_region $3 in - let region = cover start stop in - let value = { - set = $1; - kwd_contains = $2; - element = $3} - in {region; value} - } - case_instr: Case expr Of option(VBAR) cases End { let region = cover $1 $6 in @@ -794,13 +778,26 @@ expr: | conj_expr { $1 } conj_expr: - conj_expr And comp_expr { + conj_expr And set_membership { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} in ELogic (BoolExpr (And {region; value})) } +| set_membership { $1 } + +set_membership: + core_expr Contains set_membership { + let start = expr_to_region $1 + and stop = expr_to_region $3 in + let region = cover start stop in + let value = { + set = $1; + kwd_contains = $2; + element = $3} + in ESet (SetMem {region; value}) + } | comp_expr { $1 } comp_expr: @@ -926,7 +923,7 @@ unary_expr: core_expr: Int { EArith (Int $1) } -| var { EVar $1 } +| var { EVar $1 } (* TODO: Path *) | String { EString (String $1) } | Bytes { EBytes $1 } | C_False { ELogic (BoolExpr (False $1)) }