Skip to content

Commit

Permalink
[#17] Implemented destructuring removal pass
Browse files Browse the repository at this point in the history
  • Loading branch information
JustusAdam committed Nov 20, 2018
1 parent 4fa4736 commit b1c684b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
41 changes: 37 additions & 4 deletions core/src/Ohua/ALang/Passes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
-- form of a sequence of let bindings which are invocations of
-- stateful functions on local or environment variables finalised by a
-- local binding as a return value.

-- This source code is licensed under the terms described in the associated LICENSE.TXT file
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExplicitForAll #-}
Expand All @@ -24,8 +23,9 @@ module Ohua.ALang.Passes where
import Ohua.Prelude

import Control.Monad.RWS.Lazy (evalRWST)
import Control.Monad.Writer (runWriter, listen, tell)
import Control.Monad.Writer (listen, runWriter, tell)
import Data.Functor.Foldable
import Data.Generics.Uniplate.Operations (rewriteM)
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HS

Expand Down Expand Up @@ -220,8 +220,7 @@ removeCurrying e = fst <$> evalRWST (para inlinePartials e) mempty ()
val <- asks (HM.lookup bnd)
Apply <$>
(maybe
(failWith $
"No suitable value found for binding " <> show bnd)
(failWith $ "No suitable value found for binding " <> show bnd)
pure
val) <*>
arg
Expand Down Expand Up @@ -361,3 +360,37 @@ compressEnvExpressions compress = either pure compress' <=< go
compressed <- compress expr
ref <- addEnvExpression compressed
pure $ Var $ Env ref

removeDestructuring ::
MonadOhua (EnvExpr m) m
=> (Int -> EnvExpr m)
-> Expression
-> m Expression
removeDestructuring litToEnv =
evaluatingStateT mempty .
rewriteM
(\case
Lambda (Destructure ds) body ->
unstructure ds (\bnd f -> Lambda bnd $ f body)
Let (Destructure ds) expr body ->
unstructure ds (\bnd f -> Let bnd expr $ f body)
_ -> pure Nothing)
where
unstructure bnds cont = do
bnd <- generateBinding
f <-
foldl (.) id <$>
mapM
(\(idx, bnd0) ->
Let (Direct bnd0) <$> mkNthExpr idx (Var $ Local bnd))
(zip [0 ..] bnds)
pure $ Just $ cont (Direct bnd) f
mkNthExpr idx source = do
envLit <- litAsExpr idx
pure $ Var (Sf Refs.nth Nothing) `Apply` envLit `Apply` source
litAsExpr lit = gets (HM.lookup lit) >>= maybe (createNewLitExpr lit) pure
createNewLitExpr lit = do
he <- addEnvExpression (litToEnv lit)
let e = Var $ Env he
modify (HM.insert lit e)
pure e
3 changes: 3 additions & 0 deletions core/src/Ohua/ALang/Refs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ smapG = "ohua.lang/smapG"

generate :: QualifiedBinding
generate = "ohua.lang/generate"

nth :: QualifiedBinding
nth = "ohua.lang/nth"

0 comments on commit b1c684b

Please sign in to comment.