Skip to content

Commit

Permalink
avoid duplication in closure extension
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Oct 4, 2024
1 parent 620906b commit 3164c72
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/Juvix/Compiler/Nockma/Translation/FromTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -868,22 +868,23 @@ extendClosure ::
Tree.NodeExtendClosure ->
Sem r (Term Natural)
extendClosure Tree.NodeExtendClosure {..} = do
args <- mapM compile _nodeExtendClosureArgs
-- TODO: closure is evaluated multiple times
closure <- compile _nodeExtendClosureFun
let argsNum = getClosureField ClosureArgsNum closure
oldArgs = getClosureField ClosureArgs closure
allArgs <- append oldArgs argsNum (remakeList args)
newArgsNum <- add argsNum (nockIntegralLiteral (length _nodeExtendClosureArgs))
return . makeClosure $ \case
FunCode -> getClosureField FunCode closure
ClosureTotalArgsNum -> getClosureField ClosureTotalArgsNum closure
ClosureArgsNum -> newArgsNum
ClosureArgs -> allArgs
ArgsTuple -> getClosureField ArgsTuple closure
FunctionsLibrary -> getClosureField FunctionsLibrary closure
StandardLibrary -> getClosureField StandardLibrary closure
AnomaGetOrder -> getClosureField AnomaGetOrder closure
closureFun <- compile _nodeExtendClosureFun
withTemp closureFun $ \ref -> do
args <- mapM compile _nodeExtendClosureArgs
closure <- addressTempRef ref
let argsNum = getClosureField ClosureArgsNum closure
oldArgs = getClosureField ClosureArgs closure
allArgs <- append oldArgs argsNum (remakeList args)
newArgsNum <- add argsNum (nockIntegralLiteral (length _nodeExtendClosureArgs))
return . makeClosure $ \case
FunCode -> getClosureField FunCode closure
ClosureTotalArgsNum -> getClosureField ClosureTotalArgsNum closure
ClosureArgsNum -> newArgsNum
ClosureArgs -> allArgs
ArgsTuple -> getClosureField ArgsTuple closure
FunctionsLibrary -> getClosureField FunctionsLibrary closure
StandardLibrary -> getClosureField StandardLibrary closure
AnomaGetOrder -> getClosureField AnomaGetOrder closure

-- Calling convention for Anoma stdlib
--
Expand Down

0 comments on commit 3164c72

Please sign in to comment.