Skip to content

Commit

Permalink
continue refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Oct 25, 2022
1 parent 8c88c08 commit 8521e69
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 275 deletions.
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Asm/Translation/FromCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ genCode infoTable fi =
goLet :: Bool -> Int -> BinderList Value -> Core.Let -> Code'
goLet isTail tempSize refs (Core.Let {..}) =
DL.append
(DL.snoc (go False tempSize refs _letValue) (mkInstr PushTemp))
(DL.snoc (go False tempSize refs (_letItem ^. Core.letItemValue)) (mkInstr PushTemp))
(snocPopTemp isTail $ go isTail (tempSize + 1) (BL.extend (Ref (DRef (TempRef tempSize))) refs) _letBody)

goCase :: Bool -> Int -> BinderList Value -> Core.Case -> Code'
Expand Down
8 changes: 4 additions & 4 deletions src/Juvix/Compiler/Core/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ eval !ctx !env0 = convertRuntimeNodes . eval' env0
NCst {} -> n
NApp (App i l r) ->
case eval' env l of
Closure env' (Lambda _ b) -> let !v = eval' env r in eval' (v : env') b
Closure env' (Lambda _ _ b) -> let !v = eval' env r in eval' (v : env') b
v -> evalError "invalid application" (mkApp i v (substEnv env r))
NBlt (BuiltinApp _ op args) -> applyBuiltin n env op args
NCtr (Constr i tag args) -> mkConstr i tag (map' (eval' env) args)
NLam l@Lambda {} -> Closure env l
NLet (Let _ v b) -> let !v' = eval' env v in eval' (v' : env) b
NLet (Let _ (LetItem _ v) b) -> let !v' = eval' env v in eval' (v' : env) b
NRec (LetRec _ vs b) ->
let !vs' = map (eval' env') (toList vs)
let !vs' = map (eval' env' . (^. letItemValue)) (toList vs)
!env' = revAppend vs' env
in foldr GHC.pseq (eval' env' b) vs'
NCase (Case i v bs def) ->
Expand All @@ -86,7 +86,7 @@ eval !ctx !env0 = convertRuntimeNodes . eval' env0

branch :: Node -> Env -> [Node] -> Tag -> Maybe Node -> [CaseBranch] -> Node
branch n !env !args !tag !def = \case
(CaseBranch _ tag' _ b) : _ | tag' == tag -> eval' (revAppend args env) b
(CaseBranch _ tag' _ _ b) : _ | tag' == tag -> eval' (revAppend args env) b
_ : bs' -> branch n env args tag def bs'
[] -> case def of
Just b -> eval' env b
Expand Down
15 changes: 12 additions & 3 deletions src/Juvix/Compiler/Core/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ cosmos f = ufoldA reassemble f
-- binder of the variable.
-- if fv = x1, x2, .., xn
-- the result is of the form λx1 λx2 .. λ xn b
captureFreeVars :: [(Index, Info)] -> Node -> Node
captureFreeVars :: [(Index, Binder)] -> Node -> Node
captureFreeVars fv
| n == 0 = id
| otherwise = mkLambdasB infos . mapFreeVars
Expand Down Expand Up @@ -116,7 +116,7 @@ developBeta = umap go
where
go :: Node -> Node
go n = case n of
NApp (App _ (NLam (Lambda _ body)) arg) -> subst arg body
NApp (App _ (NLam (Lambda {..})) arg) -> subst arg _lambdaBody
_ -> n

etaExpand :: Int -> Node -> Node
Expand All @@ -138,12 +138,21 @@ convertClosures = umap go
where
go :: Node -> Node
go n = case n of
Closure env (Lambda i b) -> substEnv env (mkLambda i b)
Closure env (Lambda i bi b) -> substEnv env (mkLambda i bi b)
_ -> n

convertRuntimeNodes :: Node -> Node
convertRuntimeNodes = convertClosures

argumentInfoFromBinder :: Binder -> ArgumentInfo
argumentInfoFromBinder i =
ArgumentInfo
{ _argumentName = i ^. binderName,
_argumentType = i ^. binderType,
_argumentIsImplicit = Explicit
}

-- TODO remove ?
argumentInfoFromInfo :: Info -> ArgumentInfo
argumentInfoFromInfo i =
ArgumentInfo
Expand Down
Loading

0 comments on commit 8521e69

Please sign in to comment.