Compare commits
47
Commits
1d584c7946
...
superfuck
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
99d9e460fc | ||
|
|
c9eb8f6f85 | ||
|
|
030b36cd98 | ||
|
|
bf2fc08ec4 | ||
|
|
950d123760 | ||
|
|
8a20c4f4aa | ||
|
|
d4c1385225 | ||
|
|
2ceefdb3df | ||
|
|
7c0642655f | ||
|
|
bf5595f185 | ||
|
|
bbcc924b34 | ||
|
|
73063d2b2c | ||
|
|
4beeb7c4cd | ||
|
|
c340ede84f | ||
|
|
66386cda64 | ||
|
|
b234a52d4b | ||
|
|
6ff01a8607 |
+9
-1
@@ -8,4 +8,12 @@
|
||||
. ((eval
|
||||
. (progn (defun apply-cabal-fmt-h ()
|
||||
(haskell-mode-buffer-apply-command "cabal-fmt"))
|
||||
(add-hook 'before-save-hook #'apply-cabal-fmt-h nil t))))))
|
||||
(add-hook 'before-save-hook #'apply-cabal-fmt-h nil t)))))
|
||||
(scheme-mode
|
||||
. ((eval . (dolist (s '(kappa κ prim))
|
||||
(put s 'scheme-indent-function 1)))))
|
||||
(nil
|
||||
. ((eval
|
||||
. (progn (defun display-ansi ()
|
||||
(interactive)
|
||||
(ansi-color-apply-on-region (point-min) (point-max))))))))
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
use flake
|
||||
watch_file gyehoek.cabal cabal.project
|
||||
PATH_add $(dirname $(cabal list-bin gyehoek))
|
||||
|
||||
+2
-1
@@ -8,4 +8,5 @@ dist-newstyle
|
||||
*.tix
|
||||
.direnv
|
||||
result
|
||||
play/
|
||||
play/
|
||||
trace.html
|
||||
@@ -1,5 +1,10 @@
|
||||
packages: *.cabal
|
||||
tests: True
|
||||
-- required for doctest-parallel
|
||||
write-ghc-environment-files: always
|
||||
|
||||
-- https://github.com/martijnbastiaan/doctest-parallel/pull/66
|
||||
allow-older: Cabal:process
|
||||
|
||||
source-repository-package
|
||||
type: git
|
||||
|
||||
@@ -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,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
|
||||
|
||||
@@ -20,12 +20,13 @@
|
||||
overlays = [
|
||||
haskellNix.overlay
|
||||
(final: prev: {
|
||||
gyehoek-runtime = final.callPackage ./runtime {
|
||||
gyehoek-wasm-runtime = final.callPackage ./wasm-runtime {
|
||||
crane-lib = inputs.crane.mkLib final;
|
||||
};
|
||||
gyehoek = final.haskell-nix.project' {
|
||||
src = ./.;
|
||||
compiler-nix-name = "ghc912";
|
||||
configureArgs = "-f-doctest";
|
||||
modules = [({ pkgs, lib, ...}: {
|
||||
packages.gyehoek.components.tests.test.preCheck =
|
||||
let
|
||||
@@ -33,14 +34,16 @@
|
||||
pkgs.git # tasty uses git diff
|
||||
];
|
||||
in ''
|
||||
export GYEHOEK_RUNTIME=${lib.getExe final.gyehoek-runtime}
|
||||
export GYEHOEK_WASM_RUNTIME=${
|
||||
lib.getExe final.gyehoek-wasm-runtime
|
||||
}
|
||||
export PATH=${lib.makeBinPath bin}:$PATH
|
||||
'';
|
||||
})];
|
||||
shell = {
|
||||
withHoogle = true;
|
||||
inputsFrom = [
|
||||
final.gyehoek-runtime
|
||||
final.gyehoek-wasm-runtime
|
||||
];
|
||||
tools = {
|
||||
cabal = {};
|
||||
@@ -91,7 +94,7 @@
|
||||
hf.packages.${system} // lib.fix (packages: {
|
||||
gyehoek = hf.packages.${system}."gyehoek:exe:gyehoek";
|
||||
default = packages.gyehoek;
|
||||
inherit (pkgs) gyehoek-runtime;
|
||||
inherit (pkgs) gyehoek-wasm-runtime;
|
||||
}));
|
||||
|
||||
devShells = each-system
|
||||
|
||||
@@ -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 @@
|
||||
(let ((p (cons 123 456)))
|
||||
(cons (cdr p) (car p)))
|
||||
@@ -1,2 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > 720
|
||||
out > 2432902008176640000
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
(if (zero? n)
|
||||
1
|
||||
(* n (fac (- n 1)))))))
|
||||
(fac 6))
|
||||
(fac 20))
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
ret > ExitSuccess
|
||||
out > 123
|
||||
@@ -0,0 +1 @@
|
||||
123
|
||||
@@ -0,0 +1,4 @@
|
||||
[0;31m([0m[0;95;1;3mbegin[0m
|
||||
[0m책을[0m
|
||||
[0m더[0m
|
||||
[0m먹으세요~![0m[0;31m)[0m
|
||||
@@ -0,0 +1,4 @@
|
||||
[0;31m([0m[0;95;1;3mbegin[0m
|
||||
[0m책을[0m
|
||||
[0m더[0m
|
||||
[0m먹으세요~![0m[0;31m)[0m
|
||||
@@ -0,0 +1,5 @@
|
||||
[0;31m([0m[0;95;1;3mlambda[0m
|
||||
[0;33m([0m[0m어간[0m
|
||||
[0m어미[0m[0;33m)[0m
|
||||
[0;33m([0m[0mdisplay[0m
|
||||
[0m꾸깃[0m[0;33m)[0m[0;31m)[0m
|
||||
@@ -0,0 +1,2 @@
|
||||
[0;31m([0m[0;95;1;3mlambda[0m [0;33m([0m[0m어간[0m [0m어미[0m[0;33m)[0m
|
||||
[0;33m([0m[0mdisplay[0m [0m꾸깃[0m[0;33m)[0m[0;31m)[0m
|
||||
@@ -0,0 +1 @@
|
||||
[0;31m([0m[0;31m)[0m
|
||||
@@ -0,0 +1 @@
|
||||
[0;31m([0m[0;33m([0m[0;32m([0m[0;34m([0m[0;35m([0m[0;35m)[0m[0;34m)[0m[0;32m)[0m[0;33m)[0m[0;31m)[0m
|
||||
@@ -0,0 +1,4 @@
|
||||
[0;31m([0m[0m가[0m
|
||||
[0m나[0m
|
||||
[0m다[0m
|
||||
[0m라[0m[0;31m)[0m
|
||||
@@ -0,0 +1 @@
|
||||
[0;31m([0m[0m가[0m [0m나[0m [0m다[0m [0m라[0m[0;31m)[0m
|
||||
@@ -0,0 +1,41 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/bool/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF ( SimpleBoolean True )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/bool/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 4
|
||||
}
|
||||
)
|
||||
} :< SimpleF ( SimpleBoolean True )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/bool/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 10
|
||||
}
|
||||
)
|
||||
} :< SimpleF ( SimpleBoolean False )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/bool/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 13
|
||||
}
|
||||
)
|
||||
} :< SimpleF ( SimpleBoolean False )
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
#;(a datum comment can
|
||||
span multiple lines)
|
||||
|
||||
(but it ends here)
|
||||
@@ -0,0 +1,36 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/decimal/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleNumber 45.0 )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/decimal/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 4
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleNumber 5667.0 )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/decimal/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 10
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleNumber
|
||||
( -123.0 )
|
||||
)
|
||||
]
|
||||
@@ -0,0 +1,62 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-dot-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< CompoundF
|
||||
( DotListF
|
||||
(
|
||||
( MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-dot-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 2
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "가" )
|
||||
) :|
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-dot-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 5
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "나" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-dot-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 8
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "다" )
|
||||
]
|
||||
)
|
||||
( MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-dot-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 13
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "라" )
|
||||
)
|
||||
)
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
(가 나 다 . 라)
|
||||
@@ -0,0 +1,91 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< CompoundF
|
||||
( ListF Ordinary
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 2
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "가" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 5
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "나" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 8
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "다" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 11
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "라" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 14
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleNumber 1.0 )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 16
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleNumber 2.0 )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list-flat/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 18
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleNumber 3.0 )
|
||||
]
|
||||
)
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
(가 나 다 라 1 2 3)
|
||||
@@ -0,0 +1,148 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< CompoundF
|
||||
( DotListF
|
||||
(
|
||||
( MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 2
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "a" )
|
||||
) :|
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 4
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "b" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 6
|
||||
}
|
||||
)
|
||||
} :< CompoundF
|
||||
( ListF Ordinary
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 7
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "c" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 9
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "d" )
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
( MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 14
|
||||
}
|
||||
)
|
||||
} :< CompoundF
|
||||
( ListF Ordinary
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 15
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "가" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 18
|
||||
}
|
||||
)
|
||||
} :< CompoundF
|
||||
( DotListF
|
||||
(
|
||||
( MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 19
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "나" )
|
||||
) :| []
|
||||
)
|
||||
( MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 24
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "다" )
|
||||
)
|
||||
)
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/list/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 28
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "라" )
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
(a b (c d) . (가 (나 . 다) 라))
|
||||
@@ -0,0 +1,11 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/meta-expression/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< MetaF "aHaskellVariable + abc * 2"
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
#{aHaskellVariable + abc * 2}
|
||||
@@ -0,0 +1,2 @@
|
||||
##{case 123 of { 123 -> blah
|
||||
; xyz -> flah }}
|
||||
@@ -0,0 +1,11 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/meta-splice-expression/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< MetaSpliceF "takeWhile (\x -> even x) aHaskellList"
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
##{takeWhile (\x -> even x) aHaskellList}
|
||||
@@ -0,0 +1,11 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/meta-splice-variable/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< MetaSpliceF "aHaskellList"
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
##{aHaskellList}
|
||||
@@ -0,0 +1,11 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/meta-variable/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< MetaF "aHaskellVariable"
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
#{aHaskellVariable}
|
||||
@@ -0,0 +1,7 @@
|
||||
[ SynNone :< SimpleF
|
||||
( SimpleSymbol ".." )
|
||||
, SynNone :< SimpleF
|
||||
( SimpleSymbol ".abc" )
|
||||
, SynNone :< SimpleF
|
||||
( SimpleSymbol "....abcc" )
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
.. .abc ....abcc
|
||||
@@ -0,0 +1,23 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "+" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 3
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "-" )
|
||||
]
|
||||
@@ -0,0 +1,12 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/string/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleString "가나다라" )
|
||||
]
|
||||
@@ -0,0 +1,58 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier-token/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "abc" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier-token/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 4
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleString "xyz" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier-token/source.scm"
|
||||
, sourceLine = Pos 2
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "수학" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier-token/source.scm"
|
||||
, sourceLine = Pos 2
|
||||
, sourceColumn = Pos 5
|
||||
}
|
||||
)
|
||||
} :< CompoundF
|
||||
( ListF Ordinary
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier-token/source.scm"
|
||||
, sourceLine = Pos 2
|
||||
, sourceColumn = Pos 6
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "數學" )
|
||||
]
|
||||
)
|
||||
]
|
||||
@@ -0,0 +1,2 @@
|
||||
abc"xyz"
|
||||
수학(數學)
|
||||
@@ -0,0 +1,100 @@
|
||||
[ MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "abc" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 5
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "bala-hwa$" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 15
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "x!!!" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 20
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "z" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 22
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "z123" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 27
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "나는너무졸리다" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 1
|
||||
, sourceColumn = Pos 42
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "學" )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 3
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "車室." )
|
||||
, MkAnn
|
||||
{ syntax = SynNone
|
||||
, position = Just
|
||||
( SourcePos
|
||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||
, sourceLine = Pos 5
|
||||
, sourceColumn = Pos 1
|
||||
}
|
||||
)
|
||||
} :< SimpleF
|
||||
( SimpleSymbol "三個女人一臺戲。" )
|
||||
]
|
||||
@@ -0,0 +1,5 @@
|
||||
abc bala-hwa$ x!!! z z123 나는너무졸리다 學
|
||||
|
||||
車室.
|
||||
|
||||
三個女人一臺戲。
|
||||
@@ -1,9 +0,0 @@
|
||||
[ Fix
|
||||
( SimpleF ( Boolean True ) )
|
||||
, Fix
|
||||
( SimpleF ( Boolean True ) )
|
||||
, Fix
|
||||
( SimpleF ( Boolean False ) )
|
||||
, Fix
|
||||
( SimpleF ( Boolean False ) )
|
||||
]
|
||||
@@ -1,15 +0,0 @@
|
||||
[ Fix
|
||||
( SimpleF
|
||||
( Number 45.0 )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Number 5667.0 )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Number
|
||||
( -123.0 )
|
||||
)
|
||||
)
|
||||
]
|
||||
@@ -1,9 +0,0 @@
|
||||
[ Fix
|
||||
( SimpleF
|
||||
( Symbol "+" )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Symbol "-" )
|
||||
)
|
||||
]
|
||||
@@ -1,5 +0,0 @@
|
||||
[ Fix
|
||||
( SimpleF
|
||||
( String "가나다라" )
|
||||
)
|
||||
]
|
||||
@@ -1,29 +0,0 @@
|
||||
[ Fix
|
||||
( SimpleF
|
||||
( Symbol "abc" )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Symbol "balahwa$" )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Symbol "x!!!" )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Symbol "z" )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Symbol "z123" )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Symbol "나는너무졸리다" )
|
||||
)
|
||||
, Fix
|
||||
( SimpleF
|
||||
( Symbol "學" )
|
||||
)
|
||||
]
|
||||
@@ -1 +0,0 @@
|
||||
abc balahwa$ x!!! z z123 나는너무졸리다 學
|
||||
+39
-5
@@ -13,6 +13,11 @@ build-type: Simple
|
||||
-- extra-doc-files: CHANGELOG.md
|
||||
-- extra-source-files:
|
||||
|
||||
flag doctest
|
||||
description: enable the doctest suite
|
||||
default: True
|
||||
manual: True
|
||||
|
||||
common ghcstuffs-dev
|
||||
ghc-options:
|
||||
-Wno-unused-matches -Wno-missing-signatures -Wno-typed-holes
|
||||
@@ -53,28 +58,34 @@ library
|
||||
-- cabal-fmt: expand src
|
||||
exposed-modules:
|
||||
Gyehoek.CPS.Close
|
||||
Gyehoek.CPS.Contify
|
||||
Gyehoek.CPS.Convert
|
||||
Gyehoek.CPS.Eval
|
||||
Gyehoek.CPS.Lower
|
||||
Gyehoek.CPS.Hoist
|
||||
Gyehoek.CPS.Stackify
|
||||
Gyehoek.CPS.Syntax
|
||||
Gyehoek.Driver
|
||||
Gyehoek.GenSym
|
||||
Gyehoek.Jalmot
|
||||
Gyehoek.Language
|
||||
Gyehoek.Lift1
|
||||
Gyehoek.Options
|
||||
Gyehoek.Prelude
|
||||
Gyehoek.Scheme.Syntax
|
||||
Gyehoek.Sexp
|
||||
Gyehoek.Sexp.Grammar
|
||||
Gyehoek.Sexp.Grammar.Base
|
||||
Gyehoek.Sexp.Print
|
||||
Gyehoek.Sexp.QQ
|
||||
Gyehoek.Sexp.Read
|
||||
Gyehoek.Sexp.Syntax
|
||||
Gyehoek.Stack.Lower
|
||||
Gyehoek.Stack.Syntax
|
||||
Gyehoek.Stack.VM
|
||||
Gyehoek.Wasm
|
||||
|
||||
build-depends:
|
||||
, base ^>=4.21.2.0
|
||||
, base ^>=4.21.2.0
|
||||
, binary
|
||||
, bytestring
|
||||
, comonad
|
||||
@@ -91,16 +102,18 @@ library
|
||||
, hashable
|
||||
, invertible-grammar
|
||||
, lens
|
||||
, lucid
|
||||
, megaparsec
|
||||
, mtl
|
||||
, optparse-applicative
|
||||
, ordered-containers
|
||||
, pretty-simple
|
||||
, prettyprinter
|
||||
, prettyprinter-ansi-terminal
|
||||
, prettyprinter-lucid
|
||||
, process
|
||||
, recursion-schemes
|
||||
, scientific
|
||||
, sexp-grammar
|
||||
, string-interpolate
|
||||
, template-haskell
|
||||
, text
|
||||
@@ -108,6 +121,7 @@ library
|
||||
, typed-process
|
||||
, unordered-containers
|
||||
, vector
|
||||
, tardis
|
||||
|
||||
hs-source-dirs: src
|
||||
default-language: GHC2024
|
||||
@@ -126,8 +140,11 @@ test-suite test
|
||||
Gyehoek.Test.CPS.Syntax
|
||||
Gyehoek.Test.Golden
|
||||
Gyehoek.Test.Scheme.Syntax
|
||||
Gyehoek.Test.Sexp
|
||||
Gyehoek.Test.Sexp.Print
|
||||
Gyehoek.Test.Sexp.QQ
|
||||
Gyehoek.Test.Sexp.Read
|
||||
Gyehoek.Test.Stack.VM
|
||||
Gyehoek.TestUtil
|
||||
Root
|
||||
|
||||
build-depends:
|
||||
@@ -141,7 +158,6 @@ test-suite test
|
||||
, lens
|
||||
, pretty-simple
|
||||
, process-extras
|
||||
, sexp-grammar
|
||||
, tasty
|
||||
, tasty-expected-failure
|
||||
, tasty-hunit
|
||||
@@ -149,3 +165,21 @@ test-suite test
|
||||
, text
|
||||
|
||||
default-language: GHC2024
|
||||
|
||||
-- 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
|
||||
, gyehoek
|
||||
|
||||
default-extensions: CPP
|
||||
main-is: doctest.hs
|
||||
|
||||
if flag(doctest)
|
||||
build-depends: doctest-parallel >=0.1
|
||||
|
||||
else
|
||||
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
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
{-# LANGUAGE ApplicativeDo #-}
|
||||
module Gyehoek.CPS.Contify
|
||||
( contifyProgram
|
||||
) where
|
||||
|
||||
import Control.Monad.Tardis
|
||||
import Gyehoek.CPS.Syntax
|
||||
import Gyehoek.Prelude
|
||||
import qualified Data.HashSet as HS
|
||||
import Control.Lens.Unsound (adjoin)
|
||||
import Debug.Pretty.Simple
|
||||
import qualified Data.HashMap.Strict as H
|
||||
import Control.Monad.Writer.Lazy
|
||||
import Control.Monad.Trans.Tardis (liftTardisT)
|
||||
|
||||
|
||||
-- | ain't no way...
|
||||
-- type T = WriterT (HashSet Name) (Tardis (HashSet Name) (HashSet Name))
|
||||
type T = TardisT (HashSet Name) (HashSet Name) (Writer (HashSet Name))
|
||||
|
||||
evalT :: T a -> a
|
||||
-- evalT = (`evalTardis` (mempty,mempty)) . fmap fst . runWriterT
|
||||
evalT = fst . runWriter . (`evalTardisT` (mempty,mempty))
|
||||
|
||||
runT :: T a -> (a, HashSet Name)
|
||||
-- runT = (`evalTardis` (mempty,mempty)) . runWriterT
|
||||
runT = runWriter . (`evalTardisT` (mempty,mempty))
|
||||
|
||||
-- | inline function if it hasn't been used in the past, and won't
|
||||
-- be used in the future.
|
||||
tryInline :: Name -> Kappa -> T Kexp
|
||||
tryInline kname kap = do
|
||||
modifyBackwards (HS.insert kname)
|
||||
p <- getsPast (HS.member kname)
|
||||
modifyForwards (HS.insert kname)
|
||||
q <- getsFuture (HS.member kname)
|
||||
let c = p || q
|
||||
liftTardisT . tell $ if c then HS.singleton kname else mempty
|
||||
pure $ if c
|
||||
then KexpVar kname
|
||||
else KexpKappa kap
|
||||
|
||||
getKap :: HashMap Name Abs -> Name -> Maybe Kappa
|
||||
getKap g kname = g ^? ix kname . #AbsKappa
|
||||
|
||||
contify :: HashMap Name Abs -> Exp -> T Exp
|
||||
contify g = transformM \case
|
||||
ExpApply f xs (KexpVar kname) | Just kap <- getKap g kname
|
||||
-> ExpApply f xs <$> tryInline kname kap
|
||||
ExpPrim p (KexpVar kname) | Just kap <- getKap g kname
|
||||
-> ExpPrim p <$> tryInline kname kap
|
||||
e -> pure e
|
||||
|
||||
contifyProgram :: HoistedProgram -> Eff es HoistedProgram
|
||||
contifyProgram p = do
|
||||
let g = p.bindings
|
||||
let (p',contifiedVars) =
|
||||
runT $
|
||||
traverseOf
|
||||
(adjoin
|
||||
(#bindings . each . body)
|
||||
(#body . body))
|
||||
(contify g)
|
||||
p
|
||||
pTraceShowM contifiedVars
|
||||
-- pure $ p' & #bindings %~ H.filterWithKey \k _ -> HS.member k contifiedVars
|
||||
pure p'
|
||||
+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)
|
||||
|
||||
+240
-60
@@ -1,79 +1,259 @@
|
||||
{-# 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 Gyehoek.CPS.Syntax hiding (Hob(..), Obj(..), cont)
|
||||
import Gyehoek.Sexp qualified as S
|
||||
import Control.Lens hiding (assign)
|
||||
import Data.Maybe (fromMaybe)
|
||||
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_)
|
||||
|
||||
|
||||
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
|
||||
|
||||
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 Loc Loc Mutability
|
||||
| EVec (List Loc) Mutability
|
||||
| EString (List Loc) Mutability
|
||||
| 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>"
|
||||
ENull -> S.List []
|
||||
EPair car cdr _mut -> S.DotList [gofetch car] (gofetch cdr)
|
||||
EVec xs _mut -> S.Vector . fmap gofetch $ xs
|
||||
EString xs _mut -> S.String _
|
||||
|
||||
data DynPoints = MkDynPoints
|
||||
deriving (Generic, Data)
|
||||
|
||||
|
||||
|
||||
evalVal :: Env -> Val -> M Answer E
|
||||
|
||||
evalVal g (ValVar x) = var g x >>= fetch
|
||||
|
||||
evalVal g (ValImm imm) = pure case imm of
|
||||
ImmLabel l -> error [i|#{l}|]
|
||||
ImmInt n -> EInt n
|
||||
ImmBool b -> EBool b
|
||||
ImmUndefined -> 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 p k) = do
|
||||
p' <- evalPrim g =<< traverse (evalVal g) p
|
||||
evalKexp g k >>= \case
|
||||
EProcedure fp -> fp p' dps
|
||||
_ -> wrong [i|prim(#{p})의 계속을 나쁘다|]
|
||||
|
||||
eval g dps e = error [i|unimplemented #{e}|]
|
||||
|
||||
evalPrim :: Env -> Prim E -> M Answer (List E)
|
||||
evalPrim g = \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
|
||||
PrimValues xs -> pure xs
|
||||
p -> wrong [i|prim(#{p})은 벌써 나지 않다|]
|
||||
where
|
||||
arith2 f (EInt x) (EInt y) = pure [EInt $ f x y]
|
||||
arith2 f x y = wrong [i|나쁜 인자: #{x}, #{y}|]
|
||||
|
||||
|
||||
|
||||
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,342 +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 Language.Sexp.Located qualified as SL
|
||||
import Control.Monad.Fix
|
||||
import qualified Gyehoek.Sexp
|
||||
import Data.Text qualified as T
|
||||
import Data.Foldable (fold)
|
||||
import Gyehoek.Sexp (encodeOrShow)
|
||||
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 -> SL.Sexp
|
||||
getArgRegister n = SL.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) =
|
||||
([expr|(@gyehoek :origin #{origin})|]<>)
|
||||
<$> case p of
|
||||
PrimAdd x y -> lowerBinOp "i32.add" g x y k
|
||||
PrimMul x y -> lowerBinOp "i32.mul" g x y k
|
||||
where origin = encodeOrShow @_ @Text e
|
||||
|
||||
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
|
||||
let origin = encodeOrShow @_ @Text e
|
||||
pure [expr|
|
||||
(@gyehoek :origin #{origin})
|
||||
(@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
|
||||
let origin = encodeOrShow @_ @Text e
|
||||
pure [expr|
|
||||
(@gyehoek :origin #{origin})
|
||||
(@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
|
||||
let origin = encodeOrShow @_ @Text e
|
||||
pure [expr|
|
||||
(@gyehoek :origin #{origin})
|
||||
(@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 $ case Gyehoek.Sexp.encode e of
|
||||
Left _ -> show e
|
||||
Right x -> T.unpack x
|
||||
|
||||
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
|
||||
let origin = encodeOrShow @_ @Text e
|
||||
idx <- Wasm.defineFunction [wat|
|
||||
(func (param i32)
|
||||
(@gyehoek :origin #{origin})
|
||||
(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
|
||||
let origin = encodeOrShow @_ @Text e
|
||||
idx <- Wasm.defineFunction [wat|
|
||||
(func (param i32)
|
||||
(@gyehoek :origin #{origin})
|
||||
(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' = SL.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
|
||||
let origin = encodeOrShow @_ @Text e
|
||||
Wasm.defineFunction [wat|
|
||||
(func $scm-entry (param i32)
|
||||
(@gyehoek :origin #{origin})
|
||||
(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
|
||||
+126
-105
@@ -1,7 +1,6 @@
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
module Gyehoek.CPS.Stackify
|
||||
( stackifyExp
|
||||
, stackifyProgram
|
||||
( stackifyProgram
|
||||
, module Gyehoek.CPS.Syntax
|
||||
) where
|
||||
|
||||
@@ -13,133 +12,155 @@ import Gyehoek.GenSym
|
||||
import Effectful.Writer.Static.Shared
|
||||
import Data.Foldable
|
||||
import qualified Data.HashMap.Strict as H
|
||||
import Data.List (elemIndex)
|
||||
import Data.List (elemIndex, nub, intersect)
|
||||
import Data.Text qualified as T
|
||||
import Gyehoek.Prelude
|
||||
import Debug.Pretty.Simple
|
||||
import qualified Gyehoek.Sexp as S
|
||||
import Data.Monoid
|
||||
|
||||
|
||||
type Stackify = Writer Stk.Program
|
||||
data BlockBuilder
|
||||
= Code (List Stk.Instr) BlockBuilder
|
||||
| Tail Stk.Tail
|
||||
deriving (Show, Generic)
|
||||
|
||||
runStackify :: Eff (Stackify : es) a -> Eff es (a, Stk.Program)
|
||||
runStackify = runWriter
|
||||
buildBlock :: BlockBuilder -> Stk.Block
|
||||
buildBlock = go [] where
|
||||
go acc (Code xs bb) = go (acc ++ xs) bb
|
||||
go acc (Tail t) = Stk.MkBlock acc t
|
||||
|
||||
live :: Free a => Env -> a -> List Name
|
||||
live g e = free' e & filter \x ->
|
||||
x `H.member` g.bound
|
||||
&& not (x `elem` g.contStack)
|
||||
-- affine
|
||||
_ValName :: Traversal' Val Name
|
||||
_ValName = failing #_ValVar (#_ValImm . #_ImmLabel . #_MkLabel)
|
||||
|
||||
stackify
|
||||
:: (GenSym :> es, Stackify :> es)
|
||||
=> Env -> Exp -> Eff es (Seq Stk.Instr)
|
||||
:: forall es. (GenSym :> 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
|
||||
tell [Stk.MkBlock f xs $
|
||||
[Stk.Pop x | x <- ls] <> toList m']
|
||||
let g' = g & #bound . at f ?~ Stk.ValLabel f
|
||||
& #liveness . at f ?~ ls
|
||||
stackify g' e
|
||||
stackify _ (ExpContinue (ValVar k) xs) =
|
||||
Code [ ] _
|
||||
|
||||
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
|
||||
tell [Stk.MkBlock f xs . toList $ m']
|
||||
stackify g e
|
||||
|
||||
stackify g (ExpIf c t f) = do
|
||||
t' <- stackify g t
|
||||
f' <- stackify g f
|
||||
pure [ Stk.If (stackifyVal g c) (toList t') (toList f') ]
|
||||
|
||||
stackify g (ExpApply f xs ktail) = do
|
||||
pure $
|
||||
[ Stk.PushCont k ]
|
||||
<> fromList [ Stk.Push (Stk.ValReg l) | l <- ls ]
|
||||
<> [ Stk.Call (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 [ Stk.Call (Stk.ValLabel k) xs' ]
|
||||
Just j -> do
|
||||
ktail <- gensym' $ k ^. _Wrapped'
|
||||
pure $
|
||||
Seq.replicate j (Stk.PopCont "_")
|
||||
<> [ Stk.PopCont ktail
|
||||
, Stk.Call (Stk.ValReg ktail) (stackifyVal g <$> 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 $ [ Stk.Prim x (stackifyVal g <$> p) ] <> e'
|
||||
stackify _ (ExpPrim p k) = _
|
||||
|
||||
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}|]
|
||||
stackifyAbs :: (GenSym :> es) => Env -> Label -> Abs -> Eff es Stk.Routine
|
||||
|
||||
var :: Env -> Name -> Stk.Val
|
||||
var g v = case g ^. #bound . at v of
|
||||
Just x -> x
|
||||
Nothing -> Stk.ValLabel v
|
||||
stackifyAbs g lbl (MkAbs xs mtail e) =
|
||||
Stk.MkRoutine lbl . buildBlock . preamble <$> stackify g e
|
||||
where
|
||||
preamble = Code (popArgs $ (mtail ^.. _Just) ++ xs)
|
||||
|
||||
bindReg :: Name -> (Name, Stk.Val)
|
||||
bindReg x = (x, Stk.ValReg x)
|
||||
popArgs :: List Name -> List Stk.Instr
|
||||
popArgs = fmap (Stk.Pop . MkReg) . reverse
|
||||
|
||||
pushArgs :: List Name -> List Stk.Instr
|
||||
pushArgs = _
|
||||
|
||||
|
||||
|
||||
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"]
|
||||
emptyEnv = MkEnv
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
stackifyExp :: GenSym :> es => Name -> Exp -> Eff es Stk.Program
|
||||
stackifyExp lbl e = do
|
||||
(code,p) <- runStackify $ stackify emptyEnv e
|
||||
pure $ p <> Stk.MkProgram [ Stk.MkBlock lbl [] (toList code) ]
|
||||
stackifyProgram
|
||||
:: forall es. GenSym :> es
|
||||
=> HoistedProgram -> Eff es Stk.Program
|
||||
stackifyProgram p = p
|
||||
& ifoldMapOf
|
||||
((#bindings . itraversed)
|
||||
<> (#body . to (H.singleton "start" . AbsLambda) . itraversed))
|
||||
(\l -> Ap . stackifyBinding l)
|
||||
& getAp
|
||||
where
|
||||
g = emptyEnv
|
||||
stackifyBinding lbl ab =
|
||||
Stk.MkProgram . H.singleton lbl <$> stackifyAbs @es g lbl ab
|
||||
|
||||
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))
|
||||
p :: HoistedProgram
|
||||
p = [cps|
|
||||
(letrec (($r12-code32
|
||||
(κ (x13)
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (r12 start-ktail0)
|
||||
(continue start-ktail0 x13)))))
|
||||
($prim-k7-code22
|
||||
(κ (r6)
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (prim-k7 lambda-tail1 n fac)
|
||||
(prim
|
||||
(make-shared-closure ($r8-code19) (lambda-tail1 n))
|
||||
(κ (r8)
|
||||
(fac r6 r8)))))))
|
||||
($prim-k11-code16
|
||||
(κ (r10)
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (prim-k11 lambda-tail1)
|
||||
(continue lambda-tail1 r10)))))
|
||||
($falsey-cont5-code26
|
||||
(κ ()
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (truthy-cont4 falsey-cont5 lambda-tail1 n fac)
|
||||
(prim
|
||||
(make-shared-closure ($prim-k7-code22) (lambda-tail1 n fac))
|
||||
(κ (prim-k7)
|
||||
(prim (- n 1) prim-k7)))))))
|
||||
($r8-code19
|
||||
(κ (x9)
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (r8 lambda-tail1 n)
|
||||
(prim
|
||||
(make-shared-closure ($prim-k11-code16) (lambda-tail1))
|
||||
(κ (prim-k11)
|
||||
(prim (* n x9) prim-k11)))))))
|
||||
($truthy-cont4-code25
|
||||
(κ ()
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (truthy-cont4 falsey-cont5 lambda-tail1 n fac)
|
||||
(continue lambda-tail1 1)))))
|
||||
($fac-code35
|
||||
(λ (n lambda-tail1)
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (fac)
|
||||
(prim
|
||||
(make-shared-closure ($prim-k3-code29) (lambda-tail1 n fac))
|
||||
(κ (prim-k3)
|
||||
(prim (zero? n) prim-k3)))))))
|
||||
($prim-k3-code29
|
||||
(κ (r2)
|
||||
(prim
|
||||
(get-env)
|
||||
(κ (prim-k3 lambda-tail1 n fac)
|
||||
(prim
|
||||
(make-shared-closure
|
||||
($truthy-cont4-code25 $falsey-cont5-code26)
|
||||
(lambda-tail1 n fac))
|
||||
(κ (truthy-cont4 falsey-cont5)
|
||||
(if r2
|
||||
truthy-cont4
|
||||
falsey-cont5))))))))
|
||||
(λ (start-ktail0)
|
||||
(prim
|
||||
(make-shared-closure ($fac-code35) ())
|
||||
(κ (fac)
|
||||
(prim
|
||||
(make-shared-closure ($r12-code32) (start-ktail0))
|
||||
(κ (r12)
|
||||
(fac 20 r12)))))))
|
||||
|]
|
||||
|
||||
+272
-167
@@ -10,15 +10,18 @@ module Gyehoek.CPS.Syntax
|
||||
, Kappa(..)
|
||||
, Lambda(..)
|
||||
, Exp(..)
|
||||
, Kexp(..)
|
||||
, ExpF(..)
|
||||
, Name(..)
|
||||
, Prim(..)
|
||||
, Program(..)
|
||||
, HoistedProgram(..)
|
||||
, Lit(..)
|
||||
, Imm(..)
|
||||
, Obj(..)
|
||||
, Hob(..)
|
||||
, pattern Void
|
||||
, Label(..)
|
||||
, Reg(..)
|
||||
, pattern Halt
|
||||
, pattern Halt1
|
||||
, _MkKappa
|
||||
@@ -37,27 +40,34 @@ 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
|
||||
|
||||
import Language.SexpGrammar qualified as S
|
||||
import Gyehoek.Sexp qualified
|
||||
import Gyehoek.Scheme.Syntax (Name (..), Prim(..), primSexpIso, Lit(..), pattern Void)
|
||||
import Language.SexpGrammar.Generic
|
||||
import Gyehoek.Scheme.Syntax (Name (..), Prim(..), primDatumIso, Lit(..))
|
||||
import Gyehoek.Sexp qualified as S
|
||||
import Control.Category
|
||||
import Prelude hiding ((.), id)
|
||||
import Language.Haskell.TH.Quote (QuasiQuoter)
|
||||
import Language.Sexp.Located (Sexp)
|
||||
import qualified Data.InvertibleGrammar.Base as IG
|
||||
import Data.InvertibleGrammar.Base (type (:-)((:-)))
|
||||
import qualified Data.HashSet as HS
|
||||
import Data.Monoid (Endo)
|
||||
import Data.Functor.Foldable.TH
|
||||
import qualified Gyehoek.Sexp as GS
|
||||
import qualified Language.Sexp.Located as SL
|
||||
import Data.Data.Lens (uniplate)
|
||||
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
|
||||
|
||||
@@ -66,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)
|
||||
|
||||
@@ -82,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)
|
||||
|
||||
@@ -99,46 +125,92 @@ 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
|
||||
-- makeLenses ''Kappa
|
||||
makePrisms ''Exp
|
||||
-- makeLenses ''Exp
|
||||
-- makeFieldsNoPrefix ''Exp
|
||||
-- makeFieldsNoPrefix ''Kappa
|
||||
-- makeLensesWith abbreviatedFields ''Exp
|
||||
-- makeLensesFor [("binders", "_binders"), ("body", "_body")] ''Exp
|
||||
makeFieldsId ''Exp
|
||||
makeFieldsId ''Kappa
|
||||
makeFieldsId ''Lambda
|
||||
@@ -158,147 +230,187 @@ _AbsLambda' = prism'
|
||||
(\case AbsLambda' bs ktail e -> Just (bs,ktail,e)
|
||||
_ -> Nothing)
|
||||
|
||||
instance Plated Exp where
|
||||
plate = uniplate
|
||||
-- plate k = \case
|
||||
-- ExpPrim p kap -> ExpPrim p <$> body k kap
|
||||
-- ExpLetRec bs e -> ExpLetRec <$> (each . _2 . body) k bs <*> k e
|
||||
-- ExpContinue c xs -> pure $ ExpContinue c xs
|
||||
-- ExpIf c t f -> ExpIf c <$> k t <*> k f
|
||||
-- ExpApply f xs ktail -> pure $ ExpApply f xs ktail
|
||||
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)))
|
||||
|
||||
|
||||
-- SexpIso instances
|
||||
-- DatumIso instances
|
||||
|
||||
instance S.SexpIso Val where
|
||||
sexpIso = match
|
||||
$ With (\imm -> imm . S.sexpIso)
|
||||
$ With (\var -> var . S.sexpIso)
|
||||
$ End
|
||||
instance S.DatumIso Val where
|
||||
datumIso = S.match
|
||||
$ S.With (\imm -> imm . S.datumIso)
|
||||
$ S.With (\var -> var . S.datumIso)
|
||||
$ S.End
|
||||
|
||||
instance S.SexpIso Obj where
|
||||
sexpIso = match
|
||||
$ With (\imm -> imm . S.sexpIso)
|
||||
$ With (\hob -> hob . S.sexpIso)
|
||||
$ End
|
||||
instance S.DatumIso Obj where
|
||||
datumIso = S.match
|
||||
$ S.With (\imm -> imm . S.datumIso)
|
||||
$ S.With (\hob -> hob . S.datumIso)
|
||||
$ S.End
|
||||
|
||||
instance S.SexpIso Imm where
|
||||
sexpIso = match
|
||||
$ With (. S.int)
|
||||
$ With (. GS.schemeBool)
|
||||
$ With (. labelName)
|
||||
$ End
|
||||
instance S.DatumIso Imm where
|
||||
datumIso = S.match
|
||||
$ S.With (. S.int)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (. S.unreadable (const "#<undefined>"))
|
||||
$ S.End
|
||||
|
||||
labelName :: S.SexpGrammar Name
|
||||
labelName = S.coproduct
|
||||
[ S.sexpIso @Name >>> Gyehoek.Sexp.prismIso
|
||||
(S.expected "label")
|
||||
(prefixed @Name "$")
|
||||
, S.list $ S.el (S.sym "$") >>> S.el (S.sexpIso @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.SexpIso Hob where
|
||||
sexpIso = match
|
||||
$ With (. closure)
|
||||
$ End
|
||||
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 :: S.Grammar S.Position (Sexp :- t) (List Obj :- Name :- t)
|
||||
closure :: G (Datum :- t) (List Obj :- Label :- t)
|
||||
closure = IG.Flip $ IG.PartialIso
|
||||
(\(env:-code:-t) -> SL.Modified SL.Hash [GS.sx|(#{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.SexpIso Lambda where
|
||||
sexpIso = match
|
||||
$ With (. lambda)
|
||||
$ End
|
||||
instance S.DatumIso Lambda where
|
||||
datumIso = S.with (lam >>>)
|
||||
where
|
||||
lambda = S.list $
|
||||
S.el Gyehoek.Sexp.lambdaKeyword
|
||||
>>> S.el binders
|
||||
>>> S.el S.sexpIso
|
||||
binders :: forall t.
|
||||
IG.Grammar S.Position (Sexp :- t) (Name :- List Name :- t)
|
||||
binders = S.list $
|
||||
S.rest (S.sexpIso @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")
|
||||
)
|
||||
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.flipped S.snoced
|
||||
>>> S.swap
|
||||
|
||||
instance S.SexpIso Kappa where
|
||||
sexpIso = match
|
||||
$ With (. kappa)
|
||||
$ End
|
||||
where
|
||||
kappa = S.list $
|
||||
S.el Gyehoek.Sexp.kappaKeyword
|
||||
>>> S.el (S.list $ S.rest S.sexpIso)
|
||||
>>> S.el S.sexpIso
|
||||
instance S.DatumIso Kappa where
|
||||
datumIso = S.with \g ->
|
||||
S.lambdaLike S.kappaKeyword
|
||||
(S.datumIso @(List Name))
|
||||
(S.el $ S.datumIso @Exp)
|
||||
>>> g
|
||||
|
||||
instance S.SexpIso Abs where
|
||||
sexpIso = match
|
||||
$ With (\lambda -> lambda . S.sexpIso)
|
||||
$ With (\kappa -> kappa . S.sexpIso)
|
||||
$ End
|
||||
instance S.DatumIso Abs where
|
||||
datumIso = S.match
|
||||
$ S.With (\lambda -> lambda . S.datumIso)
|
||||
$ S.With (\kappa -> kappa . S.datumIso)
|
||||
$ S.End
|
||||
|
||||
instance S.SexpIso Exp where
|
||||
sexpIso = match
|
||||
$ With (. prim)
|
||||
$ With (. letrec)
|
||||
$ With (. continue)
|
||||
$ With (. if_)
|
||||
$ With (. app)
|
||||
$ End
|
||||
instance S.DatumIso Exp where
|
||||
datumIso = S.match
|
||||
$ S.With (. prim)
|
||||
$ S.With (. letrec)
|
||||
$ S.With (. continue)
|
||||
$ S.With (. if_)
|
||||
$ S.With (. app)
|
||||
$ S.End
|
||||
where
|
||||
continue = S.list $
|
||||
S.el (S.sym "continue")
|
||||
>>> S.el S.sexpIso
|
||||
>>> S.rest S.sexpIso
|
||||
letrec = Gyehoek.Sexp.let_ "letrec" S.sexpIso S.sexpIso S.sexpIso
|
||||
if_ = S.list $ S.el (S.sym "if")
|
||||
>>> S.el S.sexpIso >>> S.el S.sexpIso >>> S.el S.sexpIso
|
||||
S.el (S.decorate S.SynBuiltin >>> S.sym "continue")
|
||||
>>> S.el (S.decorate S.SynProcedure >>> S.datumIso)
|
||||
>>> S.rest S.datumIso
|
||||
letrec = S.letLike "letrec" S.datumIso S.datumIso S.datumIso
|
||||
if_ = S.ifLike "if"
|
||||
S.datumIso S.datumIso S.datumIso
|
||||
app :: forall t.
|
||||
IG.Grammar S.Position (Sexp :- t) (Name :- ([Val] :- (Val :- t)))
|
||||
app = S.list $ S.el (S.sexpIso @Val)
|
||||
-- >>> S.flipped Gyehoek.Sexp.nonEmptyGrammar
|
||||
>>> S.rest (S.sexpIso @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"
|
||||
))
|
||||
where
|
||||
_ = S.flipped $ Gyehoek.Sexp.nonEmptyGrammar @S.Position @Val
|
||||
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.swap
|
||||
prim = S.list $
|
||||
S.el (S.sym "prim")
|
||||
>>> S.el (primSexpIso id (S.sexpIso @Val))
|
||||
>>> S.el S.sexpIso
|
||||
S.el (S.decorate S.SynBuiltin >>> S.sym "prim")
|
||||
>>> S.el (primDatumIso id (S.datumIso @Val))
|
||||
>>> S.el S.datumIso
|
||||
|
||||
instance S.SexpIso Program where
|
||||
sexpIso = with \prog -> S.sexpIso @Exp >>> prog
|
||||
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 @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 :: Sexp -> a
|
||||
toCPS :: HasCallStack => Datum -> a
|
||||
|
||||
instance CPS Exp where toCPS = Gyehoek.Sexp.fromSexp
|
||||
instance CPS Val where toCPS = Gyehoek.Sexp.fromSexp
|
||||
instance CPS Kappa where toCPS = Gyehoek.Sexp.fromSexp
|
||||
instance CPS Lambda where toCPS = Gyehoek.Sexp.fromSexp
|
||||
instance CPS Abs where toCPS = Gyehoek.Sexp.fromSexp
|
||||
instance CPS Program where toCPS = Gyehoek.Sexp.fromSexp
|
||||
instance CPS Exp where toCPS = S.fromDatumUnsafe S.datumIso
|
||||
instance CPS Val where toCPS = S.fromDatumUnsafe S.datumIso
|
||||
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 :: QuasiQuoter
|
||||
cps = Gyehoek.Sexp.makeSx' [| toCPS |]
|
||||
cps :: S.QuasiQuoter
|
||||
cps = S.makeSx' [| toCPS |]
|
||||
|
||||
|
||||
|
||||
@@ -318,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
|
||||
|
||||
@@ -328,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) =
|
||||
@@ -352,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
|
||||
|
||||
+57
-28
@@ -1,5 +1,5 @@
|
||||
module Gyehoek.Driver
|
||||
(main, lower_e2e, convert_e2e, parse_e2e, readScm, eval_e2e)
|
||||
(main, lower_e2e, convert_e2e, parse_e2e, readScm, eval_e2e, eval_cps_e2e, eval_cps2_e2e)
|
||||
where
|
||||
|
||||
import Gyehoek.Options
|
||||
@@ -17,7 +17,7 @@ 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.Stack.Lower
|
||||
import Gyehoek.CPS.Eval qualified as CPS
|
||||
import Control.Monad
|
||||
import Text.Pretty.Simple (pShowNoColor)
|
||||
@@ -26,19 +26,23 @@ 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 Gyehoek.Stack.VM (eval, writeObj, Obj, traceEval)
|
||||
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)
|
||||
import Gyehoek.CPS.Contify (contifyProgram)
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
opts <- execParser $ info (helper <*> parser) fullDesc
|
||||
runEff . runFileSystem . runGenSym . driver $ opts
|
||||
runJalmotIO . runFileSystem . runGenSym . driver $ opts
|
||||
|
||||
|
||||
|
||||
@@ -65,11 +69,12 @@ fileName :: FilePath -> FilePath
|
||||
fileName "-" = "<interactive>"
|
||||
fileName e = e
|
||||
|
||||
readScm :: FileSystem :> es => FilePath -> Eff es Scm.Program
|
||||
readScm
|
||||
:: forall es. (Jalmot :> es, FileSystem :> es)
|
||||
=> FilePath -> Eff es Scm.Program
|
||||
readScm f =
|
||||
withFile f FS.ReadMode $ \h ->
|
||||
Sexp.parseSexps @Scm.CommandOrDef (fileName f) <$> hGetContents h
|
||||
>>= either error (pure . Scm.MkProgram)
|
||||
S.decodeDataWith @es S.dataIso =<< hGetContents h
|
||||
|
||||
inspectWasm :: IOE :> es => Text -> Eff es ()
|
||||
inspectWasm wat = do
|
||||
@@ -107,7 +112,7 @@ dumpOrRun dump run acquire do_dump do_run =
|
||||
when run (do_run x)
|
||||
|
||||
driver
|
||||
:: (GenSym :> es, FileSystem :> es, IOE :> es)
|
||||
:: (GenSym :> es, FileSystem :> es, Jalmot :> es, IOE :> es)
|
||||
=> Options -> Eff es ()
|
||||
driver opts = do
|
||||
scm <- readScm opts.sourceFile
|
||||
@@ -115,41 +120,65 @@ driver opts = do
|
||||
hPutStrLn FS.stdout . view strict . pShowNoColor $ scm
|
||||
cps <- convertProgram scm
|
||||
when opts.dumpCPS do
|
||||
hPutStrLn FS.stdout $ Sexp.encodePretty cps ^?! _Right
|
||||
S.writeDatum cps
|
||||
closedCps <- closeProgram cps
|
||||
when opts.dumpClosed do
|
||||
hPutStrLn FS.stdout $ Sexp.encodePretty closedCps ^?! _Right
|
||||
S.writeDatum closedCps
|
||||
hoistedCps <- hoistProgram closedCps
|
||||
when opts.dumpHoisted do
|
||||
S.writeDatum hoistedCps
|
||||
-- contifiedCps <- contifyProgram hoistedCps
|
||||
-- when opts.dumpContified do
|
||||
-- hPutStrLn FS.stdout =<< S.encodeWith S.datumIso contifiedCps
|
||||
let rt_is p = is (_Just . p) opts.runtime
|
||||
dumpOrRun opts.dumpStackified (rt_is #Stackify)
|
||||
(stackifyProgram closedCps)
|
||||
(hPutStrLn FS.stdout . Stk.encodeProgram)
|
||||
(eval >>> fmap writeObj
|
||||
>>> T.unwords
|
||||
(stackifyProgram hoistedCps)
|
||||
(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
|
||||
-- dumpOrRun opts.inspectWasm (rt_is #Wasm)
|
||||
-- (lowerProgram cps)
|
||||
-- inspectWasm
|
||||
-- (\wat -> withFile opts.output FS.WriteMode \h -> hPutStrLn h wat)
|
||||
when opts.traceStackified do
|
||||
stackifyProgram hoistedCps >>= traceEval
|
||||
|
||||
parse_e2e :: FilePath -> IO Scm.Program
|
||||
parse_e2e = runEff . runFileSystem . readScm
|
||||
parse_e2e = runJalmotIO . runFileSystem . readScm
|
||||
|
||||
convert_e2e :: FilePath -> IO CPS.Program
|
||||
convert_e2e = runEff . runFileSystem . runGenSym
|
||||
convert_e2e = runJalmotIO . runFileSystem . runGenSym
|
||||
. (closeProgram <=< convertProgram <=< readScm)
|
||||
|
||||
lower_e2e :: FilePath -> IO Text
|
||||
lower_e2e =
|
||||
runEff . runFileSystem . runGenSym
|
||||
runJalmotIO . runFileSystem . runGenSym
|
||||
. (lowerProgram <=< closeProgram <=< convertProgram <=< readScm)
|
||||
|
||||
eval_e2e :: FilePath -> IO (List Obj)
|
||||
eval_e2e fp = runEff . runFileSystem . runGenSym $ do
|
||||
eval_e2e fp = runJalmotIO . runFileSystem . runGenSym $ do
|
||||
stk <- stackifyProgram <=< closeProgram <=< convertProgram <=< readScm $ fp
|
||||
pure . eval $ stk
|
||||
eval stk
|
||||
|
||||
eval_cps_e2e :: FilePath -> IO Text
|
||||
eval_cps_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
||||
readScm fp
|
||||
>>= convertProgram
|
||||
>>= closeProgram
|
||||
>>= CPS.evalProgram
|
||||
>>= pure . S.encodeOrShowData' S.dataIso
|
||||
|
||||
eval_cps2_e2e :: FilePath -> IO Text
|
||||
eval_cps2_e2e fp = runJalmotIO . runFileSystem . runGenSym $
|
||||
readScm fp
|
||||
>>= convertProgram
|
||||
-- >>= closeProgram
|
||||
>>= CPS.evalProgram
|
||||
>>= pure . S.encodeOrShowData' S.dataIso
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
module Gyehoek.Jalmot
|
||||
( Jalmot
|
||||
, Exception(..)
|
||||
, AJalmot(..)
|
||||
, AJalmotCS(..)
|
||||
, module Effectful.Error.Static
|
||||
, runJalmot
|
||||
, runJalmotIO
|
||||
, runJalmotIOE
|
||||
, runJalmotUnsafe
|
||||
, runJalmotCS
|
||||
)
|
||||
where
|
||||
|
||||
import Gyehoek.Prelude
|
||||
import Text.Megaparsec.Error (ParseErrorBundle, errorBundlePretty)
|
||||
import Data.Void (Void)
|
||||
import Effectful.Exception
|
||||
import Effectful.Error.Static
|
||||
import qualified Data.InvertibleGrammar as Grammar
|
||||
import Gyehoek.Sexp.Syntax (Ann)
|
||||
import Prettyprinter (defaultLayoutOptions, layoutPretty, pretty)
|
||||
import Prettyprinter.Render.String (renderString)
|
||||
import Control.Exception.Base (throw)
|
||||
|
||||
|
||||
deriving instance Show p => Show (Grammar.ErrorMessage p)
|
||||
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
|
||||
deriving (Show)
|
||||
|
||||
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
|
||||
Right a -> pure a
|
||||
Left (cs,jm) -> throwIO $ MkAJalmotCS cs jm
|
||||
|
||||
runJalmotIO :: Eff '[Jalmot, IOE] a -> IO a
|
||||
runJalmotIO = runEff . runJalmotIOE
|
||||
|
||||
runJalmotUnsafe :: Eff '[Jalmot] a -> a
|
||||
runJalmotUnsafe m = case runPureEff . runJalmot $ m of
|
||||
Left (cs,e) -> throw $ MkAJalmotCS cs e
|
||||
Right x -> x
|
||||
|
||||
instance Exception AJalmot where
|
||||
displayException = \case
|
||||
ReaderError eb -> errorBundlePretty eb
|
||||
GrammarError err ->
|
||||
pretty err
|
||||
& layoutPretty defaultLayoutOptions
|
||||
& renderString
|
||||
VMError err -> [i|#{err}|]
|
||||
EvalError err -> [i|#{err}|]
|
||||
|
||||
instance Exception AJalmotCS where
|
||||
backtraceDesired = const False
|
||||
displayException (MkAJalmotCS cs jm) =
|
||||
"\n" <> displayException jm <> "\n\n" <> prettyCallStack cs <> "\n"
|
||||
+1
-22
@@ -1,25 +1,4 @@
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
{-# LANGUAGE AllowAmbiguousTypes #-}
|
||||
module Gyehoek.Language
|
||||
( Language(..)
|
||||
(
|
||||
) where
|
||||
|
||||
import Data.Kind (Type)
|
||||
import Gyehoek.Prelude
|
||||
import Language.SexpGrammar (Position, Grammar, (:-), Sexp)
|
||||
|
||||
|
||||
class Language l where
|
||||
type Program l :: Type
|
||||
languageName :: Text
|
||||
programGrammar :: forall t. Grammar Position (List Sexp :- t) (Program l :- t)
|
||||
|
||||
readProgramFile
|
||||
:: forall l es. Language l
|
||||
=> FilePath -> Eff es (Program l)
|
||||
readProgramFile fp = _
|
||||
|
||||
readProgramStringPos
|
||||
:: forall l. Language l
|
||||
=> Position -> Text -> Either Text (Program l)
|
||||
readProgramStringPos pos s = _
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
module Gyehoek.Lift1
|
||||
( Lift1(..)
|
||||
, lift1
|
||||
) where
|
||||
|
||||
import Gyehoek.Prelude hiding ((:<))
|
||||
import Language.Haskell.TH (Quote, Exp, listE)
|
||||
import Language.Haskell.TH.Syntax (Lift (..))
|
||||
import Control.Comonad.Cofree (Cofree(..))
|
||||
|
||||
|
||||
-- 뻘짓뻘짓뻘짓뻘짓뻘짓
|
||||
class Lift1 f where
|
||||
liftLift :: Quote m => (a -> m Exp) -> f a -> m Exp
|
||||
|
||||
lift1 :: (Lift1 f, Lift a, Quote m) => f a -> m Exp
|
||||
lift1 = liftLift lift
|
||||
|
||||
|
||||
--- instances
|
||||
|
||||
instance Lift1 f => Lift1 (Cofree f) where
|
||||
liftLift l (a :< e) = [|(:<) $(l a) $(liftLift (liftLift l) e)|]
|
||||
|
||||
instance Lift1 List where
|
||||
liftLift l xs = listE $ l <$> xs
|
||||
|
||||
instance Lift1 NonEmpty where
|
||||
liftLift l (x :| xs) = [|(:|) $(l x) $(liftLift l xs)|]
|
||||
+16
-4
@@ -13,7 +13,7 @@ import Data.Foldable
|
||||
import Gyehoek.Prelude hiding (argument)
|
||||
|
||||
|
||||
data Runtime = Stackify | Wasm | CPS
|
||||
data Runtime = Stackify | Wasm | CPS | HigherOrderCPS
|
||||
deriving (Show, Generic, Eq)
|
||||
|
||||
data Language
|
||||
@@ -29,6 +29,10 @@ data Options = MkOptions
|
||||
, dumpCPS :: Bool
|
||||
, dumpParsed :: Bool
|
||||
, dumpStackified :: Bool
|
||||
, dumpHoisted :: Bool
|
||||
, dumpContified :: Bool
|
||||
, traceStackified :: Bool
|
||||
, noColour :: Bool
|
||||
, runtime :: Maybe Runtime
|
||||
, inspectWasm :: Bool
|
||||
, output :: FilePath
|
||||
@@ -50,7 +54,8 @@ 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
|
||||
|
||||
@@ -60,13 +65,20 @@ parser = do
|
||||
dumpCPS <- switch (long "dump-cps")
|
||||
dumpStackified <- switch (long "dump-stackified")
|
||||
dumpParsed <- switch (long "dump-parsed")
|
||||
dumpHoisted <- switch (long "dump-hoisted")
|
||||
dumpContified <- switch (long "dump-contified")
|
||||
traceStackified <- switch (long "trace-stackified")
|
||||
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 $
|
||||
|
||||
@@ -16,9 +16,13 @@ module Gyehoek.Prelude
|
||||
, Hashable
|
||||
, NonEmpty((:|))
|
||||
, Natural
|
||||
, (>>>)
|
||||
, (>=>)
|
||||
, (<=<)
|
||||
, wrappedIso
|
||||
) where
|
||||
|
||||
import Control.Lens
|
||||
import Control.Lens hiding (List, (:<))
|
||||
import Data.List (List)
|
||||
import Data.Text (Text)
|
||||
import Effectful
|
||||
@@ -35,4 +39,7 @@ import GHC.Stack (HasCallStack)
|
||||
import Data.Hashable (Hashable)
|
||||
import Data.List.NonEmpty (NonEmpty((:|)))
|
||||
import Numeric.Natural (Natural)
|
||||
import Control.Category ((>>>))
|
||||
import Control.Monad
|
||||
import Data.Generics.Wrapped (Wrapped(..))
|
||||
|
||||
|
||||
+104
-135
@@ -17,29 +17,20 @@ module Gyehoek.Scheme.Syntax
|
||||
, Def(..)
|
||||
, Exp(..)
|
||||
, ExpF(..)
|
||||
, Sexp(..)
|
||||
, Program(..)
|
||||
, CommandOrDef(..)
|
||||
, primSexpIso
|
||||
, pattern Void
|
||||
, primDatumIso
|
||||
, free
|
||||
, subst
|
||||
, getName
|
||||
, scm
|
||||
, readExp
|
||||
, readProgram
|
||||
, free'
|
||||
, freeWithBound'
|
||||
, freeO
|
||||
, encodeProgram
|
||||
)
|
||||
where
|
||||
|
||||
import Data.List (intersperse)
|
||||
import Language.SexpGrammar
|
||||
( SexpIso(..), list, el, rest, sym, symbol )
|
||||
import Language.SexpGrammar qualified as Sexp
|
||||
import Language.SexpGrammar.Generic
|
||||
import Effectful
|
||||
import Prelude hiding ((.), id)
|
||||
import Control.Category
|
||||
@@ -58,6 +49,9 @@ import qualified Effectful.FileSystem.IO as FS
|
||||
import qualified Data.Text.Encoding as T
|
||||
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, G, DataIso, (:-)((:-)))
|
||||
import Gyehoek.Prelude
|
||||
|
||||
|
||||
@@ -87,9 +81,15 @@ 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
|
||||
deriving stock (Show, Generic, Functor, Foldable, Traversable, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
@@ -97,16 +97,11 @@ instance Each (Prim e) (Prim e') e e'
|
||||
|
||||
data Lit
|
||||
= LitInt Int
|
||||
| LitNil
|
||||
| LitBool Bool
|
||||
| LitString Text
|
||||
| LitQuote Sexp
|
||||
deriving stock (Show, Generic, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
pattern Void :: Lit
|
||||
pattern Void = LitNil
|
||||
|
||||
data Def
|
||||
= DefConstant Name Exp
|
||||
| DefProcedure Name (List Name) (List Exp)
|
||||
@@ -117,7 +112,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
|
||||
@@ -126,13 +121,6 @@ data Exp
|
||||
deriving stock (Show, Generic, Data)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
data Sexp
|
||||
= SexpCons Sexp Sexp
|
||||
| SexpSymbol Text
|
||||
| SexpLit Lit
|
||||
deriving stock (Show, Generic, Data, Eq)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
data CommandOrDef
|
||||
= Command Exp
|
||||
| Definition Def
|
||||
@@ -140,7 +128,7 @@ data CommandOrDef
|
||||
deriving stock (Show, Generic, Data)
|
||||
deriving anyclass (NFData)
|
||||
|
||||
data Program = MkProgram
|
||||
newtype Program = MkProgram
|
||||
{ commandsAndDefs :: List CommandOrDef
|
||||
}
|
||||
deriving stock (Show, Generic, Data)
|
||||
@@ -159,102 +147,107 @@ makeBaseFunctor ''Exp
|
||||
|
||||
|
||||
|
||||
instance SexpIso Name where
|
||||
sexpIso = symbol >>> Sexp.partialOsi f g
|
||||
instance DatumIso Name where
|
||||
datumIso = S.decorate S.SynVariable
|
||||
>>> S.symbol
|
||||
>>> S.iso coerce coerce
|
||||
|
||||
primDatumIso
|
||||
:: (Text -> Text)
|
||||
-> S.DatumGrammar a -> S.DatumGrammar (Prim a)
|
||||
primDatumIso namefn a = S.match
|
||||
$ S.With (. ht2 "+")
|
||||
$ S.With (. ht2 "-")
|
||||
$ S.With (. ht2 "*")
|
||||
$ S.With (. ht2 "/")
|
||||
$ S.With (. ht2 "cons")
|
||||
$ S.With (. ht1 "car")
|
||||
$ S.With (. ht1 "cdr")
|
||||
$ S.With (. ht1 "immediate?")
|
||||
$ S.With (. ht1 "cons?")
|
||||
$ S.With (. ht1 "integer?")
|
||||
$ S.With (. ht1 "write")
|
||||
$ S.With (. ht1 "zero?")
|
||||
$ S.With (. ht0 "newline")
|
||||
$ S.With (. ht1' "make-closure")
|
||||
$ S.With (. S.headTagged2 (namefn "make-shared-closure")
|
||||
(S.list $ S.rest a)
|
||||
(S.list $ S.rest a))
|
||||
$ S.With (. ht0 "get-env")
|
||||
$ S.With (. ht0 "env")
|
||||
$ S.With (. 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.End
|
||||
where
|
||||
f = Right . MkName
|
||||
g (MkName s) = s
|
||||
idn = S.el . S.sym . namefn
|
||||
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
|
||||
|
||||
primSexpIso :: (Text -> Text) -> Sexp.SexpGrammar a -> Sexp.SexpGrammar (Prim a)
|
||||
primSexpIso namefn a = match
|
||||
$ With (. ht2 "+")
|
||||
$ With (. ht2 "-")
|
||||
$ With (. ht2 "*")
|
||||
$ With (. ht2 "/")
|
||||
$ With (. ht2 "cons")
|
||||
$ With (. ht1 "car")
|
||||
$ With (. ht1 "cdr")
|
||||
$ With (. ht1 "immediate?")
|
||||
$ With (. ht1 "cons?")
|
||||
$ With (. ht1 "integer?")
|
||||
$ With (. ht1 "write")
|
||||
$ With (. ht1 "zero?")
|
||||
$ With (. nullop "newline")
|
||||
$ With (. ht1' "make-closure")
|
||||
$ With (. GS.headTagged2 (namefn "env-ref") a Sexp.int)
|
||||
$ With (. ht1 "env-code")
|
||||
$ With (. ht1 "call/cc")
|
||||
$ End
|
||||
instance DatumIso a => DatumIso (Prim a) where
|
||||
datumIso = primDatumIso id S.datumIso
|
||||
|
||||
instance DatumIso Lit where
|
||||
datumIso = S.match
|
||||
$ S.With (. S.int)
|
||||
$ S.With (. S.boolean)
|
||||
$ S.With (. S.string)
|
||||
$ S.End
|
||||
|
||||
instance DatumIso Def where
|
||||
datumIso = S.match
|
||||
$ S.With (. defconst)
|
||||
$ S.With (. defun)
|
||||
$ S.End
|
||||
where
|
||||
idn s = el (sym (namefn s))
|
||||
nullop s = list $ idn s
|
||||
ht1 s = GS.headTagged1 (namefn s) a
|
||||
ht2 s = GS.headTagged2 (namefn s) a a
|
||||
ht1' s = GS.headTagged1' (namefn s) a a
|
||||
|
||||
instance SexpIso a => SexpIso (Prim a) where
|
||||
-- sexpIso = primSexpIso ("prim:"<>) sexpIso
|
||||
sexpIso = primSexpIso id sexpIso
|
||||
|
||||
instance SexpIso Lit where
|
||||
sexpIso = match
|
||||
$ With (. sexpIso)
|
||||
$ With (. sym "nil")
|
||||
$ With (. GS.schemeBool)
|
||||
$ With (. sexpIso)
|
||||
$ With (. GS.prefixSugar "quote" Sexp.Quote sexpIso)
|
||||
$ End
|
||||
|
||||
instance SexpIso Sexp where
|
||||
sexpIso = match
|
||||
$ With (\conss -> conss . GS.todo)
|
||||
$ With (\s -> s . symbol)
|
||||
$ With (\lit -> lit . sexpIso)
|
||||
$ End
|
||||
|
||||
instance SexpIso Def where
|
||||
sexpIso = match
|
||||
$ With (. defconst)
|
||||
$ With (. defun)
|
||||
$ End
|
||||
where
|
||||
defconst = list $ el (sym "define") >>> el sexpIso >>> el sexpIso
|
||||
defun = list $ el (sym "define") >>> el args >>> rest sexpIso
|
||||
args = list $ el sexpIso >>> rest sexpIso
|
||||
defconst = S.list $ S.el (S.sym "define")
|
||||
>>> S.el S.datumIso >>> S.el S.datumIso
|
||||
defun = S.list $ S.el (S.sym "define")
|
||||
>>> S.el args >>> S.rest S.datumIso
|
||||
args = S.list $ S.el S.datumIso >>> S.rest S.datumIso
|
||||
|
||||
instance SexpIso Exp where
|
||||
sexpIso = match
|
||||
$ With (. GS.let_ "let" sexpIso sexpIso sexpIso)
|
||||
$ With (. GS.let_ "letrec" sexpIso sexpIso sexpIso)
|
||||
$ With (. sexpIso)
|
||||
$ With (\bgn -> bgn . list (el (sym "begin") >>> rest sexpIso))
|
||||
$ With (. if_)
|
||||
$ With (. sexpIso)
|
||||
$ With (. lam)
|
||||
$ With (. sexpIso)
|
||||
$ With (\app -> app . list (el sexpIso >>> rest sexpIso))
|
||||
$ End
|
||||
instance DatumIso Exp where
|
||||
datumIso = S.match
|
||||
$ 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 (. begin)
|
||||
$ S.With (. S.ifLike "if" S.datumIso S.datumIso S.datumIso)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (. lam)
|
||||
$ S.With (. S.datumIso)
|
||||
$ S.With (\app -> app . S.list (S.el S.datumIso >>> S.rest S.datumIso))
|
||||
$ S.End
|
||||
where
|
||||
if_ = list $ el (sym "if") >>> el sexpIso >>> el sexpIso >>> el sexpIso
|
||||
lam = list
|
||||
( el GS.lambdaKeyword
|
||||
>>> el (sexpIso @(List Name))
|
||||
>>> el sexpIso )
|
||||
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 SexpIso CommandOrDef where
|
||||
sexpIso = match
|
||||
$ With (\_Command -> _Command . sexpIso)
|
||||
$ With (\_Definition -> _Definition . sexpIso)
|
||||
$ With (\_Begin -> _Begin . bgn)
|
||||
$ End
|
||||
where
|
||||
bgn = list $ el (sym "begin") >>> rest sexpIso
|
||||
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.rest S.datumIso))
|
||||
$ S.End
|
||||
|
||||
instance DataIso Program where
|
||||
dataIso = S.dataIso @(List CommandOrDef) >>> S.iso coerce coerce
|
||||
|
||||
|
||||
-- utilities
|
||||
|
||||
scm :: QuasiQuoter
|
||||
scm = GS.makeSx [|| GS.fromSexp @Exp ||]
|
||||
scm = GS.makeSx [|| S.fromDatumUnsafe @Exp S.datumIso ||]
|
||||
|
||||
freeWithBound' :: Foldable f => f Name -> Exp -> List Name
|
||||
freeWithBound' bound = filter (`elem` bound) . free'
|
||||
@@ -309,27 +302,3 @@ subst f = \e -> cata go e mempty where
|
||||
go (ExpLetF _ _) _ = error "todo lol"
|
||||
go (ExpLambdaF bs e) bound = e $ insertFrom bs bound
|
||||
go e bound = embed $ fmap ($ bound) e
|
||||
|
||||
|
||||
|
||||
fileName :: FilePath -> FilePath
|
||||
fileName "-" = "<interactive>"
|
||||
fileName e = e
|
||||
|
||||
hGetContents :: FS.FileSystem :> es => FS.Handle -> Eff es Text
|
||||
hGetContents h = T.decodeUtf8 <$> FB.hGetContents h
|
||||
|
||||
readProgram :: IOE :> es => FilePath -> Eff es Program
|
||||
readProgram fp = runFileSystem $
|
||||
FS.withFile fp FS.ReadMode $ \h ->
|
||||
GS.parseSexps @CommandOrDef (fileName fp) <$> hGetContents h
|
||||
>>= either error (pure . MkProgram)
|
||||
|
||||
readExp :: IOE :> es => FilePath -> Eff es Exp
|
||||
readExp fp = readProgram fp <&> (^?! #commandsAndDefs . _head . #Command)
|
||||
|
||||
encodeProgram :: Program -> Text
|
||||
encodeProgram p = p.commandsAndDefs
|
||||
& fmap ((^?! _Right) . GS.encodePretty)
|
||||
& intersperse "\n\n"
|
||||
& mconcat
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user