Compare commits
37
Commits
3aac990fac
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
309f722712 | ||
|
|
31cc2b1720 | ||
|
|
3cdae9eab4 | ||
|
|
c6036ffbb4 | ||
|
|
6ebe92e7cd | ||
|
|
0f9ba3c51e | ||
|
|
10bd6b733a | ||
|
|
d1588bd917 | ||
|
|
c495fc064a | ||
|
|
ac39175767 | ||
|
|
31c610db34 | ||
|
|
1ec3d35282 | ||
|
|
9f37d10e4f | ||
|
|
ba5dc401d9 | ||
|
|
bc599df65f | ||
|
|
f26ac50d4e | ||
|
|
3196d8db84 | ||
|
|
75e6c963c7 | ||
|
|
25f1f008bd | ||
|
|
276c2c1249 | ||
|
|
03797d573b | ||
|
|
0df7280236 | ||
|
|
a09c00badd | ||
|
|
e7c0ae9161 | ||
|
|
5ccb3f3e1a | ||
|
|
9cb169f9b8 | ||
|
|
c0a44c89b4 | ||
|
|
49292d5d01 | ||
|
|
679cc076ad | ||
|
|
8048573cd8 | ||
|
|
bbb5d6e99f | ||
|
|
1f40120740 | ||
|
|
196dd0d1b3 | ||
|
|
009a154a6e | ||
|
|
21b9f0e69d | ||
|
|
87baed9efc | ||
|
|
796b967686 |
@@ -9,6 +9,9 @@
|
||||
. (progn (defun apply-cabal-fmt-h ()
|
||||
(haskell-mode-buffer-apply-command "cabal-fmt"))
|
||||
(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
|
||||
. ((eval
|
||||
. (progn (defun display-ansi ()
|
||||
|
||||
+2
-1
@@ -8,4 +8,5 @@ dist-newstyle
|
||||
*.tix
|
||||
.direnv
|
||||
result
|
||||
play/
|
||||
play/
|
||||
trace.html
|
||||
@@ -132,3 +132,34 @@ multiple ~env-ref~ calls could probably be replaced with a primitive that loads
|
||||
$code)
|
||||
1))))
|
||||
#+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,100 @@
|
||||
* rationale?
|
||||
|
||||
previously, the VM's stack was used for storing local variables across blocks; a Scheme procedure was split into several low-level routines (one for the procedure itself and one for each continuation), and the stack was used as a communication channel for these separate routines. in contrast, registers were local to each routine. this aligns with Wasm's model of functions pretty well, with Wasm /locals/ acting as the VM's /registers/, and a global mutable stack serving as fallback.
|
||||
|
||||
this worked quite well until it became time to implement ~call/cc~.
|
||||
|
||||
we are considering making the following alterations to the VM:
|
||||
- explicitly segment the stack into frames.
|
||||
- passing procedures and return addresses on the stack.
|
||||
- new instructions:
|
||||
+ ~(tail-call /n/)~
|
||||
+ ~(call /n/)~
|
||||
+ ~(load /r/ /n/)~
|
||||
+ ~(return /n/)~
|
||||
|
||||
* scratchpad
|
||||
|
||||
#+begin_src scheme
|
||||
(letrec ((fac (λ (n)
|
||||
(if (zero? n)
|
||||
1
|
||||
(* n (fac (- n 1)))))))
|
||||
(fac 3))
|
||||
#+end_src
|
||||
|
||||
#+begin_src scheme
|
||||
(λ (ktail0)
|
||||
(letrec ((fac
|
||||
(λ (n ktail1)
|
||||
(zero?
|
||||
n
|
||||
(κ (x0)
|
||||
(if x0
|
||||
(continue ktail1 1)
|
||||
(- n 1
|
||||
(κ (x1)
|
||||
(fac x1
|
||||
(κ (x2)
|
||||
(* n x2 ktail1)))))))))))
|
||||
(fac 3)))
|
||||
#+end_src
|
||||
|
||||
#+begin_example
|
||||
n ktail1
|
||||
| |
|
||||
| | x0
|
||||
| | |
|
||||
| | ^
|
||||
| |
|
||||
| | x1
|
||||
| | |
|
||||
| | ^
|
||||
| |
|
||||
| | x2
|
||||
| | |
|
||||
^ ^ ^
|
||||
#+end_example
|
||||
|
||||
#+begin_src scheme
|
||||
(define $fac-c0
|
||||
(pop! %x0 0) ; [ x0 $fac-c0 n $fac ktail1 ]
|
||||
(if %x0 ; [ $fac-c0 n $fac ktail1 ]
|
||||
;; every variable but `ktail1' is dead so we pop them all.
|
||||
;; this probably means that `if' should take two continuations
|
||||
;; rather than two blocks.
|
||||
(then (push! 1) ; [ $fac-c0 n $fac ktail1 ]
|
||||
(return 1)) ; [ 1 $fac-c0 n $fac ktail1 ]
|
||||
(else (load %n 1) ; [ $fac-c0 n $fac ktail1 ]
|
||||
(prim %x1 (- %n 1)) ; [ $fac-c0 n $fac ktail1 ]
|
||||
(push! $fac-c1) ; [ $fac-c0 n $fac ktail1 ]
|
||||
(push! $fac) ; [ $fac-c1 $fac-c0 n $fac ktail1 ]
|
||||
(push! %x1) ; [ $fac $fac-c1 $fac-c0 n $fac ktail1 ]
|
||||
(call 1) ; [ x1 $fac $fac-c1 $fac-c0 n $fac ktail1 ]
|
||||
)))
|
||||
|
||||
(define $fac-c1
|
||||
(pop! %x2) ; [ x2 $fac-c1 $fac-c0 n $fac ktail1 ]
|
||||
(load %n 3) ; [ $fac-c1 $fac-c0 n $fac ktail1 ]
|
||||
(prim %x3 (* %n %x2))
|
||||
(push! %x3) ; [ $fac-c1 $fac-c0 n $fac ktail1 ]
|
||||
(return 1) ; [ x3 $fac-c1 $fac-c0 n $fac ktail1 ]
|
||||
)
|
||||
|
||||
(define $fac
|
||||
(load %ktail1 2) ; [ n $fac ktail1 ]
|
||||
(load %n 0) ; [ n $fac ktail1 ]
|
||||
(push! $fac-c0) ; [ n $fac ktail1 ]
|
||||
(push! $zero?) ; [ $fac-c0 n $fac ktail1 ]
|
||||
(push! %n) ; [ $zero? $fac-c0 n $fac ktail1 ]
|
||||
(call 1) ; [ n $zero? $fac-c0 n $fac ktail1 ]
|
||||
)
|
||||
|
||||
(define $start
|
||||
(push! $fac) ; [ $start ktail0 ]
|
||||
(push! 3) ; [ $fac $start ktail0 ]
|
||||
(tail-call 1) ; [ 3 $fac $start ktail0 ]
|
||||
;; ↑ `tail-call' knows how to dispose of the caller's stack frame.
|
||||
)
|
||||
#+end_src
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
(begin 123 456) ; => 456
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > #t
|
||||
@@ -0,0 +1,12 @@
|
||||
(letrec ((iter (λ (n f)
|
||||
(if (zero? n)
|
||||
#f
|
||||
(begin (f n)
|
||||
(iter (- n 1) f))))))
|
||||
(call/cc
|
||||
(λ (k)
|
||||
(iter 10 (λ (n)
|
||||
;; i don't feel like implementing (= n 5) right now lmfao
|
||||
(if (zero? (- n 5))
|
||||
(k #t)
|
||||
#f))))))
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > #t
|
||||
@@ -0,0 +1,4 @@
|
||||
(call/cc
|
||||
(λ (k)
|
||||
(begin (k #t)
|
||||
#f)))
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > #t
|
||||
@@ -0,0 +1,5 @@
|
||||
;; confer ../callcc-early-exit-4
|
||||
(letrec ((app (λ (f x)
|
||||
(begin (f x)
|
||||
#f))))
|
||||
(call/cc (λ (k) (app k #t))))
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > #t
|
||||
@@ -0,0 +1,5 @@
|
||||
;; confer ../callcc-early-exit-3
|
||||
(letrec ((app (λ (f x)
|
||||
(begin (f x)
|
||||
#f))))
|
||||
(call/cc (λ (k) (app (λ (x) (k x)) #t))))
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > #t
|
||||
@@ -0,0 +1,4 @@
|
||||
(call/cc
|
||||
(λ (k)
|
||||
(begin ((λ () (k #t)))
|
||||
#f)))
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > 12
|
||||
@@ -0,0 +1,4 @@
|
||||
(* 2 (call/cc
|
||||
(λ (k)
|
||||
(begin (k 6)
|
||||
3))))
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > 456
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > 155
|
||||
@@ -0,0 +1,10 @@
|
||||
(letrec ((factorial (λ (n)
|
||||
(if (zero? n)
|
||||
1
|
||||
(* n (factorial (- n 1)))))))
|
||||
(letrec ((sum-of-factorials
|
||||
(λ (n)
|
||||
(if (zero? n)
|
||||
0
|
||||
(+ (factorial n) (sum-of-factorials (- n 1)))))))
|
||||
(+ 2 (sum-of-factorials 5))))
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > (6 . 7)
|
||||
@@ -0,0 +1 @@
|
||||
(cons 6 7)
|
||||
@@ -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)))
|
||||
+20
-17
@@ -14,9 +14,9 @@ build-type: Simple
|
||||
-- extra-source-files:
|
||||
|
||||
flag doctest
|
||||
description: enable the doctest suite
|
||||
default: True
|
||||
manual: True
|
||||
description: enable the doctest suite
|
||||
default: True
|
||||
manual: True
|
||||
|
||||
common ghcstuffs-dev
|
||||
ghc-options:
|
||||
@@ -60,12 +60,12 @@ library
|
||||
Gyehoek.CPS.Close
|
||||
Gyehoek.CPS.Convert
|
||||
Gyehoek.CPS.Eval
|
||||
Gyehoek.CPS.Lower
|
||||
Gyehoek.CPS.Stackify
|
||||
Gyehoek.CPS.Hoist
|
||||
Gyehoek.CPS.Syntax
|
||||
Gyehoek.Driver
|
||||
Gyehoek.GenSym
|
||||
Gyehoek.Jalmot
|
||||
Gyehoek.Language
|
||||
Gyehoek.Lift1
|
||||
Gyehoek.Options
|
||||
Gyehoek.Prelude
|
||||
@@ -77,8 +77,6 @@ library
|
||||
Gyehoek.Sexp.QQ
|
||||
Gyehoek.Sexp.Read
|
||||
Gyehoek.Sexp.Syntax
|
||||
Gyehoek.Stack.Syntax
|
||||
Gyehoek.Stack.VM
|
||||
Gyehoek.Wasm
|
||||
|
||||
build-depends:
|
||||
@@ -99,6 +97,7 @@ library
|
||||
, hashable
|
||||
, invertible-grammar
|
||||
, lens
|
||||
, lucid
|
||||
, megaparsec
|
||||
, mtl
|
||||
, optparse-applicative
|
||||
@@ -106,6 +105,7 @@ library
|
||||
, pretty-simple
|
||||
, prettyprinter
|
||||
, prettyprinter-ansi-terminal
|
||||
, prettyprinter-lucid
|
||||
, process
|
||||
, recursion-schemes
|
||||
, scientific
|
||||
@@ -116,6 +116,7 @@ library
|
||||
, typed-process
|
||||
, unordered-containers
|
||||
, vector
|
||||
, tardis
|
||||
|
||||
hs-source-dirs: src
|
||||
default-language: GHC2024
|
||||
@@ -130,14 +131,11 @@ test-suite test
|
||||
-- cabal-fmt: expand test -Main
|
||||
other-modules:
|
||||
Gyehoek.Test.CPS.Eval
|
||||
Gyehoek.Test.CPS.Stackify
|
||||
Gyehoek.Test.CPS.Syntax
|
||||
Gyehoek.Test.Golden
|
||||
Gyehoek.Test.Scheme.Syntax
|
||||
Gyehoek.Test.Sexp.Print
|
||||
Gyehoek.Test.Sexp.QQ
|
||||
Gyehoek.Test.Sexp.Read
|
||||
Gyehoek.Test.Stack.VM
|
||||
Gyehoek.TestUtil
|
||||
Root
|
||||
|
||||
@@ -162,13 +160,18 @@ test-suite test
|
||||
|
||||
-- https://github.com/martijnbastiaan/doctest-parallel/pull/66
|
||||
test-suite doctest
|
||||
import: ghcstuffs, ghcstuffs-dev
|
||||
type: exitcode-stdio-1.0
|
||||
hs-source-dirs: test
|
||||
build-depends: base
|
||||
import: ghcstuffs, ghcstuffs-dev
|
||||
type: exitcode-stdio-1.0
|
||||
hs-source-dirs: test
|
||||
build-depends:
|
||||
, base
|
||||
, gyehoek
|
||||
|
||||
default-extensions: CPP
|
||||
main-is: doctest.hs
|
||||
main-is: doctest.hs
|
||||
|
||||
if flag(doctest)
|
||||
build-depends: doctest-parallel >=0.1
|
||||
build-depends: doctest-parallel >=0.1
|
||||
|
||||
else
|
||||
cpp-options: "-DGYEHOEK_NO_DOCTEST"
|
||||
cpp-options: -DGYEHOEK_NO_DOCTEST
|
||||
|
||||
+38
-24
@@ -4,38 +4,52 @@ module Gyehoek.CPS.Close
|
||||
) where
|
||||
|
||||
import Gyehoek.CPS.Syntax
|
||||
import Data.List (nub)
|
||||
import Gyehoek.GenSym
|
||||
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
|
||||
close = transformM \case
|
||||
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 = freeWithBound' [f] lam
|
||||
let m' = ifoldr
|
||||
(\n x q -> [cps|(prim (env-ref #{f} #{n})
|
||||
(κ (#{x}) #{q}))|])
|
||||
m frees
|
||||
pure [cps|
|
||||
(letrec ((#{f_code} (λ (#{f} ##{bs} #{kb})
|
||||
#{m'})))
|
||||
(prim (make-closure ($ #{f_code}) ##{frees})
|
||||
(κ (#{f}) #{e})))
|
||||
|]
|
||||
genCodeName :: GenSym :> es => Name -> Eff es Name
|
||||
genCodeName f = gensym' @Name $ f ^. _Wrapped' . to (<> "-code")
|
||||
|
||||
ExpApply f xs ktail -> do
|
||||
code <- gensym' @Name "code"
|
||||
bindEnv :: List Name -> Exp -> Exp
|
||||
bindEnv frees m = [cps|
|
||||
(prim (get-env) (κ #{frees} #{m}))
|
||||
|]
|
||||
|
||||
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|
|
||||
(prim (env-code #{f})
|
||||
(κ (#{code})
|
||||
(#{code} #{f} ##{xs} #{ktail})))
|
||||
(letrec #{bs'}
|
||||
(prim (make-shared-closure #{codes} #{frees})
|
||||
(κ #{boundNames}
|
||||
#{e})))
|
||||
|]
|
||||
|
||||
e -> pure e
|
||||
|
||||
close :: forall es. GenSym :> es => Exp -> Eff es Exp
|
||||
close = transformM close1
|
||||
|
||||
closeProgram :: GenSym :> es => Program -> Eff es Program
|
||||
closeProgram = traverseOf #body close
|
||||
closeProgram = traverseOf (#body . #body) close
|
||||
|
||||
+51
-35
@@ -12,6 +12,7 @@ import Data.List.NonEmpty (NonEmpty((:|)))
|
||||
import Control.Monad.Cont qualified as Cont
|
||||
import qualified Data.List.NonEmpty as NE
|
||||
import Gyehoek.Prelude
|
||||
import Debug.Pretty.Simple
|
||||
|
||||
|
||||
-- 뻘짓이어라
|
||||
@@ -23,66 +24,78 @@ telescope f = Cont.runCont . traverse (Cont.cont . f)
|
||||
|
||||
|
||||
|
||||
one :: a -> List a
|
||||
one a = [a]
|
||||
|
||||
oneOrUndefined :: List Val -> Val
|
||||
oneOrUndefined = \case
|
||||
[x] -> x
|
||||
_ -> ValImm ImmUndefined
|
||||
|
||||
convert1 :: (GenSym :> es) => Scm.Exp -> (Val -> Eff es Exp) -> Eff es Exp
|
||||
convert1 e k = convert e (k . oneOrUndefined)
|
||||
|
||||
-- | Transform an expression with a meta-continuation.
|
||||
convert
|
||||
:: forall es. (GenSym :> es)
|
||||
=> Scm.Exp -> (Val -> Eff es Exp) -> Eff es Exp
|
||||
=> Scm.Exp -> (List Val -> Eff es Exp) -> Eff es Exp
|
||||
|
||||
convert (Scm.ExpVar x) k = k $ ValVar x
|
||||
convert (Scm.ExpLit l) k = k . ValImm $ case l of
|
||||
convert (Scm.ExpVar x) k = k [ValVar x]
|
||||
convert (Scm.ExpLit l) k = k . one . ValImm $ case l of
|
||||
LitInt n -> ImmInt n
|
||||
LitBool b -> ImmBool b
|
||||
_ -> _
|
||||
|
||||
-- special case: call/cc is desugared during cps-conversion...
|
||||
convert (Scm.ExpPrim (PrimCallCC withcc)) k = do
|
||||
convert withcc \withcc' -> do
|
||||
cc <- gensym' @Name "cc"
|
||||
r <- gensym' "r"
|
||||
m <- k $ 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 =
|
||||
telescope (convert @es) p \p' -> do
|
||||
r <- gensym' "r"
|
||||
ExpPrim p' . MkKappa [r] <$> k (ValVar r)
|
||||
telescope (convert1 @es) p \p' -> do
|
||||
r_l <- gensym' "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
|
||||
f <- gensym' "lambda-body"
|
||||
lam <- convertLambda xs e
|
||||
ke <- k $ ValVar f
|
||||
ke <- k [ValVar f]
|
||||
pure [cps|
|
||||
(letrec ((#{f} #{lam}))
|
||||
#{ke})
|
||||
|]
|
||||
|
||||
convert (Scm.ExpApply f xs) k =
|
||||
telescope (convert @es) (f:|xs) \(f':|xs') -> do
|
||||
r <- gensym' "r"
|
||||
telescope (convert1 @es) (f:|xs) \(f':|xs') -> do
|
||||
r <- gensym' @Name "r"
|
||||
x <- gensym' "x"
|
||||
m <- k (ValVar x)
|
||||
pure $ ExpLetRec [(r, AbsKappa' [x] m)] $ ExpApply f' xs' r
|
||||
m <- k [ValVar x]
|
||||
pure $ ExpLetRec [(r, AbsKappa' [x] m)] $
|
||||
ExpApply f' xs' (KexpVar r)
|
||||
|
||||
convert (Scm.ExpBegin xs) k = _
|
||||
convert (Scm.ExpBegin xs) k = telescope (convert @es) xs (k . NE.last)
|
||||
|
||||
convert (Scm.ExpIf c t f) k =
|
||||
convert c \c' ->
|
||||
ExpIf c' <$> convert t k <*> convert f k
|
||||
convert1 c \c' -> do
|
||||
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
|
||||
-- are the left-hand sides and whose arguments are the right-hand
|
||||
-- sides.
|
||||
convert (Scm.ExpLet bs e) k =
|
||||
let rhss = bs ^.. each . _2
|
||||
in telescope (convert @es) rhss \rhss' -> do
|
||||
in telescope (convert1 @es) rhss \rhss' -> do
|
||||
e' <- convert e k
|
||||
kbody <- gensym' @Name "let-body"
|
||||
let bs' = bs ^.. each . _1
|
||||
@@ -105,12 +118,15 @@ convertLambda
|
||||
=> List Name -> Scm.Exp -> Eff es Lambda
|
||||
convertLambda bs m = do
|
||||
ktail <- gensym' "lambda-tail"
|
||||
m' <- convert m $ pure . ExpContinue ktail . (:[])
|
||||
m' <- convert1 m $ pure . ExpContinue (ValVar ktail) . (:[])
|
||||
pure [cps|(λ (##{bs} #{ktail}) #{m'})|]
|
||||
|
||||
convertProgram :: forall es. (GenSym :> es) => Scm.Program -> Eff es Program
|
||||
convertProgram p =
|
||||
MkProgram <$> telescope (convert @es) (p ^.. each . _Left) (pure . Halt)
|
||||
convertProgram p = do
|
||||
ktail <- gensym' "start-ktail"
|
||||
m <- telescope (convert1 @es) (p ^.. each . _Left)
|
||||
(pure . ExpContinue (ValVar ktail))
|
||||
pure . MkProgram $ MkLambda [] ktail m
|
||||
|
||||
convertExp :: forall es. (GenSym :> es) => Scm.Exp -> Eff es Exp
|
||||
convertExp e = convert e (pure . Halt1)
|
||||
convertExp e = convert e (pure . Halt)
|
||||
|
||||
+285
-61
@@ -1,79 +1,303 @@
|
||||
{-# LANGUAGE ViewPatterns #-}
|
||||
{-# LANGUAGE DeriveAnyClass #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
module Gyehoek.CPS.Eval
|
||||
( evalProgram
|
||||
, module Gyehoek.CPS.Syntax
|
||||
, evalExp
|
||||
, eGrammar
|
||||
) where
|
||||
|
||||
import Gyehoek.CPS.Syntax
|
||||
import Control.Lens
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Gyehoek.CPS.Syntax hiding (Hob(..), Obj(..), cont)
|
||||
import Gyehoek.Sexp qualified as S
|
||||
import Control.Lens hiding (assign)
|
||||
import Data.Maybe (fromMaybe, isJust)
|
||||
import Text.Show.Functions ()
|
||||
import qualified Data.HashMap.Strict as H
|
||||
import Gyehoek.Prelude
|
||||
import Gyehoek.Prelude hiding (assign)
|
||||
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
|
||||
{ vars :: HashMap Name Obj
|
||||
, labels :: HashMap Name (Env, Abs)
|
||||
newtype Loc = MkLoc { getLoc :: Int }
|
||||
deriving stock (Generic, Data)
|
||||
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) =
|
||||
case g ^. #labels . at k of
|
||||
Just (h, AbsKappa' bs m) -> eval h' m
|
||||
where h' = h & #vars <>~ envOfBinds bs (evalVal g <$> xs)
|
||||
_ -> error [i|not a kappa: #{k}|]
|
||||
emptyStore :: Store
|
||||
emptyStore = MkStore
|
||||
{ nextLoc = MkLoc 0
|
||||
, heap = mempty
|
||||
}
|
||||
|
||||
eval g (ExpApply ((^?! #ValVar) -> f) xs ktail) =
|
||||
case g ^?! #labels . at f of
|
||||
Just (h,AbsLambda' bs kb m) -> eval h' m
|
||||
where h' = h & #vars <>~ envOfBinds bs (evalVal g <$> xs)
|
||||
& #labels . at kb .~ (g ^. #labels . at ktail)
|
||||
Nothing -> error [i|undefined label: #{f}|]
|
||||
|
||||
eval g (ExpLetRec [(b, ab)] e) = eval g' e
|
||||
where g' = g & #labels . at b ?~ (g,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
|
||||
_ -> 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
|
||||
newtype Env = MkEnv { getEnv :: HashMap Name Loc }
|
||||
deriving stock (Show, Generic, Data)
|
||||
deriving newtype (Semigroup, Monoid)
|
||||
|
||||
emptyEnv :: Env
|
||||
emptyEnv = MkEnv
|
||||
{ vars = mempty
|
||||
-- a kinda silly hack to make sure `halt` is handled correctly when
|
||||
-- it appears as the tail continuation of an application. the
|
||||
-- special case of `eval` responsible for `halt` only covers terms
|
||||
-- of the form `(continue $halt xs …)`; other terms such as
|
||||
-- `($some-fn xs $halt)` just see an undefined label `$halt`.
|
||||
, labels = H.singleton "halt"
|
||||
( emptyEnv
|
||||
, AbsKappa' ["h0"] $ Halt [ValVar "h0"]
|
||||
)
|
||||
}
|
||||
emptyEnv = mempty
|
||||
|
||||
evalProgram :: Program -> List Obj
|
||||
evalProgram (MkProgram e) = eval emptyEnv e
|
||||
type instance Index Env = Name
|
||||
type instance IxValue Env = Loc
|
||||
|
||||
instance Ixed Env where ix j = #getEnv . ix j
|
||||
instance At Env where at j = #getEnv . at j
|
||||
|
||||
update :: Loc -> E -> Store -> Store
|
||||
update (MkLoc loc) v = #heap %~ IM.alter f loc
|
||||
where
|
||||
f (Just _) = Just v
|
||||
f Nothing = error "segfault lol"
|
||||
|
||||
updates :: Foldable f => f (Loc, E) -> Store -> Store
|
||||
updates = alaf Endo foldMap (uncurry update)
|
||||
|
||||
fetch :: Loc -> M r E
|
||||
fetch (MkLoc loc) = gets (^?! #heap . ix loc)
|
||||
|
||||
new :: M r Loc
|
||||
new = state \st -> (st.nextLoc, st & #nextLoc %~ succ)
|
||||
|
||||
new' :: E -> M r Loc
|
||||
new' e = state \st ->
|
||||
( st.nextLoc
|
||||
, st & #nextLoc %~ succ & at st.nextLoc ?~ e
|
||||
)
|
||||
|
||||
defines :: Traversable t => t (Name, E) -> M Answer Env
|
||||
defines = alaf Ap foldMap \(name,e) -> do
|
||||
l <- new' e
|
||||
pure $ bind name l
|
||||
|
||||
var :: HasCallStack => Env -> Name -> M Answer Loc
|
||||
var g x = case g ^. at x of
|
||||
Just l -> pure l
|
||||
Nothing -> wrong [i|unbound variable #{x}|]
|
||||
|
||||
type CmdCont = Store -> Answer
|
||||
type ExpCont = List E -> CmdCont
|
||||
|
||||
type M r = ContT r (State Store)
|
||||
|
||||
data Answer
|
||||
= AnswerValues (List E)
|
||||
| AnswerError AJalmot
|
||||
deriving (Show, Generic)
|
||||
|
||||
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,325 +0,0 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
{-# LANGUAGE OverloadedRecordDot #-}
|
||||
{-# LANGUAGE OverloadedLabels #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
{-# LANGUAGE MultilineStrings #-}
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
{-# LANGUAGE ApplicativeDo #-}
|
||||
{-# LANGUAGE RecursiveDo #-}
|
||||
{- HLINT ignore "Use camelCase" -}
|
||||
module Gyehoek.CPS.Lower
|
||||
(lower, lowerProgram) where
|
||||
|
||||
import Gyehoek.CPS.Syntax
|
||||
import Data.Vector.Strict (Vector)
|
||||
import Control.Lens hiding (op)
|
||||
import Numeric.Natural
|
||||
import qualified Data.Vector.Strict as V
|
||||
import Gyehoek.Wasm qualified as Wasm
|
||||
import Gyehoek.Wasm hiding (Expr)
|
||||
import Control.Monad.Fix
|
||||
import Data.Text qualified as T
|
||||
import Data.Foldable (fold)
|
||||
import Gyehoek.Jalmot
|
||||
import Gyehoek.Sexp qualified as S
|
||||
import Gyehoek.Prelude
|
||||
|
||||
|
||||
data Env = MkEnv
|
||||
{ vars :: Vector Name
|
||||
, kvars :: Vector Name
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
type instance Index Env = Natural
|
||||
type instance IxValue Env = Name
|
||||
|
||||
instance Ixed Env where
|
||||
ix i = #vars . ix (fromIntegral i)
|
||||
|
||||
|
||||
|
||||
tonat :: Integral a => a -> Natural
|
||||
tonat = fromIntegral
|
||||
|
||||
-- | @makeSmallFixnum@ emits an expression injecting the i32 on top
|
||||
-- of the stack into the SCM unitype.
|
||||
makeSmallFixnum :: Wasm.Expr
|
||||
makeSmallFixnum = [expr|
|
||||
(@gyehoek "construct small fixnum")
|
||||
(i32.const 1)
|
||||
i32.shl
|
||||
ref.i31
|
||||
|]
|
||||
|
||||
getArgRegister :: Natural -> S.Datum
|
||||
getArgRegister n = S.Symbol [i|$arg#{n}|]
|
||||
|
||||
-- | Given an expression @e@ leaving a @ref eq@ atop the stack,
|
||||
-- @pushArg rt n e@ sets the nth slot of the arg-passing array to the
|
||||
-- result of @e@.
|
||||
pushArg :: Natural -> Wasm.Expr -> Wasm.Expr
|
||||
pushArg n e = [expr|
|
||||
(@gyehoek begin pushArg)
|
||||
##{e}
|
||||
(global.set #{reg})
|
||||
(@gyehoek end pushArg)
|
||||
|]
|
||||
where reg = getArgRegister n
|
||||
|
||||
-- | Pop the nth arg from the arg-passing array onto the stack.
|
||||
popArg :: Natural -> Wasm.Expr
|
||||
popArg n = [expr|
|
||||
(@gyehoek begin popArg)
|
||||
(global.get #{reg})
|
||||
ref.as_non_null
|
||||
(@gyehoek end popArg)
|
||||
|]
|
||||
where reg = getArgRegister n
|
||||
|
||||
|
||||
|
||||
lowerVal :: (HasCallStack, GenMod :> es) => Env -> Val -> Eff es Wasm.Expr
|
||||
|
||||
lowerVal g (ValImm imm) =
|
||||
pure $ case imm of
|
||||
ImmInt n -> [expr|
|
||||
(i32.const #{n})
|
||||
##{makeSmallFixnum}
|
||||
|]
|
||||
ImmBool b -> [expr|
|
||||
(i32.const #{b'})
|
||||
ref.i31
|
||||
|]
|
||||
where b' :: Int = if b then 0b11 else 0b01
|
||||
_ -> _
|
||||
|
||||
lowerVal g (ValVar x) = do
|
||||
pure [expr|(global.get #{l})|]
|
||||
where
|
||||
l = getArgRegister . fromIntegral . succ $ V.elemIndex x g.vars ^?! _Just
|
||||
|
||||
lower' :: (GenMod :> es) => Env -> Exp -> Eff es Wasm.Expr
|
||||
|
||||
lower' g (Halt [v]) = do
|
||||
arg <- pushArg 0 <$> lowerVal g v
|
||||
pure [expr|
|
||||
##{arg}
|
||||
(return_call $halt (i32.const 1))
|
||||
|]
|
||||
|
||||
lower' g e@(ExpPrim p k) =
|
||||
case p of
|
||||
PrimAdd x y -> lowerBinOp "i32.add" g x y k
|
||||
PrimMul x y -> lowerBinOp "i32.mul" g x y k
|
||||
|
||||
lower' g (ExpIf c t f) = do
|
||||
c' <- lowerVal g c
|
||||
t' <- lower' g t
|
||||
f' <- lower' g f
|
||||
pure [expr|
|
||||
##{c'}
|
||||
(call $gh-truthy?)
|
||||
(if (then ##{t'})
|
||||
(else ##{f'}))
|
||||
|]
|
||||
|
||||
lower' g (ExpLetRec [(r,AbsKappa kap)] e) = do
|
||||
idx <- lowerKappa g kap
|
||||
let g' = g & #kvars <>~ [r]
|
||||
e' <- lower' g' e
|
||||
pure [expr|
|
||||
(@gyehoek "push cont" :idx #{idx})
|
||||
(array.set $cont-stack-type
|
||||
(global.get $cont-stack)
|
||||
(global.get $cont-stack-top)
|
||||
(ref.func #{idx}))
|
||||
(global.set $cont-stack-top
|
||||
(i32.add (global.get $cont-stack-top)
|
||||
(i32.const 1)))
|
||||
##{e'}
|
||||
|]
|
||||
|
||||
lower' g (ExpLetRec [(r,AbsLambda lam)] e) = do
|
||||
idx <- lowerLambda g lam
|
||||
let g' = g & #vars <>~ [r]
|
||||
let n = succ $ length g.vars
|
||||
e' <- lower' g' e
|
||||
let reg = getArgRegister . fromIntegral $ n
|
||||
pure [expr|
|
||||
(i32.const 0)
|
||||
(ref.func #{idx})
|
||||
(struct.new $closure)
|
||||
(global.set #{reg})
|
||||
##{e'}
|
||||
|]
|
||||
|
||||
lower' g e@(ExpApply f xs ktail) = do
|
||||
let nargs = length xs
|
||||
f' <- lowerVal g f
|
||||
let l = succ $ V.elemIndex ktail g.kvars ^?! _Just
|
||||
args <- fold <$>
|
||||
itraverse (\i -> fmap (pushArg $ tonat i) . lowerVal g) xs
|
||||
pure [expr|
|
||||
(@gyehoek "load args")
|
||||
##{args}
|
||||
(i32.const 1)
|
||||
##{f'}
|
||||
(ref.cast (ref $closure))
|
||||
(struct.get $closure $code)
|
||||
(return_call_ref $cont-type)
|
||||
(@gyehoek todo
|
||||
(f' ##{f'})
|
||||
(ktail #{l}))
|
||||
|]
|
||||
|
||||
lower' g e@(ExpContinue k xs) = do
|
||||
let nargs = length xs
|
||||
args <- fold <$>
|
||||
itraverse (\i -> fmap (pushArg $ tonat i) . lowerVal g) xs
|
||||
pure [expr|
|
||||
(@gyehoek "push args")
|
||||
##{args}
|
||||
(@gyehoek "nargs")
|
||||
(i32.const #{nargs})
|
||||
(@gyehoek "pop cont stack")
|
||||
(global.get $cont-stack-top)
|
||||
(i32.const #{l})
|
||||
i32.sub
|
||||
(global.set $cont-stack-top)
|
||||
(global.get $cont-stack)
|
||||
(global.get $cont-stack-top)
|
||||
(array.get $cont-stack-type)
|
||||
ref.as_non_null
|
||||
(return_call_ref $cont-type)
|
||||
|]
|
||||
where
|
||||
l = succ $ V.elemIndex k g.kvars ^?! _Just
|
||||
|
||||
lower' g e = error . S.encodeOrShow' S.datumIso $ e
|
||||
|
||||
lowerKappa :: GenMod :> es => Env -> Kappa -> Eff es Idx
|
||||
lowerKappa g e@(MkKappa xs m) = do
|
||||
let g' = g & #vars <>~ V.fromList xs
|
||||
m' <- lower' g' m
|
||||
idx <- Wasm.defineFunction [wat|
|
||||
(func (param i32)
|
||||
(local (ref eq) (ref eq) (ref eq) (ref eq) (ref eq))
|
||||
##{m'})
|
||||
|]
|
||||
Wasm.emit [wats|(elem declare funcref (ref.func #{idx}))|]
|
||||
pure idx
|
||||
|
||||
lowerLambda :: GenMod :> es => Env -> Lambda -> Eff es Idx
|
||||
lowerLambda g e@(MkLambda xs ktail m) = do
|
||||
let g' = g & #vars .~ V.fromList xs
|
||||
& #kvars <>~ [ktail]
|
||||
m' <- lower' g' m
|
||||
idx <- Wasm.defineFunction [wat|
|
||||
(func (param i32)
|
||||
(local (ref eq) (ref eq) (ref eq) (ref eq) (ref eq))
|
||||
##{m'})
|
||||
|]
|
||||
Wasm.emit [wats|(elem declare funcref (ref.func #{idx}))|]
|
||||
pure idx
|
||||
|
||||
lowerBinOp
|
||||
:: (GenMod :> es)
|
||||
=> Text -> Env -> Val -> Val -> Kappa -> Eff es Wasm.Expr
|
||||
lowerBinOp op g x y (MkKappa [r] e) = do
|
||||
let op' = S.Symbol op
|
||||
let g' = g & #vars <>~ [r]
|
||||
let n = succ $ length (g ^. #vars)
|
||||
let reg = getArgRegister . fromIntegral $ n
|
||||
x' <- lowerVal g x
|
||||
y' <- lowerVal g y
|
||||
e' <- lower' g' e
|
||||
pure [expr|
|
||||
##{x'}
|
||||
(i31.get_s (ref.cast (ref i31)))
|
||||
(i32.const 1)
|
||||
i32.shr_u
|
||||
##{y'}
|
||||
(i31.get_s (ref.cast (ref i31)))
|
||||
(i32.const 1)
|
||||
i32.shr_u
|
||||
#{op'}
|
||||
##{makeSmallFixnum}
|
||||
(global.set #{reg})
|
||||
##{e'}
|
||||
|]
|
||||
|
||||
|
||||
|
||||
emitRuntime :: GenMod :> es => Eff es ()
|
||||
emitRuntime = mfix \runtime -> do
|
||||
Wasm.defineFunctions [wats|
|
||||
(import "gyehoek" "write" (func $gh-write (param (ref eq))))
|
||||
(import "gyehoek" "truthy?" (func $gh-truthy? (param (ref eq))
|
||||
(result i32)))
|
||||
|]
|
||||
-- cont stack
|
||||
Wasm.defineTypes [wats|
|
||||
(type $heap-object (sub (struct (field $hash (mut i32)))))
|
||||
(type $cont-type (func (param i32)))
|
||||
(type $cont-stack-type (array (mut (ref null $cont-type))))
|
||||
(type $closure (sub $heap-object
|
||||
(struct (field $hash (mut i32))
|
||||
(field $code (ref $cont-type)))))
|
||||
|]
|
||||
Wasm.defineGlobals [wats|
|
||||
(global $cont-stack-top (mut i32) (i32.const 0))
|
||||
(global $cont-stack (ref $cont-stack-type)
|
||||
(array.new_default $cont-stack-type (i32.const 128)))
|
||||
|]
|
||||
-- arg registers
|
||||
Wasm.defineGlobals [wats|
|
||||
(global $arg0 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg1 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg2 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg3 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg4 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg5 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg6 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg7 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg8 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg9 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg10 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg11 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg12 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg13 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg14 (mut (ref null eq)) (ref.null eq))
|
||||
(global $arg15 (mut (ref null eq)) (ref.null eq))
|
||||
|]
|
||||
-- other things 😼
|
||||
Wasm.defineGlobal [wat|
|
||||
(global $result (mut (ref null eq))
|
||||
(ref.null eq))
|
||||
|]
|
||||
-- procedures
|
||||
let arg = popArg 0
|
||||
Wasm.defineFunction [wat|
|
||||
(func $halt (param i32)
|
||||
##{arg}
|
||||
(global.set $result))
|
||||
|]
|
||||
pure ()
|
||||
|
||||
lower :: Exp -> Eff es Text
|
||||
lower e = fmap Wasm.renderModule . Wasm.execGenMod $ do
|
||||
runtime <- emitRuntime
|
||||
let g = MkEnv mempty mempty
|
||||
e' <- lower' g e
|
||||
Wasm.defineFunction [wat|
|
||||
(func $scm-entry (param i32)
|
||||
(local (ref eq) (ref eq) (ref eq) (ref eq) (ref eq))
|
||||
##{e'})
|
||||
|]
|
||||
Wasm.defineFunction [wat|
|
||||
(func (export "main")
|
||||
(call $scm-entry (i32.const 0))
|
||||
(call $gh-write (ref.as_non_null (global.get $result))))
|
||||
|]
|
||||
|
||||
lowerProgram :: Program -> Eff es Text
|
||||
lowerProgram (MkProgram e) = lower e
|
||||
@@ -1,159 +0,0 @@
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
module Gyehoek.CPS.Stackify
|
||||
( stackifyExp
|
||||
, 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)
|
||||
import Gyehoek.Prelude
|
||||
|
||||
|
||||
type Stackify = Writer Stk.Program
|
||||
|
||||
runStackify :: Eff (Stackify : es) a -> Eff es (a, Stk.Program)
|
||||
runStackify = runWriter
|
||||
|
||||
live :: Free a => Env -> a -> List Name
|
||||
live g e = free' e & filter \x ->
|
||||
x `H.member` 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, kap@(AbsKappa' xs m))] e) = do
|
||||
let vs = (f, Stk.ValLabel f) : (bindReg <$> xs)
|
||||
let ls = live g kap
|
||||
m' <- stackify (g & #bound .~ H.fromList (vs ++ (bindReg <$> ls))) m
|
||||
emitRoutine $
|
||||
Stk.MkRoutine f xs . buildBlock $
|
||||
Code [Stk.Pop x | x <- ls] m'
|
||||
let g' = g & #bound . at f ?~ Stk.ValLabel f
|
||||
& #liveness . at f ?~ ls
|
||||
stackify g' e
|
||||
|
||||
stackify g (ExpLetRec [(f, AbsLambda' xs k m)] e) = do
|
||||
let vs = (k:xs) <&> \x -> (x, Stk.ValReg x)
|
||||
m' <- stackify (g & #bound .~ H.fromList vs
|
||||
& #contStack %~ (k:)) m
|
||||
emitRoutine $ Stk.MkRoutine f xs (buildBlock m')
|
||||
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) = pure $
|
||||
Code [ Stk.PushCont k ] $
|
||||
Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
|
||||
Tail (Stk.TailCall (stackifyVal g f) (stackifyVal g <$> xs))
|
||||
where
|
||||
k = var g ktail
|
||||
ls = fold $ (k ^? #ValImm . #ImmLabel)
|
||||
>>= \klbl -> g ^. #liveness . at klbl
|
||||
|
||||
stackify g (ExpContinue k xs) =
|
||||
-- return continuations require popping the stack. how do we know
|
||||
-- when a continuation is a return continuation? is this a correct
|
||||
-- test?
|
||||
case elemIndex k g.contStack of
|
||||
Nothing -> pure . Tail $ Stk.TailCall (Stk.ValLabel k) xs'
|
||||
Just j -> do
|
||||
ktail <- gensym' @Name $ k ^. _Wrapped'
|
||||
pure $
|
||||
Code (replicate j $ Stk.PopCont "_") $
|
||||
Code [Stk.PopCont ktail] $
|
||||
Tail (Stk.TailCall (Stk.ValReg ktail) xs')
|
||||
where xs' = stackifyVal g <$> xs
|
||||
|
||||
stackify g (ExpPrim p (MkKappa [x] e)) = do
|
||||
e' <- stackify (g & #bound . at x ?~ Stk.ValReg x) e
|
||||
pure $
|
||||
Code [ Stk.Prim x (stackifyVal g <$> p) ] $
|
||||
e'
|
||||
|
||||
stackify _ e = error [i|unimplemented exp: #{e}|]
|
||||
|
||||
stackifyVal :: Env -> Val -> Stk.Val
|
||||
stackifyVal g = \case
|
||||
ValImm imm -> Stk.ValImm imm
|
||||
ValVar v -> var g v
|
||||
v -> error [i|unimplemented val: #{v}|]
|
||||
|
||||
var :: Env -> Name -> Stk.Val
|
||||
var g v = case g ^. #bound . at v of
|
||||
Just x -> x
|
||||
Nothing -> Stk.ValLabel v
|
||||
|
||||
bindReg :: Name -> (Name, Stk.Val)
|
||||
bindReg x = (x, Stk.ValReg x)
|
||||
|
||||
|
||||
|
||||
data Env = MkEnv
|
||||
{ bound :: HashMap Name Stk.Val
|
||||
-- | 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 Name (List Name)
|
||||
, contStack :: List Name
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
emptyEnv :: Env
|
||||
emptyEnv = MkEnv mempty mempty ["halt"]
|
||||
|
||||
|
||||
|
||||
stackifyExp :: GenSym :> es => Name -> Exp -> Eff es Stk.Program
|
||||
stackifyExp lbl e = do
|
||||
(code,p) <- runStackify $ stackify emptyEnv e
|
||||
pure $ p <> [ Stk.MkRoutine lbl [] (buildBlock code) ]
|
||||
|
||||
stackifyProgram :: GenSym :> es => Program -> Eff es Stk.Program
|
||||
stackifyProgram (MkProgram e) = stackifyExp "main" e
|
||||
|
||||
|
||||
|
||||
fac :: Program
|
||||
fac = [cps|
|
||||
(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))
|
||||
|]
|
||||
+207
-80
@@ -10,14 +10,18 @@ module Gyehoek.CPS.Syntax
|
||||
, Kappa(..)
|
||||
, Lambda(..)
|
||||
, Exp(..)
|
||||
, Kexp(..)
|
||||
, ExpF(..)
|
||||
, Name(..)
|
||||
, Prim(..)
|
||||
, Program(..)
|
||||
, HoistedProgram(..)
|
||||
, Lit(..)
|
||||
, Imm(..)
|
||||
, Obj(..)
|
||||
, Hob(..)
|
||||
, Label(..)
|
||||
, Reg(..)
|
||||
, pattern Halt
|
||||
, pattern Halt1
|
||||
, _MkKappa
|
||||
@@ -36,7 +40,13 @@ module Gyehoek.CPS.Syntax
|
||||
, Abs(..)
|
||||
, Free(..)
|
||||
, pattern ValLabel
|
||||
, labelName -- don't like that this is part of the api
|
||||
, pattern ObjLabel
|
||||
, absBody
|
||||
, pattern MkAbs
|
||||
, _MkAbs
|
||||
, unhoist
|
||||
, pattern ExpJump
|
||||
, _ExpJump
|
||||
)
|
||||
where
|
||||
|
||||
@@ -52,6 +62,12 @@ import Gyehoek.Prelude hiding (op)
|
||||
import Gyehoek.Sexp (Datum)
|
||||
import Gyehoek.Sexp (G, (:-)(..))
|
||||
import qualified Data.InvertibleGrammar.Base as IG
|
||||
import Gyehoek.GenSym (Gen)
|
||||
import Data.String (IsString)
|
||||
import Control.Applicative
|
||||
import qualified Data.HashMap.Strict as H
|
||||
import GHC.Records (HasField (..))
|
||||
import Data.Bifunctor
|
||||
|
||||
-- Data types
|
||||
|
||||
@@ -60,13 +76,24 @@ data Val
|
||||
| ValVar Name
|
||||
deriving (Show, Generic, Data, Eq)
|
||||
|
||||
pattern ValLabel :: Name -> Val
|
||||
pattern ValLabel :: Label -> Val
|
||||
pattern ValLabel x = ValImm (ImmLabel x)
|
||||
|
||||
newtype Label = MkLabel { inner :: Name }
|
||||
deriving stock (Generic, Data)
|
||||
deriving newtype (Show, Eq, Gen, IsString, Hashable)
|
||||
deriving anyclass (NFData, Wrapped)
|
||||
|
||||
newtype Reg = MkReg { inner :: Name }
|
||||
deriving stock (Generic, Data)
|
||||
deriving newtype (Show, Eq, Gen, IsString, Hashable)
|
||||
deriving anyclass (NFData, Wrapped)
|
||||
|
||||
data Imm
|
||||
= ImmInt Int
|
||||
| ImmBool Bool
|
||||
| ImmLabel Name
|
||||
| ImmLabel Label
|
||||
| ImmUndefined
|
||||
deriving stock (Show, Generic, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
@@ -76,9 +103,14 @@ data Obj
|
||||
deriving stock (Show, Generic, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
pattern ObjLabel l = ObjImm (ImmLabel l)
|
||||
|
||||
-- | a heap object.
|
||||
data Hob
|
||||
= HobClosure { label :: Name, 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
|
||||
deriving stock (Show, Generic, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
@@ -93,38 +125,90 @@ data Abs
|
||||
| AbsLambda Lambda
|
||||
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 AbsLambda' :: [Name] -> Name -> Exp -> Abs
|
||||
pattern AbsLambda' :: List Name -> Name -> Exp -> Abs
|
||||
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
|
||||
= ExpPrim (Prim Val) Kappa
|
||||
= ExpPrim (Prim Val) Kexp
|
||||
| ExpLetRec { binders :: List (Name, Abs), body :: Exp }
|
||||
| ExpContinue Name (List Val)
|
||||
| ExpIf Val Exp Exp
|
||||
| ExpContinue Val (List Val)
|
||||
| ExpIf Val Name Name
|
||||
| ExpApply
|
||||
{ op :: Val
|
||||
, args :: List Val
|
||||
, cont :: Name
|
||||
, cont :: Kexp
|
||||
}
|
||||
deriving (Show, Generic, Data, Eq)
|
||||
|
||||
data Kexp
|
||||
= KexpVar Name
|
||||
| KexpKappa Kappa
|
||||
deriving (Show, Generic, Data, Eq)
|
||||
|
||||
pattern Halt :: List Val -> Exp
|
||||
pattern Halt xs = ExpContinue "halt" xs
|
||||
pattern Halt xs = ExpContinue (ValLabel "halt") xs
|
||||
|
||||
pattern Halt1 :: Val -> Exp
|
||||
pattern Halt1 x = ExpContinue "halt" [x]
|
||||
pattern Halt1 x = ExpContinue (ValLabel "halt") [x]
|
||||
|
||||
data Def = DefConstant Name Exp
|
||||
deriving (Show, Generic, Data)
|
||||
|
||||
data Program = MkProgram
|
||||
{ body :: Exp
|
||||
{ body :: Lambda
|
||||
}
|
||||
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 ''Exp
|
||||
makeFieldsId ''Exp
|
||||
@@ -148,6 +232,21 @@ _AbsLambda' = prism'
|
||||
|
||||
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
|
||||
|
||||
@@ -167,51 +266,64 @@ instance S.DatumIso Imm where
|
||||
datumIso = S.match
|
||||
$ S.With (. S.int)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (. labelName)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (. S.unreadable (const "#<undefined>"))
|
||||
$ S.End
|
||||
|
||||
labelName :: S.DatumGrammar Name
|
||||
labelName = S.coproduct
|
||||
[ S.decorate S.SynConstant >>> S.datumIso @Name >>> S.prismIso
|
||||
(S.expected "label")
|
||||
(prefixed @Name "$")
|
||||
, S.list $ S.el (S.sym "$") >>> S.el (S.datumIso @Name)
|
||||
]
|
||||
instance S.DatumIso Label where
|
||||
datumIso = S.with \g -> S.coproduct
|
||||
[ S.decorate S.SynConstant >>> S.datumIso @Name >>> S.prismIso
|
||||
(S.expected "label")
|
||||
(prefixed @Name "$")
|
||||
, S.list $ S.el (S.sym "$") >>> S.el (S.datumIso @Name)
|
||||
]
|
||||
>>> g
|
||||
|
||||
instance S.DatumIso Reg where
|
||||
datumIso = S.with \g ->
|
||||
S.decorate S.SynVariable >>> S.datumIso @Name >>> S.prismIso
|
||||
(S.expected "register")
|
||||
(prefixed @Name "%")
|
||||
>>> g
|
||||
|
||||
instance S.DatumIso Hob where
|
||||
datumIso = S.match
|
||||
$ S.With (. closure)
|
||||
$ S.With (. cont)
|
||||
$ S.With (. conspair)
|
||||
$ S.End
|
||||
where
|
||||
conspair = S.dottedList (S.el S.datumIso) S.datumIso
|
||||
-- closures can be printed, but not parsed.
|
||||
closure :: G (Datum :- t) (List Obj :- Name :- t)
|
||||
closure :: G (Datum :- t) (List Obj :- Label :- t)
|
||||
closure = IG.Flip $ IG.PartialIso
|
||||
(\(env:-code:-t) -> [S.sx|(<closure> #{code} ##{env})|] :- 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)
|
||||
|
||||
instance S.DatumIso Lambda where
|
||||
datumIso = S.match
|
||||
$ S.With (. lambda)
|
||||
$ S.End
|
||||
datumIso = S.with (lam >>>)
|
||||
where
|
||||
lambda = S.list $
|
||||
S.el S.lambdaKeyword
|
||||
>>> S.el binders
|
||||
>>> S.el S.datumIso
|
||||
lam :: forall t. G (Datum :- t) (Exp :- Name :- List Name :- t)
|
||||
lam = S.lambdaLike
|
||||
S.lambdaKeyword
|
||||
binders
|
||||
(S.el $ S.datumIso @Exp)
|
||||
binders :: forall t. G (Datum :- t) (Name :- List Name :- t)
|
||||
binders = S.list $
|
||||
S.rest (S.datumIso @Name)
|
||||
>>> S.onTail (S.flipped $ IG.PartialIso
|
||||
(\(ktail:-args:-t) -> (args ++ [ktail]) :- t)
|
||||
(\(args:-t) -> case args ^? _Snoc of
|
||||
Just (args',ktail) -> Right $ ktail :- args' :- t
|
||||
Nothing -> Left $ S.expected "cont param")
|
||||
)
|
||||
binders =
|
||||
S.list (S.rest $ S.datumIso @Name)
|
||||
>>> S.flipped S.snoced
|
||||
>>> S.swap
|
||||
|
||||
instance S.DatumIso Kappa where
|
||||
datumIso = S.with \g ->
|
||||
S.lambdaLike S.kappaKeyword
|
||||
(S.list $ S.rest (S.datumIso @Name))
|
||||
(S.datumIso @(List Name))
|
||||
(S.el $ S.datumIso @Exp)
|
||||
>>> g
|
||||
|
||||
@@ -238,35 +350,56 @@ instance S.DatumIso Exp where
|
||||
if_ = S.ifLike "if"
|
||||
S.datumIso S.datumIso S.datumIso
|
||||
app :: forall t.
|
||||
G (Datum :- t) (Name :- ([Val] :- (Val :- t)))
|
||||
app = S.list $ S.el (S.datumIso @Val)
|
||||
-- >>> S.flipped Gyehoek.Datum.nonEmptyGrammar
|
||||
G (Datum :- t) (Kexp :- List Val :- Val :- t)
|
||||
app = S.list $
|
||||
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.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)
|
||||
>>> S.onTail S.swap
|
||||
prim = S.list $
|
||||
S.el (S.decorate S.SynBuiltin >>> S.sym "prim")
|
||||
>>> S.el (primDatumIso id (S.datumIso @Val))
|
||||
>>> 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
|
||||
datumIso = S.with \prog -> S.datumIso @Exp >>> 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
|
||||
|
||||
class Data a => CPS a where
|
||||
toCPS :: Datum -> a
|
||||
toCPS :: HasCallStack => Datum -> a
|
||||
|
||||
instance CPS Exp where toCPS = S.fromDatumUnsafe S.datumIso
|
||||
instance CPS Val where toCPS = S.fromDatumUnsafe S.datumIso
|
||||
@@ -274,6 +407,7 @@ instance CPS Kappa 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 Program where toCPS = S.fromDatumUnsafe S.datumIso
|
||||
instance CPS HoistedProgram where toCPS = S.fromDatumUnsafe S.datumIso
|
||||
|
||||
cps :: S.QuasiQuoter
|
||||
cps = S.makeSx' [| toCPS |]
|
||||
@@ -296,7 +430,8 @@ class Free a where
|
||||
freeWithBound :: HashSet Name -> a -> HashSet Name
|
||||
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' = freeWithBound' mempty
|
||||
|
||||
@@ -306,22 +441,32 @@ instance Free Abs where
|
||||
freeWithBound' bound (AbsKappa kap) = freeWithBound' bound kap
|
||||
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
|
||||
freeWithBound' bound = \case
|
||||
ExpPrim p k ->
|
||||
p & toListOf (folded . #ValVar . filtered (`notElem` bound))
|
||||
& (<> freeWithBound' bound k)
|
||||
(p ^.. folded . #ValVar . filtered (`notElem` bound))
|
||||
++ freeWithBound' bound k
|
||||
ExpLetRec bs m ->
|
||||
foldMapOf (each . _2) (freeWithBound' bound') bs
|
||||
<> freeWithBound' bound' m
|
||||
where bound' = bound & insertFrom (bs ^.. each . _1)
|
||||
ExpContinue k xs -> filter (`notElem` bound) (k : xs ^.. each . #ValVar)
|
||||
ExpContinue k xs -> filter (`notElem` bound) ((k:xs) ^.. each . #ValVar)
|
||||
ExpIf c t f ->
|
||||
(c ^.. #ValVar . filtered (`notElem` bound))
|
||||
<> freeWithBound' bound t <> freeWithBound' bound f
|
||||
<> mif (`notElem` bound) t <> mif (`notElem` bound) f
|
||||
ExpApply f xs k ->
|
||||
(f:xs) ^.. (each . #ValVar . filtered (`notElem` bound))
|
||||
<> (k ^.. filtered (`notElem` bound))
|
||||
<> freeWithBound' bound k
|
||||
|
||||
instance Free Kappa where
|
||||
freeWithBound' bound (MkKappa xs m) =
|
||||
@@ -330,21 +475,3 @@ instance Free Kappa where
|
||||
instance Free Lambda where
|
||||
freeWithBound' bound (MkLambda xs k m) =
|
||||
freeWithBound' (bound & insertFrom (k:xs)) m
|
||||
|
||||
|
||||
|
||||
class Vars a where
|
||||
-- | Traverse the immediate variables of an expression.
|
||||
vars :: Traversal' a Name
|
||||
|
||||
instance Vars Val where
|
||||
vars k (ValVar x) = ValVar <$> k x
|
||||
vars _ x = pure x
|
||||
|
||||
instance Vars a => Vars (Prim a) where
|
||||
vars k p = traverseOf (each . vars) k p
|
||||
|
||||
instance Vars Exp where
|
||||
vars k (ExpPrim p kap) = ExpPrim <$> vars k p <*> pure kap
|
||||
vars k (ExpContinue kname xs) = ExpContinue <$> k kname <*> pure xs
|
||||
vars _ e = pure e
|
||||
|
||||
+29
-33
@@ -1,7 +1,7 @@
|
||||
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
|
||||
|
||||
|
||||
import Gyehoek.Options
|
||||
import Prelude hiding (readFile)
|
||||
import Options.Applicative
|
||||
@@ -17,7 +17,6 @@ import qualified Data.Text.Encoding as T
|
||||
import System.IO (Handle)
|
||||
import System.IO qualified as IO
|
||||
import Gyehoek.CPS.Convert
|
||||
import Gyehoek.CPS.Lower
|
||||
import Gyehoek.CPS.Eval qualified as CPS
|
||||
import Control.Monad
|
||||
import Text.Pretty.Simple (pShowNoColor)
|
||||
@@ -25,24 +24,22 @@ import System.Process.Typed
|
||||
import System.Environment.Blank (getEnvDefault)
|
||||
import qualified Data.Text.IO as TIO
|
||||
import qualified Data.ByteString.Lazy as BS
|
||||
import Gyehoek.CPS.Stackify (stackifyProgram)
|
||||
import Gyehoek.Stack.VM (eval, writeObj, Obj)
|
||||
import qualified Data.Text as T
|
||||
import Gyehoek.Stack.Syntax qualified as Stk
|
||||
import Gyehoek.CPS.Close (closeProgram)
|
||||
import Control.Lens.Extras (is)
|
||||
import Control.Arrow ((>>>))
|
||||
import Gyehoek.Prelude
|
||||
import Gyehoek.Jalmot
|
||||
import qualified Gyehoek.Sexp as S
|
||||
|
||||
import Gyehoek.CPS.Hoist (hoistProgram)
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
opts <- execParser $ info (helper <*> parser) fullDesc
|
||||
runJalmotIO . runFileSystem . runGenSym . driver $ opts
|
||||
|
||||
|
||||
|
||||
|
||||
-- hPutStr :: FileSystem :> es => Handle -> Text -> Eff es ()
|
||||
-- hPutStr h = FB.hPutStr h . T.encodeUtf8
|
||||
@@ -118,27 +115,20 @@ driver opts = do
|
||||
hPutStrLn FS.stdout . view strict . pShowNoColor $ scm
|
||||
cps <- convertProgram scm
|
||||
when opts.dumpCPS do
|
||||
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso cps
|
||||
S.writeDatum cps
|
||||
closedCps <- closeProgram cps
|
||||
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
|
||||
dumpOrRun opts.dumpStackified (rt_is #Stackify)
|
||||
(stackifyProgram closedCps)
|
||||
(hPutStrLn FS.stdout <=< S.encodeDataWith S.dataIso)
|
||||
(eval >>> fmap writeObj
|
||||
>>> T.unwords
|
||||
>>> hPutStrLn FS.stdout)
|
||||
when (rt_is #HigherOrderCPS) do
|
||||
CPS.evalProgram cps
|
||||
>>= S.writeData
|
||||
when (rt_is #CPS) do
|
||||
closedCps
|
||||
& CPS.evalProgram
|
||||
& fmap writeObj
|
||||
& T.unwords
|
||||
& hPutStrLn FS.stdout
|
||||
dumpOrRun opts.inspectWasm (rt_is #Wasm)
|
||||
(lowerProgram cps)
|
||||
inspectWasm
|
||||
(\wat -> withFile opts.output FS.WriteMode \h -> hPutStrLn h wat)
|
||||
CPS.evalProgram closedCps
|
||||
>>= S.writeData
|
||||
|
||||
parse_e2e :: FilePath -> IO Scm.Program
|
||||
parse_e2e = runJalmotIO . runFileSystem . readScm
|
||||
@@ -147,12 +137,18 @@ convert_e2e :: FilePath -> IO CPS.Program
|
||||
convert_e2e = runJalmotIO . runFileSystem . runGenSym
|
||||
. (closeProgram <=< convertProgram <=< readScm)
|
||||
|
||||
lower_e2e :: FilePath -> IO Text
|
||||
lower_e2e =
|
||||
runJalmotIO . runFileSystem . runGenSym
|
||||
. (lowerProgram <=< closeProgram <=< convertProgram <=< readScm)
|
||||
eval_cps1_e2e :: FilePath -> IO Text
|
||||
eval_cps1_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
||||
readScm fp
|
||||
>>= convertProgram
|
||||
>>= closeProgram
|
||||
>>= CPS.evalProgram
|
||||
>>= pure . S.encodeOrShowData' S.dataIso
|
||||
|
||||
eval_e2e :: FilePath -> IO (List Obj)
|
||||
eval_e2e fp = runJalmotIO . runFileSystem . runGenSym $ do
|
||||
stk <- stackifyProgram <=< closeProgram <=< convertProgram <=< readScm $ fp
|
||||
pure . eval $ stk
|
||||
eval_cps2_e2e :: FilePath -> IO Text
|
||||
eval_cps2_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
||||
readScm fp
|
||||
>>= convertProgram
|
||||
-- >>= closeProgram
|
||||
>>= CPS.evalProgram
|
||||
>>= pure . S.encodeOrShowData' S.dataIso
|
||||
|
||||
@@ -8,6 +8,7 @@ module Gyehoek.Jalmot
|
||||
, runJalmotIO
|
||||
, runJalmotIOE
|
||||
, runJalmotUnsafe
|
||||
, runJalmotCS
|
||||
)
|
||||
where
|
||||
|
||||
@@ -29,6 +30,8 @@ deriving instance Data p => Data (Grammar.ErrorMessage p)
|
||||
data AJalmot
|
||||
= ReaderError (ParseErrorBundle Text Void)
|
||||
| GrammarError (Grammar.ErrorMessage Ann)
|
||||
| VMError Text
|
||||
| EvalError Text
|
||||
deriving (Show, Generic, Data)
|
||||
|
||||
data AJalmotCS = MkAJalmotCS !CallStack !AJalmot
|
||||
@@ -39,6 +42,9 @@ type Jalmot = Error AJalmot
|
||||
runJalmot :: Eff (Jalmot : es) a -> Eff es (Either (CallStack, AJalmot) a)
|
||||
runJalmot = runError
|
||||
|
||||
runJalmotCS :: Eff (Jalmot : es) a -> Eff es (Either AJalmotCS a)
|
||||
runJalmotCS = (mapped . _Left %~ uncurry MkAJalmotCS) . runError
|
||||
|
||||
runJalmotIOE :: IOE :> es => Eff (Jalmot : es) a -> Eff es a
|
||||
runJalmotIOE eff =
|
||||
runJalmot eff >>= \case
|
||||
@@ -60,6 +66,8 @@ instance Exception AJalmot where
|
||||
pretty err
|
||||
& layoutPretty defaultLayoutOptions
|
||||
& renderString
|
||||
VMError err -> [i|#{err}|]
|
||||
EvalError err -> [i|#{err}|]
|
||||
|
||||
instance Exception AJalmotCS where
|
||||
backtraceDesired = const False
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
module Gyehoek.Language
|
||||
(
|
||||
) where
|
||||
|
||||
+22
-10
@@ -13,14 +13,13 @@ import Data.Foldable
|
||||
import Gyehoek.Prelude hiding (argument)
|
||||
|
||||
|
||||
data Runtime = Stackify | Wasm | CPS
|
||||
data Runtime = Wasm | CPS | HigherOrderCPS
|
||||
deriving (Show, Generic, Eq)
|
||||
|
||||
data Language
|
||||
= LanguageScheme
|
||||
| LanguageCPS
|
||||
| LanguageClosed
|
||||
| LanguageStackified
|
||||
| LanguageWasm
|
||||
deriving (Show, Generic, Eq)
|
||||
|
||||
@@ -28,29 +27,30 @@ data Options = MkOptions
|
||||
{ dumpClosed :: Bool
|
||||
, dumpCPS :: Bool
|
||||
, dumpParsed :: Bool
|
||||
, dumpStackified :: Bool
|
||||
, dumpHoisted :: Bool
|
||||
, noColour :: Bool
|
||||
, runtime :: Maybe Runtime
|
||||
, inspectWasm :: Bool
|
||||
, output :: FilePath
|
||||
, sourceFile :: FilePath
|
||||
, sourceLanguage :: Language
|
||||
, targetLanguage :: Language
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
languageValues = ["scheme","cps","closed","stackified","wasm"]
|
||||
languageValues = ["scheme","cps","closed","wasm"]
|
||||
languageReader = maybeReader \case
|
||||
"scheme" -> Just LanguageScheme
|
||||
"cps" -> Just LanguageCPS
|
||||
"closed" -> Just LanguageClosed
|
||||
"stackified" -> Just LanguageStackified
|
||||
"wasm" -> Just LanguageWasm
|
||||
_ -> Nothing
|
||||
|
||||
runtimeValues = ["stackify","wasm","cps","none"]
|
||||
runtimeReader = maybeReader \case
|
||||
"stackify" -> Just (Just Stackify)
|
||||
"wasm" -> Just (Just Wasm)
|
||||
"cps" -> Just (Just CPS)
|
||||
"cps1" -> Just (Just CPS)
|
||||
("cps";"higher-order-cps") -> Just (Just HigherOrderCPS)
|
||||
"none" -> Just Nothing
|
||||
_ -> Nothing
|
||||
|
||||
@@ -58,15 +58,19 @@ parser :: Parser Options
|
||||
parser = do
|
||||
dumpClosed <- switch (long "dump-closed")
|
||||
dumpCPS <- switch (long "dump-cps")
|
||||
dumpStackified <- switch (long "dump-stackified")
|
||||
dumpParsed <- switch (long "dump-parsed")
|
||||
dumpHoisted <- switch (long "dump-hoisted")
|
||||
noColour <- switch . fold $
|
||||
[ long "no-colour"
|
||||
, long "no-color"
|
||||
]
|
||||
inspectWasm <- switch $ long "inspect-wasm" <> short 'p'
|
||||
runtime <- option runtimeReader . fold $
|
||||
[ long "runtime"
|
||||
, short 'R'
|
||||
, value (Just Stackify)
|
||||
, value (Just HigherOrderCPS)
|
||||
, completeWith runtimeValues
|
||||
, showDefaultWith $ const "stackify"
|
||||
, showDefaultWith $ const "higher-order-cps"
|
||||
, metavar "RUNTIME"
|
||||
]
|
||||
sourceLanguage <- option languageReader . fold $
|
||||
@@ -77,6 +81,14 @@ parser = do
|
||||
, showDefaultWith $ const "scheme"
|
||||
, metavar "LANGUAGE"
|
||||
]
|
||||
targetLanguage <- option languageReader . fold $
|
||||
[ long "target"
|
||||
, short 'T'
|
||||
, value LanguageCPS
|
||||
, completeWith languageValues
|
||||
, showDefaultWith $ const "cps"
|
||||
, metavar "LANGUAGE"
|
||||
]
|
||||
output <- strOption . fold $
|
||||
[ long "output"
|
||||
, short 'o'
|
||||
|
||||
@@ -19,6 +19,7 @@ module Gyehoek.Prelude
|
||||
, (>>>)
|
||||
, (>=>)
|
||||
, (<=<)
|
||||
, wrappedIso
|
||||
) where
|
||||
|
||||
import Control.Lens hiding (List, (:<))
|
||||
@@ -40,4 +41,5 @@ import Data.List.NonEmpty (NonEmpty((:|)))
|
||||
import Numeric.Natural (Natural)
|
||||
import Control.Category ((>>>))
|
||||
import Control.Monad
|
||||
import Data.Generics.Wrapped (Wrapped(..))
|
||||
|
||||
|
||||
+120
-14
@@ -10,6 +10,7 @@
|
||||
{-# LANGUAGE OrPatterns #-}
|
||||
{-# LANGUAGE PatternSynonyms #-}
|
||||
{-# LANGUAGE DeriveAnyClass #-}
|
||||
{-# LANGUAGE ViewPatterns #-}
|
||||
module Gyehoek.Scheme.Syntax
|
||||
( Name(..)
|
||||
, Prim(..)
|
||||
@@ -51,8 +52,10 @@ import qualified Effectful.FileSystem.IO.ByteString as FB
|
||||
import qualified Data.Set.Ordered as O
|
||||
import Gyehoek.Sexp.Grammar qualified as Sexp
|
||||
import Gyehoek.Sexp.Grammar qualified as S
|
||||
import Gyehoek.Sexp.Grammar (DatumIso, DataIso)
|
||||
import Gyehoek.Sexp.Grammar (DatumIso, G, DataIso, (:-)((:-)))
|
||||
import Gyehoek.Prelude
|
||||
import Control.Lens.Extras (is)
|
||||
import qualified Data.Scientific as Sci
|
||||
|
||||
|
||||
newtype Name = MkName { inner :: Text }
|
||||
@@ -81,9 +84,17 @@ data Prim e
|
||||
| PrimZeroP e
|
||||
| PrimNewline
|
||||
| PrimMakeClosure { code :: e, env :: List e }
|
||||
| PrimEnvRef e Int
|
||||
| PrimEnvCode e
|
||||
| PrimMakeSharedClosure { codes :: List e, env :: List e }
|
||||
| PrimGetEnv
|
||||
| PrimEnv
|
||||
| PrimEnvRef Int
|
||||
| PrimCallCC e
|
||||
| PrimCaptureCC
|
||||
| PrimInvokeCC e (List e)
|
||||
| PrimValues (List e)
|
||||
| PrimCallWithValues e e
|
||||
| PrimPairP e
|
||||
| PrimList (List e)
|
||||
deriving stock (Show, Generic, Functor, Foldable, Traversable, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
@@ -106,7 +117,7 @@ data Exp
|
||||
= ExpLet (List (Name, Exp)) Exp
|
||||
| ExpLetRec (List (Name, Exp)) Exp
|
||||
| ExpPrim (Prim Exp)
|
||||
| ExpBegin (List Exp)
|
||||
| ExpBegin (NonEmpty Exp)
|
||||
| ExpIf Exp Exp Exp
|
||||
| ExpLit Lit
|
||||
| ExpLambda (List Name) Exp
|
||||
@@ -122,8 +133,37 @@ data CommandOrDef
|
||||
deriving stock (Show, Generic, Data)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
newtype Program = MkProgram
|
||||
{ commandsAndDefs :: List CommandOrDef
|
||||
newtype LibName = MkLibName { inner :: NonEmpty Name }
|
||||
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 anyclass (NFData)
|
||||
@@ -162,21 +202,31 @@ primDatumIso namefn a = S.match
|
||||
$ S.With (. ht1 "integer?")
|
||||
$ S.With (. ht1 "write")
|
||||
$ S.With (. ht1 "zero?")
|
||||
$ S.With (. nullop "newline")
|
||||
$ S.With (. ht0 "newline")
|
||||
$ S.With (. ht1' "make-closure")
|
||||
$ S.With (. S.headTagged2 (namefn "env-ref") a S.int)
|
||||
$ S.With (. ht1 "env-code")
|
||||
$ 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 (. S.headTagged1 (namefn "env-ref") S.int)
|
||||
$ S.With (. ht1 "call/cc")
|
||||
$ S.With (. ht0 "capture/cc")
|
||||
$ S.With (. ht1' "invoke/cc")
|
||||
$ S.With (. ht0' "values")
|
||||
$ S.With (. ht2 "call-with-values")
|
||||
$ S.With (. ht1 "pair?")
|
||||
$ S.With (. ht0' "list")
|
||||
$ S.End
|
||||
where
|
||||
idn = S.el . S.sym . namefn
|
||||
nullop s = S.list $ idn s
|
||||
ht0 s = S.list $ idn s
|
||||
ht1 s = S.headTagged1 (namefn s) a
|
||||
ht2 s = S.headTagged2 (namefn s) a a
|
||||
ht1' s = S.headTagged1' (namefn s) a a
|
||||
ht0' s = S.headTagged0' (namefn s) a
|
||||
|
||||
instance DatumIso a => DatumIso (Prim a) where
|
||||
-- datumIso = primDatumIso ("prim:"<>) datumIso
|
||||
datumIso = primDatumIso id S.datumIso
|
||||
|
||||
instance DatumIso Lit where
|
||||
@@ -203,7 +253,7 @@ instance DatumIso Exp where
|
||||
$ S.With (. S.letLike "let" S.datumIso S.datumIso S.datumIso)
|
||||
$ S.With (. S.letLike "letrec" S.datumIso S.datumIso S.datumIso)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (. S.beginLike "begin" S.datumIso)
|
||||
$ S.With (. begin)
|
||||
$ S.With (. S.ifLike "if" S.datumIso S.datumIso S.datumIso)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (. lam)
|
||||
@@ -212,16 +262,72 @@ instance DatumIso Exp where
|
||||
$ S.End
|
||||
where
|
||||
lam = S.lambdaLike S.lambdaKeyword S.datumIso (S.el S.datumIso)
|
||||
begin :: forall t. G (S.Datum :- t) (NonEmpty Exp :- t)
|
||||
begin = S.beginLike "begin" $
|
||||
S.el (S.datumIso @Exp) >>> S.rest (S.datumIso @Exp)
|
||||
>>> S.onTail (S.Iso
|
||||
(\(xs:-x:-t) -> (x:|xs):-t)
|
||||
(\((x:|xs):-t) -> xs:-x:-t))
|
||||
|
||||
instance DatumIso CommandOrDef where
|
||||
datumIso = S.match
|
||||
$ S.With (\_Command -> _Command . S.datumIso)
|
||||
$ S.With (\_Definition -> _Definition . S.datumIso)
|
||||
$ S.With (\_Begin -> _Begin . S.beginLike "begin" S.datumIso)
|
||||
$ S.With (\_Begin -> _Begin . S.beginLike "begin" (S.rest S.datumIso))
|
||||
$ 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
|
||||
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
|
||||
|
||||
+47
-15
@@ -15,6 +15,7 @@ module Gyehoek.Sexp.Grammar
|
||||
, encodeDataTest
|
||||
, encodeDataTestColour
|
||||
, encodeOrShow'
|
||||
, encodeOrShowData'
|
||||
, decodeDataWith
|
||||
, encodeDataWith'
|
||||
, decodeTest
|
||||
@@ -28,6 +29,8 @@ module Gyehoek.Sexp.Grammar
|
||||
, fromDatumUnsafe
|
||||
, Control.Category.id
|
||||
, fromDataUnsafe
|
||||
, writeDatum
|
||||
, writeData
|
||||
)
|
||||
where
|
||||
|
||||
@@ -45,6 +48,8 @@ import qualified Control.Category
|
||||
import qualified Data.Vector as V
|
||||
import Data.String (IsString (fromString))
|
||||
import qualified Data.Text as T
|
||||
import System.Environment (lookupEnv)
|
||||
import Data.Foldable (toList)
|
||||
|
||||
|
||||
toDatum :: Jalmot :> es => DatumGrammar a -> a -> Eff es Datum
|
||||
@@ -59,19 +64,21 @@ toData g =
|
||||
>>> runGrammar noAnn
|
||||
>>> either (throwError . GrammarError) pure
|
||||
|
||||
fromDatum :: Jalmot :> es => DatumGrammar a -> Datum -> Eff es a
|
||||
fromDatum :: (HasCallStack, Jalmot :> es) => DatumGrammar a -> Datum -> Eff es a
|
||||
fromDatum g =
|
||||
forward (sealed g)
|
||||
>>> runGrammar noAnn
|
||||
>>> either (throwError . GrammarError) pure
|
||||
|
||||
fromDatumUnsafe :: DatumGrammar a -> Datum -> a
|
||||
fromDatumUnsafe :: HasCallStack => DatumGrammar a -> Datum -> a
|
||||
fromDatumUnsafe g = runJalmotUnsafe . fromDatum g
|
||||
|
||||
fromDataUnsafe :: DataGrammar a -> List Datum -> a
|
||||
fromDataUnsafe :: HasCallStack => DataGrammar a -> List Datum -> a
|
||||
fromDataUnsafe g = runJalmotUnsafe . fromData g
|
||||
|
||||
fromData :: Jalmot :> es => DataGrammar a -> List Datum -> Eff es a
|
||||
fromData
|
||||
:: (HasCallStack, Jalmot :> es)
|
||||
=> DataGrammar a -> List Datum -> Eff es a
|
||||
fromData g =
|
||||
forward (sealed g)
|
||||
>>> runGrammar noAnn
|
||||
@@ -124,6 +131,39 @@ encodeOrShow' g x = fromString $
|
||||
Left _ -> show x
|
||||
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
|
||||
datumIso :: DatumGrammar a
|
||||
|
||||
@@ -148,16 +188,8 @@ instance DatumIso a => DataIso (V.Vector a) where
|
||||
dataIso = iso fromList V.toList
|
||||
>>> (onHead . traversed . sealed $ datumIso @a)
|
||||
|
||||
instance DatumIso a => DataIso (NonEmpty a) where
|
||||
dataIso = nonEmptyData datumIso
|
||||
|
||||
instance (DatumIso a, DatumIso b) => DatumIso (a, b) where
|
||||
datumIso = with \tup2 -> list (el datumIso >>> el datumIso) >>> tup2
|
||||
|
||||
data Example = MkExample (List Int) Text
|
||||
deriving (Generic, Show)
|
||||
|
||||
instance DataIso Example where
|
||||
dataIso = with \g ->
|
||||
flipped snoced
|
||||
>>> onHead (traversed $ sealed int)
|
||||
>>> onTail (onHead $ sealed symbol)
|
||||
>>> swap
|
||||
>>> g
|
||||
|
||||
@@ -9,7 +9,7 @@ module Gyehoek.Sexp.Grammar.Base
|
||||
, DatumGrammar
|
||||
, DataGrammar
|
||||
, Grammar
|
||||
, ListContext
|
||||
, ListContext(..)
|
||||
, (:-)((:-))
|
||||
-- * lists
|
||||
, list
|
||||
@@ -17,6 +17,7 @@ module Gyehoek.Sexp.Grammar.Base
|
||||
, el
|
||||
, rest
|
||||
, restData
|
||||
, nonEmptyData
|
||||
, headTagged0'
|
||||
, headTagged0
|
||||
, headTagged1'
|
||||
@@ -31,6 +32,9 @@ module Gyehoek.Sexp.Grammar.Base
|
||||
, number
|
||||
, integer
|
||||
, int
|
||||
, unreadable
|
||||
-- ** symbols
|
||||
, symBuiltin
|
||||
-- * TODO: sort lol
|
||||
, prismIso
|
||||
, isoIso, decorate
|
||||
@@ -40,7 +44,7 @@ module Gyehoek.Sexp.Grammar.Base
|
||||
, lambdaLike
|
||||
, lambdaKeyword
|
||||
, kappaKeyword
|
||||
, beginLike
|
||||
, beginLike, headTagged2', dottedList
|
||||
) where
|
||||
|
||||
import Data.InvertibleGrammar
|
||||
@@ -48,13 +52,15 @@ import Data.InvertibleGrammar.Base
|
||||
import Data.InvertibleGrammar.Base as Re
|
||||
( Grammar(..))
|
||||
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.Print (printDatum')
|
||||
import Data.Scientific (Scientific)
|
||||
import qualified Data.Scientific as Sci
|
||||
import qualified Data.Text as T
|
||||
import Control.Monad.RWS (modify)
|
||||
import qualified Data.List.NonEmpty as NE
|
||||
import Data.Foldable (toList)
|
||||
|
||||
|
||||
-- $setup
|
||||
@@ -104,6 +110,39 @@ list
|
||||
-> G (Datum :- t) t'
|
||||
list = listWithIndentation Ordinary
|
||||
|
||||
-- |
|
||||
-- >>> let grammar = with \g -> dottedList (el int) int >>> g
|
||||
-- >>> decodeTest @(Int,Int) grammar "(1 . 2)"
|
||||
-- ( 1
|
||||
-- , 2
|
||||
-- )
|
||||
-- >>> let grammar = with \g -> dottedList (el int >>> el int) int >>> g
|
||||
-- >>> decodeTest @(Int,Int,Int) grammar "(1 2 . 3)"
|
||||
-- ( 1
|
||||
-- , 2
|
||||
-- , 3
|
||||
-- )
|
||||
dottedList
|
||||
:: forall t t' t''. G (ListContext :- t) (ListContext :- t')
|
||||
-> G (Datum :- t') t''
|
||||
-> G (Datum :- t) t''
|
||||
dottedList g final = begin >>> Dive (onTail (g >>> end) >>> final)
|
||||
where
|
||||
begin = locate >>> Flip (PartialIso
|
||||
(\(x:-MkListContext xs:-t) -> case NE.nonEmpty xs of
|
||||
Just xs' -> DotList xs' x :- t
|
||||
Nothing -> error "fuck")
|
||||
(\case
|
||||
DotList xs x :- t -> Right $ x :- MkListContext (NE.toList xs) :- t
|
||||
_ -> Left $ expected "dotted list"))
|
||||
end :: Grammar Ann (ListContext :- t') t'
|
||||
end = Flip $ PartialIso
|
||||
(\t -> MkListContext [] :- t)
|
||||
(\(MkListContext lst :- t) ->
|
||||
case lst of
|
||||
[] -> Right t
|
||||
d:_ -> Left $ unexpectedDatum d)
|
||||
|
||||
listWithIndentation
|
||||
:: Indentation
|
||||
-> G (ListContext :- t) (ListContext :- t')
|
||||
@@ -203,6 +242,14 @@ restData g =
|
||||
>>> g
|
||||
>>> 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
|
||||
:: Snoc s s a a
|
||||
=> Grammar p (s :- a :- t) (s :- t)
|
||||
@@ -325,6 +372,13 @@ headTagged2
|
||||
-> G (Datum :- t) (b :- a :- t)
|
||||
headTagged2 s g1 g2 = list $ el (symProcedure s) >>> el g1 >>> el g2
|
||||
|
||||
headTagged2'
|
||||
:: Text
|
||||
-> DatumGrammar a -> DatumGrammar b -> DatumGrammar c
|
||||
-> G (Datum :- t) (List c :- b :- a :- t)
|
||||
headTagged2' s g1 g2 gt =
|
||||
list $ el (symProcedure s) >>> el g1 >>> el g2 >>> rest gt
|
||||
|
||||
ifLike
|
||||
-- | keyword
|
||||
:: Text
|
||||
@@ -358,9 +412,9 @@ letLike kw name rhs e = listWithIndentation (NSpecial 1) $
|
||||
|
||||
lambdaLike
|
||||
:: (forall t. G (Datum :- t) t)
|
||||
-> DatumGrammar a
|
||||
-> G (ListContext :- a :- t) (ListContext :- t')
|
||||
-> G (Datum :- t) t'
|
||||
-> G (Datum :- t1) (a :- t2)
|
||||
-> G (ListContext :- a :- t2) (ListContext :- t3)
|
||||
-> G (Datum :- t1) t3
|
||||
lambdaLike kw formals body = listWithIndentation (NSpecial 1) $
|
||||
el (decorate SynBuiltin >>> kw)
|
||||
>>> el formals
|
||||
@@ -374,11 +428,19 @@ kappaKeyword = coproduct [ sym "κ", sym "kappa" ]
|
||||
|
||||
beginLike
|
||||
:: Text
|
||||
-> DatumGrammar a
|
||||
-> G (Datum :- t) (List a :- t)
|
||||
-> G (ListContext :- t) (ListContext :- t')
|
||||
-> G (Datum :- t) t'
|
||||
beginLike kw g =
|
||||
listWithIndentation (NSpecial 0) $
|
||||
el (symBuiltin kw) >>> rest g
|
||||
el (symBuiltin kw) >>> g
|
||||
|
||||
-- | define a printed syntax for an object which cannot be read.
|
||||
unreadable
|
||||
:: (t -> Text)
|
||||
-> G (Datum :- t) t
|
||||
unreadable f = Flip $ PartialIso
|
||||
(\t -> Unreadable (f t) :- t)
|
||||
(const $ Left mempty)
|
||||
|
||||
isoIso :: Iso' s a -> Grammar p (s :- t) (a :- t)
|
||||
isoIso l = iso (view l) (review l)
|
||||
|
||||
@@ -4,21 +4,26 @@ module Gyehoek.Sexp.Print
|
||||
, printDatum'
|
||||
, printData
|
||||
, printData'
|
||||
, htmlDatum
|
||||
, htmlData
|
||||
) where
|
||||
|
||||
import Gyehoek.Sexp.Syntax
|
||||
import Data.Text.Prettyprint.Doc
|
||||
import Prettyprinter
|
||||
import Data.Functor.Foldable
|
||||
import qualified Control.Comonad.Trans.Cofree as F
|
||||
import Prettyprinter.Util
|
||||
import Gyehoek.Prelude hiding (Simple, (:<))
|
||||
import Data.Foldable (traverse_)
|
||||
import Data.Foldable (traverse_, toList)
|
||||
import qualified Prettyprinter.Render.Terminal as ANSI
|
||||
import System.IO (stdout)
|
||||
import Prettyprinter.Render.Terminal (Color(..), color, italicized, AnsiStyle, bold, colorDull)
|
||||
import Prettyprinter.Render.Text (renderStrict)
|
||||
import qualified Data.Scientific as Sci
|
||||
import Data.List (intersperse)
|
||||
import Lucid
|
||||
import Prettyprinter.Render.Util.SimpleDocTree (treeForm)
|
||||
import Prettyprinter.Lucid (renderHtml)
|
||||
|
||||
|
||||
printDatum' :: Datum -> Text
|
||||
@@ -31,6 +36,31 @@ printDatum' =
|
||||
{ layoutPageWidth = AvailablePerLine 80 1.0
|
||||
}
|
||||
|
||||
htmlDatum :: Datum -> Html ()
|
||||
htmlDatum =
|
||||
prettyDatum 0
|
||||
>>> layoutPretty opts
|
||||
>>> treeForm
|
||||
>>> fmap highlightHtml
|
||||
>>> renderHtml
|
||||
where
|
||||
opts = LayoutOptions
|
||||
{ layoutPageWidth = AvailablePerLine 80 1.0
|
||||
}
|
||||
|
||||
htmlData :: Foldable f => f Datum -> Html ()
|
||||
htmlData =
|
||||
foldr f mempty
|
||||
>>> layoutPretty opts
|
||||
>>> treeForm
|
||||
>>> fmap highlightHtml
|
||||
>>> renderHtml
|
||||
where
|
||||
f x y = prettyDatum 0 x <> hardline <> hardline <> y
|
||||
opts = LayoutOptions
|
||||
{ layoutPageWidth = AvailablePerLine 80 1.0
|
||||
}
|
||||
|
||||
printDatum :: Datum -> Text
|
||||
printDatum = printDatumW 80
|
||||
|
||||
@@ -44,7 +74,7 @@ printDatumW :: Int -> Datum -> Text
|
||||
printDatumW w =
|
||||
prettyDatum 0
|
||||
>>> layoutSmart opts
|
||||
>>> reAnnotateS highlight
|
||||
>>> reAnnotateS highlightAnsi
|
||||
>>> ANSI.renderStrict
|
||||
where
|
||||
opts = LayoutOptions
|
||||
@@ -54,6 +84,13 @@ printDatumW w =
|
||||
prettyDatum :: Int -> Datum -> Doc Syn
|
||||
prettyDatum depth datum = case datum of
|
||||
Simple simp -> annotate (datum ^. syntax) $ prettySimple depth simp
|
||||
DotList xs x ->
|
||||
pparen depth . group . align $
|
||||
vsep [ vsep (prettyDatum (depth+1) <$> toList xs)
|
||||
, "."
|
||||
, prettyDatum (depth+1) x
|
||||
]
|
||||
|
||||
List' indent xs ->
|
||||
case indent of
|
||||
NSpecial n | keyword:args <- xs ->
|
||||
@@ -87,13 +124,14 @@ prettySimple depth = \case
|
||||
& annotate SynConstant
|
||||
SimpleString s -> annotate SynString $ viaShow s
|
||||
SimpleSymbol s -> pretty s
|
||||
SimpleUnreadable s -> pretty s
|
||||
|
||||
putDoc :: Doc Syn -> IO ()
|
||||
putDoc = ANSI.renderIO stdout
|
||||
. reAnnotateS highlight . layoutSmart defaultLayoutOptions . (<>"\n")
|
||||
. reAnnotateS highlightAnsi . layoutSmart defaultLayoutOptions . (<>"\n")
|
||||
|
||||
highlight :: Syn -> AnsiStyle
|
||||
highlight = \case
|
||||
highlightAnsi :: Syn -> AnsiStyle
|
||||
highlightAnsi = \case
|
||||
(SynBuiltin; SynMacro) -> color Magenta <> italicized <> bold
|
||||
SynProcedure -> color Blue
|
||||
SynConstant -> color Yellow
|
||||
@@ -101,3 +139,16 @@ highlight = \case
|
||||
_ -> mempty
|
||||
where
|
||||
rainbow = cycle [Red,Yellow,Green,Blue,Magenta,Cyan]
|
||||
|
||||
highlightHtml :: Syn -> Html () -> Html ()
|
||||
highlightHtml syn = span_ [class_ synClass]
|
||||
where
|
||||
synClass = case syn of
|
||||
SynBuiltin -> "syn-builtin"
|
||||
SynMacro -> "syn-macro"
|
||||
SynConstant -> "syn-constant"
|
||||
SynString -> "syn-string"
|
||||
SynProcedure -> "syn-procedure"
|
||||
SynVariable -> "syn-variable"
|
||||
SynNone -> "syn-none"
|
||||
SynParen n -> [i|syn-paren-#{mod n 5}|]
|
||||
|
||||
@@ -29,6 +29,7 @@ module Gyehoek.Sexp.Syntax
|
||||
, indentation
|
||||
, adorn
|
||||
, indentWith
|
||||
, pattern Unreadable
|
||||
, pattern Bytevector
|
||||
, pattern Symbol
|
||||
, pattern String
|
||||
@@ -79,6 +80,7 @@ data Simple
|
||||
| SimpleString Text
|
||||
| SimpleSymbol Text
|
||||
| SimpleBytevector ByteString
|
||||
| SimpleUnreadable Text
|
||||
deriving stock (Show, Eq, Data, Generic, Lift)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
@@ -230,6 +232,7 @@ pattern Character a = Simple (SimpleCharacter a)
|
||||
pattern String a = Simple (SimpleString a)
|
||||
pattern Symbol a = Simple (SimpleSymbol a)
|
||||
pattern Bytevector a = Simple (SimpleBytevector a)
|
||||
pattern Unreadable a = Simple (SimpleUnreadable a)
|
||||
|
||||
|
||||
--- Lift1 instances
|
||||
|
||||
@@ -1,145 +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
|
||||
, pattern ValLabel
|
||||
, 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(..), labelName)
|
||||
import Gyehoek.Prelude
|
||||
import Gyehoek.Sexp ((:-)((:-)))
|
||||
|
||||
|
||||
newtype Program = MkProgram
|
||||
{ routines :: HashMap Name 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 :: Name
|
||||
, params :: List Name
|
||||
, 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
|
||||
= TailCall Val (List Val)
|
||||
| If Val Block Block
|
||||
deriving stock (Show, Generic, Data)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
data Instr
|
||||
= Pop Name
|
||||
| Push Val
|
||||
| PopCont Name
|
||||
| PushCont Val
|
||||
| Prim Name (Prim Val)
|
||||
deriving stock (Show, Generic, Data)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
data Val
|
||||
= ValReg Name
|
||||
| ValImm Imm
|
||||
deriving stock (Show, Generic, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
pattern ValLabel :: Name -> Val
|
||||
pattern ValLabel x = ValImm (ImmLabel x)
|
||||
|
||||
|
||||
--- sexp work
|
||||
|
||||
pure []
|
||||
|
||||
instance S.DatumIso Instr where
|
||||
datumIso = S.match
|
||||
$ S.With (S.headTagged1 "pop!" regName >>>)
|
||||
$ S.With (S.headTagged1 "push!" S.datumIso >>>)
|
||||
$ S.With (S.headTagged1 "pop-cont!" regName >>>)
|
||||
$ S.With (S.headTagged1 "push-cont!" S.datumIso >>>)
|
||||
$ S.With (S.headTagged2 "prim" regName 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.datumIso >>>)
|
||||
$ S.With (if_ >>>)
|
||||
$ S.End
|
||||
where
|
||||
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 (regName >>>)
|
||||
$ 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.list $ S.el labelName >>> S.rest regName)
|
||||
>>> S.restData (S.dataIso @Block)
|
||||
)
|
||||
>>> rout
|
||||
|
||||
regName :: S.DatumGrammar Name
|
||||
regName = S.decorate S.SynVariable >>> S.datumIso @Name >>> S.prismIso
|
||||
(S.expected "register")
|
||||
(prefixed @Name "%")
|
||||
|
||||
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,156 +0,0 @@
|
||||
{-# LANGUAGE ViewPatterns #-}
|
||||
module Gyehoek.Stack.VM
|
||||
( VM(..)
|
||||
, Env(..)
|
||||
, eval
|
||||
, trace
|
||||
, module Gyehoek.Stack.Syntax
|
||||
, writeObj
|
||||
) where
|
||||
|
||||
import Gyehoek.Stack.Syntax
|
||||
import Control.Lens
|
||||
import qualified Data.HashMap.Strict as H
|
||||
import Data.List (unfoldr)
|
||||
import Gyehoek.Prelude
|
||||
|
||||
|
||||
data VM = MkVM
|
||||
{ stack :: List Obj
|
||||
, kstack :: List Name
|
||||
, code :: List Instr
|
||||
, tail :: Tail
|
||||
, registers :: HashMap Name Obj
|
||||
, stdout :: Text
|
||||
, result :: Maybe (List Obj)
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
data Env = MkEnv
|
||||
{ labels :: HashMap Name Routine
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
step :: Env -> VM -> VM
|
||||
step g vm = case vm ^. #code of
|
||||
c:cs -> stepI g (vm & #code .~ cs) c
|
||||
[] -> stepT g vm vm.tail
|
||||
|
||||
stepI :: Env -> VM -> Instr -> VM
|
||||
|
||||
stepI e vm (Push v) = vm & #stack %~ (evalVal e vm v :)
|
||||
|
||||
stepI e vm (PushCont k) = vm & #kstack %~ (evalToLabel e vm k :)
|
||||
|
||||
stepI e vm (Prim r p) = case evalVal e vm <$> p of
|
||||
PrimZeroP x -> case x of
|
||||
ObjImm (ImmInt n) -> ret . ObjImm . ImmBool $ n == 0
|
||||
_ -> error [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) -> ret . ObjHob $ HobClosure l env
|
||||
_ -> error [i|expected label, got #{f}|]
|
||||
PrimEnvCode env ->
|
||||
case env of
|
||||
ObjHob (HobClosure l _) -> ret . ObjImm . ImmLabel $ l
|
||||
_ -> error [i|expected closure, got #{env}|]
|
||||
PrimEnvRef env n ->
|
||||
case env of
|
||||
ObjHob (HobClosure _ xs) -> ret $ xs ^?! ix n
|
||||
_ -> error [i|expected closure, got #{env}|]
|
||||
x -> error [i|unimplemented prim: #{p}|]
|
||||
where
|
||||
ret v = vm & #registers . at r ?~ v
|
||||
arith_binop op (ObjImm (ImmInt x)) (ObjImm (ImmInt y)) =
|
||||
ret $ ObjImm (ImmInt (op x y))
|
||||
arith_binop _ x y = error [i|bad arith: #{x}, #{y}|]
|
||||
|
||||
stepI e vm (Pop r) = case vm ^. #stack of
|
||||
[] -> error "empty stack"
|
||||
(x:xs) -> vm & #registers . at r ?~ x
|
||||
& #stack .~ xs
|
||||
|
||||
stepI e vm ins@(PopCont r) = case vm ^. #kstack of
|
||||
[] -> error [i|empty cont stack: #{ins}|]
|
||||
(x:xs) -> vm & #registers . at r ?~ ObjImm (ImmLabel x)
|
||||
& #kstack .~ xs
|
||||
|
||||
stepI e vm ins = error [i|unimplemented instruction: #{ins}|]
|
||||
|
||||
stepT :: Env -> VM -> Tail -> VM
|
||||
|
||||
stepT g vm (TailCall f xs) =
|
||||
case evalToLabel g vm f of
|
||||
"halt" -> vm & #result ?~ fmap (evalVal g vm) xs
|
||||
l -> vm & #code .~ rt.start.code
|
||||
& #tail .~ rt.start.tail
|
||||
& #registers .~
|
||||
fmap (evalVal g vm) (H.fromList $ rt.params `zip` xs)
|
||||
where
|
||||
rt = case g ^. #labels . at l of
|
||||
Nothing -> error [i|undefined label: #{l}|]
|
||||
Just x -> x
|
||||
|
||||
stepT g vm (If c t f) = vm & #code .~ branch.code & #tail .~ branch.tail
|
||||
where
|
||||
branch = case evalVal g vm c of
|
||||
ObjImm (ImmBool False) -> f
|
||||
_ -> t
|
||||
|
||||
evalToLabel e vm v =
|
||||
case evalVal e vm v of
|
||||
ObjImm (ImmLabel x) -> x
|
||||
x -> error [i|not a label: #{x}|]
|
||||
|
||||
evalVal :: Env -> VM -> Val -> Obj
|
||||
evalVal e vm = \case
|
||||
ValImm imm -> ObjImm imm
|
||||
ValReg r -> case vm ^. #registers . at r of
|
||||
Just x -> x
|
||||
Nothing -> error [i|undefined register: #{r}|]
|
||||
|
||||
initialVM :: VM
|
||||
initialVM = MkVM
|
||||
{ stack = []
|
||||
, kstack = ["halt"]
|
||||
, code = []
|
||||
, tail = TailCall (ValLabel "main") []
|
||||
, registers = mempty
|
||||
, stdout = ""
|
||||
, result = Nothing
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
eval :: Program -> List Obj
|
||||
eval p = initialVM & loop \vm -> case vm ^. #result of
|
||||
Nothing -> Right $ step (initialEnv p) vm
|
||||
Just rs -> Left rs
|
||||
|
||||
trace :: Program -> List VM
|
||||
trace p = initialVM & unfoldr \vm ->
|
||||
case vm.result of
|
||||
Just _ -> Nothing
|
||||
Nothing -> Just (vm, step e vm)
|
||||
where e = initialEnv p
|
||||
|
||||
writeObj :: Obj -> Text
|
||||
writeObj (ObjImm im) = case im of
|
||||
ImmInt n -> [i|#{n}|]
|
||||
ImmBool True -> "#t"
|
||||
ImmBool False -> "#f"
|
||||
ImmLabel l -> "#<procedure>"
|
||||
writeObj (ObjHob h) = case h of
|
||||
HobClosure code env -> "#<procedure>"
|
||||
+18
-160
@@ -1,4 +1,3 @@
|
||||
{- HLINT ignore "Use newtype instead of data" -}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
{-# LANGUAGE TemplateHaskellQuotes #-}
|
||||
@@ -6,182 +5,41 @@ module Gyehoek.Wasm
|
||||
(
|
||||
-- * syntax
|
||||
Module
|
||||
, Idx
|
||||
, Program
|
||||
, Function
|
||||
, Expr
|
||||
-- ** quasiquoters
|
||||
, expr
|
||||
, S.sx
|
||||
, S.sxs
|
||||
-- * GenMod effect
|
||||
, GenMod
|
||||
, runGenMod
|
||||
, execGenMod
|
||||
, defineFunction
|
||||
, defineType
|
||||
, defineGlobal
|
||||
, emit
|
||||
, renderModule
|
||||
, watM
|
||||
, wat
|
||||
, wats
|
||||
, defineFunctions
|
||||
, defineTypes
|
||||
, defineGlobals
|
||||
)
|
||||
where
|
||||
|
||||
import Data.List (List)
|
||||
import GHC.Generics (Generic)
|
||||
import Data.Text (Text)
|
||||
import Effectful
|
||||
import Numeric.Natural (Natural)
|
||||
import Effectful.Dispatch.Dynamic
|
||||
import Effectful.State.Dynamic
|
||||
import Control.Lens
|
||||
import Data.Vector.Strict (Vector)
|
||||
import qualified Data.Vector.Strict as V
|
||||
import GHC.IsList (IsList(..))
|
||||
import Language.Haskell.TH.Quote (QuasiQuoter)
|
||||
import Data.Data (Data)
|
||||
import Gyehoek.Sexp qualified as S
|
||||
import Gyehoek.Sexp (Datum, sx, (>>>))
|
||||
import Data.Foldable (traverse_)
|
||||
import Data.Coerce (coerce)
|
||||
import Gyehoek.Sexp (Datum, (>>>))
|
||||
import Data.Data (Data)
|
||||
|
||||
|
||||
newtype Module = MkModule { inner :: Vector Datum }
|
||||
deriving (Show, Generic)
|
||||
type Program = Module
|
||||
type Function = Datum
|
||||
type Expr = List Datum
|
||||
|
||||
newtype Module = MkModule { inner :: List Datum }
|
||||
deriving (Show, Generic, Data)
|
||||
deriving newtype (Semigroup, Monoid)
|
||||
|
||||
newtype Expr = MkExpr { inner :: Vector Instr }
|
||||
deriving (Show, Generic, Data, Eq)
|
||||
deriving newtype (Semigroup, Monoid)
|
||||
|
||||
instance IsList Expr where
|
||||
type Item Expr = Instr
|
||||
fromList = MkExpr . V.fromList
|
||||
toList = V.toList . view #inner
|
||||
|
||||
newtype Instr = MkInstr { inner :: Datum }
|
||||
deriving (Show, Generic, Data, Eq)
|
||||
|
||||
newtype Idx = MkIdx { inner :: Natural }
|
||||
deriving (Generic, Data)
|
||||
deriving newtype (Show)
|
||||
|
||||
|
||||
-- GenMod
|
||||
|
||||
-- | 'GenModState' is a 'Module' paired with the numbers of functions,
|
||||
-- types, globals, etc. defined in the module.
|
||||
data GenModState = MkGenModState
|
||||
{ mod :: Module
|
||||
, funcs :: Natural
|
||||
, types :: Natural
|
||||
, globals :: Natural
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
instance Semigroup GenModState where
|
||||
m1 <> m2 = MkGenModState
|
||||
{ mod = m1.mod <> m2.mod
|
||||
, funcs = m1.funcs + m2.funcs
|
||||
, types = m1.types + m2.types
|
||||
, globals = m1.globals + m2.globals
|
||||
}
|
||||
|
||||
instance Monoid GenModState where
|
||||
mempty = MkGenModState
|
||||
{ mod = mempty
|
||||
, funcs = 0
|
||||
, types = 0
|
||||
, globals = 0
|
||||
}
|
||||
|
||||
data GenMod :: Effect where
|
||||
DefineFunction :: Datum -> GenMod m Idx
|
||||
DefineType :: Datum -> GenMod m Idx
|
||||
DefineGlobal :: Datum -> GenMod m Idx
|
||||
Emit :: Datum -> GenMod m ()
|
||||
|
||||
type instance DispatchOf GenMod = Dynamic
|
||||
|
||||
defineFunction :: GenMod :> es => Datum -> Eff es Idx
|
||||
defineFunction = send . DefineFunction
|
||||
|
||||
defineFunctions :: GenMod :> es => List Datum -> Eff es (List Idx)
|
||||
defineFunctions = traverse (send . DefineFunction)
|
||||
|
||||
defineType :: GenMod :> es => Datum -> Eff es Idx
|
||||
defineType = send . DefineType
|
||||
|
||||
defineTypes :: GenMod :> es => List Datum -> Eff es (List Idx)
|
||||
defineTypes = traverse (send . DefineType)
|
||||
|
||||
defineGlobal :: GenMod :> es => Datum -> Eff es Idx
|
||||
defineGlobal = send . DefineGlobal
|
||||
|
||||
defineGlobals :: GenMod :> es => List Datum -> Eff es (List Idx)
|
||||
defineGlobals = traverse (send . DefineGlobal)
|
||||
|
||||
emit :: GenMod :> es => List Datum -> Eff es ()
|
||||
emit = traverse_ (send . Emit)
|
||||
|
||||
appendAndIncrement
|
||||
:: State GenModState :> es
|
||||
=> LensLike' ((,) Natural) GenModState Natural
|
||||
-> Datum
|
||||
-> Eff es Idx
|
||||
appendAndIncrement l s =
|
||||
state \st -> st
|
||||
& #mod . #inner <>~ V.singleton s
|
||||
& l <<%~ succ
|
||||
& _1 %~ MkIdx
|
||||
|
||||
runGenMod :: Eff (GenMod : es) a -> Eff es (a, Module)
|
||||
runGenMod =
|
||||
let run = (mapped . _2 %~ view #mod) . runStateLocal (mempty @GenModState)
|
||||
in reinterpret run \cases
|
||||
_ (DefineFunction s) -> appendAndIncrement #funcs s
|
||||
_ (DefineType s) -> appendAndIncrement #types s
|
||||
_ (DefineGlobal s) -> appendAndIncrement #globals s
|
||||
_ (Emit s) -> #mod . #inner <>= V.singleton s
|
||||
|
||||
execGenMod :: Eff (GenMod : es) a -> Eff es Module
|
||||
execGenMod = fmap snd . runGenMod
|
||||
|
||||
renderModule :: Module -> Text
|
||||
renderModule (MkModule ss) = S.encodeWith' S.datumIso [sx|
|
||||
(module ##{ss})
|
||||
|]
|
||||
|
||||
|
||||
-- DatumIso instances
|
||||
|
||||
instance S.DatumIso Idx where
|
||||
datumIso = S.with \idx ->
|
||||
S.integer >>> S.partialOsi f g
|
||||
>>> idx
|
||||
where
|
||||
f n | n < 0 = Left $ S.unexpected "negative"
|
||||
<> S.expected "natural"
|
||||
| otherwise = Right $ fromIntegral n
|
||||
g = fromIntegral
|
||||
|
||||
instance S.DatumIso Instr where
|
||||
datumIso = S.with S.id
|
||||
|
||||
instance S.DataIso Expr where
|
||||
dataIso = S.dataIso @(Vector Instr) >>> S.iso coerce coerce
|
||||
instance S.DatumIso Module where
|
||||
datumIso = S.with \g ->
|
||||
S.list (S.el (S.sym "module") >>> S.rest S.datumIso)
|
||||
>>> g
|
||||
|
||||
|
||||
-- quasiquoters
|
||||
|
||||
expr :: QuasiQuoter
|
||||
expr = S.makeSxs
|
||||
[|| MkExpr . V.fromList . fmap (S.fromDatumUnsafe $ S.datumIso @Instr) ||]
|
||||
|
||||
wat :: QuasiQuoter
|
||||
wat = S.makeSx [|| id ||]
|
||||
wat = S.makeSxs [|| S.fromDataUnsafe (S.dataIso @(List Datum)) ||]
|
||||
|
||||
wats :: QuasiQuoter
|
||||
wats = S.makeSxs [|| id ||]
|
||||
watM :: QuasiQuoter
|
||||
watM = S.makeSx [|| S.fromDatumUnsafe (S.datumIso @Module) ||]
|
||||
|
||||
BIN
Binary file not shown.
@@ -5,50 +5,75 @@ import Test.Tasty.HUnit
|
||||
import Gyehoek.CPS.Syntax (cps, Obj(..), Hob(..), Imm(..))
|
||||
import Gyehoek.CPS.Eval qualified as Sut
|
||||
import Data.List (List)
|
||||
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 = testGroup "cps interpreter" $
|
||||
[ primitives
|
||||
, testCase "halt with constant" do
|
||||
evalsTo [ObjImm (ImmInt 123)] [cps|
|
||||
(continue halt 123)
|
||||
|]
|
||||
, testCase "identity cont" do
|
||||
evalsTo [ObjImm (ImmInt 154)] [cps|
|
||||
(letrec ((id (κ (x)
|
||||
(continue halt x))))
|
||||
(continue id 154))
|
||||
|]
|
||||
, testCase "identity function" do
|
||||
evalsTo [ObjImm (ImmInt 456)] [cps|
|
||||
(letrec ((id (λ (x ktail)
|
||||
(continue ktail x))))
|
||||
(id 456 halt))
|
||||
|]
|
||||
, testCase "square" do
|
||||
evalsTo [ObjImm (ImmInt 81)] [cps|
|
||||
(letrec ((square (λ (x ktail)
|
||||
(prim (* x x)
|
||||
(κ (r) (continue ktail r))))))
|
||||
(square 9 halt))
|
||||
|]
|
||||
]
|
||||
brokenEvalTests :: List String
|
||||
brokenEvalTests =
|
||||
[]
|
||||
-- [ "adder"
|
||||
-- , "apply2"
|
||||
-- , "apply-twice"
|
||||
-- , "arith"
|
||||
-- , "begin-1"
|
||||
-- , "callcc-constant"
|
||||
-- , "callcc-discard"
|
||||
-- , "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"
|
||||
-- , "complicated-1"
|
||||
-- , "cons-1"
|
||||
-- , "factorial"
|
||||
-- , "false"
|
||||
-- , "fn-of-fn"
|
||||
-- , "if-false"
|
||||
-- , "if-number"
|
||||
-- , "if-true"
|
||||
-- , "lambda"
|
||||
-- , "letrec-fn"
|
||||
-- , "let-fn"
|
||||
-- , "lit-int"
|
||||
-- , "square"
|
||||
-- , "true"
|
||||
-- ]
|
||||
|
||||
evalsTo :: HasCallStack => List Obj -> Sut.Program -> Assertion
|
||||
evalsTo rs p = Sut.evalProgram p @?= rs
|
||||
|
||||
primitives = testGroup "primitives"
|
||||
[ testGroup "arith"
|
||||
[ testCase "basic 1" do
|
||||
evalsTo [ObjImm (ImmInt 20)] [cps|
|
||||
(prim (* 4 5)
|
||||
(κ (x) (continue halt x)))
|
||||
|]
|
||||
, testCase "basic 2" do
|
||||
evalsTo [ObjImm (ImmInt 35)] [cps|
|
||||
(prim (* 2 16)
|
||||
(κ (x) (prim (+ x 3)
|
||||
(κ (r) (continue halt r)))))
|
||||
|]
|
||||
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
|
||||
]
|
||||
]
|
||||
|
||||
maybeBroken name broken = applyWhen (name `elem` broken) expectFail
|
||||
|
||||
cpsCase :: (FilePath -> IO Text) -> FilePath -> TestTree
|
||||
cpsCase f test =
|
||||
maybeBroken testName brokenEvalTests $
|
||||
goldenVsAction testName resultFile action printProcResult
|
||||
where
|
||||
testName = takeFileName test
|
||||
resultFile = test </> "exec"
|
||||
sourceFile = test </> "source.scm"
|
||||
action = catch @SomeException
|
||||
(do r <- f sourceFile
|
||||
pure $!! ( ExitSuccess
|
||||
, r
|
||||
, "" ))
|
||||
\e -> pure (ExitFailure 1, "", T.pack $ displayException e)
|
||||
|
||||
@@ -1,86 +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 Data.List (List)
|
||||
import Gyehoek.CPS.Syntax (cps)
|
||||
import Gyehoek.GenSym (runGenSym)
|
||||
import Effectful
|
||||
|
||||
|
||||
test_stackify =
|
||||
[ trivialReturn
|
||||
, tailCall
|
||||
, prim
|
||||
, condition
|
||||
, procedure
|
||||
]
|
||||
|
||||
evalsTo :: List Obj -> Sut.Exp -> Assertion
|
||||
evalsTo rs e =
|
||||
Stk.eval e' @?= rs
|
||||
where e' = runPureEff . runGenSym $ Sut.stackifyExp "main" e
|
||||
|
||||
trivialReturn = testGroup "trivial return"
|
||||
[ testCase "return int" do
|
||||
evalsTo [ObjImm (ImmInt 4)]
|
||||
[cps|(continue halt 4)|]
|
||||
, testCase "return bool" do
|
||||
evalsTo [ObjImm (ImmBool True)]
|
||||
[cps|(continue halt #t)|]
|
||||
evalsTo [ObjImm (ImmBool False)]
|
||||
[cps|(continue halt #f)|]
|
||||
]
|
||||
|
||||
tailCall = testGroup "tail call"
|
||||
[ testCase "square" do
|
||||
evalsTo [ObjImm (ImmInt 16)]
|
||||
[cps|(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|(prim (* 4 5)
|
||||
(κ (x) (continue halt x)))|]
|
||||
, testCase "add" do
|
||||
evalsTo [ObjImm (ImmInt 9)]
|
||||
[cps|(prim (+ 4 5)
|
||||
(κ (x) (continue halt 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|(if #t (continue halt 123) (continue halt 456))|]
|
||||
evalsTo [ObjImm (ImmInt 456)]
|
||||
[cps|(if #f (continue halt 123) (continue halt 456))|]
|
||||
|
||||
procedure = testGroup "procedure"
|
||||
[ testCase "factorial" do
|
||||
evalsTo [ObjImm (ImmInt 720)]
|
||||
[cps|(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))|]
|
||||
]
|
||||
@@ -28,22 +28,27 @@ free = testGroup "free"
|
||||
qq :: TestTree
|
||||
qq = testGroup "parser"
|
||||
[ testCase "lambda" do
|
||||
assertEqual "" (Sut.MkLambda ["x","y"] "ktail"
|
||||
(Sut.ExpContinue "ktail" [Sut.ValVar "x"]))
|
||||
assertEqual ""
|
||||
(Sut.MkLambda ["x","y"] "ktail"
|
||||
(Sut.ExpContinue (Sut.ValVar "ktail") [Sut.ValVar "x"]))
|
||||
[cps|(λ (x y ktail) (continue ktail x))|]
|
||||
assertEqual "" (Sut.MkLambda [] "ktail"
|
||||
(Sut.ExpContinue "ktail" [Sut.ValVar "x"]))
|
||||
assertEqual ""
|
||||
(Sut.MkLambda [] "ktail"
|
||||
(Sut.ExpContinue (Sut.ValVar "ktail") [Sut.ValVar "x"]))
|
||||
[cps|(λ (ktail) (continue ktail x))|]
|
||||
, testCase "kappa" do
|
||||
assertEqual "" (Sut.MkKappa ["x","y"]
|
||||
(Sut.ExpContinue "k123" [Sut.ValVar "x", Sut.ValVar "y"]))
|
||||
assertEqual ""
|
||||
(Sut.MkKappa ["x","y"]
|
||||
(Sut.ExpContinue
|
||||
(Sut.ValVar "k123")
|
||||
[Sut.ValVar "x", Sut.ValVar "y"]))
|
||||
[cps|(κ (x y) (continue k123 x y))|]
|
||||
, testCase "application" do
|
||||
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
||||
[Sut.ValVar "x",Sut.ValVar "y"]
|
||||
"k")
|
||||
(Sut.KexpVar "k"))
|
||||
[cps|(f x y k)|]
|
||||
assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
|
||||
[] "k")
|
||||
[] (Sut.KexpVar "k"))
|
||||
[cps|(f k)|]
|
||||
]
|
||||
|
||||
@@ -1,87 +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 =
|
||||
[]
|
||||
-- [ "adder"
|
||||
-- , "let-fn"
|
||||
-- , "callcc-nested1" -- requires closure-conversion
|
||||
-- ]
|
||||
|
||||
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,75 +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)
|
||||
|
||||
|
||||
evalsTo :: List Obj -> Program -> Assertion
|
||||
evalsTo rs p = Sut.eval p @?= rs
|
||||
|
||||
test_root = testGroup "stack machine"
|
||||
[ testCase "lit int" do
|
||||
evalsTo [ObjImm (ImmInt 3)] [stkP|
|
||||
(define ($main)
|
||||
(pop-cont! %ktail)
|
||||
(tail-call %ktail 3))
|
||||
|]
|
||||
, testCase "return constant" do
|
||||
evalsTo [ObjImm (ImmInt 123)] [stkP|
|
||||
(define ($main)
|
||||
(tail-call $silly))
|
||||
(define ($silly)
|
||||
(pop-cont! %ktail)
|
||||
(tail-call %ktail 123))
|
||||
|]
|
||||
, testCase "identity function" do
|
||||
evalsTo [ObjImm (ImmInt 45)] [stkP|
|
||||
(define ($main)
|
||||
(tail-call $id 45))
|
||||
(define ($id %x)
|
||||
(pop-cont! %ktail)
|
||||
(tail-call %ktail %x))
|
||||
|]
|
||||
-- , testCase "square" do
|
||||
-- evalsTo [ObjImm (ImmInt 16)] [stkP|
|
||||
-- (define ($main))
|
||||
-- |]
|
||||
, testCase "square" do
|
||||
evalsTo [ObjImm (ImmInt 16)] [stkP|
|
||||
(define ($main)
|
||||
(tail-call $square 4))
|
||||
(define ($square %x)
|
||||
(prim %x2 (* %x %x))
|
||||
(pop-cont! %ktail)
|
||||
(tail-call %ktail %x2))
|
||||
|]
|
||||
, testCase "factorial" do
|
||||
let hsfac (n :: Int) = foldr (*) (1) [1..n]
|
||||
let fac (n :: Int) = [stkP|
|
||||
(define ($fac %n)
|
||||
(prim %x0 (zero? %n))
|
||||
(if %x0
|
||||
(then (pop-cont! %ktail)
|
||||
(tail-call %ktail 1))
|
||||
(else (push! %n)
|
||||
(prim %x1 (- %n 1))
|
||||
(push-cont! $fac-k0)
|
||||
(tail-call $fac %x1))))
|
||||
(define ($fac-k0 %x2)
|
||||
(pop! %n)
|
||||
(prim %x3 (* %x2 %n))
|
||||
(pop-cont! %ktail)
|
||||
(tail-call %ktail %x3))
|
||||
(define ($main)
|
||||
(tail-call $fac #{n}))
|
||||
|]
|
||||
evalsTo [ObjImm (ImmInt 1)] $ fac 0
|
||||
evalsTo [ObjImm (ImmInt 1)] $ fac 1
|
||||
evalsTo [ObjImm (ImmInt 720)] $ fac 6
|
||||
-- 20 is the greatest `n` for which n! ≤ maxBount @Int
|
||||
evalsTo [ObjImm (ImmInt 2432902008176640000)] $ fac 20
|
||||
]
|
||||
Reference in New Issue
Block a user