Compare commits
11
Commits
idk
..
dfb44f06ba
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dfb44f06ba | ||
|
|
c896a5181b | ||
|
|
4c0bc567a0 | ||
|
|
148b6b0d8b | ||
|
|
4d96ebfc31 | ||
|
|
fc29e66311 | ||
|
|
7b411f48f9 | ||
|
|
a62f1d6579 | ||
|
|
35e1b0cbe2 | ||
|
|
64641bb258 | ||
|
|
57b1cc830d |
@@ -1,9 +0,0 @@
|
|||||||
#+title: on libraries
|
|
||||||
|
|
||||||
R⁷RS leaves it unspecified how exactly libraries correspond to files:
|
|
||||||
|
|
||||||
#+begin_quote
|
|
||||||
Programs and libraries are typically stored in files, although in some implementations they can be entered interactively into a running Scheme system. Other paradigms are possible. Implementations which store libraries in files should document the mapping from the name of a library to its location in the file system.
|
|
||||||
#+end_quote
|
|
||||||
|
|
||||||
thus the implementation of ~define-library~ is open to much interpretation. we could possibly define libraries as first-class objects, or deal with them statically. the former case is appealing to me, as it could massively simplify interactive use.
|
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
nodejs
|
nodejs
|
||||||
wasm-tools
|
wasm-tools
|
||||||
wac-cli
|
wac-cli
|
||||||
gauche
|
guile
|
||||||
rust-analyzer
|
rust-analyzer
|
||||||
wasmtime
|
wasmtime
|
||||||
# bashInteractive is necessary to work around an
|
# bashInteractive is necessary to work around an
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
ret > ExitSuccess
|
ret > ExitSuccess
|
||||||
out > 12
|
out > 12
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ret > ExitSuccess
|
|
||||||
out > (456 . 123)
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
(let ((p (cons 123 456)))
|
|
||||||
(cons (cdr p) (car p)))
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ret > ExitSuccess
|
|
||||||
out > 123
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
123
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ret > ExitSuccess
|
|
||||||
out > (0 . (1 . (4 . (9 . (16 . ())))))
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
(letrec ((my-map (λ (f l)
|
|
||||||
(if (pair? l)
|
|
||||||
(cons (f (car l))
|
|
||||||
(my-map f (cdr l)))
|
|
||||||
(list)))))
|
|
||||||
(my-map (λ (x) (* x x))
|
|
||||||
(list 0 1 2 3 4)))
|
|
||||||
+9
-6
@@ -58,9 +58,11 @@ library
|
|||||||
-- cabal-fmt: expand src
|
-- cabal-fmt: expand src
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
Gyehoek.CPS.Close
|
Gyehoek.CPS.Close
|
||||||
|
Gyehoek.CPS.Contify
|
||||||
Gyehoek.CPS.Convert
|
Gyehoek.CPS.Convert
|
||||||
Gyehoek.CPS.Eval
|
Gyehoek.CPS.Eval
|
||||||
Gyehoek.CPS.Hoist
|
Gyehoek.CPS.Hoist
|
||||||
|
Gyehoek.CPS.Stackify
|
||||||
Gyehoek.CPS.Syntax
|
Gyehoek.CPS.Syntax
|
||||||
Gyehoek.Driver
|
Gyehoek.Driver
|
||||||
Gyehoek.GenSym
|
Gyehoek.GenSym
|
||||||
@@ -69,8 +71,6 @@ library
|
|||||||
Gyehoek.Lift1
|
Gyehoek.Lift1
|
||||||
Gyehoek.Options
|
Gyehoek.Options
|
||||||
Gyehoek.Prelude
|
Gyehoek.Prelude
|
||||||
Gyehoek.Scheme.Expand
|
|
||||||
Gyehoek.Scheme.Expand.Old
|
|
||||||
Gyehoek.Scheme.Syntax
|
Gyehoek.Scheme.Syntax
|
||||||
Gyehoek.Sexp
|
Gyehoek.Sexp
|
||||||
Gyehoek.Sexp.Grammar
|
Gyehoek.Sexp.Grammar
|
||||||
@@ -79,6 +79,9 @@ library
|
|||||||
Gyehoek.Sexp.QQ
|
Gyehoek.Sexp.QQ
|
||||||
Gyehoek.Sexp.Read
|
Gyehoek.Sexp.Read
|
||||||
Gyehoek.Sexp.Syntax
|
Gyehoek.Sexp.Syntax
|
||||||
|
Gyehoek.Stack.Lower
|
||||||
|
Gyehoek.Stack.Syntax
|
||||||
|
Gyehoek.Stack.VM
|
||||||
Gyehoek.Wasm
|
Gyehoek.Wasm
|
||||||
|
|
||||||
build-depends:
|
build-depends:
|
||||||
@@ -112,16 +115,13 @@ library
|
|||||||
, recursion-schemes
|
, recursion-schemes
|
||||||
, scientific
|
, scientific
|
||||||
, string-interpolate
|
, string-interpolate
|
||||||
, tardis
|
|
||||||
, template-haskell
|
, template-haskell
|
||||||
, text
|
, text
|
||||||
, text-short
|
, text-short
|
||||||
, typed-process
|
, typed-process
|
||||||
, unordered-containers
|
, unordered-containers
|
||||||
, vector
|
, vector
|
||||||
, witherable
|
, tardis
|
||||||
, semialign
|
|
||||||
, these
|
|
||||||
|
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
default-language: GHC2024
|
default-language: GHC2024
|
||||||
@@ -136,11 +136,14 @@ test-suite test
|
|||||||
-- cabal-fmt: expand test -Main
|
-- cabal-fmt: expand test -Main
|
||||||
other-modules:
|
other-modules:
|
||||||
Gyehoek.Test.CPS.Eval
|
Gyehoek.Test.CPS.Eval
|
||||||
|
Gyehoek.Test.CPS.Stackify
|
||||||
Gyehoek.Test.CPS.Syntax
|
Gyehoek.Test.CPS.Syntax
|
||||||
|
Gyehoek.Test.Golden
|
||||||
Gyehoek.Test.Scheme.Syntax
|
Gyehoek.Test.Scheme.Syntax
|
||||||
Gyehoek.Test.Sexp.Print
|
Gyehoek.Test.Sexp.Print
|
||||||
Gyehoek.Test.Sexp.QQ
|
Gyehoek.Test.Sexp.QQ
|
||||||
Gyehoek.Test.Sexp.Read
|
Gyehoek.Test.Sexp.Read
|
||||||
|
Gyehoek.Test.Stack.VM
|
||||||
Gyehoek.TestUtil
|
Gyehoek.TestUtil
|
||||||
Root
|
Root
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
(define-syntax if-not
|
|
||||||
(syntax-rules ()
|
|
||||||
((_ c t f) (if (not c) t f))
|
|
||||||
((_ c t) (if (not c) t))))
|
|
||||||
|
|
||||||
(write (macroexpand-1 '(if-not #t 123 456)))
|
|
||||||
|
|
||||||
(define (main)
|
|
||||||
(let loop ((datum (read)))
|
|
||||||
(unless (eof-object? datum)
|
|
||||||
())))
|
|
||||||
@@ -18,7 +18,7 @@ genCodeName f = gensym' @Name $ f ^. _Wrapped' . to (<> "-code")
|
|||||||
|
|
||||||
bindEnv :: List Name -> Exp -> Exp
|
bindEnv :: List Name -> Exp -> Exp
|
||||||
bindEnv frees m = [cps|
|
bindEnv frees m = [cps|
|
||||||
(builtin (get-env) (κ #{frees} #{m}))
|
(prim (get-env) (κ #{frees} #{m}))
|
||||||
|]
|
|]
|
||||||
|
|
||||||
close1 :: forall es. GenSym :> es => Exp -> Eff es Exp
|
close1 :: forall es. GenSym :> es => Exp -> Eff es Exp
|
||||||
@@ -31,6 +31,7 @@ close1 = \case
|
|||||||
(each . _2)
|
(each . _2)
|
||||||
(freeWithBound' boundNames')
|
(freeWithBound' boundNames')
|
||||||
& nub
|
& nub
|
||||||
|
pTraceShowM frees
|
||||||
env_cont_l <- gensym' @Name "env-cont"
|
env_cont_l <- gensym' @Name "env-cont"
|
||||||
e_l <- gensym' @Name "letrec-body-cont"
|
e_l <- gensym' @Name "letrec-body-cont"
|
||||||
bs' <- for bs \(f,ab) -> do
|
bs' <- for bs \(f,ab) -> do
|
||||||
@@ -41,7 +42,7 @@ close1 = \case
|
|||||||
let codes = bs' ^.. each . _1 . to MkLabel
|
let codes = bs' ^.. each . _1 . to MkLabel
|
||||||
pure [cps|
|
pure [cps|
|
||||||
(letrec #{bs'}
|
(letrec #{bs'}
|
||||||
(builtin (make-shared-closure #{codes} #{frees})
|
(prim (make-shared-closure #{codes} #{frees})
|
||||||
(κ #{boundNames}
|
(κ #{boundNames}
|
||||||
#{e})))
|
#{e})))
|
||||||
|]
|
|]
|
||||||
@@ -53,3 +54,39 @@ close = transformM close1
|
|||||||
|
|
||||||
closeProgram :: GenSym :> es => Program -> Eff es Program
|
closeProgram :: GenSym :> es => Program -> Eff es Program
|
||||||
closeProgram = traverseOf (#body . #body) close
|
closeProgram = traverseOf (#body . #body) close
|
||||||
|
|
||||||
|
prog :: Program
|
||||||
|
prog = [cps|
|
||||||
|
(λ (start-ktail0)
|
||||||
|
(letrec ((fac
|
||||||
|
(λ (n lambda-tail1)
|
||||||
|
(letrec ((prim-k3
|
||||||
|
(κ (r2)
|
||||||
|
(letrec ((truthy-cont4
|
||||||
|
(κ ()
|
||||||
|
(continue lambda-tail1 1)))
|
||||||
|
(falsey-cont5
|
||||||
|
(κ ()
|
||||||
|
(letrec ((prim-k7
|
||||||
|
(κ (r6)
|
||||||
|
(letrec
|
||||||
|
((r8
|
||||||
|
(κ (x9)
|
||||||
|
(letrec
|
||||||
|
((prim-k11
|
||||||
|
(κ (r10)
|
||||||
|
(continue
|
||||||
|
lambda-tail1
|
||||||
|
r10))))
|
||||||
|
(prim
|
||||||
|
(* n x9)
|
||||||
|
prim-k11)))))
|
||||||
|
(fac r6 r8)))))
|
||||||
|
(prim (- n 1) prim-k7)))))
|
||||||
|
(if r2
|
||||||
|
truthy-cont4
|
||||||
|
falsey-cont5)))))
|
||||||
|
(prim (zero? n) prim-k3)))))
|
||||||
|
(letrec ((r12 (κ (x13) (continue start-ktail0 x13))))
|
||||||
|
(fac 20 r12))))
|
||||||
|
|]
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
{-# LANGUAGE ApplicativeDo #-}
|
||||||
|
module Gyehoek.CPS.Contify
|
||||||
|
( contifyProgram
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Monad.Tardis
|
||||||
|
import Gyehoek.CPS.Syntax
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import qualified Data.HashSet as HS
|
||||||
|
import Control.Lens.Unsound (adjoin)
|
||||||
|
import Debug.Pretty.Simple
|
||||||
|
import qualified Data.HashMap.Strict as H
|
||||||
|
import Control.Monad.Writer.Lazy
|
||||||
|
import Control.Monad.Trans.Tardis (liftTardisT)
|
||||||
|
|
||||||
|
|
||||||
|
-- | ain't no way...
|
||||||
|
-- type T = WriterT (HashSet Name) (Tardis (HashSet Name) (HashSet Name))
|
||||||
|
type T = TardisT (HashSet Name) (HashSet Name) (Writer (HashSet Name))
|
||||||
|
|
||||||
|
evalT :: T a -> a
|
||||||
|
-- evalT = (`evalTardis` (mempty,mempty)) . fmap fst . runWriterT
|
||||||
|
evalT = fst . runWriter . (`evalTardisT` (mempty,mempty))
|
||||||
|
|
||||||
|
runT :: T a -> (a, HashSet Name)
|
||||||
|
-- runT = (`evalTardis` (mempty,mempty)) . runWriterT
|
||||||
|
runT = runWriter . (`evalTardisT` (mempty,mempty))
|
||||||
|
|
||||||
|
-- | inline function if it hasn't been used in the past, and won't
|
||||||
|
-- be used in the future.
|
||||||
|
tryInline :: Name -> Kappa -> T Kexp
|
||||||
|
tryInline kname kap = do
|
||||||
|
modifyBackwards (HS.insert kname)
|
||||||
|
p <- getsPast (HS.member kname)
|
||||||
|
modifyForwards (HS.insert kname)
|
||||||
|
q <- getsFuture (HS.member kname)
|
||||||
|
let c = p || q
|
||||||
|
liftTardisT . tell $ if c then HS.singleton kname else mempty
|
||||||
|
pure $ if c
|
||||||
|
then KexpVar kname
|
||||||
|
else KexpKappa kap
|
||||||
|
|
||||||
|
getKap :: HashMap Name Abs -> Name -> Maybe Kappa
|
||||||
|
getKap g kname = g ^? ix kname . #AbsKappa
|
||||||
|
|
||||||
|
contify :: HashMap Name Abs -> Exp -> T Exp
|
||||||
|
contify g = transformM \case
|
||||||
|
ExpApply f xs (KexpVar kname) | Just kap <- getKap g kname
|
||||||
|
-> ExpApply f xs <$> tryInline kname kap
|
||||||
|
ExpPrim p (KexpVar kname) | Just kap <- getKap g kname
|
||||||
|
-> ExpPrim p <$> tryInline kname kap
|
||||||
|
e -> pure e
|
||||||
|
|
||||||
|
contifyProgram :: HoistedProgram -> Eff es HoistedProgram
|
||||||
|
contifyProgram p = do
|
||||||
|
let g = p.bindings
|
||||||
|
let (p',contifiedVars) =
|
||||||
|
runT $
|
||||||
|
traverseOf
|
||||||
|
(adjoin
|
||||||
|
(#bindings . each . body)
|
||||||
|
(#body . body))
|
||||||
|
(contify g)
|
||||||
|
p
|
||||||
|
pTraceShowM contifiedVars
|
||||||
|
-- pure $ p' & #bindings %~ H.filterWithKey \k _ -> HS.member k contifiedVars
|
||||||
|
pure p'
|
||||||
@@ -46,12 +46,14 @@ convert (Scm.ExpLit l) k = k . one . ValImm $ case l of
|
|||||||
LitBool b -> ImmBool b
|
LitBool b -> ImmBool b
|
||||||
_ -> _
|
_ -> _
|
||||||
|
|
||||||
convert (Scm.ExpBuiltin p) k =
|
convert (Scm.ExpPrim p) k =
|
||||||
telescope (convert1 @es) p \p' -> do
|
telescope (convert1 @es) p \p' -> do
|
||||||
r_l <- gensym' "r"
|
r_l <- gensym' "r"
|
||||||
|
k_l <- gensym' @Name "prim-k"
|
||||||
m <- k [ValVar r_l]
|
m <- k [ValVar r_l]
|
||||||
pure [cps|
|
pure [cps|
|
||||||
(builtin #{p'} (κ (#{r_l}) #{m}))
|
(letrec ((#{k_l} (κ (#{r_l}) #{m})))
|
||||||
|
(prim #{p'} #{k_l}))
|
||||||
|]
|
|]
|
||||||
|
|
||||||
convert (Scm.ExpLambda xs e) k = do
|
convert (Scm.ExpLambda xs e) k = do
|
||||||
@@ -73,7 +75,7 @@ convert (Scm.ExpApply f xs) k =
|
|||||||
|
|
||||||
convert (Scm.ExpBegin xs) k = telescope (convert @es) xs (k . NE.last)
|
convert (Scm.ExpBegin xs) k = telescope (convert @es) xs (k . NE.last)
|
||||||
|
|
||||||
convert (Scm.ExpIf c t (Just f)) k =
|
convert (Scm.ExpIf c t f) k =
|
||||||
convert1 c \c' -> do
|
convert1 c \c' -> do
|
||||||
t_l <- gensym' @Name "truthy-cont"
|
t_l <- gensym' @Name "truthy-cont"
|
||||||
f_l <- gensym' @Name "falsey-cont"
|
f_l <- gensym' @Name "falsey-cont"
|
||||||
|
|||||||
+73
-277
@@ -1,303 +1,99 @@
|
|||||||
{-# LANGUAGE ViewPatterns #-}
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
{-# LANGUAGE DeriveAnyClass #-}
|
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
|
||||||
{-# LANGUAGE OverloadedLists #-}
|
|
||||||
module Gyehoek.CPS.Eval
|
module Gyehoek.CPS.Eval
|
||||||
( evalProgram
|
( evalProgram
|
||||||
, module Gyehoek.CPS.Syntax
|
, module Gyehoek.CPS.Syntax
|
||||||
, evalExp
|
, evalExp
|
||||||
, eGrammar
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Gyehoek.CPS.Syntax hiding (Hob(..), Obj(..), cont)
|
import Gyehoek.CPS.Syntax
|
||||||
import Gyehoek.Sexp qualified as S
|
import Control.Lens
|
||||||
import Control.Lens hiding (assign)
|
import Data.Maybe (fromMaybe)
|
||||||
import Data.Maybe (fromMaybe, isJust)
|
|
||||||
import Text.Show.Functions ()
|
import Text.Show.Functions ()
|
||||||
import qualified Data.HashMap.Strict as H
|
import qualified Data.HashMap.Strict as H
|
||||||
import Gyehoek.Prelude hiding (assign)
|
import Gyehoek.Prelude
|
||||||
import Debug.Pretty.Simple
|
import Debug.Pretty.Simple
|
||||||
import Gyehoek.Jalmot
|
|
||||||
import Control.Monad.Cont
|
|
||||||
import Gyehoek.Sexp qualified as S
|
|
||||||
import GHC.Generics (Generically(..))
|
|
||||||
import Gyehoek.Sexp ((:-)(..))
|
|
||||||
import Data.List (nub, mapAccumR, compareLength)
|
|
||||||
import Data.HashSet.Lens (setOf)
|
|
||||||
import Data.IntMap.Strict (IntMap)
|
|
||||||
import Data.IntMap.Strict qualified as IM
|
|
||||||
import Data.Monoid
|
|
||||||
import Control.Monad.State
|
|
||||||
import Data.Traversable (for)
|
|
||||||
import Data.Foldable (traverse_, foldrM)
|
|
||||||
|
|
||||||
|
|
||||||
newtype Loc = MkLoc { getLoc :: Int }
|
data Env = MkEnv
|
||||||
deriving stock (Generic, Data)
|
{ vars :: HashMap Name Obj
|
||||||
deriving newtype (Show, Eq, Ord, Enum)
|
, labels :: HashMap Name (Env, Abs)
|
||||||
|
|
||||||
data Store = MkStore
|
|
||||||
{ nextLoc :: Loc
|
|
||||||
, heap :: IntMap E
|
|
||||||
}
|
}
|
||||||
deriving stock (Show, Generic)
|
|
||||||
|
|
||||||
type instance Index Store = Loc
|
|
||||||
type instance IxValue Store = E
|
|
||||||
|
|
||||||
instance Ixed Store where ix (MkLoc j) = #heap . ix j
|
|
||||||
instance At Store where at (MkLoc j) = #heap . at j
|
|
||||||
|
|
||||||
emptyStore :: Store
|
|
||||||
emptyStore = MkStore
|
|
||||||
{ nextLoc = MkLoc 0
|
|
||||||
, heap = mempty
|
|
||||||
}
|
|
||||||
|
|
||||||
newtype Env = MkEnv { getEnv :: HashMap Name Loc }
|
|
||||||
deriving stock (Show, Generic, Data)
|
|
||||||
deriving newtype (Semigroup, Monoid)
|
|
||||||
|
|
||||||
emptyEnv :: Env
|
|
||||||
emptyEnv = mempty
|
|
||||||
|
|
||||||
type instance Index Env = Name
|
|
||||||
type instance IxValue Env = Loc
|
|
||||||
|
|
||||||
instance Ixed Env where ix j = #getEnv . ix j
|
|
||||||
instance At Env where at j = #getEnv . at j
|
|
||||||
|
|
||||||
update :: Loc -> E -> Store -> Store
|
|
||||||
update (MkLoc loc) v = #heap %~ IM.alter f loc
|
|
||||||
where
|
|
||||||
f (Just _) = Just v
|
|
||||||
f Nothing = error "segfault lol"
|
|
||||||
|
|
||||||
updates :: Foldable f => f (Loc, E) -> Store -> Store
|
|
||||||
updates = alaf Endo foldMap (uncurry update)
|
|
||||||
|
|
||||||
fetch :: Loc -> M r E
|
|
||||||
fetch (MkLoc loc) = gets (^?! #heap . ix loc)
|
|
||||||
|
|
||||||
new :: M r Loc
|
|
||||||
new = state \st -> (st.nextLoc, st & #nextLoc %~ succ)
|
|
||||||
|
|
||||||
new' :: E -> M r Loc
|
|
||||||
new' e = state \st ->
|
|
||||||
( st.nextLoc
|
|
||||||
, st & #nextLoc %~ succ & at st.nextLoc ?~ e
|
|
||||||
)
|
|
||||||
|
|
||||||
defines :: Traversable t => t (Name, E) -> M Answer Env
|
|
||||||
defines = alaf Ap foldMap \(name,e) -> do
|
|
||||||
l <- new' e
|
|
||||||
pure $ bind name l
|
|
||||||
|
|
||||||
var :: HasCallStack => Env -> Name -> M Answer Loc
|
|
||||||
var g x = case g ^. at x of
|
|
||||||
Just l -> pure l
|
|
||||||
Nothing -> wrong [i|unbound variable #{x}|]
|
|
||||||
|
|
||||||
type CmdCont = Store -> Answer
|
|
||||||
type ExpCont = List E -> CmdCont
|
|
||||||
|
|
||||||
type M r = ContT r (State Store)
|
|
||||||
|
|
||||||
data Answer
|
|
||||||
= AnswerValues (List E)
|
|
||||||
| AnswerError AJalmot
|
|
||||||
deriving (Show, Generic)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
data Mutability
|
eval :: Env -> Exp -> List Obj
|
||||||
= Mut
|
|
||||||
| NoMut
|
|
||||||
deriving (Show, Generic, Data, Eq)
|
|
||||||
|
|
||||||
wrong :: Text -> M Answer a
|
eval g (Halt xs) = evalVal g <$> xs
|
||||||
wrong s = ContT \_ -> pure . AnswerError . EvalError $ s
|
|
||||||
|
|
||||||
orWrong
|
eval g (ExpContinue k xs) =
|
||||||
:: Getting (First a) s a
|
case g ^. #labels . at k' of
|
||||||
-> Text -> s -> M Answer a
|
Just (h, AbsKappa' bs m) -> eval h' m
|
||||||
orWrong p msg s = case getFirst . getConst $ p (Const . First . Just) s of
|
where
|
||||||
Nothing -> wrong msg
|
h' = h & #vars <>~ envOfBinds bs (evalVal g <$> xs)
|
||||||
Just x -> pure x
|
_ -> error [i|not a kappa: #{k}|]
|
||||||
|
|
||||||
bind :: Name -> Loc -> Env
|
|
||||||
bind k = MkEnv . H.singleton k
|
|
||||||
|
|
||||||
extends :: Foldable f => f (Name, Loc) -> Env -> Env
|
|
||||||
extends xs g = g <> foldMap (uncurry bind) xs
|
|
||||||
|
|
||||||
assign :: Loc -> E -> M Answer ()
|
|
||||||
assign l e = do
|
|
||||||
use (at l) >>= \case
|
|
||||||
Just _ -> at l ?= e
|
|
||||||
Nothing -> wrong [i|#{e}에서 #{l}이라는 주소는 없다|]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- | The denotation of an expressed value.
|
|
||||||
data E
|
|
||||||
= ESymbol Text
|
|
||||||
| ECharacter Char
|
|
||||||
| EInt Int
|
|
||||||
| EBool Bool
|
|
||||||
| EUndefined
|
|
||||||
| EUnspecified
|
|
||||||
| ENull
|
|
||||||
| EPair Mutability Loc Loc
|
|
||||||
| EVec Mutability (List Loc)
|
|
||||||
| EString Mutability (List Loc)
|
|
||||||
| EProcedure Procedure
|
|
||||||
deriving stock (Show, Generic)
|
|
||||||
|
|
||||||
type Procedure = List E -> DynPoints -> M Answer (List E)
|
|
||||||
|
|
||||||
eGrammar :: Store -> S.DatumGrammar E
|
|
||||||
eGrammar st = S.partialOsi (const . Left $ mempty) go
|
|
||||||
where
|
where
|
||||||
gofetch x = go $ st ^?! ix x
|
k' = case evalVal g k of
|
||||||
go = \case
|
ObjImm (ImmLabel x) -> x
|
||||||
ESymbol s -> S.Symbol s
|
x -> error [i|expected label, got #{x}|]
|
||||||
ECharacter c -> S.Character c
|
|
||||||
EInt n -> S.Number (fromIntegral n)
|
|
||||||
EBool b -> S.Boolean b
|
|
||||||
EUndefined -> S.Unreadable "#<undefined>"
|
|
||||||
EUnspecified -> S.Unreadable "#<unspecified>"
|
|
||||||
EProcedure _ -> S.Unreadable "#<procedure>"
|
|
||||||
ENull -> S.List []
|
|
||||||
EPair _mut car cdr -> S.DotList [gofetch car] (gofetch cdr)
|
|
||||||
EVec _mut xs -> S.Vector . fmap gofetch $ xs
|
|
||||||
EString _mut xs -> S.String _
|
|
||||||
|
|
||||||
data DynPoints = MkDynPoints
|
eval g (ExpApply f xs ktail) =
|
||||||
deriving (Generic, Data)
|
case g ^?! #labels . at f' of
|
||||||
|
Just (h,AbsLambda' bs kb m) -> eval h' m
|
||||||
|
where h' = h & #vars <>~ envOfBinds bs (evalVal g <$> xs)
|
||||||
|
& #labels . at kb .~ (g ^. #labels . at ktail)
|
||||||
truthy :: E -> Bool
|
Nothing -> error [i|undefined label: #{f}|]
|
||||||
truthy (EBool False) = False
|
|
||||||
truthy _ = True
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
evalVal :: Env -> Val -> M Answer E
|
|
||||||
|
|
||||||
evalVal g (ValVar x) = var g x >>= fetch
|
|
||||||
|
|
||||||
evalVal g (ValImm imm) = case imm of
|
|
||||||
ImmLabel (MkLabel l) -> var g l >>= fetch
|
|
||||||
ImmInt n -> pure $ EInt n
|
|
||||||
ImmBool b -> pure $ EBool b
|
|
||||||
ImmUndefined -> pure EUndefined
|
|
||||||
|
|
||||||
evalKexp :: Env -> Kexp -> M Answer E
|
|
||||||
evalKexp g (KexpVar x) = var g x >>= fetch
|
|
||||||
evalKexp g (KexpKappa kap) = evalAbs g (AbsKappa kap)
|
|
||||||
|
|
||||||
evalAbs :: Env -> Abs -> M Answer E
|
|
||||||
evalAbs g (MkAbs formals ktail e) = pure . EProcedure $ \xs dps ->
|
|
||||||
let
|
|
||||||
formals' = formals ++ foldMap (:[]) ktail
|
|
||||||
lformals = length formals'
|
|
||||||
lxs = length xs
|
|
||||||
in if lformals /= lxs
|
|
||||||
then wrong [i|함수는 #{lformals}개의 인자를 필요로 하는데 #{lxs}개 받았다.|]
|
|
||||||
else do
|
|
||||||
ls <- xs & traverse new'
|
|
||||||
let g' = g & extends (zip formals' ls)
|
|
||||||
eval g' dps e
|
|
||||||
|
|
||||||
eval :: Env -> DynPoints -> Exp -> M Answer (List E)
|
|
||||||
|
|
||||||
eval g dps (ExpJump f xs ktail) = do
|
|
||||||
f' <- evalVal g f
|
|
||||||
xs' <- traverse (evalVal g) xs
|
|
||||||
ktail' <- traverse (evalKexp g) (ktail ^.. _Just)
|
|
||||||
case f' of
|
|
||||||
EProcedure p -> p (xs' ++ ktail') dps
|
|
||||||
_ -> wrong "bad procedure"
|
|
||||||
|
|
||||||
eval g dps (ExpLetRec bs e) = do
|
|
||||||
ls <- for bs . const $ new' EUndefined
|
|
||||||
let g' = g & extends (zip (bs ^.. each . _1) ls)
|
|
||||||
bs' <- forOf (each . _2) bs (evalAbs g')
|
|
||||||
traverse_ (uncurry assign) $ zip ls (bs' ^.. each . _2)
|
|
||||||
eval g' dps e
|
|
||||||
|
|
||||||
eval g dps (ExpBuiltin (BuiltinCallCC withcc) k) = do
|
|
||||||
withcc' <- evalVal g withcc >>= orWrong #_EProcedure
|
|
||||||
[i|call/cc: 함수가 아닌 것을 받았다|]
|
|
||||||
k' <- evalKexp g k
|
|
||||||
kproc <- orWrong #_EProcedure [i|call/cc: 몰라...|] k'
|
|
||||||
let cc = EProcedure \xs dps -> case unsnoc xs of
|
|
||||||
Just (xs',_) -> kproc xs' dps
|
|
||||||
Nothing -> wrong [i|call/cc: 잘못하는데!|]
|
|
||||||
withcc' [cc,k'] dps
|
|
||||||
|
|
||||||
eval g dps (ExpBuiltin p k) = do
|
|
||||||
p' <- evalBuiltin g dps =<< traverse (evalVal g) p
|
|
||||||
evalKexp g k >>= \case
|
|
||||||
EProcedure fp -> fp p' dps
|
|
||||||
_ -> wrong [i|prim(#{p})의 계속을 나쁘다|]
|
|
||||||
|
|
||||||
eval g dps (ExpIf c t f) = do
|
|
||||||
c' <- evalVal g c
|
|
||||||
let b = if truthy c' then t else f
|
|
||||||
var g b >>= fetch >>= \case
|
|
||||||
EProcedure fp -> fp [] dps
|
|
||||||
_ -> wrong [i|if의 계속을 나쁘다|]
|
|
||||||
|
|
||||||
eval g dps e = error [i|unimplemented #{e}|]
|
|
||||||
|
|
||||||
evalBuiltin :: Env -> DynPoints -> Builtin E -> M Answer (List E)
|
|
||||||
evalBuiltin g dps = \case
|
|
||||||
BuiltinAdd x y -> arith2 (+) x y
|
|
||||||
BuiltinMul x y -> arith2 (*) x y
|
|
||||||
BuiltinSub x y -> arith2 (-) x y
|
|
||||||
BuiltinDiv x y -> arith2 div x y
|
|
||||||
BuiltinZeroP x -> pure1 . EBool . isJust $ x ^? #EInt . only 0
|
|
||||||
BuiltinCons x y -> pcons x y >>= pure1
|
|
||||||
BuiltinCar p -> cr p _2
|
|
||||||
BuiltinCdr p -> cr p _3
|
|
||||||
BuiltinPairP p -> pure1 . EBool . maybe False (const True) $
|
|
||||||
p ^? #_EPair
|
|
||||||
BuiltinValues xs -> pure xs
|
|
||||||
BuiltinList xs -> foldrM pcons ENull xs >>= pure1
|
|
||||||
p -> wrong [i|prim(#{p})은 벌써 나지 않다|]
|
|
||||||
where
|
where
|
||||||
pure1 x = pure [x]
|
f' = case evalVal g f of
|
||||||
pcons x y = do
|
ObjImm (ImmLabel x) -> x
|
||||||
(x',y') <- traverseOf both new' (x,y)
|
x -> error [i|expected label, got #{x}|]
|
||||||
pure $ EPair Mut x' y'
|
|
||||||
arith2 f (EInt x) (EInt y) = pure [EInt $ f x y]
|
|
||||||
arith2 f x y = wrong [i|나쁜 인자: #{x}, #{y}|]
|
|
||||||
cr p l = orWrong (#_EPair . l) [i|car/cdr는 pair을 받지 않다|] p
|
|
||||||
>>= fmap (:[]) . fetch
|
|
||||||
|
|
||||||
|
eval g (ExpLetRec [(b, ab)] e) = eval g' e
|
||||||
|
where g' = g & #labels . at b ?~ ab
|
||||||
|
|
||||||
evalExp :: Jalmot :> es => Exp -> Eff es _
|
eval g (ExpPrim p (MkKappa bs e)) = case evalVal g <$> p of
|
||||||
evalExp e = _
|
PrimAdd x y -> arithBinop (+) x y
|
||||||
|
PrimMul x y -> arithBinop (*) x y
|
||||||
evalProgram :: Jalmot :> es => Program -> Eff es (List S.Datum)
|
PrimSub x y -> arithBinop (-) x y
|
||||||
evalProgram (MkProgram lam) = case run (pure . AnswerValues) of
|
PrimDiv x y -> arithBinop div x y
|
||||||
(AnswerError jm, _) -> throwError jm
|
PrimMakeClosure x env -> ret [ObjHob (HobClosure lbl env) ]
|
||||||
(AnswerValues vs, st) -> traverse (S.toDatum $ eGrammar st) vs
|
where
|
||||||
|
lbl = case x of
|
||||||
|
ObjImm (ImmLabel l) -> l
|
||||||
|
_ -> error [i|expected label, got #{x}|]
|
||||||
|
_ -> error [i|unhandled prim: #{p}|]
|
||||||
where
|
where
|
||||||
run f = (`runState` emptyStore) . (`runContT` f) $ do
|
ret rs = eval
|
||||||
g <- setup
|
(g & #vars <>~ envOfBinds bs rs)
|
||||||
eval g MkDynPoints (ExpLetRec
|
e
|
||||||
[("_start",AbsLambda lam)]
|
arithBinop f (ObjImm (ImmInt x)) (ObjImm (ImmInt y)) =
|
||||||
(ExpApply (ValVar "_start") [] (KexpVar "halt")))
|
ret [ObjImm . ImmInt $ f x y]
|
||||||
|
arithBinop _ x y = error [i|bad arith: #{x}, #{y}|]
|
||||||
|
|
||||||
|
eval _ e = error [i|unimplemented case: #{e}|]
|
||||||
|
|
||||||
setup :: M Answer Env
|
envOfBinds bs xs = foldMap (uncurry H.singleton) (zip bs xs)
|
||||||
setup = defines @List
|
|
||||||
[ ("halt", EProcedure prim_halt)
|
|
||||||
]
|
|
||||||
|
|
||||||
prim_halt :: Procedure
|
evalVal :: Env -> Val -> Obj
|
||||||
prim_halt xs _dps = ContT \_ -> pure $ AnswerValues xs
|
evalVal g = \case
|
||||||
|
ValVar x -> fromMaybe (error [i|unbound: #{x}|]) $ g ^?! #vars . at x
|
||||||
|
ValImm x -> ObjImm x
|
||||||
|
|
||||||
|
emptyEnv :: Env
|
||||||
|
emptyEnv = MkEnv
|
||||||
|
{ vars = mempty
|
||||||
|
-- a kinda silly hack to make sure `halt` is handled correctly when
|
||||||
|
-- it appears as the tail continuation of an application. the
|
||||||
|
-- special case of `eval` responsible for `halt` only covers terms
|
||||||
|
-- of the form `(continue $halt xs …)`; other terms such as
|
||||||
|
-- `($some-fn xs $halt)` just see an undefined label `$halt`.
|
||||||
|
, labels = H.singleton "halt" $
|
||||||
|
AbsKappa' ["h0"] $ Halt [ValVar "h0"]
|
||||||
|
}
|
||||||
|
|
||||||
|
evalExp :: Exp -> List Obj
|
||||||
|
evalExp = eval emptyEnv
|
||||||
|
|
||||||
|
evalProgram :: Program -> List Obj
|
||||||
|
evalProgram (MkProgram lam) = eval emptyEnv [cps|
|
||||||
|
(letrec ((start #{lam}))
|
||||||
|
(start halt))
|
||||||
|
|]
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ import Effectful.Writer.Static.Local
|
|||||||
import Data.Foldable
|
import Data.Foldable
|
||||||
|
|
||||||
|
|
||||||
type Hoist = Writer (HashMap Label Abs)
|
type Hoist = Writer (HashMap Name Abs)
|
||||||
|
|
||||||
hoist :: Hoist :> es => Exp -> Eff es Exp
|
hoist :: Hoist :> es => Exp -> Eff es Exp
|
||||||
hoist = transformM \case
|
hoist = transformM \case
|
||||||
ExpLetRec bs m -> do
|
ExpLetRec bs m -> do
|
||||||
traverse_ (\(k,v) -> tell $ H.singleton (MkLabel k) v) bs
|
traverse_ (\(k,v) -> tell $ H.singleton k v) bs
|
||||||
pure m
|
pure m
|
||||||
e -> pure e
|
e -> pure e
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,280 @@
|
|||||||
|
{-# LANGUAGE OverloadedLists #-}
|
||||||
|
module Gyehoek.CPS.Stackify
|
||||||
|
( stackifyProgram
|
||||||
|
, module Gyehoek.CPS.Syntax
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Gyehoek.CPS.Syntax
|
||||||
|
import Gyehoek.Stack.Syntax qualified as Stk
|
||||||
|
import Data.Sequence (Seq)
|
||||||
|
import Data.Sequence qualified as Seq
|
||||||
|
import Gyehoek.GenSym
|
||||||
|
import Effectful.Writer.Static.Shared
|
||||||
|
import Data.Foldable
|
||||||
|
import qualified Data.HashMap.Strict as H
|
||||||
|
import Data.List (elemIndex, nub, intersect)
|
||||||
|
import Data.Text qualified as T
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import Debug.Pretty.Simple
|
||||||
|
import qualified Gyehoek.Sexp as S
|
||||||
|
|
||||||
|
|
||||||
|
type Stackify = Writer Stk.Program
|
||||||
|
|
||||||
|
runStackify :: Eff (Stackify : es) a -> Eff es (a, Stk.Program)
|
||||||
|
runStackify = runWriter
|
||||||
|
|
||||||
|
live :: Free a => Env -> a -> List Name
|
||||||
|
-- TODO: free' should return an OSet lol
|
||||||
|
live g e = nub (free' e) & filter \x ->
|
||||||
|
x `elem` g.bound
|
||||||
|
-- && not (x `elem` g.contStack)
|
||||||
|
|
||||||
|
-- | The expression @load g e r n@ emits a 'Stk.Load' instruction if
|
||||||
|
-- stack variable @n@ is live-out in expression @e@. Otherwise, a
|
||||||
|
-- 'Stk.Pop' instruction is emitted.
|
||||||
|
load :: Free a => Env -> a -> Reg -> Int -> Stk.Instr
|
||||||
|
load g e r 0
|
||||||
|
| Just x <- g ^? #bound . _head
|
||||||
|
, x `elem` free e
|
||||||
|
= Stk.Pop r
|
||||||
|
load g e r n = Stk.Load r n
|
||||||
|
|
||||||
|
data BlockBuilder
|
||||||
|
= Code (List Stk.Instr) BlockBuilder
|
||||||
|
| Tail Stk.Tail
|
||||||
|
deriving (Show, Generic)
|
||||||
|
|
||||||
|
buildBlock :: BlockBuilder -> Stk.Block
|
||||||
|
buildBlock = go [] where
|
||||||
|
go acc (Code xs bb) = go (acc ++ xs) bb
|
||||||
|
go acc (Tail t) = Stk.MkBlock acc t
|
||||||
|
|
||||||
|
emitRoutine :: Stackify :> es => Stk.Routine -> Eff es ()
|
||||||
|
emitRoutine rt = tell [rt]
|
||||||
|
|
||||||
|
stackify
|
||||||
|
:: forall es. (GenSym :> es, Stackify :> es)
|
||||||
|
=> Env -> Exp -> Eff es BlockBuilder
|
||||||
|
|
||||||
|
stackify g (ExpLetRec bs e) = do
|
||||||
|
for_ bs \(f,a) ->
|
||||||
|
emitRoutine =<< case a of
|
||||||
|
AbsKappa kap -> stackifyKappa g (MkLabel f) kap
|
||||||
|
AbsLambda lam -> stackifyLambda g (MkLabel f) lam
|
||||||
|
stackify g e
|
||||||
|
|
||||||
|
stackify g (ExpIf c t f) = do
|
||||||
|
let c' = stackifyVal g c
|
||||||
|
let jump l =
|
||||||
|
Stk.MkBlock
|
||||||
|
[Stk.Push . Stk.ValLabel . MkLabel $ l]
|
||||||
|
(Stk.TailCall 0)
|
||||||
|
pure . Tail $ Stk.If c' (jump t) (jump f)
|
||||||
|
|
||||||
|
stackify g (ExpApply f xs ktail) = do
|
||||||
|
pure $
|
||||||
|
Code [ Stk.Push $ stackifyVal g (ValVar $ ktail ^?! #KexpVar)
|
||||||
|
, Stk.Push $ stackifyVal g f
|
||||||
|
] $
|
||||||
|
Code (pushArgs g xs) $
|
||||||
|
Tail (Stk.Call (length xs))
|
||||||
|
|
||||||
|
stackify g e@(ExpContinue k xs)
|
||||||
|
| isn't (#_ValVar . only g.tail) k = pure $
|
||||||
|
Code [ Stk.Push (stackifyVal g k) ] $
|
||||||
|
Code (pushArgs g xs) $
|
||||||
|
Tail $ Stk.TailCall (length xs)
|
||||||
|
| otherwise = pure $
|
||||||
|
Code (pushArgs g xs) $
|
||||||
|
Tail (Stk.Return (length xs))
|
||||||
|
|
||||||
|
stackify g (ExpPrim (PrimCallCC withcc) cc) = do
|
||||||
|
let cc' = cc ^?! #KexpVar . to MkLabel
|
||||||
|
cc_l <- gensym' @Label "cc"
|
||||||
|
pure $
|
||||||
|
Code [ Stk.Push $ stackifyVal g withcc
|
||||||
|
, Stk.Push $ stackifyVal g (ValLabel cc')
|
||||||
|
] $
|
||||||
|
Tail Stk.CallCC
|
||||||
|
|
||||||
|
stackify g (ExpPrim p cc) = pure $
|
||||||
|
Code [ Stk.Push (Stk.ValLabel . MkLabel $ cc ^?! #KexpVar)
|
||||||
|
, Stk.Prim (stackifyVal g <$> p)
|
||||||
|
] $
|
||||||
|
Tail $ Stk.TailCall 1
|
||||||
|
|
||||||
|
stackify _ e = error [i|unimplemented exp: #{e}|]
|
||||||
|
|
||||||
|
loadArgs :: List Name -> List Stk.Instr
|
||||||
|
loadArgs = imapOf itraversed \n x -> Stk.Load (MkReg x) n
|
||||||
|
|
||||||
|
pushArgs :: Env -> List Val -> List Stk.Instr
|
||||||
|
pushArgs g args = [ Stk.Push $ stackifyVal g x | x <- reverse args ]
|
||||||
|
|
||||||
|
-- affine
|
||||||
|
_ValName :: Traversal' Val Name
|
||||||
|
_ValName = failing #_ValVar (#_ValImm . #_ImmLabel . #_MkLabel)
|
||||||
|
|
||||||
|
stackifyKappa
|
||||||
|
:: (Stackify :> es, GenSym :> es)
|
||||||
|
=> Env -> Label -> Kappa
|
||||||
|
-> Eff es Stk.Routine
|
||||||
|
stackifyKappa g kname (MkKappa xs m) = do
|
||||||
|
let g' = g & #bound <>:~ xs
|
||||||
|
m' <- stackify g' m
|
||||||
|
pure $
|
||||||
|
Stk.MkRoutine kname . buildBlock $
|
||||||
|
Code (loadArgs xs) $
|
||||||
|
Code [ Stk.Load (MkReg r) j
|
||||||
|
| v <- g ^.. #liveness . ix kname . each
|
||||||
|
, (j,r) <- itoListOf (#bound . itraversed) g
|
||||||
|
, r == v
|
||||||
|
] $
|
||||||
|
m'
|
||||||
|
|
||||||
|
stackifyLambda
|
||||||
|
:: (Stackify :> es, GenSym :> es)
|
||||||
|
=> Env -> Label -> Lambda
|
||||||
|
-> Eff es Stk.Routine
|
||||||
|
stackifyLambda g name (MkLambda xs k m) = do
|
||||||
|
m' <- stackify (g & #bound .~ xs & #tail .~ k) m
|
||||||
|
pure $
|
||||||
|
Stk.MkRoutine name . buildBlock $
|
||||||
|
Code (loadArgs xs) $
|
||||||
|
-- Code [Stk.Load (MkReg k) (length xs + 1)] $
|
||||||
|
m'
|
||||||
|
|
||||||
|
stackifyVal :: Env -> Val -> Stk.Val
|
||||||
|
stackifyVal g = \case
|
||||||
|
ValImm imm -> Stk.ValImm imm
|
||||||
|
ValVar v -> case regOf g v of
|
||||||
|
Just r -> Stk.ValReg r
|
||||||
|
Nothing -> Stk.ValLabel (MkLabel v)
|
||||||
|
v -> error [i|unimplemented val: #{v}|]
|
||||||
|
|
||||||
|
regOf :: Env -> Name -> Maybe Reg
|
||||||
|
regOf g x
|
||||||
|
| x `elem` g.bound || x == g.tail = Just . MkReg $ x
|
||||||
|
| otherwise = Nothing
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
data Env = MkEnv
|
||||||
|
-- | `bound` tracks the stack lifetime of bound variables.
|
||||||
|
{ bound :: List Name
|
||||||
|
-- | for each locally-bound continuation @k@, @liveness@ has an
|
||||||
|
-- entry @(k,ls)@ where @ls@ is the sequence of registers @k@
|
||||||
|
-- expects to find saved on the stack.
|
||||||
|
, liveness :: HashMap Label (List Name)
|
||||||
|
, tail :: Name
|
||||||
|
}
|
||||||
|
deriving (Show, Generic)
|
||||||
|
|
||||||
|
emptyEnv :: Env
|
||||||
|
emptyEnv = MkEnv
|
||||||
|
{ bound = mempty
|
||||||
|
, liveness = mempty
|
||||||
|
, tail = "halt"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
stackifyProgram :: GenSym :> es => HoistedProgram -> Eff es Stk.Program
|
||||||
|
stackifyProgram p = do
|
||||||
|
let liveness = p & foldMapOf
|
||||||
|
(#bindings . itraversed . withIndex . aside #AbsKappa)
|
||||||
|
\(kname,kap) -> H.singleton
|
||||||
|
(MkLabel kname)
|
||||||
|
(nub $ freeWithBound' (H.keysSet p.bindings) kap)
|
||||||
|
let g = MkEnv
|
||||||
|
{ bound = mempty
|
||||||
|
, liveness
|
||||||
|
, tail = p.body.ktail }
|
||||||
|
let e = p.body & #body %~ ExpLetRec (H.toList p.bindings)
|
||||||
|
(_,p') <- runStackify $ emitRoutine =<< stackifyLambda g "start" e
|
||||||
|
pure p'
|
||||||
|
|
||||||
|
blah :: HoistedProgram
|
||||||
|
blah = [cps|
|
||||||
|
(letrec ((prim-k3 (κ (r2) (if r2 truthy-cont4 falsey-cont5)))
|
||||||
|
(prim-k7 (κ (r6) (fac r6 r8)))
|
||||||
|
(make-closure-cont15 (κ (fac) (fac 20 r12)))
|
||||||
|
(falsey-cont5 (κ () (prim (- n 1) prim-k7)))
|
||||||
|
(r12 (κ (x13) (continue start-ktail0 x13)))
|
||||||
|
(truthy-cont4 (κ () (continue lambda-tail1 1)))
|
||||||
|
(prim-k11 (κ (r10) (continue lambda-tail1 r10)))
|
||||||
|
(r8 (κ (x9) (prim (* n x9) prim-k11)))
|
||||||
|
(fac-code14 (λ (n lambda-tail1) (prim (zero? n) prim-k3))))
|
||||||
|
(λ (start-ktail0)
|
||||||
|
(prim (make-closure $fac-code14) make-closure-cont15)))
|
||||||
|
|]
|
||||||
|
|
||||||
|
p :: HoistedProgram
|
||||||
|
p = [cps|
|
||||||
|
(letrec ((r12-code32 (κ (r12 start-ktail0 x13) (continue start-ktail0 x13)))
|
||||||
|
(prim-k7-code22
|
||||||
|
(κ (prim-k7 fac r6 r8 n x9 prim-k11 lambda-tail1 r10)
|
||||||
|
(prim
|
||||||
|
(make-shared-closure (r8) (n x9 prim-k11 lambda-tail1 r10))
|
||||||
|
letrec-body-cont18)))
|
||||||
|
(letrec-body-cont24
|
||||||
|
(κ (truthy-cont4 falsey-cont5)
|
||||||
|
(if r2
|
||||||
|
truthy-cont4
|
||||||
|
falsey-cont5)))
|
||||||
|
(letrec-body-cont18 (κ (r8) (fac r6 r8)))
|
||||||
|
(prim-k11-code16
|
||||||
|
(κ (prim-k11 lambda-tail1 r10)
|
||||||
|
(continue lambda-tail1 r10)))
|
||||||
|
(falsey-cont5-code26
|
||||||
|
(κ (truthy-cont4 falsey-cont5 lambda-tail1 n prim-k7
|
||||||
|
fac r6 r8 x9 prim-k11 r10)
|
||||||
|
(prim
|
||||||
|
(make-shared-closure
|
||||||
|
(prim-k7)
|
||||||
|
(fac r6 r8 n x9 prim-k11 lambda-tail1 r10))
|
||||||
|
letrec-body-cont21)))
|
||||||
|
(letrec-body-cont31 (κ (r12) (fac 20 r12)))
|
||||||
|
(r8-code19
|
||||||
|
(κ (r8 n x9 prim-k11 lambda-tail1 r10)
|
||||||
|
(prim
|
||||||
|
(make-shared-closure (prim-k11) (lambda-tail1 r10))
|
||||||
|
letrec-body-cont15)))
|
||||||
|
(letrec-body-cont28 (κ (prim-k3) (prim (zero? n) prim-k3)))
|
||||||
|
(truthy-cont4-code25
|
||||||
|
(κ (truthy-cont4 falsey-cont5 lambda-tail1 n prim-k7 fac
|
||||||
|
r6 r8 x9 prim-k11 r10)
|
||||||
|
(continue lambda-tail1 1)))
|
||||||
|
(fac-code35
|
||||||
|
(κ (fac n prim-k3 r2 truthy-cont4 falsey-cont5 lambda-tail1
|
||||||
|
prim-k7 r6 r8 x9 prim-k11 r10)
|
||||||
|
(prim
|
||||||
|
(make-shared-closure
|
||||||
|
(prim-k3)
|
||||||
|
(r2 truthy-cont4 falsey-cont5 lambda-tail1 n prim-k7
|
||||||
|
fac r6 r8 x9 prim-k11 r10))
|
||||||
|
letrec-body-cont28)))
|
||||||
|
(prim-k3-code29
|
||||||
|
(κ (prim-k3 r2 truthy-cont4 falsey-cont5 lambda-tail1 n prim-k7
|
||||||
|
fac r6 r8 x9 prim-k11 r10)
|
||||||
|
(prim
|
||||||
|
(make-shared-closure
|
||||||
|
(truthy-cont4 falsey-cont5)
|
||||||
|
(lambda-tail1 n prim-k7 fac r6 r8 x9 prim-k11 r10))
|
||||||
|
letrec-body-cont24)))
|
||||||
|
(letrec-body-cont21 (κ (prim-k7) (prim (- n 1) prim-k7)))
|
||||||
|
(letrec-body-cont15 (κ (prim-k11) (prim (* n x9) prim-k11)))
|
||||||
|
(letrec-body-cont34
|
||||||
|
(κ (fac)
|
||||||
|
(prim
|
||||||
|
(make-shared-closure (r12) (start-ktail0 x13))
|
||||||
|
letrec-body-cont31))))
|
||||||
|
(λ (start-ktail0)
|
||||||
|
(prim
|
||||||
|
(make-shared-closure
|
||||||
|
(fac)
|
||||||
|
(n prim-k3 r2 truthy-cont4 falsey-cont5 lambda-tail1
|
||||||
|
prim-k7 r6 r8 x9 prim-k11 r10))
|
||||||
|
letrec-body-cont34)))
|
||||||
|
|]
|
||||||
+14
-55
@@ -13,7 +13,7 @@ module Gyehoek.CPS.Syntax
|
|||||||
, Kexp(..)
|
, Kexp(..)
|
||||||
, ExpF(..)
|
, ExpF(..)
|
||||||
, Name(..)
|
, Name(..)
|
||||||
, Builtin(..)
|
, Prim(..)
|
||||||
, Program(..)
|
, Program(..)
|
||||||
, HoistedProgram(..)
|
, HoistedProgram(..)
|
||||||
, Lit(..)
|
, Lit(..)
|
||||||
@@ -25,7 +25,7 @@ module Gyehoek.CPS.Syntax
|
|||||||
, pattern Halt
|
, pattern Halt
|
||||||
, pattern Halt1
|
, pattern Halt1
|
||||||
, _MkKappa
|
, _MkKappa
|
||||||
, _ExpBuiltin
|
, _ExpPrim
|
||||||
, _ExpLetRec
|
, _ExpLetRec
|
||||||
, _ExpApply
|
, _ExpApply
|
||||||
, _AbsLambda'
|
, _AbsLambda'
|
||||||
@@ -42,15 +42,10 @@ module Gyehoek.CPS.Syntax
|
|||||||
, pattern ValLabel
|
, pattern ValLabel
|
||||||
, pattern ObjLabel
|
, pattern ObjLabel
|
||||||
, absBody
|
, absBody
|
||||||
, pattern MkAbs
|
|
||||||
, _MkAbs
|
|
||||||
, unhoist
|
|
||||||
, pattern ExpJump
|
|
||||||
, _ExpJump
|
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
import Gyehoek.Scheme.Syntax (Name (..), Builtin(..), builtinDatumIso, Lit(..))
|
import Gyehoek.Scheme.Syntax (Name (..), Prim(..), primDatumIso, Lit(..))
|
||||||
import Gyehoek.Sexp qualified as S
|
import Gyehoek.Sexp qualified as S
|
||||||
import Control.Category
|
import Control.Category
|
||||||
import Prelude hiding ((.), id)
|
import Prelude hiding ((.), id)
|
||||||
@@ -67,7 +62,6 @@ import Data.String (IsString)
|
|||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import qualified Data.HashMap.Strict as H
|
import qualified Data.HashMap.Strict as H
|
||||||
import GHC.Records (HasField (..))
|
import GHC.Records (HasField (..))
|
||||||
import Data.Bifunctor
|
|
||||||
|
|
||||||
-- Data types
|
-- Data types
|
||||||
|
|
||||||
@@ -131,39 +125,8 @@ pattern AbsKappa' xs e = AbsKappa (MkKappa xs e)
|
|||||||
pattern AbsLambda' :: List Name -> Name -> Exp -> Abs
|
pattern AbsLambda' :: List Name -> Name -> Exp -> Abs
|
||||||
pattern AbsLambda' xs e ktail = AbsLambda (MkLambda xs e ktail)
|
pattern AbsLambda' xs e ktail = AbsLambda (MkLambda xs e ktail)
|
||||||
|
|
||||||
{-# COMPLETE AbsKappa', AbsLambda' #-}
|
|
||||||
|
|
||||||
_MkAbs :: Iso' Abs (List Name, Maybe Name, Exp)
|
|
||||||
_MkAbs = iso
|
|
||||||
(\case
|
|
||||||
AbsKappa' xs e -> (xs,Nothing,e)
|
|
||||||
AbsLambda' xs ktail e -> (xs,Just ktail,e))
|
|
||||||
(\(xs,ktail,e) -> case ktail of
|
|
||||||
Just k -> AbsLambda' xs k e
|
|
||||||
Nothing -> AbsKappa' xs e)
|
|
||||||
|
|
||||||
pattern MkAbs :: List Name -> Maybe Name -> Exp -> Abs
|
|
||||||
pattern MkAbs xs ktail body <- (view _MkAbs -> (xs,ktail,body))
|
|
||||||
where MkAbs xs ktail body = review _MkAbs (xs,ktail,body)
|
|
||||||
|
|
||||||
{-# COMPLETE MkAbs #-}
|
|
||||||
|
|
||||||
_ExpJump :: Prism' Exp (Val, List Val, Maybe Kexp)
|
|
||||||
_ExpJump = prism'
|
|
||||||
(\(f,xs,ktail) -> case ktail of
|
|
||||||
Just k -> ExpApply f xs k
|
|
||||||
Nothing -> ExpContinue f xs)
|
|
||||||
\case
|
|
||||||
ExpApply f xs ktail -> Just (f,xs,Just ktail)
|
|
||||||
ExpContinue f xs -> Just (f,xs,Nothing)
|
|
||||||
_ -> Nothing
|
|
||||||
|
|
||||||
pattern ExpJump :: Val -> List Val -> Maybe Kexp -> Exp
|
|
||||||
pattern ExpJump f xs ktail <- (preview _ExpJump -> Just (f,xs,ktail))
|
|
||||||
where ExpJump f xs ktail = review _ExpJump (f,xs,ktail)
|
|
||||||
|
|
||||||
data Exp
|
data Exp
|
||||||
= ExpBuiltin (Builtin Val) Kexp
|
= ExpPrim (Prim Val) Kexp
|
||||||
| ExpLetRec { binders :: List (Name, Abs), body :: Exp }
|
| ExpLetRec { binders :: List (Name, Abs), body :: Exp }
|
||||||
| ExpContinue Val (List Val)
|
| ExpContinue Val (List Val)
|
||||||
| ExpIf Val Name Name
|
| ExpIf Val Name Name
|
||||||
@@ -176,6 +139,7 @@ data Exp
|
|||||||
|
|
||||||
data Kexp
|
data Kexp
|
||||||
= KexpVar Name
|
= KexpVar Name
|
||||||
|
-- | Only to be used after contification.
|
||||||
| KexpKappa Kappa
|
| KexpKappa Kappa
|
||||||
deriving (Show, Generic, Data, Eq)
|
deriving (Show, Generic, Data, Eq)
|
||||||
|
|
||||||
@@ -194,12 +158,12 @@ data Program = MkProgram
|
|||||||
deriving (Show, Generic, Data)
|
deriving (Show, Generic, Data)
|
||||||
|
|
||||||
data HoistedProgram = MkHoistedProgram
|
data HoistedProgram = MkHoistedProgram
|
||||||
{ bindings :: HashMap Label Abs
|
{ bindings :: HashMap Name Abs
|
||||||
, body :: Lambda
|
, body :: Lambda
|
||||||
}
|
}
|
||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
|
|
||||||
type instance Index HoistedProgram = Label
|
type instance Index HoistedProgram = Name
|
||||||
type instance IxValue HoistedProgram = Abs
|
type instance IxValue HoistedProgram = Abs
|
||||||
|
|
||||||
instance Ixed HoistedProgram where ix j = #bindings . ix j
|
instance Ixed HoistedProgram where ix j = #bindings . ix j
|
||||||
@@ -232,6 +196,7 @@ _AbsLambda' = prism'
|
|||||||
|
|
||||||
instance Plated Exp where plate = uniplate
|
instance Plated Exp where plate = uniplate
|
||||||
|
|
||||||
|
|
||||||
absBody :: Lens' Abs Exp
|
absBody :: Lens' Abs Exp
|
||||||
absBody = lens
|
absBody = lens
|
||||||
(\case
|
(\case
|
||||||
@@ -241,12 +206,6 @@ absBody = lens
|
|||||||
(AbsLambda lam) b -> AbsLambda $ lam & #body .~ b
|
(AbsLambda lam) b -> AbsLambda $ lam & #body .~ b
|
||||||
(AbsKappa kap) b -> AbsKappa $ kap & #body .~ b)
|
(AbsKappa kap) b -> AbsKappa $ kap & #body .~ b)
|
||||||
|
|
||||||
unhoist :: HoistedProgram -> Program
|
|
||||||
unhoist p =
|
|
||||||
MkProgram $ p.body & body %~ ExpLetRec
|
|
||||||
(p ^.. #bindings . itraversed . withIndex
|
|
||||||
. to (\(MkLabel l, ab) -> (l,ab)))
|
|
||||||
|
|
||||||
|
|
||||||
-- DatumIso instances
|
-- DatumIso instances
|
||||||
|
|
||||||
@@ -335,7 +294,7 @@ instance S.DatumIso Abs where
|
|||||||
|
|
||||||
instance S.DatumIso Exp where
|
instance S.DatumIso Exp where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (. builtin)
|
$ S.With (. prim)
|
||||||
$ S.With (. letrec)
|
$ S.With (. letrec)
|
||||||
$ S.With (. continue)
|
$ S.With (. continue)
|
||||||
$ S.With (. if_)
|
$ S.With (. if_)
|
||||||
@@ -365,9 +324,9 @@ instance S.DatumIso Exp where
|
|||||||
>>> S.el (S.datumIso @Kexp)
|
>>> S.el (S.datumIso @Kexp)
|
||||||
>>> S.rest (S.datumIso @Val)
|
>>> S.rest (S.datumIso @Val)
|
||||||
>>> S.onTail S.swap
|
>>> S.onTail S.swap
|
||||||
builtin = S.list $
|
prim = S.list $
|
||||||
S.el (S.decorate S.SynBuiltin >>> S.sym "builtin")
|
S.el (S.decorate S.SynBuiltin >>> S.sym "prim")
|
||||||
>>> S.el (builtinDatumIso id (S.datumIso @Val))
|
>>> S.el (primDatumIso id (S.datumIso @Val))
|
||||||
>>> S.el S.datumIso
|
>>> S.el S.datumIso
|
||||||
|
|
||||||
instance S.DatumIso Kexp where
|
instance S.DatumIso Kexp where
|
||||||
@@ -391,7 +350,7 @@ instance S.DatumIso Program where
|
|||||||
instance S.DatumIso HoistedProgram where
|
instance S.DatumIso HoistedProgram where
|
||||||
datumIso = S.with \prog ->
|
datumIso = S.with \prog ->
|
||||||
S.letLike "letrec"
|
S.letLike "letrec"
|
||||||
(S.datumIso @Label) (S.datumIso @Abs) (S.datumIso @Lambda)
|
(S.datumIso @Name) (S.datumIso @Abs) (S.datumIso @Lambda)
|
||||||
>>> S.onTail (S.iso H.fromList H.toList)
|
>>> S.onTail (S.iso H.fromList H.toList)
|
||||||
>>> prog
|
>>> prog
|
||||||
|
|
||||||
@@ -453,7 +412,7 @@ instance Free Kexp where
|
|||||||
|
|
||||||
instance Free Exp where
|
instance Free Exp where
|
||||||
freeWithBound' bound = \case
|
freeWithBound' bound = \case
|
||||||
ExpBuiltin p k ->
|
ExpPrim p k ->
|
||||||
(p ^.. folded . #ValVar . filtered (`notElem` bound))
|
(p ^.. folded . #ValVar . filtered (`notElem` bound))
|
||||||
++ freeWithBound' bound k
|
++ freeWithBound' bound k
|
||||||
ExpLetRec bs m ->
|
ExpLetRec bs m ->
|
||||||
|
|||||||
+37
-23
@@ -1,5 +1,5 @@
|
|||||||
module Gyehoek.Driver
|
module Gyehoek.Driver
|
||||||
(main, convert_e2e, parse_e2e, readScm, eval_cps1_e2e, eval_cps2_e2e)
|
(main, lower_e2e, convert_e2e, parse_e2e, readScm, eval_e2e)
|
||||||
where
|
where
|
||||||
|
|
||||||
import Gyehoek.Options
|
import Gyehoek.Options
|
||||||
@@ -17,6 +17,7 @@ import qualified Data.Text.Encoding as T
|
|||||||
import System.IO (Handle)
|
import System.IO (Handle)
|
||||||
import System.IO qualified as IO
|
import System.IO qualified as IO
|
||||||
import Gyehoek.CPS.Convert
|
import Gyehoek.CPS.Convert
|
||||||
|
import Gyehoek.Stack.Lower
|
||||||
import Gyehoek.CPS.Eval qualified as CPS
|
import Gyehoek.CPS.Eval qualified as CPS
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Text.Pretty.Simple (pShowNoColor)
|
import Text.Pretty.Simple (pShowNoColor)
|
||||||
@@ -24,7 +25,10 @@ import System.Process.Typed
|
|||||||
import System.Environment.Blank (getEnvDefault)
|
import System.Environment.Blank (getEnvDefault)
|
||||||
import qualified Data.Text.IO as TIO
|
import qualified Data.Text.IO as TIO
|
||||||
import qualified Data.ByteString.Lazy as BS
|
import qualified Data.ByteString.Lazy as BS
|
||||||
|
import Gyehoek.CPS.Stackify (stackifyProgram)
|
||||||
|
import Gyehoek.Stack.VM (eval, writeObj, Obj, traceEval)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import Gyehoek.Stack.Syntax qualified as Stk
|
||||||
import Gyehoek.CPS.Close (closeProgram)
|
import Gyehoek.CPS.Close (closeProgram)
|
||||||
import Control.Lens.Extras (is)
|
import Control.Lens.Extras (is)
|
||||||
import Control.Arrow ((>>>))
|
import Control.Arrow ((>>>))
|
||||||
@@ -32,6 +36,7 @@ import Gyehoek.Prelude
|
|||||||
import Gyehoek.Jalmot
|
import Gyehoek.Jalmot
|
||||||
import qualified Gyehoek.Sexp as S
|
import qualified Gyehoek.Sexp as S
|
||||||
import Gyehoek.CPS.Hoist (hoistProgram)
|
import Gyehoek.CPS.Hoist (hoistProgram)
|
||||||
|
import Gyehoek.CPS.Contify (contifyProgram)
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@@ -115,20 +120,35 @@ driver opts = do
|
|||||||
hPutStrLn FS.stdout . view strict . pShowNoColor $ scm
|
hPutStrLn FS.stdout . view strict . pShowNoColor $ scm
|
||||||
cps <- convertProgram scm
|
cps <- convertProgram scm
|
||||||
when opts.dumpCPS do
|
when opts.dumpCPS do
|
||||||
S.writeDatum cps
|
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso cps
|
||||||
closedCps <- closeProgram cps
|
closedCps <- closeProgram cps
|
||||||
when opts.dumpClosed do
|
when opts.dumpClosed do
|
||||||
S.writeDatum closedCps
|
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso closedCps
|
||||||
hoistedCps <- hoistProgram closedCps
|
hoistedCps <- hoistProgram closedCps
|
||||||
when opts.dumpHoisted do
|
when opts.dumpHoisted do
|
||||||
S.writeDatum hoistedCps
|
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso hoistedCps
|
||||||
|
-- contifiedCps <- contifyProgram hoistedCps
|
||||||
|
-- when opts.dumpContified do
|
||||||
|
-- hPutStrLn FS.stdout =<< S.encodeWith S.datumIso contifiedCps
|
||||||
let rt_is p = is (_Just . p) opts.runtime
|
let rt_is p = is (_Just . p) opts.runtime
|
||||||
when (rt_is #HigherOrderCPS) do
|
dumpOrRun opts.dumpStackified (rt_is #Stackify)
|
||||||
CPS.evalProgram cps
|
(stackifyProgram hoistedCps)
|
||||||
>>= S.writeData
|
(hPutStrLn FS.stdout <=< S.encodeDataWith S.dataIso)
|
||||||
|
(eval >=> fmap writeObj
|
||||||
|
>>> T.unwords
|
||||||
|
>>> hPutStrLn FS.stdout)
|
||||||
when (rt_is #CPS) do
|
when (rt_is #CPS) do
|
||||||
CPS.evalProgram closedCps
|
closedCps
|
||||||
>>= S.writeData
|
& CPS.evalProgram
|
||||||
|
& fmap writeObj
|
||||||
|
& T.unwords
|
||||||
|
& hPutStrLn FS.stdout
|
||||||
|
-- dumpOrRun opts.inspectWasm (rt_is #Wasm)
|
||||||
|
-- (lowerProgram cps)
|
||||||
|
-- inspectWasm
|
||||||
|
-- (\wat -> withFile opts.output FS.WriteMode \h -> hPutStrLn h wat)
|
||||||
|
when opts.traceStackified do
|
||||||
|
stackifyProgram hoistedCps >>= traceEval
|
||||||
|
|
||||||
parse_e2e :: FilePath -> IO Scm.Program
|
parse_e2e :: FilePath -> IO Scm.Program
|
||||||
parse_e2e = runJalmotIO . runFileSystem . readScm
|
parse_e2e = runJalmotIO . runFileSystem . readScm
|
||||||
@@ -137,18 +157,12 @@ convert_e2e :: FilePath -> IO CPS.Program
|
|||||||
convert_e2e = runJalmotIO . runFileSystem . runGenSym
|
convert_e2e = runJalmotIO . runFileSystem . runGenSym
|
||||||
. (closeProgram <=< convertProgram <=< readScm)
|
. (closeProgram <=< convertProgram <=< readScm)
|
||||||
|
|
||||||
eval_cps1_e2e :: FilePath -> IO Text
|
lower_e2e :: FilePath -> IO Text
|
||||||
eval_cps1_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
lower_e2e =
|
||||||
readScm fp
|
runJalmotIO . runFileSystem . runGenSym
|
||||||
>>= convertProgram
|
. (lowerProgram <=< closeProgram <=< convertProgram <=< readScm)
|
||||||
>>= closeProgram
|
|
||||||
>>= CPS.evalProgram
|
|
||||||
>>= pure . S.encodeOrShowData' S.dataIso
|
|
||||||
|
|
||||||
eval_cps2_e2e :: FilePath -> IO Text
|
eval_e2e :: FilePath -> IO (List Obj)
|
||||||
eval_cps2_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
eval_e2e fp = runJalmotIO . runFileSystem . runGenSym $ do
|
||||||
readScm fp
|
stk <- stackifyProgram <=< closeProgram <=< convertProgram <=< readScm $ fp
|
||||||
>>= convertProgram
|
eval stk
|
||||||
-- >>= closeProgram
|
|
||||||
>>= CPS.evalProgram
|
|
||||||
>>= pure . S.encodeOrShowData' S.dataIso
|
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ data AJalmot
|
|||||||
= ReaderError (ParseErrorBundle Text Void)
|
= ReaderError (ParseErrorBundle Text Void)
|
||||||
| GrammarError (Grammar.ErrorMessage Ann)
|
| GrammarError (Grammar.ErrorMessage Ann)
|
||||||
| VMError Text
|
| VMError Text
|
||||||
| EvalError Text
|
|
||||||
deriving (Show, Generic, Data)
|
deriving (Show, Generic, Data)
|
||||||
|
|
||||||
data AJalmotCS = MkAJalmotCS !CallStack !AJalmot
|
data AJalmotCS = MkAJalmotCS !CallStack !AJalmot
|
||||||
@@ -67,7 +66,6 @@ instance Exception AJalmot where
|
|||||||
& layoutPretty defaultLayoutOptions
|
& layoutPretty defaultLayoutOptions
|
||||||
& renderString
|
& renderString
|
||||||
VMError err -> [i|#{err}|]
|
VMError err -> [i|#{err}|]
|
||||||
EvalError err -> [i|#{err}|]
|
|
||||||
|
|
||||||
instance Exception AJalmotCS where
|
instance Exception AJalmotCS where
|
||||||
backtraceDesired = const False
|
backtraceDesired = const False
|
||||||
|
|||||||
+14
-20
@@ -13,13 +13,14 @@ import Data.Foldable
|
|||||||
import Gyehoek.Prelude hiding (argument)
|
import Gyehoek.Prelude hiding (argument)
|
||||||
|
|
||||||
|
|
||||||
data Runtime = Wasm | CPS | HigherOrderCPS
|
data Runtime = Stackify | Wasm | CPS
|
||||||
deriving (Show, Generic, Eq)
|
deriving (Show, Generic, Eq)
|
||||||
|
|
||||||
data Language
|
data Language
|
||||||
= LanguageScheme
|
= LanguageScheme
|
||||||
| LanguageCPS
|
| LanguageCPS
|
||||||
| LanguageClosed
|
| LanguageClosed
|
||||||
|
| LanguageStackified
|
||||||
| LanguageWasm
|
| LanguageWasm
|
||||||
deriving (Show, Generic, Eq)
|
deriving (Show, Generic, Eq)
|
||||||
|
|
||||||
@@ -27,30 +28,32 @@ data Options = MkOptions
|
|||||||
{ dumpClosed :: Bool
|
{ dumpClosed :: Bool
|
||||||
, dumpCPS :: Bool
|
, dumpCPS :: Bool
|
||||||
, dumpParsed :: Bool
|
, dumpParsed :: Bool
|
||||||
|
, dumpStackified :: Bool
|
||||||
, dumpHoisted :: Bool
|
, dumpHoisted :: Bool
|
||||||
, noColour :: Bool
|
, dumpContified :: Bool
|
||||||
|
, traceStackified :: Bool
|
||||||
, runtime :: Maybe Runtime
|
, runtime :: Maybe Runtime
|
||||||
, inspectWasm :: Bool
|
, inspectWasm :: Bool
|
||||||
, output :: FilePath
|
, output :: FilePath
|
||||||
, sourceFile :: FilePath
|
, sourceFile :: FilePath
|
||||||
, sourceLanguage :: Language
|
, sourceLanguage :: Language
|
||||||
, targetLanguage :: Language
|
|
||||||
}
|
}
|
||||||
deriving (Show, Generic)
|
deriving (Show, Generic)
|
||||||
|
|
||||||
languageValues = ["scheme","cps","closed","wasm"]
|
languageValues = ["scheme","cps","closed","stackified","wasm"]
|
||||||
languageReader = maybeReader \case
|
languageReader = maybeReader \case
|
||||||
"scheme" -> Just LanguageScheme
|
"scheme" -> Just LanguageScheme
|
||||||
"cps" -> Just LanguageCPS
|
"cps" -> Just LanguageCPS
|
||||||
"closed" -> Just LanguageClosed
|
"closed" -> Just LanguageClosed
|
||||||
|
"stackified" -> Just LanguageStackified
|
||||||
"wasm" -> Just LanguageWasm
|
"wasm" -> Just LanguageWasm
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
runtimeValues = ["stackify","wasm","cps","none"]
|
runtimeValues = ["stackify","wasm","cps","none"]
|
||||||
runtimeReader = maybeReader \case
|
runtimeReader = maybeReader \case
|
||||||
|
"stackify" -> Just (Just Stackify)
|
||||||
"wasm" -> Just (Just Wasm)
|
"wasm" -> Just (Just Wasm)
|
||||||
"cps1" -> Just (Just CPS)
|
"cps" -> Just (Just CPS)
|
||||||
("cps";"higher-order-cps") -> Just (Just HigherOrderCPS)
|
|
||||||
"none" -> Just Nothing
|
"none" -> Just Nothing
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
@@ -58,19 +61,18 @@ parser :: Parser Options
|
|||||||
parser = do
|
parser = do
|
||||||
dumpClosed <- switch (long "dump-closed")
|
dumpClosed <- switch (long "dump-closed")
|
||||||
dumpCPS <- switch (long "dump-cps")
|
dumpCPS <- switch (long "dump-cps")
|
||||||
|
dumpStackified <- switch (long "dump-stackified")
|
||||||
dumpParsed <- switch (long "dump-parsed")
|
dumpParsed <- switch (long "dump-parsed")
|
||||||
dumpHoisted <- switch (long "dump-hoisted")
|
dumpHoisted <- switch (long "dump-hoisted")
|
||||||
noColour <- switch . fold $
|
dumpContified <- switch (long "dump-contified")
|
||||||
[ long "no-colour"
|
traceStackified <- switch (long "trace-stackified")
|
||||||
, long "no-color"
|
|
||||||
]
|
|
||||||
inspectWasm <- switch $ long "inspect-wasm" <> short 'p'
|
inspectWasm <- switch $ long "inspect-wasm" <> short 'p'
|
||||||
runtime <- option runtimeReader . fold $
|
runtime <- option runtimeReader . fold $
|
||||||
[ long "runtime"
|
[ long "runtime"
|
||||||
, short 'R'
|
, short 'R'
|
||||||
, value (Just HigherOrderCPS)
|
, value (Just Stackify)
|
||||||
, completeWith runtimeValues
|
, completeWith runtimeValues
|
||||||
, showDefaultWith $ const "higher-order-cps"
|
, showDefaultWith $ const "stackify"
|
||||||
, metavar "RUNTIME"
|
, metavar "RUNTIME"
|
||||||
]
|
]
|
||||||
sourceLanguage <- option languageReader . fold $
|
sourceLanguage <- option languageReader . fold $
|
||||||
@@ -81,14 +83,6 @@ parser = do
|
|||||||
, showDefaultWith $ const "scheme"
|
, showDefaultWith $ const "scheme"
|
||||||
, metavar "LANGUAGE"
|
, metavar "LANGUAGE"
|
||||||
]
|
]
|
||||||
targetLanguage <- option languageReader . fold $
|
|
||||||
[ long "target"
|
|
||||||
, short 'T'
|
|
||||||
, value LanguageCPS
|
|
||||||
, completeWith languageValues
|
|
||||||
, showDefaultWith $ const "cps"
|
|
||||||
, metavar "LANGUAGE"
|
|
||||||
]
|
|
||||||
output <- strOption . fold $
|
output <- strOption . fold $
|
||||||
[ long "output"
|
[ long "output"
|
||||||
, short 'o'
|
, short 'o'
|
||||||
|
|||||||
@@ -1,536 +0,0 @@
|
|||||||
{-# LANGUAGE ViewPatterns #-}
|
|
||||||
{-# LANGUAGE DeriveAnyClass #-}
|
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
|
||||||
module Gyehoek.Scheme.Expand
|
|
||||||
(
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Gyehoek.Sexp.Syntax
|
|
||||||
import Gyehoek.Sexp qualified as S
|
|
||||||
import Gyehoek.Prelude
|
|
||||||
import Gyehoek.Scheme.Syntax hiding (Prim(..))
|
|
||||||
import Gyehoek.Scheme.Syntax qualified as Scm
|
|
||||||
import qualified Data.HashSet as HS
|
|
||||||
import qualified Data.HashMap.Strict as H
|
|
||||||
import Data.Foldable
|
|
||||||
import Data.These
|
|
||||||
import Data.Zip
|
|
||||||
import Prelude hiding (filter, zip, mapMaybe)
|
|
||||||
import qualified Data.List.NonEmpty as NE
|
|
||||||
import Data.Monoid (Ap(Ap, getAp))
|
|
||||||
import Gyehoek.Sexp.Grammar.Base ((:-)(..))
|
|
||||||
import Control.Lens.Extras (is)
|
|
||||||
import Control.Applicative (Alternative(..))
|
|
||||||
import Gyehoek.GenSym
|
|
||||||
import Data.HashSet.Lens (setOf)
|
|
||||||
import Data.List (sort)
|
|
||||||
import GHC.Exts (IsList(..))
|
|
||||||
import Gyehoek.Jalmot
|
|
||||||
import Data.Maybe (isNothing)
|
|
||||||
import Data.Kind (Type)
|
|
||||||
import Data.Traversable (for, mapAccumR)
|
|
||||||
import Effectful.State.Dynamic
|
|
||||||
import Witherable
|
|
||||||
import Data.Semigroup (Arg(..))
|
|
||||||
import Data.Ord (Down(..))
|
|
||||||
import qualified Data.Scientific as Sci
|
|
||||||
|
|
||||||
|
|
||||||
data Bind
|
|
||||||
= BindLexical { symbol :: Name, identity :: Natural }
|
|
||||||
| BindGlobal { symbol :: Name }
|
|
||||||
deriving stock (Generic, Eq, Show)
|
|
||||||
deriving anyclass (Hashable)
|
|
||||||
|
|
||||||
newtype Scope = MkScope { identity :: Natural }
|
|
||||||
deriving stock (Show, Generic, Eq, Ord)
|
|
||||||
deriving anyclass (Hashable)
|
|
||||||
deriving newtype (Gen)
|
|
||||||
|
|
||||||
type ScopeSet = HashSet Scope
|
|
||||||
|
|
||||||
data Formals
|
|
||||||
= FormalsFixed (List Name)
|
|
||||||
| FormalsRest (List Name) Name
|
|
||||||
deriving (Show, Generic)
|
|
||||||
|
|
||||||
data PrimLambda e = MkPrimLambda Formals (List e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
data PrimLet e = MkPrimLet (Maybe Name) (List (Name, e)) (List e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
data PrimIf e = MkPrimIf e e (Maybe e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
data PrimLetSyntax e = MkPrimLetSyntax (List (Name, e)) (List e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
|
|
||||||
data Prim e
|
|
||||||
= PrimLambda (PrimLambda e)
|
|
||||||
| PrimLet (PrimLet e)
|
|
||||||
| PrimIf (PrimIf e)
|
|
||||||
| PrimLetSyntax (PrimLetSyntax e)
|
|
||||||
| PrimSyntaxRules Trans
|
|
||||||
deriving (Show, Generic)
|
|
||||||
|
|
||||||
data Key = MkKey Name (HashSet Scope)
|
|
||||||
deriving stock (Show, Generic, Eq)
|
|
||||||
deriving anyclass (Hashable)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
instance S.DatumIso Scope where
|
|
||||||
datumIso = S.with (S.datumIso >>>)
|
|
||||||
|
|
||||||
hashSetGrammar
|
|
||||||
:: forall a. (Hashable a, Ord a)
|
|
||||||
=> S.DatumGrammar a -> S.DatumGrammar (HashSet a)
|
|
||||||
hashSetGrammar g =
|
|
||||||
S.list (S.rest g)
|
|
||||||
>>> S.iso HS.fromList (sort . HS.toList)
|
|
||||||
|
|
||||||
instance S.DatumIso Key where
|
|
||||||
datumIso = S.with \g ->
|
|
||||||
S.list
|
|
||||||
( S.el (S.symBuiltin "@")
|
|
||||||
>>> S.el (S.datumIso @Name)
|
|
||||||
>>> S.el (hashSetGrammar S.datumIso)
|
|
||||||
)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
instance S.DatumIso Formals where
|
|
||||||
datumIso = S.match
|
|
||||||
$ S.With (fixed >>>)
|
|
||||||
$ S.With (rest >>>)
|
|
||||||
$ S.End
|
|
||||||
where
|
|
||||||
fixed :: S.G (Datum :- t) (List Name :- t)
|
|
||||||
fixed = S.list $ S.rest $ S.datumIso @Name
|
|
||||||
rest :: S.G (Datum :- t) (Name :- List Name :- t)
|
|
||||||
rest = S.coproduct
|
|
||||||
[ S.dottedList (S.rest $ S.datumIso @Name) (S.datumIso @Name)
|
|
||||||
, S.datumIso @Name >>> S.onTail (S.push [] null (const mempty))
|
|
||||||
]
|
|
||||||
|
|
||||||
optEl
|
|
||||||
:: S.G (S.Datum :- t) (a :- t)
|
|
||||||
-> S.G (S.ListContext :- t) (S.ListContext :- Maybe a :- t)
|
|
||||||
optEl g =
|
|
||||||
S.coproduct
|
|
||||||
[ S.el g >>> S.onTail (S.partialIso Just \case
|
|
||||||
Nothing -> Left mempty
|
|
||||||
Just x -> Right x)
|
|
||||||
, S.onTail $ S.push Nothing isNothing (const mempty)
|
|
||||||
]
|
|
||||||
|
|
||||||
anykw :: Text -> S.G (Datum :- t) t
|
|
||||||
anykw s = S.Flip $ S.PartialIso
|
|
||||||
(\t -> adorn SynBuiltin (Symbol s) :- t)
|
|
||||||
\case
|
|
||||||
(Symbol _ :- t) -> Right t
|
|
||||||
_ -> Left $ S.expected "symbol"
|
|
||||||
|
|
||||||
prim_if :: S.DatumIso e => S.DatumGrammar (PrimIf e)
|
|
||||||
prim_if = S.with \g ->
|
|
||||||
S.listWithIndentation (NSpecial 1)
|
|
||||||
(S.el (anykw "if")
|
|
||||||
>>> S.el S.datumIso
|
|
||||||
>>> S.el S.datumIso
|
|
||||||
>>> optEl S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
prim_lambda :: S.DatumIso e => S.DatumGrammar (PrimLambda e)
|
|
||||||
prim_lambda = S.with \g ->
|
|
||||||
S.lambdaLike (anykw "λ") (S.datumIso @Formals) (S.rest S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
prim_let
|
|
||||||
:: forall e. S.DatumIso e => S.DatumGrammar (PrimLet e)
|
|
||||||
prim_let = S.with \g ->
|
|
||||||
(S.listWithIndentation (NSpecial 1) $
|
|
||||||
S.el (anykw "let")
|
|
||||||
>>> optEl (S.datumIso @Name)
|
|
||||||
>>> S.el (S.list $ S.rest $ S.datumIso @(Name,e))
|
|
||||||
>>> S.rest S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
prim_let_syntax :: S.DatumIso e => S.DatumGrammar (PrimLetSyntax e)
|
|
||||||
prim_let_syntax = S.with \g ->
|
|
||||||
(S.listWithIndentation (NSpecial 1) $
|
|
||||||
S.el (anykw "let-syntax")
|
|
||||||
>>> S.el (S.list $ S.rest $ S.datumIso)
|
|
||||||
>>> S.rest S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
instance S.DatumIso Bind where
|
|
||||||
datumIso = S.match
|
|
||||||
$ S.With (\g ->
|
|
||||||
S.list (S.el (S.symBuiltin "L") >>> S.el S.datumIso >>> S.el S.datumIso)
|
|
||||||
>>> g)
|
|
||||||
$ S.With (\g ->
|
|
||||||
S.list (S.el (S.symBuiltin "G") >>> S.el S.datumIso)
|
|
||||||
>>> g)
|
|
||||||
$ S.End
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{- |
|
|
||||||
|
|
||||||
* Examples
|
|
||||||
|
|
||||||
>>> :set -XTemplateHaskellQuotes
|
|
||||||
>>> pat = S.makeSx [|| S.fromDatumUnsafe @Pat S.datumIso ||]
|
|
||||||
>>> match [] [pat|(x . y)|] [S.sx|(1 2 . 2)|]
|
|
||||||
Nothing
|
|
||||||
>>> match [] [pat|(x . y)|] [S.sx|(1 . 2)|]
|
|
||||||
Just
|
|
||||||
...
|
|
||||||
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 => 2)|]
|
|
||||||
Just
|
|
||||||
...
|
|
||||||
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 -> 2)|]
|
|
||||||
Nothing
|
|
||||||
-}
|
|
||||||
match :: Foldable f
|
|
||||||
=> f Text
|
|
||||||
-- ^ Literal keywords
|
|
||||||
-> Pat
|
|
||||||
-> Datum
|
|
||||||
-> Maybe (HashMap Name Datum)
|
|
||||||
match kws p = getAp . match' p where
|
|
||||||
|
|
||||||
match' :: Pat -> Datum -> Ap Maybe (HashMap Name Datum)
|
|
||||||
|
|
||||||
match' PatWildcard _ = pure mempty
|
|
||||||
|
|
||||||
match' (PatVar x) e
|
|
||||||
| coerce x `elem` kws = case e of
|
|
||||||
Symbol x' | coerce x == x' -> pure mempty
|
|
||||||
_ -> empty
|
|
||||||
| otherwise = pure $ H.singleton x e
|
|
||||||
|
|
||||||
match' (PatList ps Nothing Nothing) (List es) = matches ps es
|
|
||||||
|
|
||||||
match' (PatList ps (Just []) Nothing) (List es) = do
|
|
||||||
let (ps',p) = ps ^?! _Snoc
|
|
||||||
(r,rest) <- fold $ alignWith f ps' es
|
|
||||||
rest' <- rest
|
|
||||||
& fmap (fmap (fmap (:[])) . match' p)
|
|
||||||
& foldr (liftA2 $ H.unionWith (<>)) mempty
|
|
||||||
& fmap (fmap List)
|
|
||||||
pure $ r <> rest'
|
|
||||||
where
|
|
||||||
f (These a b) = (,[]) <$> match' a b
|
|
||||||
f (This a) = empty
|
|
||||||
f (That b) = pure (mempty,[b])
|
|
||||||
|
|
||||||
match' (PatList ps Nothing (Just p)) (DotList es e) =
|
|
||||||
matches (p:|ps) (NE.cons e es)
|
|
||||||
|
|
||||||
match' _ _ = _
|
|
||||||
|
|
||||||
matchPrefix
|
|
||||||
:: (Semialign f, Foldable f)
|
|
||||||
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum, List Datum)
|
|
||||||
matchPrefix ps es = fold $ alignWith f ps es
|
|
||||||
where
|
|
||||||
f (These a b) = (,[]) <$> match' a b
|
|
||||||
f (This a) = empty
|
|
||||||
f (That b) = pure (mempty,[b])
|
|
||||||
|
|
||||||
matches
|
|
||||||
:: (Semialign f, Foldable f)
|
|
||||||
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum)
|
|
||||||
matches ps es = fold $ alignWith matchThese ps es
|
|
||||||
|
|
||||||
matchThese (These a b) = match' a b
|
|
||||||
matchThese _ = empty
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- | this hashmap is "curried" since we often want to traverse the
|
|
||||||
-- entire set of scopes associated with a given symbol.
|
|
||||||
newtype SymTable = MkSymTable
|
|
||||||
{ curried :: HashMap Name (HashMap (HashSet Scope) Bind) }
|
|
||||||
deriving (Show, Generic)
|
|
||||||
|
|
||||||
type instance Index SymTable = Name
|
|
||||||
type instance IxValue SymTable = HashMap (HashSet Scope) Bind
|
|
||||||
instance Ixed SymTable where ix j = #curried . ix j
|
|
||||||
instance At SymTable where at j = #curried . at j
|
|
||||||
|
|
||||||
instance Semigroup SymTable where
|
|
||||||
MkSymTable c1 <> MkSymTable c2 = MkSymTable $ H.unionWith (<>) c1 c2
|
|
||||||
|
|
||||||
instance Monoid SymTable where mempty = MkSymTable mempty
|
|
||||||
|
|
||||||
type Expand es = (State SymTable :> es, GenSym :> es)
|
|
||||||
|
|
||||||
runExpand :: SymTable -> Eff (State SymTable : GenSym : es) a -> Eff es (a, SymTable)
|
|
||||||
runExpand tab = runGenSym . runStateLocal tab
|
|
||||||
|
|
||||||
testExpand :: Datum -> IO (CommandOrDef, SymTable)
|
|
||||||
testExpand = runJalmotIO . runExpand (symTableOfEnv env_scheme_base)
|
|
||||||
. expand env_scheme_base mempty
|
|
||||||
|
|
||||||
-- | denotations
|
|
||||||
data Denot
|
|
||||||
= DenotVar
|
|
||||||
| DenotMacro Trans
|
|
||||||
| DenotPrim Name
|
|
||||||
| DenotSyntax Datum
|
|
||||||
| DenotKeyword Name
|
|
||||||
deriving stock (Show, Generic)
|
|
||||||
|
|
||||||
newtype Env = MkEnv { names :: HashMap Bind Denot }
|
|
||||||
deriving stock (Show, Generic)
|
|
||||||
deriving newtype (Semigroup, Monoid)
|
|
||||||
|
|
||||||
-- | @(environment '(scheme base))@
|
|
||||||
env_scheme_base :: Env
|
|
||||||
env_scheme_base = MkEnv . fromList . fold $
|
|
||||||
[ [ (BindGlobal p, DenotPrim p) | p <- prims ]
|
|
||||||
, [ (BindGlobal "λ", DenotPrim "lambda")
|
|
||||||
]
|
|
||||||
]
|
|
||||||
where
|
|
||||||
prims =
|
|
||||||
[ "if"
|
|
||||||
, "lambda"
|
|
||||||
, "let"
|
|
||||||
, "let-syntax"
|
|
||||||
]
|
|
||||||
|
|
||||||
symTableOfEnv :: Env -> SymTable
|
|
||||||
symTableOfEnv = ifoldMapOf (#names . itraversed) \cases
|
|
||||||
b@(BindGlobal n) d -> binding n mempty b
|
|
||||||
|
|
||||||
type instance IxValue Env = Denot
|
|
||||||
type instance Index Env = Bind
|
|
||||||
|
|
||||||
instance Ixed Env where ix j = #names . ix j
|
|
||||||
instance At Env where at j = #names . at j
|
|
||||||
|
|
||||||
err :: Jalmot :> es => Text -> Eff es a
|
|
||||||
err = throwError . EvalError
|
|
||||||
|
|
||||||
run :: S.DatumGrammar a -> Datum -> Maybe a
|
|
||||||
run g = preview #_Right . runPureEff . runJalmot . S.fromDatum g
|
|
||||||
|
|
||||||
parsePrim :: S.DatumIso e => Name -> Datum -> Maybe (Prim e)
|
|
||||||
parsePrim primname d = case primname of
|
|
||||||
"let" -> PrimLet <$> run prim_let d
|
|
||||||
"let-syntax" -> PrimLetSyntax <$> run prim_let_syntax d
|
|
||||||
"lambda" -> PrimLambda <$> run prim_lambda d
|
|
||||||
"if" -> PrimIf <$> run prim_if d
|
|
||||||
|
|
||||||
emit :: (Monoid m, State m :> es) => m -> Eff es ()
|
|
||||||
emit x = modify (<> x)
|
|
||||||
|
|
||||||
binding :: Name -> HashSet Scope -> Bind -> SymTable
|
|
||||||
binding sym scopes = MkSymTable . H.singleton sym . H.singleton scopes
|
|
||||||
|
|
||||||
envOfVar :: Bind -> Env
|
|
||||||
envOfVar = MkEnv . flip H.singleton DenotVar
|
|
||||||
|
|
||||||
gensymLexical :: GenSym :> es => Name -> Eff es Bind
|
|
||||||
gensymLexical symbol = do
|
|
||||||
identity <- gensym
|
|
||||||
pure $ BindLexical {symbol,identity}
|
|
||||||
|
|
||||||
lookupSymbol
|
|
||||||
:: forall es. (State SymTable :> es, Jalmot :> es)
|
|
||||||
=> Env -> HashSet Scope -> Name -> Eff es (Bind, Denot)
|
|
||||||
lookupSymbol g scopes x = do
|
|
||||||
ss <- fmap fold . preuse @SymTable @(Eff es) $ ix x
|
|
||||||
case nearest (MkKey x scopes) (H.keys ss) of
|
|
||||||
[] -> err [i|심벌 #{x}는 정의되지 않다|]
|
|
||||||
(_:_:_) -> err [i|심벌 #{x}는 모호하다|]
|
|
||||||
[s] | Just b <- ss ^? ix s
|
|
||||||
, Just d <- g ^? ix b -> pure (b,d)
|
|
||||||
| otherwise -> error "unreachable"
|
|
||||||
|
|
||||||
{- | Given a 'Key' and a collection of 'ScopeSet's, filter that
|
|
||||||
collection down to the largest subsets of the 'Key'\'s scope set.
|
|
||||||
This is our analogue of the lexical scoping rule which chooses the
|
|
||||||
"nearest" binding of a variable when shadowing occurs.
|
|
||||||
|
|
||||||
- If no qualifying subsets are found, the name is not in scope.
|
|
||||||
- If one subset is found, we're on the happy path!
|
|
||||||
- If more than one subset is found, we're on the rarest and
|
|
||||||
saddest path: the reference is ambiguous.
|
|
||||||
|
|
||||||
* Examples
|
|
||||||
|
|
||||||
> (let ((x {A} 123))
|
|
||||||
> (λ (x {A,B})
|
|
||||||
> (let ((y {A,B,C} 456))
|
|
||||||
> x {A,B,C})))
|
|
||||||
|
|
||||||
>>> :seti -XOverloadedLists
|
|
||||||
>>> :{
|
|
||||||
nearest
|
|
||||||
(MkKey "x" [MkScope 0, MkScope 1, MkScope 2])
|
|
||||||
[ [MkScope 0]
|
|
||||||
, [MkScope 0, MkScope 1] ]
|
|
||||||
:}
|
|
||||||
-}
|
|
||||||
nearest :: (Traversable f, Filterable f) => Key -> f ScopeSet -> List ScopeSet
|
|
||||||
nearest (MkKey symbol scopes) =
|
|
||||||
mapMaybe (\x ->
|
|
||||||
if x `HS.isSubsetOf` scopes
|
|
||||||
then Just . Down $ Arg (length x) x
|
|
||||||
else Nothing)
|
|
||||||
-- 나쁨. 안 좋다. 안 좋아하야.
|
|
||||||
>>> Data.Foldable.toList >>> sort
|
|
||||||
>>> foldr (\cases
|
|
||||||
x [] -> [x]
|
|
||||||
x acc@(y:_) -> case x `compare` y of
|
|
||||||
LT -> acc
|
|
||||||
EQ -> x:acc
|
|
||||||
GT -> [x])
|
|
||||||
[]
|
|
||||||
>>> fmap (\(Down (Arg _ x)) -> x)
|
|
||||||
|
|
||||||
expand
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Datum -> Eff es CommandOrDef
|
|
||||||
|
|
||||||
expand g scopes datum@(List (Symbol s : es)) = do
|
|
||||||
let s' = MkName s
|
|
||||||
(_,denot) <- lookupSymbol g scopes s'
|
|
||||||
case denot of
|
|
||||||
DenotVar -> Command . ExpApply (ExpVar s')
|
|
||||||
<$> traverse (expandAsExp g scopes) es
|
|
||||||
DenotPrim x -> expandPrim g scopes x datum
|
|
||||||
DenotMacro trans -> expandMacro g scopes trans datum
|
|
||||||
|
|
||||||
-- 신기하지 않은 경우들
|
|
||||||
expand g scopes datum = case datum of
|
|
||||||
List (x:xs) -> Command <$> (ExpApply <$> go x <*> traverse go xs)
|
|
||||||
Symbol s -> pure . Command . ExpVar . MkName $ s
|
|
||||||
Boolean b -> lit $ LitBool b
|
|
||||||
Number (Sci.floatingOrInteger -> Right n) -> lit $ LitInt n
|
|
||||||
where
|
|
||||||
go = expandAsExp g scopes
|
|
||||||
lit = pure . Command . ExpLit
|
|
||||||
|
|
||||||
expandMacro
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Trans -> Datum -> Eff es CommandOrDef
|
|
||||||
expandMacro g scopes trans datum = _
|
|
||||||
|
|
||||||
expandPrim
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Name -> Datum -> Eff es CommandOrDef
|
|
||||||
expandPrim g scopes primName datum
|
|
||||||
| Just prim <- parsePrim @Datum primName datum
|
|
||||||
= case prim of
|
|
||||||
PrimLetSyntax (MkPrimLetSyntax bs [body]) -> do
|
|
||||||
scopes' <- flip HS.insert scopes <$> gensym
|
|
||||||
(rhss,g') <- (_2 %~ (g<>) . fold) . Prelude.unzip <$> for bs \(x,trans) ->
|
|
||||||
expandAsExp g scopes trans >>= \case
|
|
||||||
ExpSyntaxRules trans' ->
|
|
||||||
(trans',) <$> bindLexical scopes' x (DenotMacro trans')
|
|
||||||
e -> err [i|syntax-rules를 원하는데 이것 받는다: #{e}|]
|
|
||||||
Command <$> (ExpLetSyntax
|
|
||||||
(zip (bs ^.. each . _1) rhss)
|
|
||||||
<$> expandAsExp g' scopes' body
|
|
||||||
)
|
|
||||||
PrimLet (MkPrimLet Nothing bs [body]) -> do
|
|
||||||
scopes' <- flip HS.insert scopes <$> gensym
|
|
||||||
g' <- (g<>) . fold <$> for (bs ^.. each . _1) \x ->
|
|
||||||
bindLexical scopes' x DenotVar
|
|
||||||
Command <$> (ExpLet
|
|
||||||
<$> traverseOf (each . _2) (expandAsExp g scopes) bs
|
|
||||||
<*> expandAsExp g scopes' body)
|
|
||||||
PrimLambda (MkPrimLambda (FormalsFixed xs) [body]) -> do
|
|
||||||
scopes' <- flip HS.insert scopes <$> gensym
|
|
||||||
g' <- (g<>) . fold <$> for xs \x -> bindLexical scopes' x DenotVar
|
|
||||||
Command . ExpLambda xs <$> expandAsExp g' scopes' body
|
|
||||||
PrimIf (MkPrimIf c t f) ->
|
|
||||||
Command <$> (ExpIf
|
|
||||||
<$> expandAsExp g scopes c
|
|
||||||
<*> expandAsExp g scopes t
|
|
||||||
<*> traverse (expandAsExp g scopes) f
|
|
||||||
)
|
|
||||||
| otherwise = err [i|prim #{primName}에 잘못한 신택스: #{datum}|]
|
|
||||||
|
|
||||||
expandAsExp
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Datum -> Eff es Exp
|
|
||||||
expandAsExp g scopes d = expand g scopes d >>= intoExp
|
|
||||||
|
|
||||||
bindLexical :: Expand es => ScopeSet -> Name -> Denot -> Eff es Env
|
|
||||||
bindLexical scopes symbol denot = do
|
|
||||||
identity <- gensym
|
|
||||||
let bind = BindLexical {symbol,identity}
|
|
||||||
modify (<> binding symbol scopes bind)
|
|
||||||
pure $ MkEnv (H.singleton bind denot)
|
|
||||||
|
|
||||||
intoExp :: Jalmot :> es => CommandOrDef -> Eff es Exp
|
|
||||||
intoExp (Command e) = pure e
|
|
||||||
intoExp (Begin es) = traverse intoExp es >>= \case
|
|
||||||
[] -> err "begin expression은 빔"
|
|
||||||
(x:xs) -> pure . ExpBegin $ x NE.:| xs
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
trans_when :: Trans
|
|
||||||
trans_when = MkTrans
|
|
||||||
{ ellipsis = "..."
|
|
||||||
, keywords = []
|
|
||||||
, rules =
|
|
||||||
[ MkRule
|
|
||||||
(PatList
|
|
||||||
[ PatVar "when"
|
|
||||||
, PatVar "test"
|
|
||||||
, PatVar "body" ]
|
|
||||||
(Just [])
|
|
||||||
Nothing)
|
|
||||||
(TemList
|
|
||||||
[ El $ TemVar "if"
|
|
||||||
, El $ TemVar "test"
|
|
||||||
, El $ TemList
|
|
||||||
[ El $ TemVar "begin"
|
|
||||||
, Ellipsis $ TemVar "body"
|
|
||||||
]
|
|
||||||
Nothing
|
|
||||||
]
|
|
||||||
Nothing)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
trans_and :: Trans
|
|
||||||
trans_and = MkTrans
|
|
||||||
{ ellipsis = "..."
|
|
||||||
, keywords = []
|
|
||||||
, rules =
|
|
||||||
[ MkRule
|
|
||||||
(PatList
|
|
||||||
[PatVar "and"]
|
|
||||||
Nothing
|
|
||||||
Nothing)
|
|
||||||
(TemLit (LitBool True))
|
|
||||||
, MkRule
|
|
||||||
(PatList
|
|
||||||
[PatVar "and", PatVar "x"]
|
|
||||||
Nothing
|
|
||||||
Nothing)
|
|
||||||
(TemVar "x")
|
|
||||||
, MkRule
|
|
||||||
(PatList
|
|
||||||
[PatVar "and", PatVar "x", PatVar "y"]
|
|
||||||
(Just [])
|
|
||||||
Nothing)
|
|
||||||
(TemList
|
|
||||||
[ El (TemVar "if")
|
|
||||||
, El (TemVar "x")
|
|
||||||
, El (TemList
|
|
||||||
[ El (TemVar "and")
|
|
||||||
, Ellipsis (TemVar "y")
|
|
||||||
]
|
|
||||||
Nothing)
|
|
||||||
, El . TemLit . LitBool $ False
|
|
||||||
]
|
|
||||||
Nothing)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -1,536 +0,0 @@
|
|||||||
{-# LANGUAGE ViewPatterns #-}
|
|
||||||
{-# LANGUAGE DeriveAnyClass #-}
|
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
|
||||||
module Gyehoek.Scheme.Expand.Old
|
|
||||||
(
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Gyehoek.Sexp.Syntax
|
|
||||||
import Gyehoek.Sexp qualified as S
|
|
||||||
import Gyehoek.Prelude
|
|
||||||
import Gyehoek.Scheme.Syntax hiding (Prim(..))
|
|
||||||
import Gyehoek.Scheme.Syntax qualified as Scm
|
|
||||||
import qualified Data.HashSet as HS
|
|
||||||
import qualified Data.HashMap.Strict as H
|
|
||||||
import Data.Foldable
|
|
||||||
import Data.These
|
|
||||||
import Data.Zip
|
|
||||||
import Prelude hiding (filter, zip, mapMaybe)
|
|
||||||
import qualified Data.List.NonEmpty as NE
|
|
||||||
import Data.Monoid (Ap(Ap, getAp))
|
|
||||||
import Gyehoek.Sexp.Grammar.Base ((:-)(..))
|
|
||||||
import Control.Lens.Extras (is)
|
|
||||||
import Control.Applicative (Alternative(..))
|
|
||||||
import Gyehoek.GenSym
|
|
||||||
import Data.HashSet.Lens (setOf)
|
|
||||||
import Data.List (sort)
|
|
||||||
import GHC.Exts (IsList(..))
|
|
||||||
import Gyehoek.Jalmot
|
|
||||||
import Data.Maybe (isNothing)
|
|
||||||
import Data.Kind (Type)
|
|
||||||
import Data.Traversable (for, mapAccumR)
|
|
||||||
import Effectful.State.Dynamic
|
|
||||||
import Witherable
|
|
||||||
import Data.Semigroup (Arg(..))
|
|
||||||
import Data.Ord (Down(..))
|
|
||||||
import qualified Data.Scientific as Sci
|
|
||||||
|
|
||||||
|
|
||||||
data Bind
|
|
||||||
= BindLexical { symbol :: Name, identity :: Natural }
|
|
||||||
| BindGlobal { symbol :: Name }
|
|
||||||
deriving stock (Generic, Eq, Show)
|
|
||||||
deriving anyclass (Hashable)
|
|
||||||
|
|
||||||
newtype Scope = MkScope { identity :: Natural }
|
|
||||||
deriving stock (Show, Generic, Eq, Ord)
|
|
||||||
deriving anyclass (Hashable)
|
|
||||||
deriving newtype (Gen)
|
|
||||||
|
|
||||||
type ScopeSet = HashSet Scope
|
|
||||||
|
|
||||||
data Formals
|
|
||||||
= FormalsFixed (List Name)
|
|
||||||
| FormalsRest (List Name) Name
|
|
||||||
deriving (Show, Generic)
|
|
||||||
|
|
||||||
data PrimLambda e = MkPrimLambda Formals (List e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
data PrimLet e = MkPrimLet (Maybe Name) (List (Name, e)) (List e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
data PrimIf e = MkPrimIf e e (Maybe e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
data PrimLetSyntax e = MkPrimLetSyntax (List (Name, e)) (List e)
|
|
||||||
deriving (Show, Generic, Functor, Foldable, Traversable)
|
|
||||||
|
|
||||||
data Prim e
|
|
||||||
= PrimLambda (PrimLambda e)
|
|
||||||
| PrimLet (PrimLet e)
|
|
||||||
| PrimIf (PrimIf e)
|
|
||||||
| PrimLetSyntax (PrimLetSyntax e)
|
|
||||||
| PrimSyntaxRules Trans
|
|
||||||
deriving (Show, Generic)
|
|
||||||
|
|
||||||
data Key = MkKey Name (HashSet Scope)
|
|
||||||
deriving stock (Show, Generic, Eq)
|
|
||||||
deriving anyclass (Hashable)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
instance S.DatumIso Scope where
|
|
||||||
datumIso = S.with (S.datumIso >>>)
|
|
||||||
|
|
||||||
hashSetGrammar
|
|
||||||
:: forall a. (Hashable a, Ord a)
|
|
||||||
=> S.DatumGrammar a -> S.DatumGrammar (HashSet a)
|
|
||||||
hashSetGrammar g =
|
|
||||||
S.list (S.rest g)
|
|
||||||
>>> S.iso HS.fromList (sort . HS.toList)
|
|
||||||
|
|
||||||
instance S.DatumIso Key where
|
|
||||||
datumIso = S.with \g ->
|
|
||||||
S.list
|
|
||||||
( S.el (S.symBuiltin "@")
|
|
||||||
>>> S.el (S.datumIso @Name)
|
|
||||||
>>> S.el (hashSetGrammar S.datumIso)
|
|
||||||
)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
instance S.DatumIso Formals where
|
|
||||||
datumIso = S.match
|
|
||||||
$ S.With (fixed >>>)
|
|
||||||
$ S.With (rest >>>)
|
|
||||||
$ S.End
|
|
||||||
where
|
|
||||||
fixed :: S.G (Datum :- t) (List Name :- t)
|
|
||||||
fixed = S.list $ S.rest $ S.datumIso @Name
|
|
||||||
rest :: S.G (Datum :- t) (Name :- List Name :- t)
|
|
||||||
rest = S.coproduct
|
|
||||||
[ S.dottedList (S.rest $ S.datumIso @Name) (S.datumIso @Name)
|
|
||||||
, S.datumIso @Name >>> S.onTail (S.push [] null (const mempty))
|
|
||||||
]
|
|
||||||
|
|
||||||
optEl
|
|
||||||
:: S.G (S.Datum :- t) (a :- t)
|
|
||||||
-> S.G (S.ListContext :- t) (S.ListContext :- Maybe a :- t)
|
|
||||||
optEl g =
|
|
||||||
S.coproduct
|
|
||||||
[ S.el g >>> S.onTail (S.partialIso Just \case
|
|
||||||
Nothing -> Left mempty
|
|
||||||
Just x -> Right x)
|
|
||||||
, S.onTail $ S.push Nothing isNothing (const mempty)
|
|
||||||
]
|
|
||||||
|
|
||||||
anykw :: Text -> S.G (Datum :- t) t
|
|
||||||
anykw s = S.Flip $ S.PartialIso
|
|
||||||
(\t -> adorn SynBuiltin (Symbol s) :- t)
|
|
||||||
\case
|
|
||||||
(Symbol _ :- t) -> Right t
|
|
||||||
_ -> Left $ S.expected "symbol"
|
|
||||||
|
|
||||||
prim_if :: S.DatumIso e => S.DatumGrammar (PrimIf e)
|
|
||||||
prim_if = S.with \g ->
|
|
||||||
S.listWithIndentation (NSpecial 1)
|
|
||||||
(S.el (anykw "if")
|
|
||||||
>>> S.el S.datumIso
|
|
||||||
>>> S.el S.datumIso
|
|
||||||
>>> optEl S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
prim_lambda :: S.DatumIso e => S.DatumGrammar (PrimLambda e)
|
|
||||||
prim_lambda = S.with \g ->
|
|
||||||
S.lambdaLike (anykw "λ") (S.datumIso @Formals) (S.rest S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
prim_let
|
|
||||||
:: forall e. S.DatumIso e => S.DatumGrammar (PrimLet e)
|
|
||||||
prim_let = S.with \g ->
|
|
||||||
(S.listWithIndentation (NSpecial 1) $
|
|
||||||
S.el (anykw "let")
|
|
||||||
>>> optEl (S.datumIso @Name)
|
|
||||||
>>> S.el (S.list $ S.rest $ S.datumIso @(Name,e))
|
|
||||||
>>> S.rest S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
prim_let_syntax :: S.DatumIso e => S.DatumGrammar (PrimLetSyntax e)
|
|
||||||
prim_let_syntax = S.with \g ->
|
|
||||||
(S.listWithIndentation (NSpecial 1) $
|
|
||||||
S.el (anykw "let-syntax")
|
|
||||||
>>> S.el (S.list $ S.rest $ S.datumIso)
|
|
||||||
>>> S.rest S.datumIso)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
instance S.DatumIso Bind where
|
|
||||||
datumIso = S.match
|
|
||||||
$ S.With (\g ->
|
|
||||||
S.list (S.el (S.symBuiltin "L") >>> S.el S.datumIso >>> S.el S.datumIso)
|
|
||||||
>>> g)
|
|
||||||
$ S.With (\g ->
|
|
||||||
S.list (S.el (S.symBuiltin "G") >>> S.el S.datumIso)
|
|
||||||
>>> g)
|
|
||||||
$ S.End
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{- |
|
|
||||||
|
|
||||||
* Examples
|
|
||||||
|
|
||||||
>>> :set -XTemplateHaskellQuotes
|
|
||||||
>>> pat = S.makeSx [|| S.fromDatumUnsafe @Pat S.datumIso ||]
|
|
||||||
>>> match [] [pat|(x . y)|] [S.sx|(1 2 . 2)|]
|
|
||||||
Nothing
|
|
||||||
>>> match [] [pat|(x . y)|] [S.sx|(1 . 2)|]
|
|
||||||
Just
|
|
||||||
...
|
|
||||||
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 => 2)|]
|
|
||||||
Just
|
|
||||||
...
|
|
||||||
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 -> 2)|]
|
|
||||||
Nothing
|
|
||||||
-}
|
|
||||||
match :: Foldable f
|
|
||||||
=> f Text
|
|
||||||
-- ^ Literal keywords
|
|
||||||
-> Pat
|
|
||||||
-> Datum
|
|
||||||
-> Maybe (HashMap Name Datum)
|
|
||||||
match kws p = getAp . match' p where
|
|
||||||
|
|
||||||
match' :: Pat -> Datum -> Ap Maybe (HashMap Name Datum)
|
|
||||||
|
|
||||||
match' PatWildcard _ = pure mempty
|
|
||||||
|
|
||||||
match' (PatVar x) e
|
|
||||||
| coerce x `elem` kws = case e of
|
|
||||||
Symbol x' | coerce x == x' -> pure mempty
|
|
||||||
_ -> empty
|
|
||||||
| otherwise = pure $ H.singleton x e
|
|
||||||
|
|
||||||
match' (PatList ps Nothing Nothing) (List es) = matches ps es
|
|
||||||
|
|
||||||
match' (PatList ps (Just []) Nothing) (List es) = do
|
|
||||||
let (ps',p) = ps ^?! _Snoc
|
|
||||||
(r,rest) <- fold $ alignWith f ps' es
|
|
||||||
rest' <- rest
|
|
||||||
& fmap (fmap (fmap (:[])) . match' p)
|
|
||||||
& foldr (liftA2 $ H.unionWith (<>)) mempty
|
|
||||||
& fmap (fmap List)
|
|
||||||
pure $ r <> rest'
|
|
||||||
where
|
|
||||||
f (These a b) = (,[]) <$> match' a b
|
|
||||||
f (This a) = empty
|
|
||||||
f (That b) = pure (mempty,[b])
|
|
||||||
|
|
||||||
match' (PatList ps Nothing (Just p)) (DotList es e) =
|
|
||||||
matches (p:|ps) (NE.cons e es)
|
|
||||||
|
|
||||||
match' _ _ = _
|
|
||||||
|
|
||||||
matchPrefix
|
|
||||||
:: (Semialign f, Foldable f)
|
|
||||||
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum, List Datum)
|
|
||||||
matchPrefix ps es = fold $ alignWith f ps es
|
|
||||||
where
|
|
||||||
f (These a b) = (,[]) <$> match' a b
|
|
||||||
f (This a) = empty
|
|
||||||
f (That b) = pure (mempty,[b])
|
|
||||||
|
|
||||||
matches
|
|
||||||
:: (Semialign f, Foldable f)
|
|
||||||
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum)
|
|
||||||
matches ps es = fold $ alignWith matchThese ps es
|
|
||||||
|
|
||||||
matchThese (These a b) = match' a b
|
|
||||||
matchThese _ = empty
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- | this hashmap is "curried" since we often want to traverse the
|
|
||||||
-- entire set of scopes associated with a given symbol.
|
|
||||||
newtype SymTable = MkSymTable
|
|
||||||
{ curried :: HashMap Name (HashMap (HashSet Scope) Bind) }
|
|
||||||
deriving (Show, Generic)
|
|
||||||
|
|
||||||
type instance Index SymTable = Name
|
|
||||||
type instance IxValue SymTable = HashMap (HashSet Scope) Bind
|
|
||||||
instance Ixed SymTable where ix j = #curried . ix j
|
|
||||||
instance At SymTable where at j = #curried . at j
|
|
||||||
|
|
||||||
instance Semigroup SymTable where
|
|
||||||
MkSymTable c1 <> MkSymTable c2 = MkSymTable $ H.unionWith (<>) c1 c2
|
|
||||||
|
|
||||||
instance Monoid SymTable where mempty = MkSymTable mempty
|
|
||||||
|
|
||||||
type Expand es = (State SymTable :> es, GenSym :> es)
|
|
||||||
|
|
||||||
runExpand :: SymTable -> Eff (State SymTable : GenSym : es) a -> Eff es (a, SymTable)
|
|
||||||
runExpand tab = runGenSym . runStateLocal tab
|
|
||||||
|
|
||||||
testExpand :: Datum -> IO (CommandOrDef, SymTable)
|
|
||||||
testExpand = runJalmotIO . runExpand (symTableOfEnv env_scheme_base)
|
|
||||||
. expand env_scheme_base mempty
|
|
||||||
|
|
||||||
-- | denotations
|
|
||||||
data Denot
|
|
||||||
= DenotVar
|
|
||||||
| DenotMacro Trans
|
|
||||||
| DenotPrim Name
|
|
||||||
| DenotSyntax Datum
|
|
||||||
| DenotKeyword Name
|
|
||||||
deriving stock (Show, Generic)
|
|
||||||
|
|
||||||
newtype Env = MkEnv { names :: HashMap Bind Denot }
|
|
||||||
deriving stock (Show, Generic)
|
|
||||||
deriving newtype (Semigroup, Monoid)
|
|
||||||
|
|
||||||
-- | @(environment '(scheme base))@
|
|
||||||
env_scheme_base :: Env
|
|
||||||
env_scheme_base = MkEnv . fromList . fold $
|
|
||||||
[ [ (BindGlobal p, DenotPrim p) | p <- prims ]
|
|
||||||
, [ (BindGlobal "λ", DenotPrim "lambda")
|
|
||||||
]
|
|
||||||
]
|
|
||||||
where
|
|
||||||
prims =
|
|
||||||
[ "if"
|
|
||||||
, "lambda"
|
|
||||||
, "let"
|
|
||||||
, "let-syntax"
|
|
||||||
]
|
|
||||||
|
|
||||||
symTableOfEnv :: Env -> SymTable
|
|
||||||
symTableOfEnv = ifoldMapOf (#names . itraversed) \cases
|
|
||||||
b@(BindGlobal n) d -> binding n mempty b
|
|
||||||
|
|
||||||
type instance IxValue Env = Denot
|
|
||||||
type instance Index Env = Bind
|
|
||||||
|
|
||||||
instance Ixed Env where ix j = #names . ix j
|
|
||||||
instance At Env where at j = #names . at j
|
|
||||||
|
|
||||||
err :: Jalmot :> es => Text -> Eff es a
|
|
||||||
err = throwError . EvalError
|
|
||||||
|
|
||||||
run :: S.DatumGrammar a -> Datum -> Maybe a
|
|
||||||
run g = preview #_Right . runPureEff . runJalmot . S.fromDatum g
|
|
||||||
|
|
||||||
parsePrim :: S.DatumIso e => Name -> Datum -> Maybe (Prim e)
|
|
||||||
parsePrim primname d = case primname of
|
|
||||||
"let" -> PrimLet <$> run prim_let d
|
|
||||||
"let-syntax" -> PrimLetSyntax <$> run prim_let_syntax d
|
|
||||||
"lambda" -> PrimLambda <$> run prim_lambda d
|
|
||||||
"if" -> PrimIf <$> run prim_if d
|
|
||||||
|
|
||||||
emit :: (Monoid m, State m :> es) => m -> Eff es ()
|
|
||||||
emit x = modify (<> x)
|
|
||||||
|
|
||||||
binding :: Name -> HashSet Scope -> Bind -> SymTable
|
|
||||||
binding sym scopes = MkSymTable . H.singleton sym . H.singleton scopes
|
|
||||||
|
|
||||||
envOfVar :: Bind -> Env
|
|
||||||
envOfVar = MkEnv . flip H.singleton DenotVar
|
|
||||||
|
|
||||||
gensymLexical :: GenSym :> es => Name -> Eff es Bind
|
|
||||||
gensymLexical symbol = do
|
|
||||||
identity <- gensym
|
|
||||||
pure $ BindLexical {symbol,identity}
|
|
||||||
|
|
||||||
lookupSymbol
|
|
||||||
:: forall es. (State SymTable :> es, Jalmot :> es)
|
|
||||||
=> Env -> HashSet Scope -> Name -> Eff es (Bind, Denot)
|
|
||||||
lookupSymbol g scopes x = do
|
|
||||||
ss <- fmap fold . preuse @SymTable @(Eff es) $ ix x
|
|
||||||
case nearest (MkKey x scopes) (H.keys ss) of
|
|
||||||
[] -> err [i|심벌 #{x}는 정의되지 않다|]
|
|
||||||
(_:_:_) -> err [i|심벌 #{x}는 모호하다|]
|
|
||||||
[s] | Just b <- ss ^? ix s
|
|
||||||
, Just d <- g ^? ix b -> pure (b,d)
|
|
||||||
| otherwise -> error "unreachable"
|
|
||||||
|
|
||||||
{- | Given a 'Key' and a collection of 'ScopeSet's, filter that
|
|
||||||
collection down to the largest subsets of the 'Key'\'s scope set.
|
|
||||||
This is our analogue of the lexical scoping rule which chooses the
|
|
||||||
"nearest" binding of a variable when shadowing occurs.
|
|
||||||
|
|
||||||
- If no qualifying subsets are found, the name is not in scope.
|
|
||||||
- If one subset is found, we're on the happy path!
|
|
||||||
- If more than one subset is found, we're on the rarest and
|
|
||||||
saddest path: the reference is ambiguous.
|
|
||||||
|
|
||||||
* Examples
|
|
||||||
|
|
||||||
> (let ((x {A} 123))
|
|
||||||
> (λ (x {A,B})
|
|
||||||
> (let ((y {A,B,C} 456))
|
|
||||||
> x {A,B,C})))
|
|
||||||
|
|
||||||
>>> :seti -XOverloadedLists
|
|
||||||
>>> :{
|
|
||||||
nearest
|
|
||||||
(MkKey "x" [MkScope 0, MkScope 1, MkScope 2])
|
|
||||||
[ [MkScope 0]
|
|
||||||
, [MkScope 0, MkScope 1] ]
|
|
||||||
:}
|
|
||||||
-}
|
|
||||||
nearest :: (Traversable f, Filterable f) => Key -> f ScopeSet -> List ScopeSet
|
|
||||||
nearest (MkKey symbol scopes) =
|
|
||||||
mapMaybe (\x ->
|
|
||||||
if x `HS.isSubsetOf` scopes
|
|
||||||
then Just . Down $ Arg (length x) x
|
|
||||||
else Nothing)
|
|
||||||
-- 나쁨. 안 좋다. 안 좋아하야.
|
|
||||||
>>> Data.Foldable.toList >>> sort
|
|
||||||
>>> foldr (\cases
|
|
||||||
x [] -> [x]
|
|
||||||
x acc@(y:_) -> case x `compare` y of
|
|
||||||
LT -> acc
|
|
||||||
EQ -> x:acc
|
|
||||||
GT -> [x])
|
|
||||||
[]
|
|
||||||
>>> fmap (\(Down (Arg _ x)) -> x)
|
|
||||||
|
|
||||||
expand
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Datum -> Eff es CommandOrDef
|
|
||||||
|
|
||||||
expand g scopes datum@(List (Symbol s : es)) = do
|
|
||||||
let s' = MkName s
|
|
||||||
(_,denot) <- lookupSymbol g scopes s'
|
|
||||||
case denot of
|
|
||||||
DenotVar -> Command . ExpApply (ExpVar s')
|
|
||||||
<$> traverse (expandAsExp g scopes) es
|
|
||||||
DenotPrim x -> expandPrim g scopes x datum
|
|
||||||
DenotMacro trans -> expandMacro g scopes trans datum
|
|
||||||
|
|
||||||
-- 신기하지 않은 경우들
|
|
||||||
expand g scopes datum = case datum of
|
|
||||||
List (x:xs) -> Command <$> (ExpApply <$> go x <*> traverse go xs)
|
|
||||||
Symbol s -> pure . Command . ExpVar . MkName $ s
|
|
||||||
Boolean b -> lit $ LitBool b
|
|
||||||
Number (Sci.floatingOrInteger -> Right n) -> lit $ LitInt n
|
|
||||||
where
|
|
||||||
go = expandAsExp g scopes
|
|
||||||
lit = pure . Command . ExpLit
|
|
||||||
|
|
||||||
expandMacro
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Trans -> Datum -> Eff es CommandOrDef
|
|
||||||
expandMacro g scopes trans datum = _
|
|
||||||
|
|
||||||
expandPrim
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Name -> Datum -> Eff es CommandOrDef
|
|
||||||
expandPrim g scopes primName datum
|
|
||||||
| Just prim <- parsePrim @Datum primName datum
|
|
||||||
= case prim of
|
|
||||||
PrimLetSyntax (MkPrimLetSyntax bs [body]) -> do
|
|
||||||
scopes' <- flip HS.insert scopes <$> gensym
|
|
||||||
(rhss,g') <- (_2 %~ (g<>) . fold) . Prelude.unzip <$> for bs \(x,trans) ->
|
|
||||||
expandAsExp g scopes trans >>= \case
|
|
||||||
ExpSyntaxRules trans' ->
|
|
||||||
(trans',) <$> bindLexical scopes' x (DenotMacro trans')
|
|
||||||
e -> err [i|syntax-rules를 원하는데 이것 받는다: #{e}|]
|
|
||||||
Command <$> (ExpLetSyntax
|
|
||||||
(zip (bs ^.. each . _1) rhss)
|
|
||||||
<$> expandAsExp g' scopes' body
|
|
||||||
)
|
|
||||||
PrimLet (MkPrimLet Nothing bs [body]) -> do
|
|
||||||
scopes' <- flip HS.insert scopes <$> gensym
|
|
||||||
g' <- (g<>) . fold <$> for (bs ^.. each . _1) \x ->
|
|
||||||
bindLexical scopes' x DenotVar
|
|
||||||
Command <$> (ExpLet
|
|
||||||
<$> traverseOf (each . _2) (expandAsExp g scopes) bs
|
|
||||||
<*> expandAsExp g scopes' body)
|
|
||||||
PrimLambda (MkPrimLambda (FormalsFixed xs) [body]) -> do
|
|
||||||
scopes' <- flip HS.insert scopes <$> gensym
|
|
||||||
g' <- (g<>) . fold <$> for xs \x -> bindLexical scopes' x DenotVar
|
|
||||||
Command . ExpLambda xs <$> expandAsExp g' scopes' body
|
|
||||||
PrimIf (MkPrimIf c t f) ->
|
|
||||||
Command <$> (ExpIf
|
|
||||||
<$> expandAsExp g scopes c
|
|
||||||
<*> expandAsExp g scopes t
|
|
||||||
<*> traverse (expandAsExp g scopes) f
|
|
||||||
)
|
|
||||||
| otherwise = err [i|prim #{primName}에 잘못한 신택스: #{datum}|]
|
|
||||||
|
|
||||||
expandAsExp
|
|
||||||
:: (Expand es, Jalmot :> es)
|
|
||||||
=> Env -> ScopeSet -> Datum -> Eff es Exp
|
|
||||||
expandAsExp g scopes d = expand g scopes d >>= intoExp
|
|
||||||
|
|
||||||
bindLexical :: Expand es => ScopeSet -> Name -> Denot -> Eff es Env
|
|
||||||
bindLexical scopes symbol denot = do
|
|
||||||
identity <- gensym
|
|
||||||
let bind = BindLexical {symbol,identity}
|
|
||||||
modify (<> binding symbol scopes bind)
|
|
||||||
pure $ MkEnv (H.singleton bind denot)
|
|
||||||
|
|
||||||
intoExp :: Jalmot :> es => CommandOrDef -> Eff es Exp
|
|
||||||
intoExp (Command e) = pure e
|
|
||||||
intoExp (Begin es) = traverse intoExp es >>= \case
|
|
||||||
[] -> err "begin expression은 빔"
|
|
||||||
(x:xs) -> pure . ExpBegin $ x NE.:| xs
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
trans_when :: Trans
|
|
||||||
trans_when = MkTrans
|
|
||||||
{ ellipsis = "..."
|
|
||||||
, keywords = []
|
|
||||||
, rules =
|
|
||||||
[ MkRule
|
|
||||||
(PatList
|
|
||||||
[ PatVar "when"
|
|
||||||
, PatVar "test"
|
|
||||||
, PatVar "body" ]
|
|
||||||
(Just [])
|
|
||||||
Nothing)
|
|
||||||
(TemList
|
|
||||||
[ El $ TemVar "if"
|
|
||||||
, El $ TemVar "test"
|
|
||||||
, El $ TemList
|
|
||||||
[ El $ TemVar "begin"
|
|
||||||
, Ellipsis $ TemVar "body"
|
|
||||||
]
|
|
||||||
Nothing
|
|
||||||
]
|
|
||||||
Nothing)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
trans_and :: Trans
|
|
||||||
trans_and = MkTrans
|
|
||||||
{ ellipsis = "..."
|
|
||||||
, keywords = []
|
|
||||||
, rules =
|
|
||||||
[ MkRule
|
|
||||||
(PatList
|
|
||||||
[PatVar "and"]
|
|
||||||
Nothing
|
|
||||||
Nothing)
|
|
||||||
(TemLit (LitBool True))
|
|
||||||
, MkRule
|
|
||||||
(PatList
|
|
||||||
[PatVar "and", PatVar "x"]
|
|
||||||
Nothing
|
|
||||||
Nothing)
|
|
||||||
(TemVar "x")
|
|
||||||
, MkRule
|
|
||||||
(PatList
|
|
||||||
[PatVar "and", PatVar "x", PatVar "y"]
|
|
||||||
(Just [])
|
|
||||||
Nothing)
|
|
||||||
(TemList
|
|
||||||
[ El (TemVar "if")
|
|
||||||
, El (TemVar "x")
|
|
||||||
, El (TemList
|
|
||||||
[ El (TemVar "and")
|
|
||||||
, Ellipsis (TemVar "y")
|
|
||||||
]
|
|
||||||
Nothing)
|
|
||||||
, El . TemLit . LitBool $ False
|
|
||||||
]
|
|
||||||
Nothing)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
+38
-306
@@ -10,22 +10,16 @@
|
|||||||
{-# LANGUAGE OrPatterns #-}
|
{-# LANGUAGE OrPatterns #-}
|
||||||
{-# LANGUAGE PatternSynonyms #-}
|
{-# LANGUAGE PatternSynonyms #-}
|
||||||
{-# LANGUAGE DeriveAnyClass #-}
|
{-# LANGUAGE DeriveAnyClass #-}
|
||||||
{-# LANGUAGE ViewPatterns #-}
|
|
||||||
module Gyehoek.Scheme.Syntax
|
module Gyehoek.Scheme.Syntax
|
||||||
( Name(..)
|
( Name(..)
|
||||||
, Builtin(..)
|
, Prim(..)
|
||||||
, Lit(..)
|
, Lit(..)
|
||||||
, Def(..)
|
, Def(..)
|
||||||
, Exp(..)
|
, Exp(..)
|
||||||
, ExpF(..)
|
, ExpF(..)
|
||||||
, Program(..)
|
, Program(..)
|
||||||
, CommandOrDef(..)
|
, CommandOrDef(..)
|
||||||
, Trans(..)
|
, primDatumIso
|
||||||
, Rule(..)
|
|
||||||
, Pat(..)
|
|
||||||
, Tem(..)
|
|
||||||
, El(..)
|
|
||||||
, builtinDatumIso
|
|
||||||
, free
|
, free
|
||||||
, subst
|
, subst
|
||||||
, getName
|
, getName
|
||||||
@@ -59,8 +53,6 @@ import Gyehoek.Sexp.Grammar qualified as Sexp
|
|||||||
import Gyehoek.Sexp.Grammar qualified as S
|
import Gyehoek.Sexp.Grammar qualified as S
|
||||||
import Gyehoek.Sexp.Grammar (DatumIso, G, DataIso, (:-)((:-)))
|
import Gyehoek.Sexp.Grammar (DatumIso, G, DataIso, (:-)((:-)))
|
||||||
import Gyehoek.Prelude
|
import Gyehoek.Prelude
|
||||||
import Control.Lens.Extras (is)
|
|
||||||
import qualified Data.Scientific as Sci
|
|
||||||
|
|
||||||
|
|
||||||
newtype Name = MkName { inner :: Text }
|
newtype Name = MkName { inner :: Text }
|
||||||
@@ -74,36 +66,34 @@ instance Prefixed Name where
|
|||||||
getName :: Name -> Text
|
getName :: Name -> Text
|
||||||
getName (MkName x) = x
|
getName (MkName x) = x
|
||||||
|
|
||||||
data Builtin e
|
data Prim e
|
||||||
= BuiltinAdd e e
|
= PrimAdd e e
|
||||||
| BuiltinSub e e
|
| PrimSub e e
|
||||||
| BuiltinMul e e
|
| PrimMul e e
|
||||||
| BuiltinDiv e e
|
| PrimDiv e e
|
||||||
| BuiltinCons e e
|
| PrimCons e e
|
||||||
| BuiltinCar e
|
| PrimCar e
|
||||||
| BuiltinCdr e
|
| PrimCdr e
|
||||||
| BuiltinImmediateP e
|
| PrimImmediateP e
|
||||||
| BuiltinConsP e
|
| PrimConsP e
|
||||||
| BuiltinIntegerP e
|
| PrimIntegerP e
|
||||||
| BuiltinWrite e
|
| PrimWrite e
|
||||||
| BuiltinZeroP e
|
| PrimZeroP e
|
||||||
| BuiltinNewline
|
| PrimNewline
|
||||||
| BuiltinMakeClosure { code :: e, env :: List e }
|
| PrimMakeClosure { code :: e, env :: List e }
|
||||||
| BuiltinMakeSharedClosure { codes :: List e, env :: List e }
|
| PrimMakeSharedClosure { codes :: List e, env :: List e }
|
||||||
| BuiltinGetEnv
|
| PrimGetEnv
|
||||||
| BuiltinEnv
|
| PrimEnv
|
||||||
| BuiltinEnvRef Int
|
| PrimEnvRef Int
|
||||||
| BuiltinCallCC e
|
| PrimCallCC e
|
||||||
| BuiltinCaptureCC
|
| PrimCaptureCC
|
||||||
| BuiltinInvokeCC e (List e)
|
| PrimInvokeCC e (List e)
|
||||||
| BuiltinValues (List e)
|
| PrimValues (List e)
|
||||||
| BuiltinCallWithValues e e
|
| PrimCallWithValues e e
|
||||||
| BuiltinPairP e
|
|
||||||
| BuiltinList (List e)
|
|
||||||
deriving stock (Show, Generic, Functor, Foldable, Traversable, Data, Eq)
|
deriving stock (Show, Generic, Functor, Foldable, Traversable, Data, Eq)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
instance Each (Builtin e) (Builtin e') e e'
|
instance Each (Prim e) (Prim e') e e'
|
||||||
|
|
||||||
data Lit
|
data Lit
|
||||||
= LitInt Int
|
= LitInt Int
|
||||||
@@ -118,79 +108,19 @@ data Def
|
|||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
data Formals a
|
|
||||||
= FormalsFixed (List a)
|
|
||||||
| FormalsVar (List a) a
|
|
||||||
deriving stock (Show, Generic, Data, Functor, Foldable, Traversable)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data Exp
|
data Exp
|
||||||
= ExpLet (List (Name, Exp)) Exp
|
= ExpLet (List (Name, Exp)) Exp
|
||||||
| ExpLetSyntax (List (Name, Trans)) Exp
|
|
||||||
| ExpLetRec (List (Name, Exp)) Exp
|
| ExpLetRec (List (Name, Exp)) Exp
|
||||||
| ExpBuiltin (Builtin Exp)
|
| ExpPrim (Prim Exp)
|
||||||
| ExpBegin (NonEmpty Exp)
|
| ExpBegin (NonEmpty Exp)
|
||||||
| ExpIf Exp Exp (Maybe Exp)
|
| ExpIf Exp Exp Exp
|
||||||
| ExpLit Lit
|
| ExpLit Lit
|
||||||
| ExpLambda (List Name) Exp
|
| ExpLambda (List Name) Exp
|
||||||
| ExpVar Name
|
| ExpVar Name
|
||||||
| ExpSyntaxRules Trans
|
|
||||||
| ExpApply Exp (List Exp)
|
| ExpApply Exp (List Exp)
|
||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
data Trans = MkTrans
|
|
||||||
{ ellipsis :: Name
|
|
||||||
, keywords :: List Name
|
|
||||||
, rules :: List Rule
|
|
||||||
}
|
|
||||||
deriving stock (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data Rule = MkRule
|
|
||||||
{ rhs :: Pat
|
|
||||||
, lhs :: Tem
|
|
||||||
}
|
|
||||||
deriving (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data Pat
|
|
||||||
= PatWildcard
|
|
||||||
| PatVar Name
|
|
||||||
| PatList
|
|
||||||
{ init :: List Pat
|
|
||||||
, ellipsis :: Maybe (List Pat)
|
|
||||||
, tail :: Maybe Pat
|
|
||||||
}
|
|
||||||
-- | PatVec
|
|
||||||
-- { init :: List Pat
|
|
||||||
-- , ellipsis :: Maybe (List Pat)
|
|
||||||
-- }
|
|
||||||
deriving (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data Tem
|
|
||||||
-- | @(⟨element⟩ …)@
|
|
||||||
-- @(⟨element⟩ ⟨element⟩ … . ⟨element⟩)@
|
|
||||||
= TemList
|
|
||||||
{ init :: List El
|
|
||||||
, tail :: Maybe Tem
|
|
||||||
}
|
|
||||||
-- | @(⟨ellipsis⟩ ⟨template⟩)@
|
|
||||||
| TemTrail Tem
|
|
||||||
| TemLit Lit
|
|
||||||
| TemVar Name
|
|
||||||
deriving (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data El
|
|
||||||
-- | @⟨template⟩ ⟨ellipsis⟩@
|
|
||||||
= Ellipsis Tem
|
|
||||||
-- | @⟨template⟩@
|
|
||||||
| El Tem
|
|
||||||
deriving (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data CommandOrDef
|
data CommandOrDef
|
||||||
= Command Exp
|
= Command Exp
|
||||||
| Definition Def
|
| Definition Def
|
||||||
@@ -198,37 +128,8 @@ data CommandOrDef
|
|||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
newtype LibName = MkLibName { inner :: NonEmpty Name }
|
newtype Program = MkProgram
|
||||||
deriving stock (Show, Generic, Data)
|
{ commandsAndDefs :: List CommandOrDef
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data ImportSet
|
|
||||||
= ImportLib LibName
|
|
||||||
| ImportOnly ImportSet (NonEmpty Name)
|
|
||||||
| ImportExcept ImportSet (NonEmpty Name)
|
|
||||||
| ImportPrefix ImportSet Name
|
|
||||||
| ImportRename ImportSet (NonEmpty (Name, Name))
|
|
||||||
deriving stock (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
newtype ImportDecl = MkImportDecl (NonEmpty ImportSet)
|
|
||||||
deriving stock (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data LibDecl
|
|
||||||
deriving stock (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data Lib = MkLib
|
|
||||||
{ name :: LibName
|
|
||||||
, decls :: List LibDecl
|
|
||||||
}
|
|
||||||
deriving stock (Show, Generic, Data)
|
|
||||||
deriving anyclass (NFData)
|
|
||||||
|
|
||||||
data Program = MkProgram
|
|
||||||
{ imports :: List ImportDecl
|
|
||||||
, commandsAndDefs :: List CommandOrDef
|
|
||||||
}
|
}
|
||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
@@ -251,10 +152,10 @@ instance DatumIso Name where
|
|||||||
>>> S.symbol
|
>>> S.symbol
|
||||||
>>> S.iso coerce coerce
|
>>> S.iso coerce coerce
|
||||||
|
|
||||||
builtinDatumIso
|
primDatumIso
|
||||||
:: (Text -> Text)
|
:: (Text -> Text)
|
||||||
-> S.DatumGrammar a -> S.DatumGrammar (Builtin a)
|
-> S.DatumGrammar a -> S.DatumGrammar (Prim a)
|
||||||
builtinDatumIso namefn a = S.match
|
primDatumIso namefn a = S.match
|
||||||
$ S.With (. ht2 "+")
|
$ S.With (. ht2 "+")
|
||||||
$ S.With (. ht2 "-")
|
$ S.With (. ht2 "-")
|
||||||
$ S.With (. ht2 "*")
|
$ S.With (. ht2 "*")
|
||||||
@@ -280,8 +181,6 @@ builtinDatumIso namefn a = S.match
|
|||||||
$ S.With (. ht1' "invoke/cc")
|
$ S.With (. ht1' "invoke/cc")
|
||||||
$ S.With (. ht0' "values")
|
$ S.With (. ht0' "values")
|
||||||
$ S.With (. ht2 "call-with-values")
|
$ S.With (. ht2 "call-with-values")
|
||||||
$ S.With (. ht1 "pair?")
|
|
||||||
$ S.With (. ht0' "list")
|
|
||||||
$ S.End
|
$ S.End
|
||||||
where
|
where
|
||||||
idn = S.el . S.sym . namefn
|
idn = S.el . S.sym . namefn
|
||||||
@@ -291,8 +190,8 @@ builtinDatumIso namefn a = S.match
|
|||||||
ht1' s = S.headTagged1' (namefn s) a a
|
ht1' s = S.headTagged1' (namefn s) a a
|
||||||
ht0' s = S.headTagged0' (namefn s) a
|
ht0' s = S.headTagged0' (namefn s) a
|
||||||
|
|
||||||
instance DatumIso a => DatumIso (Builtin a) where
|
instance DatumIso a => DatumIso (Prim a) where
|
||||||
datumIso = builtinDatumIso id S.datumIso
|
datumIso = primDatumIso id S.datumIso
|
||||||
|
|
||||||
instance DatumIso Lit where
|
instance DatumIso Lit where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
@@ -313,42 +212,20 @@ instance DatumIso Def where
|
|||||||
>>> S.el args >>> S.rest S.datumIso
|
>>> S.el args >>> S.rest S.datumIso
|
||||||
args = S.list $ S.el S.datumIso >>> S.rest S.datumIso
|
args = S.list $ S.el S.datumIso >>> S.rest S.datumIso
|
||||||
|
|
||||||
instance DatumIso Trans where
|
|
||||||
datumIso = S.with \g ->
|
|
||||||
S.listWithIndentation
|
|
||||||
(S.NSpecial 1)
|
|
||||||
( S.el (S.symBuiltin "syntax-rules")
|
|
||||||
>>> S.Iso (\(ctx:-t) -> ctx:-"...":-t) (\(ctx:-_:-t) -> ctx:-t)
|
|
||||||
>>> S.el (S.list $ S.rest (S.datumIso @Name))
|
|
||||||
>>> S.rest (S.datumIso @Rule)
|
|
||||||
)
|
|
||||||
>>> g
|
|
||||||
|
|
||||||
instance DatumIso Exp where
|
instance DatumIso Exp where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (. S.letLike "let" S.datumIso S.datumIso S.datumIso)
|
$ S.With (. S.letLike "let" S.datumIso S.datumIso S.datumIso)
|
||||||
$ S.With (letsyntax >>>)
|
|
||||||
$ S.With (. S.letLike "letrec" S.datumIso S.datumIso S.datumIso)
|
$ S.With (. S.letLike "letrec" S.datumIso S.datumIso S.datumIso)
|
||||||
$ S.With (. S.datumIso)
|
$ S.With (. S.datumIso)
|
||||||
$ S.With (. begin)
|
$ S.With (. begin)
|
||||||
$ S.With (. if_)
|
$ S.With (. S.ifLike "if" S.datumIso S.datumIso S.datumIso)
|
||||||
$ S.With (. S.datumIso)
|
$ S.With (. S.datumIso)
|
||||||
$ S.With (. lam)
|
$ S.With (. lam)
|
||||||
$ S.With (. S.datumIso)
|
$ S.With (. S.datumIso)
|
||||||
$ S.With (. S.datumIso)
|
|
||||||
$ S.With (\app -> app . S.list (S.el S.datumIso >>> S.rest S.datumIso))
|
$ S.With (\app -> app . S.list (S.el S.datumIso >>> S.rest S.datumIso))
|
||||||
$ S.End
|
$ S.End
|
||||||
where
|
where
|
||||||
letsyntax = S.listWithIndentation (S.NSpecial 1) $
|
|
||||||
S.el (S.symBuiltin "let-syntax")
|
|
||||||
>>> S.el (S.list $ S.rest $ S.datumIso)
|
|
||||||
>>> S.el S.datumIso
|
|
||||||
lam = S.lambdaLike S.lambdaKeyword S.datumIso (S.el S.datumIso)
|
lam = S.lambdaLike S.lambdaKeyword S.datumIso (S.el S.datumIso)
|
||||||
if_ = S.ifLike "if" S.datumIso S.datumIso $ S.datumIso @Exp >>> S.iso
|
|
||||||
Just
|
|
||||||
\case
|
|
||||||
Just x -> x
|
|
||||||
Nothing -> error "안 괜찮다ㅠㅠ"
|
|
||||||
begin :: forall t. G (S.Datum :- t) (NonEmpty Exp :- t)
|
begin :: forall t. G (S.Datum :- t) (NonEmpty Exp :- t)
|
||||||
begin = S.beginLike "begin" $
|
begin = S.beginLike "begin" $
|
||||||
S.el (S.datumIso @Exp) >>> S.rest (S.datumIso @Exp)
|
S.el (S.datumIso @Exp) >>> S.rest (S.datumIso @Exp)
|
||||||
@@ -356,101 +233,6 @@ instance DatumIso Exp where
|
|||||||
(\(xs:-x:-t) -> (x:|xs):-t)
|
(\(xs:-x:-t) -> (x:|xs):-t)
|
||||||
(\((x:|xs):-t) -> xs:-x:-t))
|
(\((x:|xs):-t) -> xs:-x:-t))
|
||||||
|
|
||||||
instance S.DatumIso Rule where
|
|
||||||
datumIso = S.with \g ->
|
|
||||||
S.list (S.el S.datumIso >>> S.el S.datumIso) >>> g
|
|
||||||
|
|
||||||
instance S.DatumIso Pat where
|
|
||||||
datumIso = S.match
|
|
||||||
$ S.With (S.sym "_" >>>)
|
|
||||||
$ S.With (S.datumIso >>>)
|
|
||||||
$ S.With (lst >>>)
|
|
||||||
$ S.End
|
|
||||||
where
|
|
||||||
lst :: S.G (S.Datum :- t) (Maybe Pat :- Maybe (List Pat) :- List Pat :- t)
|
|
||||||
lst = S.coproduct
|
|
||||||
[ S.list $
|
|
||||||
ellipsis
|
|
||||||
>>> S.onTail (S.push Nothing (is _Nothing) (const mempty))
|
|
||||||
, S.dottedList
|
|
||||||
ellipsis
|
|
||||||
( S.datumIso @Pat
|
|
||||||
>>> S.partialIso Just (maybe (Left mempty) Right) )
|
|
||||||
]
|
|
||||||
ellipsis =
|
|
||||||
S.restData split
|
|
||||||
>>> S.onTail
|
|
||||||
( S.onHead (S.traversed . S.traversed . S.sealed $
|
|
||||||
S.datumIso @Pat)
|
|
||||||
>>> S.onTail (S.onHead . S.traversed . S.sealed $
|
|
||||||
S.datumIso @Pat)
|
|
||||||
)
|
|
||||||
split
|
|
||||||
:: forall t. S.G (List S.Datum :- t)
|
|
||||||
(Maybe (List S.Datum) :- List S.Datum :- t)
|
|
||||||
split = S.Iso
|
|
||||||
(\(ps0:-t) ->
|
|
||||||
let (ps,ell) = splitEllipsis ps0
|
|
||||||
in ell :- ps :- t)
|
|
||||||
(\(ell:-ps:-t) -> (ps ++ foldMap ([S.Symbol "..."]++) ell) :- t)
|
|
||||||
|
|
||||||
splitEllipsis :: List S.Datum -> (List S.Datum, Maybe (List S.Datum))
|
|
||||||
splitEllipsis [] = ([], Nothing)
|
|
||||||
splitEllipsis (S.Symbol "..." : xs) = ([], Just xs)
|
|
||||||
splitEllipsis (x:xs) = splitEllipsis xs & _1 %~ (x:)
|
|
||||||
|
|
||||||
instance S.DataIso El where
|
|
||||||
dataIso = S.match
|
|
||||||
$ S.With (ellipsis >>>)
|
|
||||||
$ S.With (noellipsis >>>)
|
|
||||||
$ S.End
|
|
||||||
where
|
|
||||||
ellipsis = S.recontextualise $
|
|
||||||
S.el (S.datumIso @Tem) >>> S.el (S.sym "...")
|
|
||||||
noellipsis = S.recontextualise $ S.el (S.datumIso @Tem)
|
|
||||||
|
|
||||||
instance S.DatumIso Tem where
|
|
||||||
datumIso = S.match
|
|
||||||
$ S.With (lst >>>)
|
|
||||||
$ S.With (trail >>>)
|
|
||||||
$ S.With (S.datumIso @Lit >>>)
|
|
||||||
$ S.With (S.datumIso @Name >>>)
|
|
||||||
$ S.End
|
|
||||||
where
|
|
||||||
trail = S.list $ S.el (S.sym "...") >>> S.el (S.datumIso @Tem)
|
|
||||||
|
|
||||||
lst :: S.G (S.Datum :- t) (Maybe Tem :- List El :- t)
|
|
||||||
lst = S.coproduct
|
|
||||||
[ S.list els
|
|
||||||
>>> (S.push Nothing (is _Nothing) (const mempty))
|
|
||||||
, S.dottedList els $
|
|
||||||
S.datumIso @Tem
|
|
||||||
>>> S.partialIso Just (maybe (Left mempty) Right)
|
|
||||||
]
|
|
||||||
els :: S.G (S.ListContext :- t) (S.ListContext :- List El :- t)
|
|
||||||
els =
|
|
||||||
S.iso
|
|
||||||
(\(S.MkListContext ds) -> affixEllipses ds)
|
|
||||||
(S.MkListContext . foldMap \(d,b) ->
|
|
||||||
d : if b then [S.Symbol "..."] else [])
|
|
||||||
>>> S.onHead (S.traversed . S.sealed $
|
|
||||||
S.flipped S.pair
|
|
||||||
>>> S.onTail (S.datumIso @Tem)
|
|
||||||
>>> S.pair
|
|
||||||
>>> S.iso
|
|
||||||
(\(t,b) -> if b then Ellipsis t else El t)
|
|
||||||
(\case
|
|
||||||
Ellipsis t -> (t,True)
|
|
||||||
El t -> (t,False)))
|
|
||||||
>>> S.push (S.MkListContext [])
|
|
||||||
(\(S.MkListContext xs) -> null xs)
|
|
||||||
(const mempty)
|
|
||||||
affixEllipses :: List S.Datum -> List (S.Datum, Bool)
|
|
||||||
affixEllipses (x : S.Symbol "..." : xs) = (x,True) : affixEllipses xs
|
|
||||||
affixEllipses (x : xs) = (x,False) : affixEllipses xs
|
|
||||||
affixEllipses [] = []
|
|
||||||
|
|
||||||
|
|
||||||
instance DatumIso CommandOrDef where
|
instance DatumIso CommandOrDef where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (\_Command -> _Command . S.datumIso)
|
$ S.With (\_Command -> _Command . S.datumIso)
|
||||||
@@ -458,58 +240,8 @@ instance DatumIso CommandOrDef where
|
|||||||
$ S.With (\_Begin -> _Begin . S.beginLike "begin" (S.rest S.datumIso))
|
$ S.With (\_Begin -> _Begin . S.beginLike "begin" (S.rest S.datumIso))
|
||||||
$ S.End
|
$ S.End
|
||||||
|
|
||||||
instance DatumIso LibName where
|
|
||||||
datumIso = S.with \g ->
|
|
||||||
S.list (S.restData $ S.nonEmptyData comp)
|
|
||||||
>>> g
|
|
||||||
where
|
|
||||||
comp = S.partialOsi
|
|
||||||
(\case
|
|
||||||
S.Symbol s -> Right $ MkName s
|
|
||||||
S.Number (Sci.floatingOrInteger @Double @Int -> Right n)
|
|
||||||
| n > 0 -> Right $ MkName [i|#{n}|]
|
|
||||||
_ -> Left $ S.expected "library name part"
|
|
||||||
)
|
|
||||||
\(MkName s) -> S.Symbol s
|
|
||||||
|
|
||||||
instance DatumIso ImportSet where
|
|
||||||
datumIso = S.match
|
|
||||||
$ S.With (S.datumIso @LibName >>>)
|
|
||||||
$ S.With (imp "only" >>>)
|
|
||||||
$ S.With (imp "except" >>>)
|
|
||||||
$ S.With (imp' "prefix" >>>)
|
|
||||||
$ S.With (imp "rename" >>>)
|
|
||||||
$ S.End
|
|
||||||
where
|
|
||||||
imp s = S.list $ S.el (S.symBuiltin s)
|
|
||||||
>>> S.el S.datumIso >>> S.restData S.dataIso
|
|
||||||
imp' s = S.list $
|
|
||||||
S.el (S.symBuiltin s)
|
|
||||||
>>> S.el S.datumIso
|
|
||||||
>>> S.el S.datumIso
|
|
||||||
|
|
||||||
instance DatumIso ImportDecl where
|
|
||||||
datumIso = S.with \decl ->
|
|
||||||
S.list (S.el (S.symBuiltin "import") >>> S.restData S.dataIso)
|
|
||||||
>>> decl
|
|
||||||
|
|
||||||
instance DataIso Program where
|
instance DataIso Program where
|
||||||
dataIso = S.with \g ->
|
dataIso = S.dataIso @(List CommandOrDef) >>> S.iso coerce coerce
|
||||||
splitG
|
|
||||||
>>> S.onHead (S.sealed S.dataIso)
|
|
||||||
>>> S.onTail (S.onHead . S.sealed $ S.dataIso)
|
|
||||||
>>> g
|
|
||||||
where
|
|
||||||
isImport = \case
|
|
||||||
S.List (S.Symbol "import" : _) -> True
|
|
||||||
_ -> False
|
|
||||||
splitG :: G (List S.Datum :- t) (List S.Datum :- List S.Datum :- t)
|
|
||||||
splitG = S.Iso
|
|
||||||
(\(xs:-t) ->
|
|
||||||
let (ys,zs) = span isImport xs
|
|
||||||
in zs :- ys :- t
|
|
||||||
)
|
|
||||||
\(zs:-ys:-t) -> (ys ++ zs) :- t
|
|
||||||
|
|
||||||
|
|
||||||
-- utilities
|
-- utilities
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ module Gyehoek.Sexp.Grammar
|
|||||||
, encodeDataTest
|
, encodeDataTest
|
||||||
, encodeDataTestColour
|
, encodeDataTestColour
|
||||||
, encodeOrShow'
|
, encodeOrShow'
|
||||||
, encodeOrShowData'
|
|
||||||
, decodeDataWith
|
, decodeDataWith
|
||||||
, encodeDataWith'
|
, encodeDataWith'
|
||||||
, decodeTest
|
, decodeTest
|
||||||
@@ -29,8 +28,6 @@ module Gyehoek.Sexp.Grammar
|
|||||||
, fromDatumUnsafe
|
, fromDatumUnsafe
|
||||||
, Control.Category.id
|
, Control.Category.id
|
||||||
, fromDataUnsafe
|
, fromDataUnsafe
|
||||||
, writeDatum
|
|
||||||
, writeData
|
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
@@ -48,8 +45,6 @@ import qualified Control.Category
|
|||||||
import qualified Data.Vector as V
|
import qualified Data.Vector as V
|
||||||
import Data.String (IsString (fromString))
|
import Data.String (IsString (fromString))
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import System.Environment (lookupEnv)
|
|
||||||
import Data.Foldable (toList)
|
|
||||||
|
|
||||||
|
|
||||||
toDatum :: Jalmot :> es => DatumGrammar a -> a -> Eff es Datum
|
toDatum :: Jalmot :> es => DatumGrammar a -> a -> Eff es Datum
|
||||||
@@ -131,39 +126,6 @@ encodeOrShow' g x = fromString $
|
|||||||
Left _ -> show x
|
Left _ -> show x
|
||||||
Right t -> T.unpack t
|
Right t -> T.unpack t
|
||||||
|
|
||||||
encodeOrShow :: (IsString s, Show a) => DatumGrammar a -> a -> s
|
|
||||||
encodeOrShow g x = fromString $
|
|
||||||
case runPureEff . runJalmot . encodeWith g $ x of
|
|
||||||
Left _ -> show x
|
|
||||||
Right t -> T.unpack t
|
|
||||||
|
|
||||||
encodeOrShowData' :: (IsString s, Show a) => DataGrammar a -> a -> s
|
|
||||||
encodeOrShowData' g x = fromString $
|
|
||||||
case runPureEff . runJalmot . encodeDataWith' g $ x of
|
|
||||||
Left _ -> show x
|
|
||||||
Right t -> T.unpack t
|
|
||||||
|
|
||||||
encodeOrShowData :: (IsString s, Show a) => DataGrammar a -> a -> s
|
|
||||||
encodeOrShowData g x = fromString $
|
|
||||||
case runPureEff . runJalmot . encodeDataWith g $ x of
|
|
||||||
Left _ -> show x
|
|
||||||
Right t -> T.unpack t
|
|
||||||
|
|
||||||
useColour :: IO Bool
|
|
||||||
useColour = maybe True (const False) <$> lookupEnv "NO_COLOR"
|
|
||||||
|
|
||||||
writeDatum :: (Show a, DatumIso a, MonadIO m) => a -> m ()
|
|
||||||
writeDatum x = do
|
|
||||||
c <- liftIO useColour
|
|
||||||
let f = if c then encodeOrShow else encodeOrShow'
|
|
||||||
liftIO . TIO.putStrLn . f datumIso $ x
|
|
||||||
|
|
||||||
writeData :: (Show a, DataIso a, MonadIO m) => a -> m ()
|
|
||||||
writeData x = do
|
|
||||||
c <- liftIO useColour
|
|
||||||
let f = if c then encodeOrShowData else encodeOrShowData'
|
|
||||||
liftIO . TIO.putStrLn . f dataIso $ x
|
|
||||||
|
|
||||||
class DatumIso a where
|
class DatumIso a where
|
||||||
datumIso :: DatumGrammar a
|
datumIso :: DatumGrammar a
|
||||||
|
|
||||||
@@ -179,14 +141,6 @@ instance DatumIso Bool where datumIso = boolean
|
|||||||
|
|
||||||
instance DatumIso Int where datumIso = int
|
instance DatumIso Int where datumIso = int
|
||||||
|
|
||||||
instance DatumIso Natural where
|
|
||||||
datumIso = int
|
|
||||||
>>> partialOsi
|
|
||||||
(\x -> if x < 0
|
|
||||||
then Left $ expected "non-negative integer" <> unexpected [i|#{x}|]
|
|
||||||
else Right $ fromIntegral x)
|
|
||||||
fromIntegral
|
|
||||||
|
|
||||||
instance DatumIso Datum where datumIso = Control.Category.id
|
instance DatumIso Datum where datumIso = Control.Category.id
|
||||||
|
|
||||||
instance DatumIso a => DataIso (List a) where
|
instance DatumIso a => DataIso (List a) where
|
||||||
@@ -196,8 +150,5 @@ instance DatumIso a => DataIso (V.Vector a) where
|
|||||||
dataIso = iso fromList V.toList
|
dataIso = iso fromList V.toList
|
||||||
>>> (onHead . traversed . sealed $ datumIso @a)
|
>>> (onHead . traversed . sealed $ datumIso @a)
|
||||||
|
|
||||||
instance DatumIso a => DataIso (NonEmpty a) where
|
|
||||||
dataIso = nonEmptyData datumIso
|
|
||||||
|
|
||||||
instance (DatumIso a, DatumIso b) => DatumIso (a, b) where
|
instance (DatumIso a, DatumIso b) => DatumIso (a, b) where
|
||||||
datumIso = with \tup2 -> list (el datumIso >>> el datumIso) >>> tup2
|
datumIso = with \tup2 -> list (el datumIso >>> el datumIso) >>> tup2
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, el
|
, el
|
||||||
, rest
|
, rest
|
||||||
, restData
|
, restData
|
||||||
, nonEmptyData
|
|
||||||
, headTagged0'
|
, headTagged0'
|
||||||
, headTagged0
|
, headTagged0
|
||||||
, headTagged1'
|
, headTagged1'
|
||||||
@@ -33,8 +32,6 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, integer
|
, integer
|
||||||
, int
|
, int
|
||||||
, unreadable
|
, unreadable
|
||||||
-- ** symbols
|
|
||||||
, symBuiltin
|
|
||||||
-- * TODO: sort lol
|
-- * TODO: sort lol
|
||||||
, prismIso
|
, prismIso
|
||||||
, isoIso, decorate
|
, isoIso, decorate
|
||||||
@@ -44,7 +41,7 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, lambdaLike
|
, lambdaLike
|
||||||
, lambdaKeyword
|
, lambdaKeyword
|
||||||
, kappaKeyword
|
, kappaKeyword
|
||||||
, beginLike, headTagged2', dottedList, reifyContext, recontextualise, decontextualise
|
, beginLike, headTagged2', dottedList
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.InvertibleGrammar
|
import Data.InvertibleGrammar
|
||||||
@@ -52,7 +49,7 @@ import Data.InvertibleGrammar.Base
|
|||||||
import Data.InvertibleGrammar.Base as Re
|
import Data.InvertibleGrammar.Base as Re
|
||||||
( Grammar(..))
|
( Grammar(..))
|
||||||
import Data.InvertibleGrammar.Combinators
|
import Data.InvertibleGrammar.Combinators
|
||||||
import Gyehoek.Prelude hiding (flipped, traversed, iso, cons, coerced, Iso, Simple, simple)
|
import Gyehoek.Prelude hiding (iso, cons, coerced, Iso, Simple, simple)
|
||||||
import Gyehoek.Sexp.Syntax hiding (position)
|
import Gyehoek.Sexp.Syntax hiding (position)
|
||||||
import Gyehoek.Sexp.Print (printDatum')
|
import Gyehoek.Sexp.Print (printDatum')
|
||||||
import Data.Scientific (Scientific)
|
import Data.Scientific (Scientific)
|
||||||
@@ -60,7 +57,6 @@ import qualified Data.Scientific as Sci
|
|||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Control.Monad.RWS (modify)
|
import Control.Monad.RWS (modify)
|
||||||
import qualified Data.List.NonEmpty as NE
|
import qualified Data.List.NonEmpty as NE
|
||||||
import Data.Foldable (toList)
|
|
||||||
|
|
||||||
|
|
||||||
-- $setup
|
-- $setup
|
||||||
@@ -169,33 +165,6 @@ el
|
|||||||
-> G (ListContext :- t) (ListContext :- t')
|
-> G (ListContext :- t) (ListContext :- t')
|
||||||
el g = coerced (Flip cons >>> onTail g >>> Step)
|
el g = coerced (Flip cons >>> onTail g >>> Step)
|
||||||
|
|
||||||
reifyContext :: G (ListContext :- t) (List Datum :- t)
|
|
||||||
reifyContext = iso coerce coerce
|
|
||||||
|
|
||||||
decontextualise
|
|
||||||
:: G (List Datum :- t) (List Datum :- t')
|
|
||||||
-> G (ListContext :- t) t'
|
|
||||||
decontextualise g = reifyContext >>> g >>> end
|
|
||||||
where
|
|
||||||
end = Flip $ PartialIso
|
|
||||||
(\t -> [] :- t)
|
|
||||||
(\(lst :- t) ->
|
|
||||||
case lst of
|
|
||||||
[] -> Right t
|
|
||||||
d:_ -> Left $ unexpectedDatum d)
|
|
||||||
|
|
||||||
recontextualise
|
|
||||||
:: G (ListContext :- t) (ListContext :- t')
|
|
||||||
-> G (List Datum :- t) t'
|
|
||||||
recontextualise g = flipped reifyContext >>> g >>> end
|
|
||||||
where
|
|
||||||
end = Flip $ PartialIso
|
|
||||||
(\t -> MkListContext [] :- t)
|
|
||||||
(\(MkListContext lst :- t) ->
|
|
||||||
case lst of
|
|
||||||
[] -> Right t
|
|
||||||
d:_ -> Left $ unexpectedDatum d)
|
|
||||||
|
|
||||||
-- | matches the remainder of a list as repetition of a given
|
-- | matches the remainder of a list as repetition of a given
|
||||||
-- grammar.
|
-- grammar.
|
||||||
--
|
--
|
||||||
@@ -262,21 +231,13 @@ rest g =
|
|||||||
-- >>> encodeTest dataRestGrammar $ MkExample [1,2,3] "end"
|
-- >>> encodeTest dataRestGrammar $ MkExample [1,2,3] "end"
|
||||||
-- (1 2 3 end)
|
-- (1 2 3 end)
|
||||||
restData
|
restData
|
||||||
:: G (List Datum :- t) t'
|
:: G (List Datum :- t) (a :- t)
|
||||||
-> G (ListContext :- t) (ListContext :- t')
|
-> G (ListContext :- t) (ListContext :- a :- t)
|
||||||
restData g =
|
restData g =
|
||||||
iso coerce coerce
|
iso coerce coerce
|
||||||
>>> g
|
>>> g
|
||||||
>>> push (MkListContext []) (const True) mempty
|
>>> push (MkListContext []) (const True) mempty
|
||||||
|
|
||||||
nonEmptyData :: DatumGrammar a -> DataGrammar (NonEmpty a)
|
|
||||||
nonEmptyData g = partialOsi
|
|
||||||
(\case
|
|
||||||
[] -> Left $ expected "non-empty sequence"
|
|
||||||
x:xs -> Right $ x:|xs)
|
|
||||||
toList
|
|
||||||
>>> (onHead . traversed . sealed $ g)
|
|
||||||
|
|
||||||
snoced
|
snoced
|
||||||
:: Snoc s s a a
|
:: Snoc s s a a
|
||||||
=> Grammar p (s :- a :- t) (s :- t)
|
=> Grammar p (s :- a :- t) (s :- t)
|
||||||
@@ -428,7 +389,7 @@ letLike
|
|||||||
:: Text
|
:: Text
|
||||||
-> (forall t. G (Datum :- t) (a :- t))
|
-> (forall t. G (Datum :- t) (a :- t))
|
||||||
-> (forall t. G (Datum :- t) (b :- t))
|
-> (forall t. G (Datum :- t) (b :- t))
|
||||||
-> G (Datum :- List (a, b) :- t1) t2
|
-> G (Datum :- (List (a, b) :- t1)) t2
|
||||||
-> G (Datum :- t1) t2
|
-> G (Datum :- t1) t2
|
||||||
letLike kw name rhs e = listWithIndentation (NSpecial 1) $
|
letLike kw name rhs e = listWithIndentation (NSpecial 1) $
|
||||||
el (symBuiltin kw) >>> el bindings >>> el e
|
el (symBuiltin kw) >>> el bindings >>> el e
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ module Gyehoek.Sexp.Syntax
|
|||||||
, Ann(..)
|
, Ann(..)
|
||||||
, noAnn
|
, noAnn
|
||||||
, ann
|
, ann
|
||||||
, dat
|
|
||||||
, pattern List'
|
, pattern List'
|
||||||
, position
|
, position
|
||||||
, stripAnn
|
, stripAnn
|
||||||
@@ -153,22 +152,12 @@ deriveEq1 ''CompoundF
|
|||||||
deriveShow1 ''DatumF
|
deriveShow1 ''DatumF
|
||||||
deriveEq1 ''DatumF
|
deriveEq1 ''DatumF
|
||||||
|
|
||||||
|
|
||||||
--- optics
|
|
||||||
|
|
||||||
-- affine
|
|
||||||
cdr :: Traversal' Datum Datum
|
|
||||||
cdr k (a :< CompoundF (ListF ind xs)) = _
|
|
||||||
|
|
||||||
|
|
||||||
--- modification and extraction of annotations
|
--- modification and extraction of annotations
|
||||||
|
|
||||||
ann :: Lens' Datum Ann
|
ann :: Lens' Datum Ann
|
||||||
ann = _extract
|
ann = _extract
|
||||||
|
|
||||||
dat :: Lens' Datum (DatumF Datum)
|
|
||||||
dat = _unwrap
|
|
||||||
|
|
||||||
syntax :: Lens' Datum Syn
|
syntax :: Lens' Datum Syn
|
||||||
syntax = ann . #syntax
|
syntax = ann . #syntax
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
module Gyehoek.Stack.Lower
|
||||||
|
( lowerProgram
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Gyehoek.Stack.Syntax
|
||||||
|
import Gyehoek.Wasm qualified as Wasm
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import Gyehoek.Wasm (wat, watM)
|
||||||
|
|
||||||
|
|
||||||
|
lowerRoutine :: Routine -> Wasm.Function
|
||||||
|
lowerRoutine rt = _
|
||||||
|
|
||||||
|
lowerBlock :: Block -> Wasm.Expr
|
||||||
|
lowerBlock = _
|
||||||
|
|
||||||
|
lowerInstr :: Instr -> Wasm.Expr
|
||||||
|
lowerInstr = \case
|
||||||
|
-- PopCont ktail -> [wat|
|
||||||
|
|
||||||
|
-- |]
|
||||||
|
|
||||||
|
lowerProgram :: Program -> Eff es Wasm.Module
|
||||||
|
lowerProgram p = pure [watM|
|
||||||
|
(module
|
||||||
|
##{rs})
|
||||||
|
|]
|
||||||
|
where
|
||||||
|
rs = p ^.. #routines . each . to lowerRoutine
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
{-# LANGUAGE TemplateHaskellQuotes #-}
|
||||||
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
|
{-# LANGUAGE TemplateHaskell #-}
|
||||||
|
{-# LANGUAGE DeriveAnyClass #-}
|
||||||
|
module Gyehoek.Stack.Syntax
|
||||||
|
( Program(..)
|
||||||
|
, Routine(..)
|
||||||
|
, Instr(..)
|
||||||
|
, Block(..)
|
||||||
|
, Tail(..)
|
||||||
|
, Val(..)
|
||||||
|
, Lit(..)
|
||||||
|
, Obj(..)
|
||||||
|
, Imm(..)
|
||||||
|
, Hob(..)
|
||||||
|
, Prim(..)
|
||||||
|
, Name(..)
|
||||||
|
, Reg(..)
|
||||||
|
, Label(..)
|
||||||
|
, pattern ValLabel
|
||||||
|
, pattern ObjLabel
|
||||||
|
, stkP
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Lens
|
||||||
|
import qualified Gyehoek.Sexp as S
|
||||||
|
import Gyehoek.Scheme.Syntax (Name(..), Lit(..), Prim(..))
|
||||||
|
import GHC.Exts (IsList(..))
|
||||||
|
import Data.List (intersperse)
|
||||||
|
import Gyehoek.CPS.Syntax (Imm(..), Obj(..), Hob(..), pattern ObjLabel, Reg, Label)
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import Gyehoek.Sexp ((:-)((:-)))
|
||||||
|
|
||||||
|
|
||||||
|
newtype Program = MkProgram
|
||||||
|
{ routines :: HashMap Label Routine
|
||||||
|
}
|
||||||
|
deriving stock (Show, Generic, Data)
|
||||||
|
deriving newtype (Semigroup, Monoid)
|
||||||
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
|
instance IsList Program where
|
||||||
|
type Item Program = Routine
|
||||||
|
fromList rs = MkProgram
|
||||||
|
{ routines = fromList [ (r.label, r) | r <- rs ]
|
||||||
|
}
|
||||||
|
toList = toListOf $ #routines . each
|
||||||
|
|
||||||
|
data Routine = MkRoutine
|
||||||
|
{ label :: Label
|
||||||
|
, start :: Block
|
||||||
|
}
|
||||||
|
deriving stock (Show, Generic, Data)
|
||||||
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
|
data Block = MkBlock
|
||||||
|
{ code :: List Instr
|
||||||
|
, tail :: Tail
|
||||||
|
}
|
||||||
|
deriving stock (Show, Generic, Data)
|
||||||
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
|
data Tail
|
||||||
|
-- | call the procedure at stack index `n` supplied with `n`
|
||||||
|
-- arguments on top of the stack, then return by calling the
|
||||||
|
-- continuation at stack index `n+1`.
|
||||||
|
= TailCall Int
|
||||||
|
| Call Int
|
||||||
|
| If Val Block Block
|
||||||
|
| Return Int
|
||||||
|
| CallCC
|
||||||
|
deriving stock (Show, Generic, Data)
|
||||||
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
|
data Instr
|
||||||
|
= Pop Reg
|
||||||
|
| Push Val
|
||||||
|
| Load Reg Int
|
||||||
|
| Prim (Prim Val)
|
||||||
|
deriving stock (Show, Generic, Data)
|
||||||
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
|
data Val
|
||||||
|
= ValReg Reg
|
||||||
|
| ValImm Imm
|
||||||
|
deriving stock (Show, Generic, Data, Eq)
|
||||||
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
|
pattern ValLabel :: Label -> Val
|
||||||
|
pattern ValLabel x = ValImm (ImmLabel x)
|
||||||
|
|
||||||
|
|
||||||
|
--- sexp work
|
||||||
|
|
||||||
|
pure []
|
||||||
|
|
||||||
|
instance S.DatumIso Instr where
|
||||||
|
datumIso = S.match
|
||||||
|
$ S.With (S.headTagged1 "pop!" S.datumIso >>>)
|
||||||
|
$ S.With (S.headTagged1 "push!" S.datumIso >>>)
|
||||||
|
$ S.With (S.headTagged2 "load" S.datumIso S.datumIso >>>)
|
||||||
|
$ S.With (S.headTagged1 "prim" S.datumIso >>>)
|
||||||
|
$ S.End
|
||||||
|
where
|
||||||
|
|
||||||
|
instance S.DataIso Block where
|
||||||
|
dataIso = S.with \g ->
|
||||||
|
S.flipped S.snoced
|
||||||
|
>>> S.onHead (S.traversed $ S.sealed S.datumIso)
|
||||||
|
>>> S.onTail (S.datumIso @Tail)
|
||||||
|
>>> S.swap
|
||||||
|
>>> g
|
||||||
|
|
||||||
|
instance S.DatumIso Tail where
|
||||||
|
datumIso = S.match
|
||||||
|
$ S.With (S.headTagged1 "tail-call" S.datumIso >>>)
|
||||||
|
$ S.With (S.headTagged1 "call" S.datumIso >>>)
|
||||||
|
$ S.With (if_ >>>)
|
||||||
|
$ S.With (S.headTagged1 "return" S.datumIso >>>)
|
||||||
|
$ S.With (S.headTagged0 "call/cc" >>>)
|
||||||
|
$ S.End
|
||||||
|
where
|
||||||
|
-- if_ = S.ifLike "if" (S.datumIso @Val) S.datumIso S.datumIso
|
||||||
|
if_ = S.ifLike "if" (S.datumIso @Val) (branch "then") (branch "else")
|
||||||
|
branch :: Text -> S.DatumGrammar Block
|
||||||
|
branch s =
|
||||||
|
S.listWithIndentation (S.NSpecial 0) $
|
||||||
|
S.el (S.decorate S.SynBuiltin >>> S.sym s)
|
||||||
|
>>> S.restData (S.dataIso @Block)
|
||||||
|
|
||||||
|
instance S.DatumIso Val where
|
||||||
|
datumIso = S.match
|
||||||
|
$ S.With (S.datumIso >>>)
|
||||||
|
$ S.With (S.datumIso >>>)
|
||||||
|
$ S.End
|
||||||
|
|
||||||
|
instance S.DatumIso Routine where
|
||||||
|
datumIso = S.with \rout ->
|
||||||
|
S.listWithIndentation (S.NSpecial 1)
|
||||||
|
( S.el (S.decorate S.SynBuiltin >>> S.sym "define")
|
||||||
|
>>> S.el (S.datumIso @Label)
|
||||||
|
>>> S.restData (S.dataIso @Block)
|
||||||
|
)
|
||||||
|
>>> rout
|
||||||
|
|
||||||
|
instance S.DataIso Program where
|
||||||
|
dataIso = S.dataIso @(List Routine) >>> S.iso fromList toList
|
||||||
|
|
||||||
|
stkP :: S.QuasiQuoter
|
||||||
|
stkP = S.makeSxs [|| S.fromDataUnsafe (S.dataIso @Program) ||]
|
||||||
@@ -0,0 +1,521 @@
|
|||||||
|
{-# LANGUAGE ViewPatterns, MultilineStrings #-}
|
||||||
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
|
{-# LANGUAGE DeriveAnyClass #-}
|
||||||
|
module Gyehoek.Stack.VM
|
||||||
|
( VM(..)
|
||||||
|
, Env(..)
|
||||||
|
, eval
|
||||||
|
, trace
|
||||||
|
, module Gyehoek.Stack.Syntax
|
||||||
|
, writeObj
|
||||||
|
, traceEval
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Gyehoek.Stack.Syntax
|
||||||
|
import Control.Lens
|
||||||
|
import qualified Data.HashMap.Strict as H
|
||||||
|
import Data.List (unfoldr, intersperse, compareLength)
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import qualified Data.List.NonEmpty as NE
|
||||||
|
import Lucid
|
||||||
|
import Data.Foldable (traverse_)
|
||||||
|
import qualified Gyehoek.Sexp as S
|
||||||
|
import Gyehoek.Jalmot
|
||||||
|
import Text.Pretty.Simple (pStringNoColor, pShowNoColor)
|
||||||
|
import Effectful.State.Static.Local (runState, evalState, get)
|
||||||
|
import Data.Traversable
|
||||||
|
import Control.Applicative (Alternative(..))
|
||||||
|
import Gyehoek.Sexp.Print (htmlData)
|
||||||
|
import Control.DeepSeq (deepseq, ($!!))
|
||||||
|
import Gyehoek.Sexp.Print (htmlData, htmlDatum)
|
||||||
|
import Control.DeepSeq (deepseq, ($!!))
|
||||||
|
import Data.String (fromString)
|
||||||
|
import Data.Monoid (First)
|
||||||
|
import GHC.Stack (popCallStack)
|
||||||
|
import Data.Maybe (fromMaybe)
|
||||||
|
|
||||||
|
|
||||||
|
-- | non-essential information maintained only to aide in debugging.
|
||||||
|
data DebugVM = MkDebugVM
|
||||||
|
{ activeRoutine :: Label
|
||||||
|
}
|
||||||
|
deriving (Show, Generic)
|
||||||
|
|
||||||
|
newtype Frame = MkFrame { locals :: List Obj }
|
||||||
|
deriving stock (Show, Generic)
|
||||||
|
|
||||||
|
-- affine
|
||||||
|
returnAddress :: Traversal' Frame Obj
|
||||||
|
returnAddress = #locals . _last
|
||||||
|
|
||||||
|
-- affine
|
||||||
|
activeProcedure :: Traversal' Frame Obj
|
||||||
|
activeProcedure = #locals . _init . _last
|
||||||
|
|
||||||
|
newtype Stack = MkStack { frames :: NonEmpty Frame }
|
||||||
|
deriving stock (Show, Generic)
|
||||||
|
|
||||||
|
data VM = MkVM
|
||||||
|
{ stack :: Stack
|
||||||
|
, code :: List Instr
|
||||||
|
, tail :: Tail
|
||||||
|
, registers :: HashMap Reg Obj
|
||||||
|
, stdout :: Text
|
||||||
|
, result :: Maybe (List Obj)
|
||||||
|
, debug :: DebugVM
|
||||||
|
}
|
||||||
|
deriving (Show, Generic)
|
||||||
|
|
||||||
|
type instance Index Frame = Int
|
||||||
|
type instance IxValue Frame = Obj
|
||||||
|
|
||||||
|
instance Ixed Frame where
|
||||||
|
ix j = wrappedIso . ix j
|
||||||
|
|
||||||
|
instance Cons Frame Frame Obj Obj where
|
||||||
|
_Cons = prism'
|
||||||
|
(\(x,MkFrame xs) -> MkFrame (x:xs))
|
||||||
|
\case
|
||||||
|
MkFrame (x:xs) -> Just (x, MkFrame xs)
|
||||||
|
MkFrame [] -> Nothing
|
||||||
|
|
||||||
|
instance Each Frame Frame Obj Obj where each = wrappedIso . each
|
||||||
|
|
||||||
|
instance Each Stack Stack Frame Frame where each = wrappedIso . each
|
||||||
|
|
||||||
|
pushes :: Foldable f => f Obj -> Frame -> Frame
|
||||||
|
pushes = flip $ foldr cons
|
||||||
|
|
||||||
|
_NonEmpty :: Iso (NonEmpty a) (NonEmpty b) (a, List a) (b, List b)
|
||||||
|
_NonEmpty = iso
|
||||||
|
(\(x:|xs) -> (x,xs))
|
||||||
|
(\(x,xs) -> x:|xs)
|
||||||
|
|
||||||
|
pushFrame :: Frame -> Stack -> Stack
|
||||||
|
pushFrame f (MkStack xs) = MkStack $ NE.cons f xs
|
||||||
|
|
||||||
|
activeFrame :: Lens' VM Frame
|
||||||
|
activeFrame = #stack . #frames . _NonEmpty . _1
|
||||||
|
|
||||||
|
data Env = MkEnv
|
||||||
|
{ labels :: HashMap Label Routine
|
||||||
|
}
|
||||||
|
deriving (Show, Generic)
|
||||||
|
|
||||||
|
step :: Jalmot :> es => Env -> VM -> Eff es VM
|
||||||
|
step g vm = case vm ^. #code of
|
||||||
|
c:cs -> stepI g (vm & #code .~ cs) c
|
||||||
|
[] -> stepT g vm vm.tail
|
||||||
|
|
||||||
|
vmerror :: (HasCallStack, Jalmot :> es) => Text -> Eff es a
|
||||||
|
vmerror = throwError . VMError
|
||||||
|
|
||||||
|
stepI :: Jalmot :> es => Env -> VM -> Instr -> Eff es VM
|
||||||
|
|
||||||
|
stepI e vm (Load r j) = do
|
||||||
|
x <- expectOf [i|object at index #{j}|] (activeFrame . ix j) vm
|
||||||
|
pure $ vm & #registers . at r ?~ x
|
||||||
|
|
||||||
|
stepI e vm (Push v) = traverseOf activeFrame push vm
|
||||||
|
where push xs = cons <$> evalVal e vm v <*> pure xs
|
||||||
|
|
||||||
|
stepI g vm (Prim p) = stepP g vm p
|
||||||
|
|
||||||
|
stepI e vm (Pop r) = case vm ^? activeFrame . _Cons of
|
||||||
|
Nothing -> vmerror "empty stack"
|
||||||
|
Just (x,xs) -> pure $ vm & #registers . at r ?~ x
|
||||||
|
& activeFrame .~ xs
|
||||||
|
|
||||||
|
stepI e vm ins = vmerror [i|unimplemented instruction: #{ins}|]
|
||||||
|
|
||||||
|
stepT :: Jalmot :> es => Env -> VM -> Tail -> Eff es VM
|
||||||
|
|
||||||
|
stepT g vm tc@(Call nargs) = do
|
||||||
|
(args,f,ret,frm) <- parseCall nargs (vm ^. activeFrame)
|
||||||
|
& expectOf [i|bad call: #{show tc}|] _Just
|
||||||
|
rt <- getRoutine g f
|
||||||
|
let newFrame = MkFrame $ args ++ [f,ret]
|
||||||
|
pure $ vm
|
||||||
|
& jumpToRoutine rt
|
||||||
|
& activeFrame .~ frm
|
||||||
|
-- it is not essential we clear the registers, but it'll
|
||||||
|
-- make bugs more obvious.
|
||||||
|
& #registers .~ mempty
|
||||||
|
& #stack %~ \stk ->
|
||||||
|
case f of
|
||||||
|
ObjHob (HobContinuation {stack}) ->
|
||||||
|
coerce $ stack & _NonEmpty . _1 <>:~ (args ++ [f])
|
||||||
|
_ -> pushFrame newFrame stk
|
||||||
|
|
||||||
|
stepT g vm tc@(Return nret) = do
|
||||||
|
(xs,_) <- splitAtExact nret (vm ^. activeFrame . #locals)
|
||||||
|
& expectOf [i|bad return: #{show tc}|] _Just
|
||||||
|
expectOf [i|no return addr|] (activeFrame . returnAddress) vm >>= \case
|
||||||
|
ObjLabel "halt" -> pure $ vm & #result ?~ xs
|
||||||
|
ra -> do
|
||||||
|
rt <- getRoutine g ra
|
||||||
|
vm & traverseOf #stack (fmap snd . popFrame)
|
||||||
|
& mapped . activeFrame %~ pushes xs
|
||||||
|
& mapped %~ jumpToRoutine rt
|
||||||
|
-- it is not essential we clear the registers, but it'll make
|
||||||
|
-- bugs more obvious.
|
||||||
|
& mapped . #registers .~ mempty
|
||||||
|
|
||||||
|
stepT g vm tc@(TailCall nargs) = do
|
||||||
|
(args,f,ra) <- parseTailCall nargs (vm ^. activeFrame)
|
||||||
|
& expectOf [i|bad call: #{show tc}|] _Just
|
||||||
|
case f of
|
||||||
|
ObjLabel "halt" -> pure $ vm & #result ?~ args
|
||||||
|
_ -> do
|
||||||
|
rt <- getRoutine g f
|
||||||
|
let newFrame = MkFrame $ args ++ [f, ra]
|
||||||
|
pure $ vm
|
||||||
|
& jumpToRoutine rt
|
||||||
|
-- replace the active frame; don't push a new one.
|
||||||
|
& activeFrame .~ newFrame
|
||||||
|
-- it is not essential we clear the registers, but it'll make
|
||||||
|
-- bugs more obvious.
|
||||||
|
& #registers .~ mempty
|
||||||
|
|
||||||
|
stepT g vm (If c t f) = do
|
||||||
|
branch <- evalVal g vm c <&> \case
|
||||||
|
ObjImm (ImmBool False) -> f
|
||||||
|
_ -> t
|
||||||
|
pure $ jumpToBlock branch vm
|
||||||
|
|
||||||
|
stepT g vm CallCC = do
|
||||||
|
(cc,withcc,frm) <- parseCallCC (vm ^. activeFrame)
|
||||||
|
& expectOf "bad call/cc" _Just
|
||||||
|
let stk = vm.stack & #frames . _NonEmpty . _1 .~ frm
|
||||||
|
let reified_cc = ObjHob $ HobContinuation cc (coerce stk)
|
||||||
|
let newFrame = MkFrame [reified_cc, withcc, cc]
|
||||||
|
rt <- getRoutine g withcc
|
||||||
|
pure $ vm
|
||||||
|
& jumpToRoutine rt
|
||||||
|
-- replace the active frame; don't push a new one.
|
||||||
|
& activeFrame .~ newFrame
|
||||||
|
-- it is not essential we clear the registers, but it'll make
|
||||||
|
-- bugs more obvious.
|
||||||
|
& #registers .~ mempty
|
||||||
|
|
||||||
|
stepP :: Jalmot :> es => Env -> VM -> Prim Val -> Eff es VM
|
||||||
|
stepP g vm p = traverse (evalVal g vm) p >>= \case
|
||||||
|
PrimZeroP x -> case x of
|
||||||
|
ObjImm (ImmInt n) -> ret1 . ObjImm . ImmBool $ n == 0
|
||||||
|
_ -> vmerror [i|bad arg to zero?: #{x}|]
|
||||||
|
PrimAdd x y -> arith_binop (+) x y
|
||||||
|
PrimMul x y -> arith_binop (*) x y
|
||||||
|
PrimSub x y -> arith_binop (-) x y
|
||||||
|
PrimDiv x y -> arith_binop div x y
|
||||||
|
PrimMakeClosure f env ->
|
||||||
|
case f of
|
||||||
|
ObjImm (ImmLabel l) -> ret1 . ObjHob $ HobClosure l env
|
||||||
|
_ -> vmerror [i|expected label, got #{f}|]
|
||||||
|
PrimEnv -> do
|
||||||
|
x <- vm & expectOf "expected closure" (activeFrame . activeProcedure)
|
||||||
|
ret1 x
|
||||||
|
PrimEnvRef n -> do
|
||||||
|
(label,env) <- vm & expectOf "expected closure"
|
||||||
|
(activeFrame . activeProcedure . #_ObjHob . #_HobClosure)
|
||||||
|
x <- env & expectOf "expected upval" (ix n)
|
||||||
|
ret1 x
|
||||||
|
PrimCons x y -> ret1 $ ObjHob $ HobPair x y
|
||||||
|
PrimCar x -> case x of
|
||||||
|
ObjHob (HobPair car _) -> ret1 car
|
||||||
|
_ -> vmerror [i|expected pair, got ${x}|]
|
||||||
|
PrimCdr x -> case x of
|
||||||
|
ObjHob (HobPair _ cdr) -> ret1 cdr
|
||||||
|
_ -> vmerror [i|expected pair, got ${x}|]
|
||||||
|
-- PrimCaptureCC -> do
|
||||||
|
-- label <- vm & expectOf [i|bad stack, no return addr|]
|
||||||
|
-- (activeFrame . returnAddress . #_ObjImm . #_ImmLabel)
|
||||||
|
-- ret1 . ObjHob $ HobContinuation { label }
|
||||||
|
x -> vmerror [i|unimplemented prim: #{p}|]
|
||||||
|
where
|
||||||
|
ret vs = pure $ vm & activeFrame . #locals <>:~ vs
|
||||||
|
ret1 v = ret [v]
|
||||||
|
arith_binop op (ObjImm (ImmInt x)) (ObjImm (ImmInt y)) =
|
||||||
|
ret1 $ ObjImm (ImmInt (op x y))
|
||||||
|
arith_binop _ x y = vmerror [i|bad arith: #{x}, #{y}|]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
popFrame :: (HasCallStack, Jalmot :> es) => Stack -> Eff es (Frame, Stack)
|
||||||
|
popFrame stk = case stk ^. #frames . to NE.uncons of
|
||||||
|
(_, Nothing) -> vmerror "no frame to pop"
|
||||||
|
(f, Just fs) -> pure (f, stk & #frames .~ fs)
|
||||||
|
|
||||||
|
jumpToBlock :: Block -> VM -> VM
|
||||||
|
jumpToBlock b vm = vm
|
||||||
|
& #code .~ b.code
|
||||||
|
& #tail .~ b.tail
|
||||||
|
|
||||||
|
jumpToRoutine :: Routine -> VM -> VM
|
||||||
|
jumpToRoutine rt vm = vm
|
||||||
|
& jumpToBlock rt.start
|
||||||
|
& #debug . #activeRoutine .~ rt.label
|
||||||
|
|
||||||
|
getLabel :: Obj -> Maybe Label
|
||||||
|
getLabel = \case
|
||||||
|
ObjHob (HobClosure {label}) -> Just label
|
||||||
|
ObjHob (HobContinuation {cont}) -> getLabel cont
|
||||||
|
ObjImm (ImmLabel label) -> Just label
|
||||||
|
x -> Nothing
|
||||||
|
|
||||||
|
getRoutine :: (HasCallStack, Jalmot :> es) => Env -> Obj -> Eff es Routine
|
||||||
|
getRoutine g f = do
|
||||||
|
l <- getLabel f & expectOf [i|no label for #{f}|] _Just
|
||||||
|
case g ^. #labels . at l of
|
||||||
|
Just rt -> pure rt
|
||||||
|
Nothing -> vmerror [i|undefined label #{l}|]
|
||||||
|
|
||||||
|
expectOf
|
||||||
|
:: (HasCallStack, Jalmot :> es)
|
||||||
|
=> Text -> Getting (First a) s a -> s -> Eff es a
|
||||||
|
expectOf msg l = maybe (vmerror msg) pure . preview l
|
||||||
|
|
||||||
|
evalToLabel :: Jalmot :> es => Env -> VM -> Val -> Eff es Label
|
||||||
|
evalToLabel e vm v =
|
||||||
|
evalVal e vm v >>= \case
|
||||||
|
ObjImm (ImmLabel x) -> pure x
|
||||||
|
x -> vmerror [i|not a label: #{x}|]
|
||||||
|
|
||||||
|
evalVal :: Jalmot :> es => Env -> VM -> Val -> Eff es Obj
|
||||||
|
evalVal e vm = \case
|
||||||
|
ValImm imm -> pure $ ObjImm imm
|
||||||
|
ValReg r -> case vm ^. #registers . at r of
|
||||||
|
Just x -> pure x
|
||||||
|
Nothing -> vmerror [i|undefined register: #{r}|]
|
||||||
|
|
||||||
|
splitAtExact :: Int -> List a -> Maybe (List a, List a)
|
||||||
|
splitAtExact n xs = case compareLength xs n of
|
||||||
|
(EQ;GT) -> Just $ splitAt n xs
|
||||||
|
LT -> Nothing
|
||||||
|
|
||||||
|
takeExact :: Int -> List a -> Maybe (List a)
|
||||||
|
takeExact n xs = case compareLength xs n of
|
||||||
|
(EQ;GT) -> Just $ take n xs
|
||||||
|
LT -> Nothing
|
||||||
|
|
||||||
|
parseCallCC :: Frame -> Maybe (Obj, Obj, Frame)
|
||||||
|
parseCallCC frm = do
|
||||||
|
([cc,withcc],ys) <- splitAtExact 2 (frm ^. #locals)
|
||||||
|
pure (cc,withcc,MkFrame ys)
|
||||||
|
|
||||||
|
parseCall :: Int -> Frame -> Maybe (List Obj, Obj, Obj, Frame)
|
||||||
|
parseCall nargs frm = do
|
||||||
|
(xs,ys) <- splitAtExact (nargs+2) (frm ^. #locals)
|
||||||
|
let (xs',[f,ret]) = splitAt nargs xs
|
||||||
|
pure (xs',f,ret,MkFrame ys)
|
||||||
|
|
||||||
|
parseTailCall :: Int -> Frame -> Maybe (List Obj, Obj, Obj)
|
||||||
|
parseTailCall nargs frm = do
|
||||||
|
(xs,_) <- splitAtExact (nargs+1) (frm ^. #locals)
|
||||||
|
let (xs',f) = xs ^?! _Snoc
|
||||||
|
pure (xs',f,frm ^?! returnAddress)
|
||||||
|
|
||||||
|
initialVM :: VM
|
||||||
|
initialVM = MkVM
|
||||||
|
{ stack = MkStack . NE.singleton . MkFrame $
|
||||||
|
[ ObjLabel "start"
|
||||||
|
, ObjLabel "<nowhere at all>"
|
||||||
|
, ObjLabel "halt"
|
||||||
|
]
|
||||||
|
, tail = TailCall 0
|
||||||
|
, code = []
|
||||||
|
, registers = mempty
|
||||||
|
, stdout = ""
|
||||||
|
, result = Nothing
|
||||||
|
, debug = MkDebugVM
|
||||||
|
{ activeRoutine = "<nowhere>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
initialEnv :: Program -> Env
|
||||||
|
initialEnv p = MkEnv
|
||||||
|
{ labels = p.routines
|
||||||
|
}
|
||||||
|
|
||||||
|
loop :: (a -> Either b a) -> a -> b
|
||||||
|
loop f a = case f a of
|
||||||
|
Right a' -> loop f a'
|
||||||
|
Left b -> b
|
||||||
|
|
||||||
|
loopM :: Monad m => (a -> m (Either b a)) -> a -> m b
|
||||||
|
loopM f a = f a >>= \case
|
||||||
|
Right a' -> loopM f a'
|
||||||
|
Left b -> pure b
|
||||||
|
|
||||||
|
eval :: Jalmot :> es => Program -> Eff es (List Obj)
|
||||||
|
eval p = initialVM & loopM \vm -> case vm ^. #result of
|
||||||
|
Nothing -> Right <$> step (initialEnv p) vm
|
||||||
|
Just rs -> pure . Left $ rs
|
||||||
|
|
||||||
|
data Trace
|
||||||
|
= Step { vm :: VM, next :: Trace }
|
||||||
|
| StepToSuccess { vm :: VM, result :: List Obj }
|
||||||
|
| StepToFailure { vm :: VM, err :: AJalmotCS }
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
|
trace :: Program -> Trace
|
||||||
|
trace p = go (initialEnv p) initialVM
|
||||||
|
where
|
||||||
|
go g vm =
|
||||||
|
case vm.result of
|
||||||
|
Just rs -> StepToSuccess vm rs
|
||||||
|
Nothing ->
|
||||||
|
case runPureEff . runJalmotCS $ step g vm of
|
||||||
|
Left err -> StepToFailure vm err
|
||||||
|
Right vm' -> Step vm (go g vm')
|
||||||
|
|
||||||
|
writeObj :: Obj -> Text
|
||||||
|
writeObj = runJalmotUnsafe . S.encodeWith' S.datumIso
|
||||||
|
|
||||||
|
traceEval :: IOE :> es => Program -> Eff es ()
|
||||||
|
traceEval p = do
|
||||||
|
let t = trace p
|
||||||
|
liftIO . renderToFile "trace.html" . ppDoc p $ t
|
||||||
|
|
||||||
|
ppDoc :: Program -> Trace -> Html ()
|
||||||
|
ppDoc p t =
|
||||||
|
html_ do
|
||||||
|
head_ do
|
||||||
|
title_ "stackify trace"
|
||||||
|
style_ """
|
||||||
|
pre {
|
||||||
|
max-width: 95vw;
|
||||||
|
overflow: scroll;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
max-width: 95vw;
|
||||||
|
}
|
||||||
|
tbody > tr:nth-of-type(even) {
|
||||||
|
background-color: rgb(237 238 242);
|
||||||
|
}
|
||||||
|
.loc {
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
.syn-builtin, .syn-macro {
|
||||||
|
color: purple;
|
||||||
|
font-style: italic;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.syn-constant {
|
||||||
|
color: olive;
|
||||||
|
}
|
||||||
|
.syn-procedure {
|
||||||
|
color: teal;
|
||||||
|
}
|
||||||
|
td pre {
|
||||||
|
display: inline
|
||||||
|
}
|
||||||
|
.syn-paren-0 { color: maroon; }
|
||||||
|
.syn-paren-1 { color: olive; }
|
||||||
|
.syn-paren-2 { color: green; }
|
||||||
|
.syn-paren-3 { color: navy; }
|
||||||
|
.syn-paren-4 { color: purple; }
|
||||||
|
.stack-frame
|
||||||
|
{ display: inline-flex
|
||||||
|
; flex-direction: row
|
||||||
|
; column-gap: 0.5em
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
body_ do
|
||||||
|
details_ do
|
||||||
|
summary_ "stack code"
|
||||||
|
pre_ $ code_ do
|
||||||
|
htmlData . runJalmotUnsafe . S.toData S.dataIso $ p
|
||||||
|
ppTrace t
|
||||||
|
|
||||||
|
ppTrace :: Trace -> Html ()
|
||||||
|
ppTrace trace =
|
||||||
|
table_ do
|
||||||
|
thead_ $ tr_ do
|
||||||
|
traverse_ (th_ [scope_ "col"])
|
||||||
|
["routine","next instruction","stack frame"]
|
||||||
|
tbody_ do
|
||||||
|
go trace
|
||||||
|
where
|
||||||
|
go :: Trace -> Html ()
|
||||||
|
go = \case
|
||||||
|
Step vm next -> ppVM vm >> go next
|
||||||
|
StepToSuccess vm rs -> do
|
||||||
|
tr_ [class_ "trace-result"] do
|
||||||
|
td_ do
|
||||||
|
details_ do
|
||||||
|
summary_ "result"
|
||||||
|
pre_ do
|
||||||
|
code_ . toHtml . pShowNoColor $ vm
|
||||||
|
td_ [colspan_ "2"] do
|
||||||
|
sequence_ . intersperse " | " $ code_ . ppDatum <$> rs
|
||||||
|
StepToFailure vm err -> do
|
||||||
|
ppVM vm
|
||||||
|
tr_ [class_ "trace-failure"] do
|
||||||
|
td_ [colspan_ "3"] do
|
||||||
|
details_ do
|
||||||
|
summary_ "error"
|
||||||
|
pre_ do
|
||||||
|
samp_ do
|
||||||
|
fromString $ displayException err
|
||||||
|
|
||||||
|
ppVM :: VM -> Html ()
|
||||||
|
ppVM vm = do
|
||||||
|
tr_ do
|
||||||
|
td_ do
|
||||||
|
details_ do
|
||||||
|
summary_ do
|
||||||
|
var_ [class_ "loc"] do
|
||||||
|
vm ^. #debug . #activeRoutine . to ppDatum
|
||||||
|
pre_ do
|
||||||
|
code_ . toHtml . pShowNoColor $ vm
|
||||||
|
td_ do
|
||||||
|
code_ curi
|
||||||
|
td_ do
|
||||||
|
ppStack vm.stack
|
||||||
|
where
|
||||||
|
curi = vm ^?! failing (#code . _head . to ppDatum) (#tail . to ppDatum)
|
||||||
|
|
||||||
|
ppStack :: Stack -> Html ()
|
||||||
|
ppStack stk = do
|
||||||
|
span_ [class_ "stack"] do
|
||||||
|
stk ^.. each
|
||||||
|
& fmap ppFrame
|
||||||
|
& intersperse " | "
|
||||||
|
& sequence_
|
||||||
|
|
||||||
|
ppFrame :: Frame -> Html ()
|
||||||
|
ppFrame frm = do
|
||||||
|
span_ [class_ "stack-frame"] do
|
||||||
|
sequence_ $ frm ^.. #locals . each . to ppDatum
|
||||||
|
|
||||||
|
ppData :: S.DataIso a => a -> Html ()
|
||||||
|
ppData = htmlData . runJalmotUnsafe . S.toData S.dataIso
|
||||||
|
|
||||||
|
ppDatum :: S.DatumIso a => a -> Html ()
|
||||||
|
ppDatum = htmlDatum . runJalmotUnsafe . S.toDatum S.datumIso
|
||||||
|
|
||||||
|
fac (n :: Int) = [stkP|
|
||||||
|
(define $start
|
||||||
|
(push! $fac)
|
||||||
|
(push! #{n})
|
||||||
|
(tail-call 1))
|
||||||
|
|
||||||
|
(define $fac
|
||||||
|
(load %n 0)
|
||||||
|
(prim %x0 (zero? %n))
|
||||||
|
(if %x0
|
||||||
|
(then (push! 1)
|
||||||
|
(return 1))
|
||||||
|
(else (prim %x1 (- %n 1))
|
||||||
|
(push! $fac-c0)
|
||||||
|
(push! $fac)
|
||||||
|
(push! %x1)
|
||||||
|
(call 1))))
|
||||||
|
|
||||||
|
(define $fac-c0
|
||||||
|
(pop! %x2)
|
||||||
|
(pop! %n)
|
||||||
|
(prim %x3 (* %n %x2))
|
||||||
|
(push! %x3)
|
||||||
|
(return 1))
|
||||||
|
|]
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
{- HLINT ignore "Use newtype instead of data" -}
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
{-# LANGUAGE OverloadedLists #-}
|
{-# LANGUAGE OverloadedLists #-}
|
||||||
{-# LANGUAGE TemplateHaskellQuotes #-}
|
{-# LANGUAGE TemplateHaskellQuotes #-}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
(import (scheme eval))
|
((λ ()
|
||||||
|
(* 2 (call/cc
|
||||||
(eval '(λ (x) x)
|
(λ (k)
|
||||||
(environment))
|
(begin (k 6)
|
||||||
|
3))))))
|
||||||
|
|||||||
@@ -5,75 +5,53 @@ import Test.Tasty.HUnit
|
|||||||
import Gyehoek.CPS.Syntax (cps, Obj(..), Hob(..), Imm(..))
|
import Gyehoek.CPS.Syntax (cps, Obj(..), Hob(..), Imm(..))
|
||||||
import Gyehoek.CPS.Eval qualified as Sut
|
import Gyehoek.CPS.Eval qualified as Sut
|
||||||
import Data.List (List)
|
import Data.List (List)
|
||||||
import Test.Tasty.ExpectedFailure (ignoreTestBecause, expectFail)
|
import Test.Tasty.ExpectedFailure (ignoreTestBecause)
|
||||||
import System.Directory (listDirectory)
|
|
||||||
import Test.Tasty.Silver
|
|
||||||
import System.FilePath
|
|
||||||
import Control.Exception
|
|
||||||
import qualified Gyehoek.Driver as Driver
|
|
||||||
import Control.DeepSeq (($!!))
|
|
||||||
import System.Exit (ExitCode(..))
|
|
||||||
import qualified Data.Text as T
|
|
||||||
import Data.Function (applyWhen)
|
|
||||||
import Gyehoek.Prelude
|
|
||||||
|
|
||||||
|
|
||||||
brokenEvalTests :: List String
|
test_cpsInterpreter =
|
||||||
brokenEvalTests =
|
ignoreTestBecause "i forgorrrr" $
|
||||||
[]
|
testGroup "cps interpreter" $
|
||||||
-- [ "adder"
|
[ primitives
|
||||||
-- , "apply2"
|
, testCase "halt with constant" do
|
||||||
-- , "apply-twice"
|
evalsTo [ObjImm (ImmInt 123)] [cps|
|
||||||
-- , "arith"
|
(continue halt 123)
|
||||||
-- , "begin-1"
|
|]
|
||||||
-- , "callcc-constant"
|
, testCase "identity cont" do
|
||||||
-- , "callcc-discard"
|
evalsTo [ObjImm (ImmInt 154)] [cps|
|
||||||
-- , "callcc-early-exit-1"
|
(letrec ((id (κ (x)
|
||||||
-- , "callcc-early-exit-2"
|
(continue halt x))))
|
||||||
-- , "callcc-early-exit-3"
|
(continue id 154))
|
||||||
-- , "callcc-early-exit-4"
|
|]
|
||||||
-- , "callcc-early-exit-5"
|
, testCase "identity function" do
|
||||||
-- , "callcc-early-exit-6"
|
evalsTo [ObjImm (ImmInt 456)] [cps|
|
||||||
-- , "callcc-nested-1"
|
(letrec ((id (λ (x ktail)
|
||||||
-- , "callcc-nested-2"
|
(continue ktail x))))
|
||||||
-- , "complicated-1"
|
(id 456 halt))
|
||||||
-- , "cons-1"
|
|]
|
||||||
-- , "factorial"
|
, testCase "square" do
|
||||||
-- , "false"
|
evalsTo [ObjImm (ImmInt 81)] [cps|
|
||||||
-- , "fn-of-fn"
|
(letrec ((square (λ (x ktail)
|
||||||
-- , "if-false"
|
(prim (* x x)
|
||||||
-- , "if-number"
|
(κ (r) (continue ktail r))))))
|
||||||
-- , "if-true"
|
(square 9 halt))
|
||||||
-- , "lambda"
|
|]
|
||||||
-- , "letrec-fn"
|
]
|
||||||
-- , "let-fn"
|
|
||||||
-- , "lit-int"
|
|
||||||
-- , "square"
|
|
||||||
-- , "true"
|
|
||||||
-- ]
|
|
||||||
|
|
||||||
test_eval :: IO TestTree
|
evalsTo :: HasCallStack => List Obj -> Sut.Exp -> Assertion
|
||||||
test_eval = do
|
evalsTo rs e = Sut.evalExp e @?= rs
|
||||||
cs <- listDirectory "golden/exec"
|
|
||||||
<&> fmap ("golden/exec" </>)
|
primitives = testGroup "primitives"
|
||||||
pure $ testGroup "cps interpreter"
|
[ testGroup "arith"
|
||||||
[ testGroup "higher-order" $ cpsCase Driver.eval_cps2_e2e <$> cs
|
[ testCase "basic 1" do
|
||||||
-- , testGroup "first-order" $ cpsCase Driver.eval_cps1_e2e <$> cs
|
evalsTo [ObjImm (ImmInt 20)] [cps|
|
||||||
|
(prim (* 4 5)
|
||||||
|
(κ (x) (continue halt x)))
|
||||||
|
|]
|
||||||
|
, testCase "basic 2" do
|
||||||
|
evalsTo [ObjImm (ImmInt 35)] [cps|
|
||||||
|
(prim (* 2 16)
|
||||||
|
(κ (x) (prim (+ x 3)
|
||||||
|
(κ (r) (continue halt r)))))
|
||||||
|
|]
|
||||||
]
|
]
|
||||||
|
]
|
||||||
maybeBroken name broken = applyWhen (name `elem` broken) expectFail
|
|
||||||
|
|
||||||
cpsCase :: (FilePath -> IO Text) -> FilePath -> TestTree
|
|
||||||
cpsCase f test =
|
|
||||||
maybeBroken testName brokenEvalTests $
|
|
||||||
goldenVsAction testName resultFile action printProcResult
|
|
||||||
where
|
|
||||||
testName = takeFileName test
|
|
||||||
resultFile = test </> "exec"
|
|
||||||
sourceFile = test </> "source.scm"
|
|
||||||
action = catch @SomeException
|
|
||||||
(do r <- f sourceFile
|
|
||||||
pure $!! ( ExitSuccess
|
|
||||||
, r
|
|
||||||
, "" ))
|
|
||||||
\e -> pure (ExitFailure 1, "", T.pack $ displayException e)
|
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
module Gyehoek.Test.CPS.Stackify where
|
||||||
|
|
||||||
|
import Test.Tasty (TestTree, testGroup)
|
||||||
|
import Test.Tasty.HUnit
|
||||||
|
import qualified Gyehoek.CPS.Stackify as Sut
|
||||||
|
import Gyehoek.Stack.VM as Stk
|
||||||
|
import Gyehoek.CPS.Syntax (cps)
|
||||||
|
import Gyehoek.CPS.Syntax qualified as CPS
|
||||||
|
import Gyehoek.GenSym (runGenSym)
|
||||||
|
import Effectful
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import Gyehoek.Jalmot
|
||||||
|
|
||||||
|
|
||||||
|
test_stackify =
|
||||||
|
[ trivialReturn
|
||||||
|
, tailCall
|
||||||
|
, prim
|
||||||
|
, condition
|
||||||
|
, procedure
|
||||||
|
]
|
||||||
|
|
||||||
|
evalsTo :: HasCallStack => List Obj -> Sut.Program -> Assertion
|
||||||
|
evalsTo rs e = runJalmotUnsafe (Stk.eval e') @?= rs
|
||||||
|
where
|
||||||
|
e' = e & Sut.stackifyProgram & runGenSym & runPureEff
|
||||||
|
|
||||||
|
trivialReturn = testGroup "trivial return"
|
||||||
|
[ testCase "return int" do
|
||||||
|
evalsTo [ObjImm (ImmInt 4)]
|
||||||
|
[cps|(λ (ktail) (continue ktail 4))|]
|
||||||
|
, testCase "return bool" do
|
||||||
|
evalsTo [ObjImm (ImmBool True)]
|
||||||
|
[cps|(λ (ktail) (continue ktail #t))|]
|
||||||
|
evalsTo [ObjImm (ImmBool False)]
|
||||||
|
[cps|(λ (ktail) (continue ktail #f))|]
|
||||||
|
]
|
||||||
|
|
||||||
|
tailCall = testGroup "tail call"
|
||||||
|
[ testCase "square" do
|
||||||
|
evalsTo [ObjImm (ImmInt 16)] [cps|
|
||||||
|
(λ (ktail0)
|
||||||
|
(letrec ((square (λ (x ktail)
|
||||||
|
(prim (* x x)
|
||||||
|
(κ (x0) (continue ktail x0))))))
|
||||||
|
(square 4 halt)))
|
||||||
|
|]
|
||||||
|
]
|
||||||
|
|
||||||
|
prim = testGroup "prim"
|
||||||
|
[ testCase "multiply" do
|
||||||
|
evalsTo [ObjImm (ImmInt 20)]
|
||||||
|
[cps|(λ (ktail0)
|
||||||
|
(prim (* 4 5)
|
||||||
|
(κ (x) (continue ktail0 x))))|]
|
||||||
|
, testCase "add" do
|
||||||
|
evalsTo [ObjImm (ImmInt 9)]
|
||||||
|
[cps|(λ (ktail0)
|
||||||
|
(prim (+ 4 5)
|
||||||
|
(κ (x) (continue ktail0 x))))|]
|
||||||
|
-- , testGroup "call/cc"
|
||||||
|
-- [ testCase "trivial" do
|
||||||
|
-- evalsTo [ObjImm (ImmInt 123)]
|
||||||
|
-- [cps|(letrec ((f (λ (cc ktail) (continue cc 123))))
|
||||||
|
-- (prim (call/cc f)))|]
|
||||||
|
-- ]
|
||||||
|
]
|
||||||
|
|
||||||
|
condition = testCase "if" do
|
||||||
|
evalsTo [ObjImm (ImmInt 123)]
|
||||||
|
[cps|(λ (ktail0)
|
||||||
|
(if #t (continue ktail0 123) (continue ktail0 456)))|]
|
||||||
|
evalsTo [ObjImm (ImmInt 456)]
|
||||||
|
[cps|(λ (ktail0)
|
||||||
|
(if #f (continue ktail0 123) (continue ktail0 456)))|]
|
||||||
|
|
||||||
|
procedure = testGroup "procedure"
|
||||||
|
[ testCase "factorial" do
|
||||||
|
evalsTo [ObjImm (ImmInt 720)]
|
||||||
|
[cps|(λ (ktail0)
|
||||||
|
(letrec ((fac (λ (n ktail)
|
||||||
|
(prim (zero? n)
|
||||||
|
(κ (x0)
|
||||||
|
(if x0
|
||||||
|
(continue ktail 1)
|
||||||
|
(prim (- n 1)
|
||||||
|
(κ (x1)
|
||||||
|
(letrec ((fac-k0
|
||||||
|
(κ (x2)
|
||||||
|
(prim (* n x2)
|
||||||
|
(κ (x3)
|
||||||
|
(continue ktail x3))))))
|
||||||
|
(fac x1 fac-k0))))))))))
|
||||||
|
(fac 6 halt)))|]
|
||||||
|
]
|
||||||
@@ -46,9 +46,9 @@ qq = testGroup "parser"
|
|||||||
, testCase "application" do
|
, testCase "application" do
|
||||||
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
||||||
[Sut.ValVar "x",Sut.ValVar "y"]
|
[Sut.ValVar "x",Sut.ValVar "y"]
|
||||||
(Sut.KexpVar "k"))
|
"k")
|
||||||
[cps|(f x y k)|]
|
[cps|(f x y k)|]
|
||||||
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
||||||
[] (Sut.KexpVar "k"))
|
[] "k")
|
||||||
[cps|(f k)|]
|
[cps|(f k)|]
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
module Gyehoek.Test.Golden where
|
||||||
|
|
||||||
|
import Test.Tasty (TestTree, testGroup)
|
||||||
|
import Test.Tasty.Silver
|
||||||
|
import Gyehoek.Driver qualified as Driver
|
||||||
|
import System.FilePath
|
||||||
|
import Data.List (List)
|
||||||
|
import Data.Functor ((<&>))
|
||||||
|
import System.Directory
|
||||||
|
import Data.Function
|
||||||
|
import System.Environment.Blank (getEnvDefault)
|
||||||
|
import qualified System.Process.Text as PT
|
||||||
|
import Control.Exception (SomeException (SomeException), Exception (..), catch)
|
||||||
|
import Gyehoek.Stack.VM (writeObj)
|
||||||
|
import Data.Text qualified as T
|
||||||
|
import System.Exit (ExitCode(..))
|
||||||
|
import Test.Tasty.ExpectedFailure (expectFail, ignoreTestBecause)
|
||||||
|
import Control.DeepSeq (($!!))
|
||||||
|
import Text.Pretty.Simple (pShow, pShowNoColor)
|
||||||
|
import Control.Lens (strict, view)
|
||||||
|
import Gyehoek.Sexp.Read qualified as Read
|
||||||
|
import Effectful
|
||||||
|
|
||||||
|
|
||||||
|
brokenWasmTests :: List String
|
||||||
|
brokenWasmTests =
|
||||||
|
[
|
||||||
|
]
|
||||||
|
|
||||||
|
brokenStackifyTests :: List String
|
||||||
|
brokenStackifyTests =
|
||||||
|
[
|
||||||
|
]
|
||||||
|
|
||||||
|
test_root :: IO TestTree
|
||||||
|
test_root = do
|
||||||
|
all_cases <- listDirectory "golden/exec"
|
||||||
|
let tests = all_cases
|
||||||
|
& fmap ("golden/exec"</>)
|
||||||
|
testGroup "execution" <$> sequenceA
|
||||||
|
[ ignoreTestBecause "wasm codegen is on the backburner"
|
||||||
|
<$> wasmTests tests
|
||||||
|
, stackifyTests tests
|
||||||
|
]
|
||||||
|
|
||||||
|
maybeBroken name broken = applyWhen (name `elem` broken) expectFail
|
||||||
|
|
||||||
|
wasmTests :: List FilePath -> IO TestTree
|
||||||
|
wasmTests files = do
|
||||||
|
cmd <- getEnvDefault "GYEHOEK_WASM_RUNTIME"
|
||||||
|
"runtime/target/debug/gyehoek-wasm-runtime"
|
||||||
|
pure $ testGroup "wasm" $ files <&> \test ->
|
||||||
|
let testname = takeFileName test
|
||||||
|
scmfile = test </> "source.scm"
|
||||||
|
resultfile = test </> "exec"
|
||||||
|
action = do
|
||||||
|
t <- Driver.lower_e2e scmfile
|
||||||
|
PT.readProcessWithExitCode cmd ["-"] t
|
||||||
|
in maybeBroken testname brokenWasmTests $
|
||||||
|
goldenVsAction
|
||||||
|
testname
|
||||||
|
resultfile
|
||||||
|
action
|
||||||
|
printProcResult
|
||||||
|
|
||||||
|
stackifyTests :: List FilePath -> IO TestTree
|
||||||
|
stackifyTests files = do
|
||||||
|
pure $ testGroup "stackified" $ files <&> \test ->
|
||||||
|
let testname = takeFileName test
|
||||||
|
scmfile = test </> "source.scm"
|
||||||
|
resultfile = test </> "exec"
|
||||||
|
action =
|
||||||
|
catch @SomeException
|
||||||
|
(do rs <- Driver.eval_e2e scmfile
|
||||||
|
pure $!! ( ExitSuccess
|
||||||
|
, T.unwords . fmap writeObj $ rs
|
||||||
|
, "" ))
|
||||||
|
\e -> pure (ExitFailure 1, "", T.pack $ displayException e)
|
||||||
|
in maybeBroken testname brokenStackifyTests $
|
||||||
|
goldenVsAction
|
||||||
|
testname
|
||||||
|
resultfile
|
||||||
|
action
|
||||||
|
printProcResult
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
{-# LANGUAGE OverloadedLists #-}
|
||||||
|
module Gyehoek.Test.Stack.VM where
|
||||||
|
|
||||||
|
import Test.Tasty (TestTree, testGroup)
|
||||||
|
import Test.Tasty.HUnit
|
||||||
|
import Gyehoek.Stack.Syntax
|
||||||
|
import Gyehoek.Stack.VM qualified as Sut
|
||||||
|
import Data.List (List)
|
||||||
|
import Gyehoek.Jalmot
|
||||||
|
import Gyehoek.Prelude (i)
|
||||||
|
|
||||||
|
|
||||||
|
evalsTo :: List Obj -> Program -> Assertion
|
||||||
|
evalsTo rs p = runJalmotUnsafe (Sut.eval p) @?= rs
|
||||||
|
|
||||||
|
test_root = testGroup "stack machine"
|
||||||
|
[ testCase "immediate halt" do
|
||||||
|
evalsTo [] [stkP|
|
||||||
|
(define $start
|
||||||
|
(return 0))
|
||||||
|
|]
|
||||||
|
, testCase "lit int" do
|
||||||
|
evalsTo [ObjImm (ImmInt 3)] [stkP|
|
||||||
|
(define $start
|
||||||
|
(push! 3)
|
||||||
|
(return 1))
|
||||||
|
|]
|
||||||
|
, testCase "non-tail identity function" do
|
||||||
|
evalsTo [ObjImm (ImmInt 123)] [stkP|
|
||||||
|
(define $id
|
||||||
|
(return 1))
|
||||||
|
(define $c
|
||||||
|
(return 1))
|
||||||
|
(define $start
|
||||||
|
(push! $c)
|
||||||
|
(push! $id)
|
||||||
|
(push! 123)
|
||||||
|
(call 1))
|
||||||
|
|]
|
||||||
|
, testCase "tail identity function" do
|
||||||
|
evalsTo [ObjImm (ImmInt 123)] [stkP|
|
||||||
|
(define $id
|
||||||
|
(return 1))
|
||||||
|
(define $start
|
||||||
|
(push! $id)
|
||||||
|
(push! 123)
|
||||||
|
(tail-call 1))
|
||||||
|
|]
|
||||||
|
, testCase "return constant" do
|
||||||
|
evalsTo [ObjImm (ImmInt 123)] [stkP|
|
||||||
|
(define $start
|
||||||
|
(push! $silly)
|
||||||
|
(tail-call 1))
|
||||||
|
(define $silly
|
||||||
|
(push! 123)
|
||||||
|
(return 1))
|
||||||
|
|]
|
||||||
|
, testCase "return multiple" do
|
||||||
|
evalsTo [ObjImm (ImmInt n) | n <- [1,2,3]] [stkP|
|
||||||
|
(define $start
|
||||||
|
(push! 3)
|
||||||
|
(push! 2)
|
||||||
|
(push! 1)
|
||||||
|
(return 3))
|
||||||
|
|]
|
||||||
|
, testCase "return none" do
|
||||||
|
evalsTo [] [stkP|
|
||||||
|
(define $start
|
||||||
|
(return 0))
|
||||||
|
|]
|
||||||
|
, testCase "square" do
|
||||||
|
evalsTo [ObjImm (ImmInt 16)] [stkP|
|
||||||
|
(define $start
|
||||||
|
(push! $square)
|
||||||
|
(push! 4)
|
||||||
|
(tail-call 1))
|
||||||
|
(define $square
|
||||||
|
(pop! %x)
|
||||||
|
(prim (* %x %x))
|
||||||
|
(return 1))
|
||||||
|
|]
|
||||||
|
, testGroup "factorial"
|
||||||
|
let
|
||||||
|
hsfac (n :: Int) = foldr @List (*) 1 [1..n]
|
||||||
|
fac (n :: Int) = [stkP|
|
||||||
|
(define $start
|
||||||
|
(push! $fac)
|
||||||
|
(push! #{n})
|
||||||
|
(tail-call 1))
|
||||||
|
(define $fac
|
||||||
|
(load %n 0)
|
||||||
|
(prim (zero? %n))
|
||||||
|
(pop! %x0)
|
||||||
|
(if %x0
|
||||||
|
(then (push! 1)
|
||||||
|
(return 1))
|
||||||
|
(else (push! $fac-c0)
|
||||||
|
(push! $fac)
|
||||||
|
(prim (- %n 1))
|
||||||
|
(call 1))))
|
||||||
|
(define $fac-c0
|
||||||
|
(pop! %x2)
|
||||||
|
(pop! %n)
|
||||||
|
(prim (* %n %x2))
|
||||||
|
(return 1))
|
||||||
|
|]
|
||||||
|
mkcase n = testCase [i|#{n}|] do
|
||||||
|
evalsTo [ObjImm . ImmInt $ hsfac n] $ fac n
|
||||||
|
-- 20 is the greatest `n` for which n! ≤ maxBount @Int
|
||||||
|
in [ mkcase n | n <- [0,1,6,20] ]
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user