From 2893fc0412f6cd122047b49df606770a141888d7 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Thu, 2 Jul 2020 23:21:14 +0200 Subject: [PATCH] fix detection of matching on bool in the spiller --- src/passes/11-interpreter/interpreter.ml | 9 +++++++-- src/passes/11-spilling/compiler.ml | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/passes/11-interpreter/interpreter.ml b/src/passes/11-interpreter/interpreter.ml index b875613e2..3ae0815a3 100644 --- a/src/passes/11-interpreter/interpreter.ml +++ b/src/passes/11-interpreter/interpreter.ml @@ -342,8 +342,13 @@ and eval : Ast_typed.expression -> env -> (value , _) result let {hd;tl;body;tv=_} = cases.match_cons in let env' = Env.extend (Env.extend env (hd,head)) (tl, V_List tail) in eval body env' - | Match_variant {cases=[{constructor=Constructor t;body=match_true};{constructor=Constructor f; body=match_false}];_}, V_Ct (C_bool b) - when String.equal t "true" && String.equal f "false" -> + | Match_variant {cases;_}, V_Ct (C_bool b) -> + let ctor_body (case : matching_content_case) = (case.constructor, case.body) in + let cases = CMap.of_list (List.map ctor_body cases) in + let get_case c = + (CMap.find (Constructor c) cases) in + let match_true = get_case "true" in + let match_false = get_case "false" in if b then eval match_true env else eval match_false env | Match_variant {cases ; tv=_} , V_Construct (matched_c , proj) -> diff --git a/src/passes/11-spilling/compiler.ml b/src/passes/11-spilling/compiler.ml index 9d0f560a8..bc450842e 100644 --- a/src/passes/11-spilling/compiler.ml +++ b/src/passes/11-spilling/compiler.ml @@ -468,11 +468,20 @@ and compile_expression (ae:AST.expression) : (expression , spilling_error) resul in return @@ E_if_cons (expr' , nil , cons) ) - | Match_variant {cases=[{constructor=Constructor t;body=match_true};{constructor=Constructor f;body=match_false}];_} - when String.equal t "true" && String.equal f "false" -> - let%bind (t , f) = bind_map_pair (compile_expression) (match_true, match_false) in - return @@ E_if_bool (expr', t, f) | Match_variant {cases ; tv} -> ( + match expr'.type_expression.type_content with + | T_base TB_bool -> + let ctor_body (case : AST.matching_content_case) = (case.constructor, case.body) in + let cases = AST.CMap.of_list (List.map ctor_body cases) in + let get_case c = + trace_option + (corner_case ~loc:__LOC__ ("missing " ^ c ^ " case in match")) + (AST.CMap.find_opt (Constructor c) cases) in + let%bind match_true = get_case "true" in + let%bind match_false = get_case "false" in + let%bind (t , f) = bind_map_pair (compile_expression) (match_true, match_false) in + return @@ E_if_bool (expr', t, f) + | _ -> let%bind tree = trace_strong (corner_case ~loc:__LOC__ "getting lr tree") @@ tree_of_sum tv in