This commit is contained in:
msyds committed 2026-09-01 05:05:47 -06:00
1 parent 7b411f48f9
commit fc29e66311
6 files changed
+68 -3

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
module Gyehoek.CPS.Contify
(
) where
import Gyehoek.Prelude
+24
View File
@@ -0,0 +1,24 @@
module Gyehoek.CPS.Hoist
( hoistProgram
) where
import Gyehoek.CPS.Syntax
import Gyehoek.Prelude
import qualified Data.HashMap.Strict as H
import Effectful.Writer.Static.Local
import Data.Foldable
type Hoist = Writer (HashMap Name Abs)
hoist :: Hoist :> es => Exp -> Eff es Exp
hoist = transformM \case
ExpLetRec bs m -> do
traverse_ (\(k,v) -> tell $ H.singleton k v) bs
pure m
e -> pure e
hoistProgram :: Program -> Eff es HoistedProgram
hoistProgram p = do
(body,bindings) <- runWriter $ traverseOf #body hoist p.body
pure $ MkHoistedProgram {body,bindings}
+26
View File
@@ -15,6 +15,7 @@ module Gyehoek.CPS.Syntax
, Name(..)
, Prim(..)
, Program(..)
, HoistedProgram(..)
, Lit(..)
, Imm(..)
, Obj(..)
@@ -58,6 +59,7 @@ import qualified Data.InvertibleGrammar.Base as IG
import Gyehoek.GenSym (Gen)
import Data.String (IsString)
import Control.Applicative
import qualified Data.HashMap.Strict as H
-- Data types
@@ -135,6 +137,7 @@ data Exp
data Kexp
= KexpVar Name
-- | Only to be used after contification.
| KexpKappa Kappa
deriving (Show, Generic, Data, Eq)
@@ -152,6 +155,22 @@ data Program = MkProgram
}
deriving (Show, Generic, Data)
data HoistedProgram = MkHoistedProgram
{ bindings :: HashMap Name Abs
, body :: Lambda
}
deriving stock (Show, Generic, Data)
type instance Index HoistedProgram = Name
type instance IxValue HoistedProgram = Abs
instance Ixed HoistedProgram where ix j = #bindings . ix j
instance At HoistedProgram where at j = #bindings . at j
instance Each HoistedProgram HoistedProgram Abs Abs where
each = #bindings . each
makePrisms ''Kappa
makePrisms ''Exp
makeFieldsId ''Exp
@@ -307,6 +326,13 @@ instance S.DatumIso Kexp where
instance S.DatumIso Program where
datumIso = S.with \prog -> S.datumIso @Lambda >>> prog
instance S.DatumIso HoistedProgram where
datumIso = S.with \prog ->
S.letLike "letrec"
(S.datumIso @Name) (S.datumIso @Abs) (S.datumIso @Lambda)
>>> S.onTail (S.iso H.fromList H.toList)
>>> prog
-- quasiquoters
+4
View File
@@ -35,6 +35,7 @@ import Control.Arrow ((>>>))
import Gyehoek.Prelude
import Gyehoek.Jalmot
import qualified Gyehoek.Sexp as S
import Gyehoek.CPS.Hoist (hoistProgram)
main :: IO ()
@@ -122,6 +123,9 @@ driver opts = do
closedCps <- closeProgram cps
when opts.dumpClosed do
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso closedCps
hoistedCps <- hoistProgram closedCps
when opts.dumpHoisted do
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso hoistedCps
let rt_is p = is (_Just . p) opts.runtime
dumpOrRun opts.dumpStackified (rt_is #Stackify)
(stackifyProgram closedCps)
+2
View File
@@ -29,6 +29,7 @@ data Options = MkOptions
, dumpCPS :: Bool
, dumpParsed :: Bool
, dumpStackified :: Bool
, dumpHoisted :: Bool
, traceStackified :: Bool
, runtime :: Maybe Runtime
, inspectWasm :: Bool
@@ -61,6 +62,7 @@ parser = do
dumpCPS <- switch (long "dump-cps")
dumpStackified <- switch (long "dump-stackified")
dumpParsed <- switch (long "dump-parsed")
dumpHoisted <- switch (long "dump-hoisted")
traceStackified <- switch (long "trace-stackified")
inspectWasm <- switch $ long "inspect-wasm" <> short 'p'
runtime <- option runtimeReader . fold $