Compare commits
19
Commits
vm-rewrite
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
309f722712 | ||
|
|
31cc2b1720 | ||
|
|
3cdae9eab4 | ||
|
|
c6036ffbb4 | ||
|
|
6ebe92e7cd | ||
|
|
0f9ba3c51e | ||
|
|
10bd6b733a | ||
|
|
d1588bd917 | ||
|
|
c495fc064a | ||
|
|
ac39175767 | ||
|
|
31c610db34 | ||
|
|
1ec3d35282 | ||
|
|
9f37d10e4f | ||
|
|
ba5dc401d9 | ||
|
|
bc599df65f | ||
|
|
f26ac50d4e | ||
|
|
3196d8db84 | ||
|
|
75e6c963c7 | ||
|
|
25f1f008bd |
@@ -9,6 +9,9 @@
|
|||||||
. (progn (defun apply-cabal-fmt-h ()
|
. (progn (defun apply-cabal-fmt-h ()
|
||||||
(haskell-mode-buffer-apply-command "cabal-fmt"))
|
(haskell-mode-buffer-apply-command "cabal-fmt"))
|
||||||
(add-hook 'before-save-hook #'apply-cabal-fmt-h nil t)))))
|
(add-hook 'before-save-hook #'apply-cabal-fmt-h nil t)))))
|
||||||
|
(scheme-mode
|
||||||
|
. ((eval . (dolist (s '(kappa κ prim))
|
||||||
|
(put s 'scheme-indent-function 1)))))
|
||||||
(nil
|
(nil
|
||||||
. ((eval
|
. ((eval
|
||||||
. (progn (defun display-ansi ()
|
. (progn (defun display-ansi ()
|
||||||
|
|||||||
@@ -132,3 +132,34 @@ multiple ~env-ref~ calls could probably be replaced with a primitive that loads
|
|||||||
$code)
|
$code)
|
||||||
1))))
|
1))))
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
** example
|
||||||
|
|
||||||
|
#+begin_src scheme
|
||||||
|
(λ (n m ktail)
|
||||||
|
(letrec ((f (λ (x ktail-0) (+ x n ktail-0)))
|
||||||
|
(g (λ (y ktail-1) (+ y g ktail-1))))
|
||||||
|
(prim (cons f g) ktail)))
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+begin_src scheme
|
||||||
|
(λ (n m ktail)
|
||||||
|
(letrec ((f-code (λ (x ktail-0)
|
||||||
|
(prim (env-get 2)
|
||||||
|
(κ (n)
|
||||||
|
(+ x n ktail-0)))))
|
||||||
|
(g-code (λ (y ktail-1)
|
||||||
|
(prim (env-get 3)
|
||||||
|
(κ (m)
|
||||||
|
(+ y m ktail-1))))))
|
||||||
|
(letrec ((with-closure-code
|
||||||
|
(κ (f g)
|
||||||
|
(prim (get-env 0)
|
||||||
|
(κ (ktail)
|
||||||
|
(prim cons f g ktail))))))
|
||||||
|
(prim (make-shared-closure (with-closure-code)
|
||||||
|
ktail)
|
||||||
|
(κ (with-closure)
|
||||||
|
(prim (make-shared-closure (f-code g-code) n m)
|
||||||
|
with-closure))))))
|
||||||
|
#+end_src
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#+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.
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
ret > ExitSuccess
|
||||||
|
out > (456 . 123)
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
(let ((p (cons 123 456)))
|
||||||
|
(cons (cdr p) (car p)))
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
ret > ExitSuccess
|
||||||
|
out > 123
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
123
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
ret > ExitSuccess
|
||||||
|
out > (0 . (1 . (4 . (9 . (16 . ())))))
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
(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)))
|
||||||
+6
-10
@@ -60,12 +60,12 @@ library
|
|||||||
Gyehoek.CPS.Close
|
Gyehoek.CPS.Close
|
||||||
Gyehoek.CPS.Convert
|
Gyehoek.CPS.Convert
|
||||||
Gyehoek.CPS.Eval
|
Gyehoek.CPS.Eval
|
||||||
Gyehoek.CPS.Stackify
|
Gyehoek.CPS.Hoist
|
||||||
Gyehoek.CPS.Syntax
|
Gyehoek.CPS.Syntax
|
||||||
Gyehoek.Driver
|
Gyehoek.Driver
|
||||||
Gyehoek.Language
|
|
||||||
Gyehoek.GenSym
|
Gyehoek.GenSym
|
||||||
Gyehoek.Jalmot
|
Gyehoek.Jalmot
|
||||||
|
Gyehoek.Language
|
||||||
Gyehoek.Lift1
|
Gyehoek.Lift1
|
||||||
Gyehoek.Options
|
Gyehoek.Options
|
||||||
Gyehoek.Prelude
|
Gyehoek.Prelude
|
||||||
@@ -77,9 +77,6 @@ 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:
|
||||||
@@ -100,6 +97,7 @@ library
|
|||||||
, hashable
|
, hashable
|
||||||
, invertible-grammar
|
, invertible-grammar
|
||||||
, lens
|
, lens
|
||||||
|
, lucid
|
||||||
, megaparsec
|
, megaparsec
|
||||||
, mtl
|
, mtl
|
||||||
, optparse-applicative
|
, optparse-applicative
|
||||||
@@ -107,6 +105,7 @@ library
|
|||||||
, pretty-simple
|
, pretty-simple
|
||||||
, prettyprinter
|
, prettyprinter
|
||||||
, prettyprinter-ansi-terminal
|
, prettyprinter-ansi-terminal
|
||||||
|
, prettyprinter-lucid
|
||||||
, process
|
, process
|
||||||
, recursion-schemes
|
, recursion-schemes
|
||||||
, scientific
|
, scientific
|
||||||
@@ -117,8 +116,7 @@ library
|
|||||||
, typed-process
|
, typed-process
|
||||||
, unordered-containers
|
, unordered-containers
|
||||||
, vector
|
, vector
|
||||||
, lucid
|
, tardis
|
||||||
, prettyprinter-lucid
|
|
||||||
|
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
default-language: GHC2024
|
default-language: GHC2024
|
||||||
@@ -133,14 +131,11 @@ 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
|
||||||
|
|
||||||
@@ -171,6 +166,7 @@ test-suite doctest
|
|||||||
build-depends:
|
build-depends:
|
||||||
, base
|
, base
|
||||||
, gyehoek
|
, gyehoek
|
||||||
|
|
||||||
default-extensions: CPP
|
default-extensions: CPP
|
||||||
main-is: doctest.hs
|
main-is: doctest.hs
|
||||||
|
|
||||||
|
|||||||
+38
-29
@@ -7,40 +7,49 @@ import Gyehoek.CPS.Syntax
|
|||||||
import Data.List (nub)
|
import Data.List (nub)
|
||||||
import Gyehoek.GenSym
|
import Gyehoek.GenSym
|
||||||
import Gyehoek.Prelude
|
import Gyehoek.Prelude
|
||||||
|
import Debug.Pretty.Simple
|
||||||
|
import Gyehoek.Sexp qualified as S
|
||||||
|
import Data.HashSet.Lens
|
||||||
|
import Data.Traversable
|
||||||
|
|
||||||
|
|
||||||
close :: GenSym :> es => Exp -> Eff es Exp
|
genCodeName :: GenSym :> es => Name -> Eff es Name
|
||||||
close = transformM \case
|
genCodeName f = gensym' @Name $ f ^. _Wrapped' . to (<> "-code")
|
||||||
ExpLetRec [(f, AbsLambda lam@(MkLambda bs kb m))] e -> do
|
|
||||||
f_code <- gensym' @Name $ f ^. _Wrapped' . to (<> "-code")
|
|
||||||
-- it would probably be most sane to generate a symbol for `env`,
|
|
||||||
-- but we're reusing the lambda binding so we don't have to
|
|
||||||
-- explicitly substitute recursive calls.
|
|
||||||
let frees = nub $ free' lam
|
|
||||||
let m' = ifoldr
|
|
||||||
(\n x q ->
|
|
||||||
let p = if x == f then PrimEnv @Val else PrimEnvRef n
|
|
||||||
in [cps|
|
|
||||||
(prim #{p}
|
|
||||||
(κ (#{x}) #{q}))
|
|
||||||
|])
|
|
||||||
m frees
|
|
||||||
pure [cps|
|
|
||||||
(letrec ((#{f_code} (λ (##{bs} #{kb})
|
|
||||||
#{m'})))
|
|
||||||
(prim (make-closure #{f_code} ##{frees})
|
|
||||||
(κ (#{f}) #{e})))
|
|
||||||
|]
|
|
||||||
|
|
||||||
-- ExpApply f xs ktail -> do
|
bindEnv :: List Name -> Exp -> Exp
|
||||||
-- code <- gensym' @Name "code"
|
bindEnv frees m = [cps|
|
||||||
-- pure [cps|
|
(prim (get-env) (κ #{frees} #{m}))
|
||||||
-- (prim (env-code #{f})
|
|]
|
||||||
-- (κ (#{code})
|
|
||||||
-- (#{code} #{f} ##{xs} #{ktail})))
|
close1 :: forall es. GenSym :> es => Exp -> Eff es Exp
|
||||||
-- |]
|
close1 = \case
|
||||||
|
lr@(ExpLetRec bs e) -> do
|
||||||
|
let boundNames = bs ^.. each . _1
|
||||||
|
let boundNames' = setOf each boundNames
|
||||||
|
let frees = bs
|
||||||
|
& foldMapOf
|
||||||
|
(each . _2)
|
||||||
|
(freeWithBound' boundNames')
|
||||||
|
& nub
|
||||||
|
env_cont_l <- gensym' @Name "env-cont"
|
||||||
|
e_l <- gensym' @Name "letrec-body-cont"
|
||||||
|
bs' <- for bs \(f,ab) -> do
|
||||||
|
f_code_l <- genCodeName f
|
||||||
|
pure ( f_code_l
|
||||||
|
, ab & absBody %~ bindEnv (boundNames ++ frees)
|
||||||
|
)
|
||||||
|
let codes = bs' ^.. each . _1 . to MkLabel
|
||||||
|
pure [cps|
|
||||||
|
(letrec #{bs'}
|
||||||
|
(prim (make-shared-closure #{codes} #{frees})
|
||||||
|
(κ #{boundNames}
|
||||||
|
#{e})))
|
||||||
|
|]
|
||||||
|
|
||||||
e -> pure e
|
e -> pure e
|
||||||
|
|
||||||
|
close :: forall es. GenSym :> es => Exp -> Eff es Exp
|
||||||
|
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
|
||||||
|
|||||||
+22
-22
@@ -46,26 +46,18 @@ convert (Scm.ExpLit l) k = k . one . ValImm $ case l of
|
|||||||
LitBool b -> ImmBool b
|
LitBool b -> ImmBool b
|
||||||
_ -> _
|
_ -> _
|
||||||
|
|
||||||
-- special case: call/cc is desugared during cps-conversion...
|
|
||||||
-- convert (Scm.ExpPrim (PrimCallCC withcc)) k = do
|
|
||||||
-- convert1 withcc \withcc' -> do
|
|
||||||
-- cc <- gensym' @Name "cc"
|
|
||||||
-- r <- gensym' "r"
|
|
||||||
-- m <- k . one $ ValVar r
|
|
||||||
-- ccish <- gensym' @Name "cc-ish"
|
|
||||||
-- x <- gensym' @Name "x"
|
|
||||||
-- pure [cps|
|
|
||||||
-- (letrec ((#{cc} (κ (#{r}) #{m})))
|
|
||||||
-- (letrec ((#{ccish} (λ (#{x} _) (continue #{cc} #{x}))))
|
|
||||||
-- (#{withcc'} #{ccish} #{cc})))
|
|
||||||
-- |]
|
|
||||||
|
|
||||||
-- ...while all other prims are left as-is for later stages to
|
|
||||||
-- handle..
|
|
||||||
convert (Scm.ExpPrim p) k =
|
convert (Scm.ExpPrim p) k =
|
||||||
telescope (convert1 @es) p \p' -> do
|
telescope (convert1 @es) p \p' -> do
|
||||||
r <- gensym' "r"
|
r_l <- gensym' "r"
|
||||||
ExpPrim p' . MkKappa [r] <$> k [ValVar r]
|
-- k_l <- gensym' @Name "prim-k"
|
||||||
|
m <- k [ValVar r_l]
|
||||||
|
pure [cps|
|
||||||
|
(prim #{p'} (κ (#{r_l}) #{m}))
|
||||||
|
|]
|
||||||
|
-- pure [cps|
|
||||||
|
-- (letrec ((#{k_l} (κ (#{r_l}) #{m})))
|
||||||
|
-- (prim #{p'} #{k_l}))
|
||||||
|
-- |]
|
||||||
|
|
||||||
convert (Scm.ExpLambda xs e) k = do
|
convert (Scm.ExpLambda xs e) k = do
|
||||||
f <- gensym' "lambda-body"
|
f <- gensym' "lambda-body"
|
||||||
@@ -78,17 +70,25 @@ convert (Scm.ExpLambda xs e) k = do
|
|||||||
|
|
||||||
convert (Scm.ExpApply f xs) k =
|
convert (Scm.ExpApply f xs) k =
|
||||||
telescope (convert1 @es) (f:|xs) \(f':|xs') -> do
|
telescope (convert1 @es) (f:|xs) \(f':|xs') -> do
|
||||||
r <- gensym' "r"
|
r <- gensym' @Name "r"
|
||||||
x <- gensym' "x"
|
x <- gensym' "x"
|
||||||
m <- k [ValVar x]
|
m <- k [ValVar x]
|
||||||
pure $ ExpLetRec [(r, AbsKappa' [x] m)] $
|
pure $ ExpLetRec [(r, AbsKappa' [x] m)] $
|
||||||
ExpApply f' xs' r
|
ExpApply f' xs' (KexpVar r)
|
||||||
|
|
||||||
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 f) k =
|
convert (Scm.ExpIf c t f) k =
|
||||||
convert1 c \c' ->
|
convert1 c \c' -> do
|
||||||
ExpIf c' <$> convert t k <*> convert f k
|
t_l <- gensym' @Name "truthy-cont"
|
||||||
|
f_l <- gensym' @Name "falsey-cont"
|
||||||
|
t' <- convert t k
|
||||||
|
f' <- convert f k
|
||||||
|
pure [cps|
|
||||||
|
(letrec ((#{t_l} (κ () #{t'}))
|
||||||
|
(#{f_l} (κ () #{f'})))
|
||||||
|
(if #{c'} #{t_l} #{f_l}))
|
||||||
|
|]
|
||||||
|
|
||||||
-- let-bindings are desugared into continuation calls whose parameters
|
-- let-bindings are desugared into continuation calls whose parameters
|
||||||
-- are the left-hand sides and whose arguments are the right-hand
|
-- are the left-hand sides and whose arguments are the right-hand
|
||||||
|
|||||||
+282
-83
@@ -1,104 +1,303 @@
|
|||||||
{-# 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
|
import Gyehoek.CPS.Syntax hiding (Hob(..), Obj(..), cont)
|
||||||
import Control.Lens
|
import Gyehoek.Sexp qualified as S
|
||||||
import Data.Maybe (fromMaybe)
|
import Control.Lens hiding (assign)
|
||||||
|
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
|
import Gyehoek.Prelude hiding (assign)
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
data Env = MkEnv
|
newtype Loc = MkLoc { getLoc :: Int }
|
||||||
{ vars :: HashMap Name Obj
|
deriving stock (Generic, Data)
|
||||||
, labels :: HashMap Name (Env, Abs)
|
deriving newtype (Show, Eq, Ord, Enum)
|
||||||
|
|
||||||
|
data Store = MkStore
|
||||||
|
{ nextLoc :: Loc
|
||||||
|
, heap :: IntMap E
|
||||||
}
|
}
|
||||||
deriving (Show, Generic)
|
deriving stock (Show, Generic)
|
||||||
|
|
||||||
eval :: Env -> Exp -> List Obj
|
type instance Index Store = Loc
|
||||||
|
type instance IxValue Store = E
|
||||||
|
|
||||||
eval g (Halt xs) = evalVal g <$> xs
|
instance Ixed Store where ix (MkLoc j) = #heap . ix j
|
||||||
|
instance At Store where at (MkLoc j) = #heap . at j
|
||||||
|
|
||||||
eval g (ExpContinue k xs) =
|
emptyStore :: Store
|
||||||
case g ^. #labels . at k' of
|
emptyStore = MkStore
|
||||||
Just (h, AbsKappa' bs m) -> eval h' m
|
{ nextLoc = MkLoc 0
|
||||||
where
|
, heap = mempty
|
||||||
h' = h & #vars <>~ envOfBinds bs (evalVal g <$> xs)
|
}
|
||||||
_ -> error [i|not a kappa: #{k}|]
|
|
||||||
where
|
|
||||||
k' = case evalVal g k of
|
|
||||||
ObjImm (ImmLabel x) -> x
|
|
||||||
x -> error [i|expected label, got #{x}|]
|
|
||||||
|
|
||||||
eval g (ExpApply f xs ktail) =
|
newtype Env = MkEnv { getEnv :: HashMap Name Loc }
|
||||||
case g ^?! #labels . at f' of
|
deriving stock (Show, Generic, Data)
|
||||||
Just (h,AbsLambda' bs kb m) -> eval h' m
|
deriving newtype (Semigroup, Monoid)
|
||||||
where h' = h & #vars <>~ envOfBinds bs (evalVal g <$> xs)
|
|
||||||
& #labels . at kb .~ (g ^. #labels . at ktail)
|
|
||||||
Nothing -> error [i|undefined label: #{f}|]
|
|
||||||
where
|
|
||||||
f' = case evalVal g f of
|
|
||||||
ObjImm (ImmLabel x) -> x
|
|
||||||
x -> error [i|expected label, got #{x}|]
|
|
||||||
|
|
||||||
eval g (ExpLetRec [(b, ab)] e) = eval g' e
|
|
||||||
where g' = g & #labels . at b ?~ ab
|
|
||||||
|
|
||||||
eval g (ExpPrim p (MkKappa bs e)) = case evalVal g <$> p of
|
|
||||||
PrimAdd x y -> arithBinop (+) x y
|
|
||||||
PrimMul x y -> arithBinop (*) x y
|
|
||||||
PrimSub x y -> arithBinop (-) x y
|
|
||||||
PrimDiv x y -> arithBinop div x y
|
|
||||||
PrimMakeClosure x env -> ret [ObjHob (HobClosure lbl env) ]
|
|
||||||
where
|
|
||||||
lbl = case x of
|
|
||||||
ObjImm (ImmLabel l) -> l
|
|
||||||
_ -> error [i|expected label, got #{x}|]
|
|
||||||
PrimEnvCode x -> ret . (:[]) . ObjImm . ImmLabel $ code
|
|
||||||
where
|
|
||||||
code = case x of
|
|
||||||
ObjHob (HobClosure lbl _) -> lbl
|
|
||||||
_ -> error [i|expected closure, got #{x}|]
|
|
||||||
_ -> error [i|unhandled prim: #{p}|]
|
|
||||||
where
|
|
||||||
ret rs = eval
|
|
||||||
(g & #vars <>~ envOfBinds bs rs)
|
|
||||||
e
|
|
||||||
arithBinop f (ObjImm (ImmInt x)) (ObjImm (ImmInt y)) =
|
|
||||||
ret [ObjImm . ImmInt $ f x y]
|
|
||||||
arithBinop _ x y = error [i|bad arith: #{x}, #{y}|]
|
|
||||||
|
|
||||||
eval _ e = error [i|unimplemented case: #{e}|]
|
|
||||||
|
|
||||||
envOfBinds bs xs = foldMap (uncurry H.singleton) (zip bs xs)
|
|
||||||
|
|
||||||
evalVal :: Env -> Val -> Obj
|
|
||||||
evalVal g = \case
|
|
||||||
ValVar x -> fromMaybe (error [i|unbound: #{x}|]) $ g ^?! #vars . at x
|
|
||||||
ValImm x -> ObjImm x
|
|
||||||
|
|
||||||
emptyEnv :: Env
|
emptyEnv :: Env
|
||||||
emptyEnv = MkEnv
|
emptyEnv = mempty
|
||||||
{ 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
|
type instance Index Env = Name
|
||||||
evalExp = eval emptyEnv
|
type instance IxValue Env = Loc
|
||||||
|
|
||||||
evalProgram :: Program -> List Obj
|
instance Ixed Env where ix j = #getEnv . ix j
|
||||||
evalProgram (MkProgram lam) = eval emptyEnv [cps|
|
instance At Env where at j = #getEnv . at j
|
||||||
(letrec ((start #{lam}))
|
|
||||||
(start halt))
|
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)
|
||||||
|
|
||||||
|
data Mutability
|
||||||
|
= Mut
|
||||||
|
| NoMut
|
||||||
|
deriving (Show, Generic, Data, Eq)
|
||||||
|
|
||||||
|
wrong :: Text -> M Answer a
|
||||||
|
wrong s = ContT \_ -> pure . AnswerError . EvalError $ s
|
||||||
|
|
||||||
|
orWrong
|
||||||
|
:: Getting (First a) s a
|
||||||
|
-> Text -> s -> M Answer a
|
||||||
|
orWrong p msg s = case getFirst . getConst $ p (Const . First . Just) s of
|
||||||
|
Nothing -> wrong msg
|
||||||
|
Just x -> pure x
|
||||||
|
|
||||||
|
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
|
||||||
|
gofetch x = go $ st ^?! ix x
|
||||||
|
go = \case
|
||||||
|
ESymbol s -> S.Symbol s
|
||||||
|
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
|
||||||
|
deriving (Generic, Data)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
truthy :: E -> Bool
|
||||||
|
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 (ExpPrim (PrimCallCC 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 (ExpPrim p k) = do
|
||||||
|
p' <- evalPrim 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}|]
|
||||||
|
|
||||||
|
evalPrim :: Env -> DynPoints -> Prim E -> M Answer (List E)
|
||||||
|
evalPrim g dps = \case
|
||||||
|
PrimAdd x y -> arith2 (+) x y
|
||||||
|
PrimMul x y -> arith2 (*) x y
|
||||||
|
PrimSub x y -> arith2 (-) x y
|
||||||
|
PrimDiv x y -> arith2 div x y
|
||||||
|
PrimZeroP x -> pure1 . EBool . isJust $ x ^? #EInt . only 0
|
||||||
|
PrimCons x y -> pcons x y >>= pure1
|
||||||
|
PrimCar p -> cr p _2
|
||||||
|
PrimCdr p -> cr p _3
|
||||||
|
PrimPairP p -> pure1 . EBool . maybe False (const True) $
|
||||||
|
p ^? #_EPair
|
||||||
|
PrimValues xs -> pure xs
|
||||||
|
PrimList xs -> foldrM pcons ENull xs >>= pure1
|
||||||
|
p -> wrong [i|prim(#{p})은 벌써 나지 않다|]
|
||||||
|
where
|
||||||
|
pure1 x = pure [x]
|
||||||
|
pcons x y = do
|
||||||
|
(x',y') <- traverseOf both new' (x,y)
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
evalExp :: Jalmot :> es => Exp -> Eff es _
|
||||||
|
evalExp e = _
|
||||||
|
|
||||||
|
evalProgram :: Jalmot :> es => Program -> Eff es (List S.Datum)
|
||||||
|
evalProgram (MkProgram lam) = case run (pure . AnswerValues) of
|
||||||
|
(AnswerError jm, _) -> throwError jm
|
||||||
|
(AnswerValues vs, st) -> traverse (S.toDatum $ eGrammar st) vs
|
||||||
|
where
|
||||||
|
run f = (`runState` emptyStore) . (`runContT` f) $ do
|
||||||
|
g <- setup
|
||||||
|
eval g MkDynPoints (ExpLetRec
|
||||||
|
[("_start",AbsLambda lam)]
|
||||||
|
(ExpApply (ValVar "_start") [] (KexpVar "halt")))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
setup :: M Answer Env
|
||||||
|
setup = defines @List
|
||||||
|
[ ("halt", EProcedure prim_halt)
|
||||||
|
]
|
||||||
|
|
||||||
|
prim_halt :: Procedure
|
||||||
|
prim_halt xs _dps = ContT \_ -> pure $ AnswerValues xs
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
module Gyehoek.CPS.Hoist
|
||||||
|
( hoistProgram
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Gyehoek.CPS.Syntax
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import qualified Data.HashMap.Strict as H
|
||||||
|
import Effectful.Writer.Static.Local
|
||||||
|
import Data.Foldable
|
||||||
|
|
||||||
|
|
||||||
|
type Hoist = Writer (HashMap Label Abs)
|
||||||
|
|
||||||
|
hoist :: Hoist :> es => Exp -> Eff es Exp
|
||||||
|
hoist = transformM \case
|
||||||
|
ExpLetRec bs m -> do
|
||||||
|
traverse_ (\(k,v) -> tell $ H.singleton (MkLabel k) v) bs
|
||||||
|
pure m
|
||||||
|
e -> pure e
|
||||||
|
|
||||||
|
hoistProgram :: Program -> Eff es HoistedProgram
|
||||||
|
hoistProgram p = do
|
||||||
|
(body,bindings) <- runWriter $ traverseOf #body hoist p.body
|
||||||
|
pure $ MkHoistedProgram {body,bindings}
|
||||||
@@ -1,191 +0,0 @@
|
|||||||
{-# 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)
|
|
||||||
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)
|
|
||||||
|
|
||||||
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
|
|
||||||
:: (GenSym :> es, Stackify :> es)
|
|
||||||
=> Env -> Exp -> Eff es BlockBuilder
|
|
||||||
|
|
||||||
stackify g (ExpLetRec [(f, AbsKappa kap)] e) = do
|
|
||||||
kap' <- stackifyKappa g kap
|
|
||||||
emitRoutine (Stk.MkRoutine (MkLabel f) . buildBlock $ kap')
|
|
||||||
stackify g e
|
|
||||||
|
|
||||||
stackify g (ExpLetRec [(f, AbsLambda lam)] e) = do
|
|
||||||
lam' <- stackifyLambda g (MkLabel f) lam
|
|
||||||
emitRoutine lam'
|
|
||||||
stackify g e
|
|
||||||
|
|
||||||
stackify g (ExpIf c t f) = do
|
|
||||||
let c' = stackifyVal g c
|
|
||||||
t' <- buildBlock <$> stackify g t
|
|
||||||
f' <- buildBlock <$> stackify g f
|
|
||||||
pure . Tail $ Stk.If c' t' f'
|
|
||||||
|
|
||||||
stackify g (ExpApply f xs ktail) = do
|
|
||||||
pure $
|
|
||||||
Code [ Stk.Push $ stackifyVal g (ValVar ktail)
|
|
||||||
, Stk.Push $ stackifyVal g f
|
|
||||||
] $
|
|
||||||
Code (pushArgs g xs) $
|
|
||||||
Tail (Stk.Call (length xs))
|
|
||||||
|
|
||||||
-- assume that `k` is the continuation on top of the stack lol.
|
|
||||||
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 p kap) = do
|
|
||||||
kap' <- stackifyKappa g kap
|
|
||||||
pure $ Code [ Stk.Prim (stackifyVal g <$> p) ] kap'
|
|
||||||
|
|
||||||
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 -> Kappa
|
|
||||||
-> Eff es BlockBuilder
|
|
||||||
stackifyKappa g (MkKappa xs m) = do
|
|
||||||
let g' = g & #bound <>:~ xs
|
|
||||||
Code (loadArgs g'.bound)
|
|
||||||
<$> stackify g' 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 => Program -> Eff es Stk.Program
|
|
||||||
stackifyProgram (MkProgram lam) = do
|
|
||||||
let g = emptyEnv
|
|
||||||
(_,p) <- runStackify $ emitRoutine =<< stackifyLambda g "start" lam
|
|
||||||
pure p
|
|
||||||
|
|
||||||
letfn :: Program
|
|
||||||
letfn = [cps|
|
|
||||||
(λ (start-ktail0)
|
|
||||||
(letrec ((lambda-body1
|
|
||||||
(λ (x lambda-tail2)
|
|
||||||
(prim (* x x) (κ (r3) (continue lambda-tail2 r3))))))
|
|
||||||
(letrec ((let-body6
|
|
||||||
(κ (square)
|
|
||||||
(letrec ((r4 (κ (x5) (continue start-ktail0 x5))))
|
|
||||||
(square 4 r4)))))
|
|
||||||
(continue let-body6 lambda-body1))))
|
|
||||||
|]
|
|
||||||
|
|
||||||
blah :: Program
|
|
||||||
blah = [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)))
|
|
||||||
|]
|
|
||||||
+147
-26
@@ -10,10 +10,12 @@ module Gyehoek.CPS.Syntax
|
|||||||
, Kappa(..)
|
, Kappa(..)
|
||||||
, Lambda(..)
|
, Lambda(..)
|
||||||
, Exp(..)
|
, Exp(..)
|
||||||
|
, Kexp(..)
|
||||||
, ExpF(..)
|
, ExpF(..)
|
||||||
, Name(..)
|
, Name(..)
|
||||||
, Prim(..)
|
, Prim(..)
|
||||||
, Program(..)
|
, Program(..)
|
||||||
|
, HoistedProgram(..)
|
||||||
, Lit(..)
|
, Lit(..)
|
||||||
, Imm(..)
|
, Imm(..)
|
||||||
, Obj(..)
|
, Obj(..)
|
||||||
@@ -39,6 +41,12 @@ module Gyehoek.CPS.Syntax
|
|||||||
, Free(..)
|
, Free(..)
|
||||||
, pattern ValLabel
|
, pattern ValLabel
|
||||||
, pattern ObjLabel
|
, pattern ObjLabel
|
||||||
|
, absBody
|
||||||
|
, pattern MkAbs
|
||||||
|
, _MkAbs
|
||||||
|
, unhoist
|
||||||
|
, pattern ExpJump
|
||||||
|
, _ExpJump
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
@@ -56,6 +64,10 @@ import Gyehoek.Sexp (G, (:-)(..))
|
|||||||
import qualified Data.InvertibleGrammar.Base as IG
|
import qualified Data.InvertibleGrammar.Base as IG
|
||||||
import Gyehoek.GenSym (Gen)
|
import Gyehoek.GenSym (Gen)
|
||||||
import Data.String (IsString)
|
import Data.String (IsString)
|
||||||
|
import Control.Applicative
|
||||||
|
import qualified Data.HashMap.Strict as H
|
||||||
|
import GHC.Records (HasField (..))
|
||||||
|
import Data.Bifunctor
|
||||||
|
|
||||||
-- Data types
|
-- Data types
|
||||||
|
|
||||||
@@ -96,6 +108,8 @@ pattern ObjLabel l = ObjImm (ImmLabel l)
|
|||||||
-- | a heap object.
|
-- | a heap object.
|
||||||
data Hob
|
data Hob
|
||||||
= HobClosure { label :: Label, env :: List Obj }
|
= HobClosure { label :: Label, env :: List Obj }
|
||||||
|
-- should a continuation have a label, or an Obj?
|
||||||
|
| HobContinuation { cont :: Obj, stack :: NonEmpty (List Obj) }
|
||||||
| HobPair Obj Obj
|
| HobPair Obj Obj
|
||||||
deriving stock (Show, Generic, Data, Eq)
|
deriving stock (Show, Generic, Data, Eq)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
@@ -111,24 +125,60 @@ data Abs
|
|||||||
| AbsLambda Lambda
|
| AbsLambda Lambda
|
||||||
deriving (Show, Generic, Data, Eq)
|
deriving (Show, Generic, Data, Eq)
|
||||||
|
|
||||||
pattern AbsKappa' :: [Name] -> Exp -> Abs
|
pattern AbsKappa' :: List Name -> Exp -> Abs
|
||||||
pattern AbsKappa' xs e = AbsKappa (MkKappa xs e)
|
pattern AbsKappa' xs e = AbsKappa (MkKappa xs e)
|
||||||
|
|
||||||
pattern AbsLambda' :: [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
|
||||||
= ExpPrim (Prim Val) Kappa
|
= 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 Exp Exp
|
| ExpIf Val Name Name
|
||||||
| ExpApply
|
| ExpApply
|
||||||
{ op :: Val
|
{ op :: Val
|
||||||
, args :: List Val
|
, args :: List Val
|
||||||
, cont :: Name
|
, cont :: Kexp
|
||||||
}
|
}
|
||||||
deriving (Show, Generic, Data, Eq)
|
deriving (Show, Generic, Data, Eq)
|
||||||
|
|
||||||
|
data Kexp
|
||||||
|
= KexpVar Name
|
||||||
|
| KexpKappa Kappa
|
||||||
|
deriving (Show, Generic, Data, Eq)
|
||||||
|
|
||||||
pattern Halt :: List Val -> Exp
|
pattern Halt :: List Val -> Exp
|
||||||
pattern Halt xs = ExpContinue (ValLabel "halt") xs
|
pattern Halt xs = ExpContinue (ValLabel "halt") xs
|
||||||
|
|
||||||
@@ -143,6 +193,22 @@ data Program = MkProgram
|
|||||||
}
|
}
|
||||||
deriving (Show, Generic, Data)
|
deriving (Show, Generic, Data)
|
||||||
|
|
||||||
|
data HoistedProgram = MkHoistedProgram
|
||||||
|
{ bindings :: HashMap Label Abs
|
||||||
|
, body :: Lambda
|
||||||
|
}
|
||||||
|
deriving stock (Show, Generic, Data)
|
||||||
|
|
||||||
|
type instance Index HoistedProgram = Label
|
||||||
|
type instance IxValue HoistedProgram = Abs
|
||||||
|
|
||||||
|
instance Ixed HoistedProgram where ix j = #bindings . ix j
|
||||||
|
|
||||||
|
instance At HoistedProgram where at j = #bindings . at j
|
||||||
|
|
||||||
|
instance Each HoistedProgram HoistedProgram Abs Abs where
|
||||||
|
each = #bindings . each
|
||||||
|
|
||||||
makePrisms ''Kappa
|
makePrisms ''Kappa
|
||||||
makePrisms ''Exp
|
makePrisms ''Exp
|
||||||
makeFieldsId ''Exp
|
makeFieldsId ''Exp
|
||||||
@@ -166,6 +232,21 @@ _AbsLambda' = prism'
|
|||||||
|
|
||||||
instance Plated Exp where plate = uniplate
|
instance Plated Exp where plate = uniplate
|
||||||
|
|
||||||
|
absBody :: Lens' Abs Exp
|
||||||
|
absBody = lens
|
||||||
|
(\case
|
||||||
|
AbsLambda lam -> lam.body
|
||||||
|
AbsKappa kap -> kap.body)
|
||||||
|
(\cases
|
||||||
|
(AbsLambda lam) b -> AbsLambda $ lam & #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
|
||||||
|
|
||||||
@@ -208,6 +289,7 @@ instance S.DatumIso Reg where
|
|||||||
instance S.DatumIso Hob where
|
instance S.DatumIso Hob where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (. closure)
|
$ S.With (. closure)
|
||||||
|
$ S.With (. cont)
|
||||||
$ S.With (. conspair)
|
$ S.With (. conspair)
|
||||||
$ S.End
|
$ S.End
|
||||||
where
|
where
|
||||||
@@ -215,7 +297,13 @@ instance S.DatumIso Hob where
|
|||||||
-- closures can be printed, but not parsed.
|
-- closures can be printed, but not parsed.
|
||||||
closure :: G (Datum :- t) (List Obj :- Label :- t)
|
closure :: G (Datum :- t) (List Obj :- Label :- t)
|
||||||
closure = IG.Flip $ IG.PartialIso
|
closure = IG.Flip $ IG.PartialIso
|
||||||
(\(env:-code:-t) -> S.Unreadable "#<procedure>" :- t)
|
(\(env:-code:-t) -> S.Unreadable [i|\#<procedure $#{code}>|] :- t)
|
||||||
|
(const . Left $ mempty)
|
||||||
|
cont :: G (Datum :- t) (NonEmpty (List Obj) :- _ :- t)
|
||||||
|
cont = IG.Flip $ IG.PartialIso
|
||||||
|
(\(_ :- l :- t) ->
|
||||||
|
let x = S.encodeOrShow' @Text S.datumIso l
|
||||||
|
in S.Unreadable [i|\#<continuation #{x}>|] :- t)
|
||||||
(const . Left $ mempty)
|
(const . Left $ mempty)
|
||||||
|
|
||||||
instance S.DatumIso Lambda where
|
instance S.DatumIso Lambda where
|
||||||
@@ -262,30 +350,51 @@ instance S.DatumIso Exp where
|
|||||||
if_ = S.ifLike "if"
|
if_ = S.ifLike "if"
|
||||||
S.datumIso S.datumIso S.datumIso
|
S.datumIso S.datumIso S.datumIso
|
||||||
app :: forall t.
|
app :: forall t.
|
||||||
G (Datum :- t) (Name :- ([Val] :- (Val :- t)))
|
G (Datum :- t) (Kexp :- List Val :- Val :- t)
|
||||||
app = S.list $ S.el (S.datumIso @Val)
|
app = S.list $
|
||||||
-- >>> S.flipped Gyehoek.Datum.nonEmptyGrammar
|
S.flipped (S.PartialIso
|
||||||
|
(\(S.MkListContext ctx :- t) ->
|
||||||
|
case ctx of
|
||||||
|
f:kexp:xs -> S.MkListContext (f : snoc xs kexp) :- t
|
||||||
|
_ -> error "unreachable")
|
||||||
|
(\(S.MkListContext ctx :- t) ->
|
||||||
|
case unsnoc ctx of
|
||||||
|
Just (f:xs,kexp) -> Right $ S.MkListContext (f:kexp:xs) :- t
|
||||||
|
_ -> Left $ S.expected "continuation arg"))
|
||||||
|
>>> S.el (S.datumIso @Val)
|
||||||
|
>>> S.el (S.datumIso @Kexp)
|
||||||
>>> S.rest (S.datumIso @Val)
|
>>> S.rest (S.datumIso @Val)
|
||||||
-- >>> _
|
>>> S.onTail S.swap
|
||||||
>>> S.onTail (S.flipped $ IG.PartialIso
|
|
||||||
(\(karg :- args :- op :- t) ->
|
|
||||||
(args ++ [ValVar karg]) :- op :- t)
|
|
||||||
(\(xs :- op :- t) -> case xs ^? _Snoc of
|
|
||||||
Just (args,preview #ValVar -> Just karg) ->
|
|
||||||
Right $ karg:- args :- op :- t
|
|
||||||
_ -> Left $ S.expected "continuation arg"
|
|
||||||
))
|
|
||||||
-- prim = S.headTagged2 "prim"
|
|
||||||
-- (primDatumIso id (S.datumIso @Val))
|
|
||||||
-- (S.datumIso @Kappa)
|
|
||||||
prim = S.list $
|
prim = S.list $
|
||||||
S.el (S.decorate S.SynBuiltin >>> S.sym "prim")
|
S.el (S.decorate S.SynBuiltin >>> S.sym "prim")
|
||||||
>>> S.el (primDatumIso id (S.datumIso @Val))
|
>>> S.el (primDatumIso id (S.datumIso @Val))
|
||||||
>>> S.el S.datumIso
|
>>> S.el S.datumIso
|
||||||
|
|
||||||
|
instance S.DatumIso Kexp where
|
||||||
|
datumIso = S.match
|
||||||
|
$ S.With (S.datumIso @Name >>>)
|
||||||
|
$ S.With (S.datumIso @Kappa >>>)
|
||||||
|
$ S.End
|
||||||
|
|
||||||
instance S.DatumIso Program where
|
instance S.DatumIso Program where
|
||||||
datumIso = S.with \prog -> S.datumIso @Lambda >>> prog
|
datumIso = S.with \prog -> S.datumIso @Lambda >>> prog
|
||||||
|
|
||||||
|
-- the printed representation is pretty dishonest in its current
|
||||||
|
-- state. consider the following hoisted program:
|
||||||
|
--
|
||||||
|
-- (letrec ((k (κ () (continue start-ktail 123))))
|
||||||
|
-- (λ (start-ktail)
|
||||||
|
-- (continue k)))
|
||||||
|
--
|
||||||
|
-- here, `start-ktail` is bound in `k`, but the printed representation
|
||||||
|
-- fails to reflect that.
|
||||||
|
instance S.DatumIso HoistedProgram where
|
||||||
|
datumIso = S.with \prog ->
|
||||||
|
S.letLike "letrec"
|
||||||
|
(S.datumIso @Label) (S.datumIso @Abs) (S.datumIso @Lambda)
|
||||||
|
>>> S.onTail (S.iso H.fromList H.toList)
|
||||||
|
>>> prog
|
||||||
|
|
||||||
|
|
||||||
-- quasiquoters
|
-- quasiquoters
|
||||||
|
|
||||||
@@ -298,6 +407,7 @@ instance CPS Kappa where toCPS = S.fromDatumUnsafe S.datumIso
|
|||||||
instance CPS Lambda where toCPS = S.fromDatumUnsafe S.datumIso
|
instance CPS Lambda where toCPS = S.fromDatumUnsafe S.datumIso
|
||||||
instance CPS Abs where toCPS = S.fromDatumUnsafe S.datumIso
|
instance CPS Abs where toCPS = S.fromDatumUnsafe S.datumIso
|
||||||
instance CPS Program where toCPS = S.fromDatumUnsafe S.datumIso
|
instance CPS Program where toCPS = S.fromDatumUnsafe S.datumIso
|
||||||
|
instance CPS HoistedProgram where toCPS = S.fromDatumUnsafe S.datumIso
|
||||||
|
|
||||||
cps :: S.QuasiQuoter
|
cps :: S.QuasiQuoter
|
||||||
cps = S.makeSx' [| toCPS |]
|
cps = S.makeSx' [| toCPS |]
|
||||||
@@ -320,7 +430,8 @@ class Free a where
|
|||||||
freeWithBound :: HashSet Name -> a -> HashSet Name
|
freeWithBound :: HashSet Name -> a -> HashSet Name
|
||||||
freeWithBound bound = HS.fromList . freeWithBound' bound
|
freeWithBound bound = HS.fromList . freeWithBound' bound
|
||||||
|
|
||||||
-- | Free variables given in the order of their appearance.
|
-- | Free variables given in the same left-to-right order they
|
||||||
|
-- appear.
|
||||||
free' :: a -> List Name
|
free' :: a -> List Name
|
||||||
free' = freeWithBound' mempty
|
free' = freeWithBound' mempty
|
||||||
|
|
||||||
@@ -330,11 +441,21 @@ instance Free Abs where
|
|||||||
freeWithBound' bound (AbsKappa kap) = freeWithBound' bound kap
|
freeWithBound' bound (AbsKappa kap) = freeWithBound' bound kap
|
||||||
freeWithBound' bound (AbsLambda lam) = freeWithBound' bound lam
|
freeWithBound' bound (AbsLambda lam) = freeWithBound' bound lam
|
||||||
|
|
||||||
|
mif :: Alternative f => (a -> Bool) -> a -> f a
|
||||||
|
mif p a
|
||||||
|
| p a = pure a
|
||||||
|
| otherwise = empty
|
||||||
|
|
||||||
|
instance Free Kexp where
|
||||||
|
freeWithBound' bound = \case
|
||||||
|
KexpVar x -> mif (`notElem` bound) x
|
||||||
|
KexpKappa kap -> freeWithBound' bound kap
|
||||||
|
|
||||||
instance Free Exp where
|
instance Free Exp where
|
||||||
freeWithBound' bound = \case
|
freeWithBound' bound = \case
|
||||||
ExpPrim p k ->
|
ExpPrim p k ->
|
||||||
p & toListOf (folded . #ValVar . filtered (`notElem` bound))
|
(p ^.. folded . #ValVar . filtered (`notElem` bound))
|
||||||
& (<> freeWithBound' bound k)
|
++ freeWithBound' bound k
|
||||||
ExpLetRec bs m ->
|
ExpLetRec bs m ->
|
||||||
foldMapOf (each . _2) (freeWithBound' bound') bs
|
foldMapOf (each . _2) (freeWithBound' bound') bs
|
||||||
<> freeWithBound' bound' m
|
<> freeWithBound' bound' m
|
||||||
@@ -342,10 +463,10 @@ instance Free Exp where
|
|||||||
ExpContinue k xs -> filter (`notElem` bound) ((k:xs) ^.. each . #ValVar)
|
ExpContinue k xs -> filter (`notElem` bound) ((k:xs) ^.. each . #ValVar)
|
||||||
ExpIf c t f ->
|
ExpIf c t f ->
|
||||||
(c ^.. #ValVar . filtered (`notElem` bound))
|
(c ^.. #ValVar . filtered (`notElem` bound))
|
||||||
<> freeWithBound' bound t <> freeWithBound' bound f
|
<> mif (`notElem` bound) t <> mif (`notElem` bound) f
|
||||||
ExpApply f xs k ->
|
ExpApply f xs k ->
|
||||||
(f:xs) ^.. (each . #ValVar . filtered (`notElem` bound))
|
(f:xs) ^.. (each . #ValVar . filtered (`notElem` bound))
|
||||||
<> (k ^.. filtered (`notElem` bound))
|
<> freeWithBound' bound k
|
||||||
|
|
||||||
instance Free Kappa where
|
instance Free Kappa where
|
||||||
freeWithBound' bound (MkKappa xs m) =
|
freeWithBound' bound (MkKappa xs m) =
|
||||||
|
|||||||
+26
-32
@@ -1,5 +1,5 @@
|
|||||||
module Gyehoek.Driver
|
module Gyehoek.Driver
|
||||||
(main, lower_e2e, convert_e2e, parse_e2e, readScm, eval_e2e)
|
(main, convert_e2e, parse_e2e, readScm, eval_cps1_e2e, eval_cps2_e2e)
|
||||||
where
|
where
|
||||||
|
|
||||||
import Gyehoek.Options
|
import Gyehoek.Options
|
||||||
@@ -17,7 +17,6 @@ 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)
|
||||||
@@ -25,16 +24,14 @@ 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 ((>>>))
|
||||||
import Gyehoek.Prelude
|
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)
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@@ -118,29 +115,20 @@ 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
|
||||||
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso cps
|
S.writeDatum cps
|
||||||
closedCps <- closeProgram cps
|
closedCps <- closeProgram cps
|
||||||
when opts.dumpClosed do
|
when opts.dumpClosed do
|
||||||
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso closedCps
|
S.writeDatum closedCps
|
||||||
|
hoistedCps <- hoistProgram closedCps
|
||||||
|
when opts.dumpHoisted do
|
||||||
|
S.writeDatum hoistedCps
|
||||||
let rt_is p = is (_Just . p) opts.runtime
|
let rt_is p = is (_Just . p) opts.runtime
|
||||||
dumpOrRun opts.dumpStackified (rt_is #Stackify)
|
when (rt_is #HigherOrderCPS) do
|
||||||
(stackifyProgram closedCps)
|
CPS.evalProgram cps
|
||||||
(hPutStrLn FS.stdout <=< S.encodeDataWith S.dataIso)
|
>>= S.writeData
|
||||||
(eval >=> fmap writeObj
|
|
||||||
>>> T.unwords
|
|
||||||
>>> hPutStrLn FS.stdout)
|
|
||||||
when (rt_is #CPS) do
|
when (rt_is #CPS) do
|
||||||
closedCps
|
CPS.evalProgram closedCps
|
||||||
& CPS.evalProgram
|
>>= S.writeData
|
||||||
& 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 closedCps >>= traceEval
|
|
||||||
|
|
||||||
parse_e2e :: FilePath -> IO Scm.Program
|
parse_e2e :: FilePath -> IO Scm.Program
|
||||||
parse_e2e = runJalmotIO . runFileSystem . readScm
|
parse_e2e = runJalmotIO . runFileSystem . readScm
|
||||||
@@ -149,12 +137,18 @@ convert_e2e :: FilePath -> IO CPS.Program
|
|||||||
convert_e2e = runJalmotIO . runFileSystem . runGenSym
|
convert_e2e = runJalmotIO . runFileSystem . runGenSym
|
||||||
. (closeProgram <=< convertProgram <=< readScm)
|
. (closeProgram <=< convertProgram <=< readScm)
|
||||||
|
|
||||||
lower_e2e :: FilePath -> IO Text
|
eval_cps1_e2e :: FilePath -> IO Text
|
||||||
lower_e2e =
|
eval_cps1_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
||||||
runJalmotIO . runFileSystem . runGenSym
|
readScm fp
|
||||||
. (lowerProgram <=< closeProgram <=< convertProgram <=< readScm)
|
>>= convertProgram
|
||||||
|
>>= closeProgram
|
||||||
|
>>= CPS.evalProgram
|
||||||
|
>>= pure . S.encodeOrShowData' S.dataIso
|
||||||
|
|
||||||
eval_e2e :: FilePath -> IO (List Obj)
|
eval_cps2_e2e :: FilePath -> IO Text
|
||||||
eval_e2e fp = runJalmotIO . runFileSystem . runGenSym $ do
|
eval_cps2_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
||||||
stk <- stackifyProgram <=< closeProgram <=< convertProgram <=< readScm $ fp
|
readScm fp
|
||||||
eval stk
|
>>= convertProgram
|
||||||
|
-- >>= closeProgram
|
||||||
|
>>= CPS.evalProgram
|
||||||
|
>>= pure . S.encodeOrShowData' S.dataIso
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ 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
|
||||||
@@ -66,6 +67,7 @@ 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
|
||||||
|
|||||||
+22
-12
@@ -13,14 +13,13 @@ import Data.Foldable
|
|||||||
import Gyehoek.Prelude hiding (argument)
|
import Gyehoek.Prelude hiding (argument)
|
||||||
|
|
||||||
|
|
||||||
data Runtime = Stackify | Wasm | CPS
|
data Runtime = Wasm | CPS | HigherOrderCPS
|
||||||
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)
|
||||||
|
|
||||||
@@ -28,30 +27,30 @@ data Options = MkOptions
|
|||||||
{ dumpClosed :: Bool
|
{ dumpClosed :: Bool
|
||||||
, dumpCPS :: Bool
|
, dumpCPS :: Bool
|
||||||
, dumpParsed :: Bool
|
, dumpParsed :: Bool
|
||||||
, dumpStackified :: Bool
|
, dumpHoisted :: Bool
|
||||||
, traceStackified :: Bool
|
, noColour :: 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","stackified","wasm"]
|
languageValues = ["scheme","cps","closed","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)
|
||||||
"cps" -> Just (Just CPS)
|
"cps1" -> Just (Just CPS)
|
||||||
|
("cps";"higher-order-cps") -> Just (Just HigherOrderCPS)
|
||||||
"none" -> Just Nothing
|
"none" -> Just Nothing
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
@@ -59,16 +58,19 @@ 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")
|
||||||
traceStackified <- switch (long "trace-stackified")
|
dumpHoisted <- switch (long "dump-hoisted")
|
||||||
|
noColour <- switch . fold $
|
||||||
|
[ long "no-colour"
|
||||||
|
, 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 Stackify)
|
, value (Just HigherOrderCPS)
|
||||||
, completeWith runtimeValues
|
, completeWith runtimeValues
|
||||||
, showDefaultWith $ const "stackify"
|
, showDefaultWith $ const "higher-order-cps"
|
||||||
, metavar "RUNTIME"
|
, metavar "RUNTIME"
|
||||||
]
|
]
|
||||||
sourceLanguage <- option languageReader . fold $
|
sourceLanguage <- option languageReader . fold $
|
||||||
@@ -79,6 +81,14 @@ 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'
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
{-# LANGUAGE OrPatterns #-}
|
{-# LANGUAGE OrPatterns #-}
|
||||||
{-# LANGUAGE PatternSynonyms #-}
|
{-# LANGUAGE PatternSynonyms #-}
|
||||||
{-# LANGUAGE DeriveAnyClass #-}
|
{-# LANGUAGE DeriveAnyClass #-}
|
||||||
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
module Gyehoek.Scheme.Syntax
|
module Gyehoek.Scheme.Syntax
|
||||||
( Name(..)
|
( Name(..)
|
||||||
, Prim(..)
|
, Prim(..)
|
||||||
@@ -53,6 +54,8 @@ 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 }
|
||||||
@@ -81,13 +84,17 @@ data Prim e
|
|||||||
| PrimZeroP e
|
| PrimZeroP e
|
||||||
| PrimNewline
|
| PrimNewline
|
||||||
| PrimMakeClosure { code :: e, env :: List e }
|
| PrimMakeClosure { code :: e, env :: List e }
|
||||||
|
| PrimMakeSharedClosure { codes :: List e, env :: List e }
|
||||||
|
| PrimGetEnv
|
||||||
| PrimEnv
|
| PrimEnv
|
||||||
| PrimEnvRef Int
|
| PrimEnvRef Int
|
||||||
| PrimEnvCode e
|
|
||||||
| PrimCallCC e
|
| PrimCallCC e
|
||||||
| PrimCaptureCC
|
| PrimCaptureCC
|
||||||
|
| PrimInvokeCC e (List e)
|
||||||
| PrimValues (List e)
|
| PrimValues (List e)
|
||||||
| PrimCallWithValues e e
|
| PrimCallWithValues e e
|
||||||
|
| PrimPairP e
|
||||||
|
| PrimList (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)
|
||||||
|
|
||||||
@@ -126,8 +133,37 @@ data CommandOrDef
|
|||||||
deriving stock (Show, Generic, Data)
|
deriving stock (Show, Generic, Data)
|
||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
newtype Program = MkProgram
|
newtype LibName = MkLibName { inner :: NonEmpty Name }
|
||||||
{ commandsAndDefs :: List CommandOrDef
|
deriving stock (Show, Generic, Data)
|
||||||
|
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)
|
||||||
@@ -168,13 +204,19 @@ primDatumIso namefn a = S.match
|
|||||||
$ S.With (. ht1 "zero?")
|
$ S.With (. ht1 "zero?")
|
||||||
$ S.With (. ht0 "newline")
|
$ S.With (. ht0 "newline")
|
||||||
$ S.With (. ht1' "make-closure")
|
$ S.With (. ht1' "make-closure")
|
||||||
|
$ S.With (. S.headTagged2 (namefn "make-shared-closure")
|
||||||
|
(S.list $ S.rest a)
|
||||||
|
(S.list $ S.rest a))
|
||||||
|
$ S.With (. ht0 "get-env")
|
||||||
$ S.With (. ht0 "env")
|
$ S.With (. ht0 "env")
|
||||||
$ S.With (. S.headTagged1 (namefn "env-ref") S.int)
|
$ S.With (. S.headTagged1 (namefn "env-ref") S.int)
|
||||||
$ S.With (. ht1 "env-code")
|
|
||||||
$ S.With (. ht1 "call/cc")
|
$ S.With (. ht1 "call/cc")
|
||||||
$ S.With (. ht0 "capture/cc")
|
$ S.With (. ht0 "capture/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
|
||||||
@@ -234,8 +276,58 @@ 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.dataIso @(List CommandOrDef) >>> S.iso coerce coerce
|
dataIso = S.with \g ->
|
||||||
|
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,6 +15,7 @@ module Gyehoek.Sexp.Grammar
|
|||||||
, encodeDataTest
|
, encodeDataTest
|
||||||
, encodeDataTestColour
|
, encodeDataTestColour
|
||||||
, encodeOrShow'
|
, encodeOrShow'
|
||||||
|
, encodeOrShowData'
|
||||||
, decodeDataWith
|
, decodeDataWith
|
||||||
, encodeDataWith'
|
, encodeDataWith'
|
||||||
, decodeTest
|
, decodeTest
|
||||||
@@ -28,6 +29,8 @@ module Gyehoek.Sexp.Grammar
|
|||||||
, fromDatumUnsafe
|
, fromDatumUnsafe
|
||||||
, Control.Category.id
|
, Control.Category.id
|
||||||
, fromDataUnsafe
|
, fromDataUnsafe
|
||||||
|
, writeDatum
|
||||||
|
, writeData
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
@@ -45,6 +48,8 @@ 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
|
||||||
@@ -126,6 +131,39 @@ 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
|
||||||
|
|
||||||
@@ -150,5 +188,8 @@ 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
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, DatumGrammar
|
, DatumGrammar
|
||||||
, DataGrammar
|
, DataGrammar
|
||||||
, Grammar
|
, Grammar
|
||||||
, ListContext
|
, ListContext(..)
|
||||||
, (:-)((:-))
|
, (:-)((:-))
|
||||||
-- * lists
|
-- * lists
|
||||||
, list
|
, list
|
||||||
@@ -17,6 +17,7 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, el
|
, el
|
||||||
, rest
|
, rest
|
||||||
, restData
|
, restData
|
||||||
|
, nonEmptyData
|
||||||
, headTagged0'
|
, headTagged0'
|
||||||
, headTagged0
|
, headTagged0
|
||||||
, headTagged1'
|
, headTagged1'
|
||||||
@@ -32,6 +33,8 @@ 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
|
||||||
@@ -49,7 +52,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 (iso, cons, coerced, Iso, Simple, simple)
|
import Gyehoek.Prelude hiding (traversed, 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)
|
||||||
@@ -57,6 +60,7 @@ 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
|
||||||
@@ -238,6 +242,14 @@ restData g =
|
|||||||
>>> 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)
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
{-# 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
|
|
||||||
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.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) ||]
|
|
||||||
@@ -1,496 +0,0 @@
|
|||||||
{-# 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
|
|
||||||
& #stack %~ pushFrame newFrame
|
|
||||||
-- it is not essential we clear the registers, but it'll
|
|
||||||
-- make bugs more obvious.
|
|
||||||
& #registers .~ mempty
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
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}|]
|
|
||||||
PrimEnvCode env ->
|
|
||||||
case env of
|
|
||||||
ObjHob (HobClosure l _) -> ret1 . ObjImm . ImmLabel $ l
|
|
||||||
_ -> vmerror [i|expected closure, got #{env}|]
|
|
||||||
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}|]
|
|
||||||
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
|
|
||||||
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
|
|
||||||
|
|
||||||
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,4 +1,3 @@
|
|||||||
{- HLINT ignore "Use newtype instead of data" -}
|
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
{-# LANGUAGE OverloadedLists #-}
|
{-# LANGUAGE OverloadedLists #-}
|
||||||
{-# LANGUAGE TemplateHaskellQuotes #-}
|
{-# LANGUAGE TemplateHaskellQuotes #-}
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -5,53 +5,75 @@ 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)
|
import Test.Tasty.ExpectedFailure (ignoreTestBecause, expectFail)
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
test_cpsInterpreter =
|
brokenEvalTests :: List String
|
||||||
ignoreTestBecause "i forgorrrr" $
|
brokenEvalTests =
|
||||||
testGroup "cps interpreter" $
|
[]
|
||||||
[ primitives
|
-- [ "adder"
|
||||||
, testCase "halt with constant" do
|
-- , "apply2"
|
||||||
evalsTo [ObjImm (ImmInt 123)] [cps|
|
-- , "apply-twice"
|
||||||
(continue halt 123)
|
-- , "arith"
|
||||||
|]
|
-- , "begin-1"
|
||||||
, testCase "identity cont" do
|
-- , "callcc-constant"
|
||||||
evalsTo [ObjImm (ImmInt 154)] [cps|
|
-- , "callcc-discard"
|
||||||
(letrec ((id (κ (x)
|
-- , "callcc-early-exit-1"
|
||||||
(continue halt x))))
|
-- , "callcc-early-exit-2"
|
||||||
(continue id 154))
|
-- , "callcc-early-exit-3"
|
||||||
|]
|
-- , "callcc-early-exit-4"
|
||||||
, testCase "identity function" do
|
-- , "callcc-early-exit-5"
|
||||||
evalsTo [ObjImm (ImmInt 456)] [cps|
|
-- , "callcc-early-exit-6"
|
||||||
(letrec ((id (λ (x ktail)
|
-- , "callcc-nested-1"
|
||||||
(continue ktail x))))
|
-- , "callcc-nested-2"
|
||||||
(id 456 halt))
|
-- , "complicated-1"
|
||||||
|]
|
-- , "cons-1"
|
||||||
, testCase "square" do
|
-- , "factorial"
|
||||||
evalsTo [ObjImm (ImmInt 81)] [cps|
|
-- , "false"
|
||||||
(letrec ((square (λ (x ktail)
|
-- , "fn-of-fn"
|
||||||
(prim (* x x)
|
-- , "if-false"
|
||||||
(κ (r) (continue ktail r))))))
|
-- , "if-number"
|
||||||
(square 9 halt))
|
-- , "if-true"
|
||||||
|]
|
-- , "lambda"
|
||||||
|
-- , "letrec-fn"
|
||||||
|
-- , "let-fn"
|
||||||
|
-- , "lit-int"
|
||||||
|
-- , "square"
|
||||||
|
-- , "true"
|
||||||
|
-- ]
|
||||||
|
|
||||||
|
test_eval :: IO TestTree
|
||||||
|
test_eval = do
|
||||||
|
cs <- listDirectory "golden/exec"
|
||||||
|
<&> fmap ("golden/exec" </>)
|
||||||
|
pure $ testGroup "cps interpreter"
|
||||||
|
[ testGroup "higher-order" $ cpsCase Driver.eval_cps2_e2e <$> cs
|
||||||
|
-- , testGroup "first-order" $ cpsCase Driver.eval_cps1_e2e <$> cs
|
||||||
]
|
]
|
||||||
|
|
||||||
evalsTo :: HasCallStack => List Obj -> Sut.Exp -> Assertion
|
maybeBroken name broken = applyWhen (name `elem` broken) expectFail
|
||||||
evalsTo rs e = Sut.evalExp e @?= rs
|
|
||||||
|
|
||||||
primitives = testGroup "primitives"
|
cpsCase :: (FilePath -> IO Text) -> FilePath -> TestTree
|
||||||
[ testGroup "arith"
|
cpsCase f test =
|
||||||
[ testCase "basic 1" do
|
maybeBroken testName brokenEvalTests $
|
||||||
evalsTo [ObjImm (ImmInt 20)] [cps|
|
goldenVsAction testName resultFile action printProcResult
|
||||||
(prim (* 4 5)
|
where
|
||||||
(κ (x) (continue halt x)))
|
testName = takeFileName test
|
||||||
|]
|
resultFile = test </> "exec"
|
||||||
, testCase "basic 2" do
|
sourceFile = test </> "source.scm"
|
||||||
evalsTo [ObjImm (ImmInt 35)] [cps|
|
action = catch @SomeException
|
||||||
(prim (* 2 16)
|
(do r <- f sourceFile
|
||||||
(κ (x) (prim (+ x 3)
|
pure $!! ( ExitSuccess
|
||||||
(κ (r) (continue halt r)))))
|
, r
|
||||||
|]
|
, "" ))
|
||||||
]
|
\e -> pure (ExitFailure 1, "", T.pack $ displayException e)
|
||||||
]
|
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
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"]
|
||||||
"k")
|
(Sut.KexpVar "k"))
|
||||||
[cps|(f x y k)|]
|
[cps|(f x y k)|]
|
||||||
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
||||||
[] "k")
|
[] (Sut.KexpVar "k"))
|
||||||
[cps|(f k)|]
|
[cps|(f k)|]
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,93 +0,0 @@
|
|||||||
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 =
|
|
||||||
[ "callcc-early-exit-1"
|
|
||||||
, "callcc-early-exit-2"
|
|
||||||
, "callcc-early-exit-3"
|
|
||||||
, "callcc-early-exit-4"
|
|
||||||
, "callcc-early-exit-5"
|
|
||||||
, "callcc-early-exit-6"
|
|
||||||
, "callcc-nested-1"
|
|
||||||
, "callcc-nested-2"
|
|
||||||
, "callcc-discard"
|
|
||||||
, "callcc-constant"
|
|
||||||
]
|
|
||||||
|
|
||||||
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
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
{-# 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