Skip to content

Commit

Permalink
Core evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Sep 3, 2024
1 parent 1a77aa2 commit f904923
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Juvix/Compiler/Core/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ geval opts herr tab env0 = eval' env0
match n env vs = \case
br : brs ->
case matchPatterns [] vs (toList (br ^. matchBranchPatterns)) of
Just args -> eval' (args ++ env) (br ^. matchBranchBody)
Just args -> matchRhs (args ++ env) (br ^. matchBranchRhs)
Nothing -> match n env vs brs
where
matchPatterns :: [Node] -> [Node] -> [Pattern] -> Maybe [Node]
Expand All @@ -169,6 +169,18 @@ geval opts herr tab env0 = eval' env0
| tag == _patternConstrTag =
matchPatterns (v : acc) args _patternConstrArgs
patmatch _ _ _ = Nothing

matchRhs :: [Node] -> MatchBranchRhs -> Node
matchRhs env' = \case
MatchBranchRhsExpression e -> eval' env' e
MatchBranchRhsIfs ifs -> matchIfs env' (toList ifs)

matchIfs :: [Node] -> [SideIfBranch] -> Node
matchIfs env' = \case
SideIfBranch {..} : ifs -> case eval' env' _sideIfBranchCondition of
NCtr (Constr _ (BuiltinTag TagTrue) []) -> eval' env' _sideIfBranchBody
_ -> matchIfs env' ifs
[] -> match n env vs brs
[] ->
evalError "no matching pattern" (substEnv env n)

Expand Down

0 comments on commit f904923

Please sign in to comment.