5 Commits
Author SHA1 Message Date
msyds 3aac990fac
build / build (push) Failing after 1m45s
2026-08-24 11:13:34 -06:00
msyds 6a6d92bcda 2026-08-24 10:41:30 -06:00
msyds b5c823f5fe continue takes var 2026-08-24 10:41:19 -06:00
msyds f6bc2947ef
build / build (push) Failing after 11m35s
2026-08-24 09:54:19 -06:00
msyds 1b5c93b030 2026-08-24 06:58:16 -06:00
91 changed files with 1337 additions and 3033 deletions
-3
View File
@@ -9,9 +9,6 @@
. (progn (defun apply-cabal-fmt-h () . (progn (defun apply-cabal-fmt-h ()
(haskell-mode-buffer-apply-command "cabal-fmt")) (haskell-mode-buffer-apply-command "cabal-fmt"))
(add-hook 'before-save-hook #'apply-cabal-fmt-h nil t))))) (add-hook 'before-save-hook #'apply-cabal-fmt-h nil t)))))
(scheme-mode
. ((eval . (dolist (s '(kappa κ prim))
(put s 'scheme-indent-function 1)))))
(nil (nil
. ((eval . ((eval
. (progn (defun display-ansi () . (progn (defun display-ansi ()
-1
View File
@@ -9,4 +9,3 @@ dist-newstyle
.direnv .direnv
result result
play/ play/
trace.html
-31
View File
@@ -132,34 +132,3 @@ multiple ~env-ref~ calls could probably be replaced with a primitive that loads
$code) $code)
1)))) 1))))
#+end_src #+end_src
** example
#+begin_src scheme
(λ (n m ktail)
(letrec ((f (λ (x ktail-0) (+ x n ktail-0)))
(g (λ (y ktail-1) (+ y g ktail-1))))
(prim (cons f g) ktail)))
#+end_src
#+begin_src scheme
(λ (n m ktail)
(letrec ((f-code (λ (x ktail-0)
(prim (env-get 2)
(κ (n)
(+ x n ktail-0)))))
(g-code (λ (y ktail-1)
(prim (env-get 3)
(κ (m)
(+ y m ktail-1))))))
(letrec ((with-closure-code
(κ (f g)
(prim (get-env 0)
(κ (ktail)
(prim cons f g ktail))))))
(prim (make-shared-closure (with-closure-code)
ktail)
(κ (with-closure)
(prim (make-shared-closure (f-code g-code) n m)
with-closure))))))
#+end_src
-19
View File
@@ -1,19 +0,0 @@
#+title: on libraries
* libraries and the file system
R⁷RS leaves it unspecified how exactly libraries correspond to files:
#+begin_quote
Programs and libraries are typically stored in files, although in some implementations they can be entered interactively into a running Scheme system. Other paradigms are possible. Implementations which store libraries in files should document the mapping from the name of a library to its location in the file system.
#+end_quote
thus the implementation of ~define-library~ is open to much interpretation. we could possibly define libraries as first-class objects, or deal with them statically. the former case is appealing to me, as it could massively simplify interactive use.
* semantics of declaration order
mercifully, R⁷RS is similarly ambiguous when it comes to the significance of declaration order. the authors note explicitly example two equally acceptable approaches:
#+begin_quote
One possible implementation of libraries is as follows: _After all cond-expand library declarations are expanded, a new environment is constructed for the library consisting of all imported bindings._ The expressions from all begin, include and include-ci library declarations are expanded in that environment in the order in which they occur in the library. _Alternatively, cond-expand and import declarations may be processed in left to right order interspersed with the processing of other declarations_, with the environment growing as imported bindings are added to it by each import declaration.
#+end_quote
-100
View File
@@ -1,100 +0,0 @@
* 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
+1 -1
View File
@@ -55,7 +55,7 @@
nodejs nodejs
wasm-tools wasm-tools
wac-cli wac-cli
gauche guile
rust-analyzer rust-analyzer
wasmtime wasmtime
# bashInteractive is necessary to work around an # bashInteractive is necessary to work around an
-1
View File
@@ -1 +0,0 @@
(begin 123 456) ; => 456
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > #t
@@ -1,12 +0,0 @@
(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))))))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > #t
@@ -1,4 +0,0 @@
(call/cc
(λ (k)
(begin (k #t)
#f)))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > #t
@@ -1,5 +0,0 @@
;; confer ../callcc-early-exit-4
(letrec ((app (λ (f x)
(begin (f x)
#f))))
(call/cc (λ (k) (app k #t))))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > #t
@@ -1,5 +0,0 @@
;; confer ../callcc-early-exit-3
(letrec ((app (λ (f x)
(begin (f x)
#f))))
(call/cc (λ (k) (app (λ (x) (k x)) #t))))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > #t
@@ -1,4 +0,0 @@
(call/cc
(λ (k)
(begin ((λ () (k #t)))
#f)))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > 12
@@ -1,4 +0,0 @@
(* 2 (call/cc
(λ (k)
(begin (k 6)
3))))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > 456
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > 155
-10
View File
@@ -1,10 +0,0 @@
(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))))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > (6 . 7)
-1
View File
@@ -1 +0,0 @@
(cons 6 7)
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > (456 . 123)
-2
View File
@@ -1,2 +0,0 @@
(let ((p (cons 123 456)))
(cons (cdr p) (car p)))
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > 123
-1
View File
@@ -1 +0,0 @@
123
-2
View File
@@ -1,2 +0,0 @@
ret > ExitSuccess
out > (0 . (1 . (4 . (9 . (16 . ())))))
-7
View File
@@ -1,7 +0,0 @@
(letrec ((my-map (λ (f l)
(if (pair? l)
(cons (f (car l))
(my-map f (cdr l)))
(list)))))
(my-map (λ (x) (* x x))
(list 0 1 2 3 4)))
+3 -3
View File
@@ -1,4 +1,4 @@
(begin (begin
책을 책을
더 더
먹으세요~!) 먹으세요~!)
+3 -3
View File
@@ -1,4 +1,4 @@
(begin (begin
책을 책을
더 더
먹으세요~!) 먹으세요~!)
+4 -4
View File
@@ -1,5 +1,5 @@
(lambda (lambda
(어간 (어간
어미) 어미)
(display (display
꾸깃)) 꾸깃))
+2 -2
View File
@@ -1,2 +1,2 @@
(lambda (어간 어미) (lambda (어간 어미)
(display 꾸깃)) (display 꾸깃))
+4 -4
View File
@@ -1,4 +1,4 @@
(가 (가
나 나
다 다
라) 라)
+1 -1
View File
@@ -1 +1 @@
(가 나 다 라) (가 나 다 라)
+8 -4
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/bool/source.scm" { sourceName = "golden/read/bool/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -8,7 +9,8 @@
) )
} :< SimpleF ( SimpleBoolean True ) } :< SimpleF ( SimpleBoolean True )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/bool/source.scm" { sourceName = "golden/read/bool/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -17,7 +19,8 @@
) )
} :< SimpleF ( SimpleBoolean True ) } :< SimpleF ( SimpleBoolean True )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/bool/source.scm" { sourceName = "golden/read/bool/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -26,7 +29,8 @@
) )
} :< SimpleF ( SimpleBoolean False ) } :< SimpleF ( SimpleBoolean False )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/bool/source.scm" { sourceName = "golden/read/bool/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+6 -3
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/decimal/source.scm" { sourceName = "golden/read/decimal/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -9,7 +10,8 @@
} :< SimpleF } :< SimpleF
( SimpleNumber 45.0 ) ( SimpleNumber 45.0 )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/decimal/source.scm" { sourceName = "golden/read/decimal/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -19,7 +21,8 @@
} :< SimpleF } :< SimpleF
( SimpleNumber 5667.0 ) ( SimpleNumber 5667.0 )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/decimal/source.scm" { sourceName = "golden/read/decimal/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+10 -5
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-dot-flat/source.scm" { sourceName = "golden/read/list-dot-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -10,7 +11,8 @@
( DotListF ( DotListF
( (
( MkAnn ( MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-dot-flat/source.scm" { sourceName = "golden/read/list-dot-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -21,7 +23,8 @@
( SimpleSymbol "가" ) ( SimpleSymbol "가" )
) :| ) :|
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-dot-flat/source.scm" { sourceName = "golden/read/list-dot-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -31,7 +34,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "나" ) ( SimpleSymbol "나" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-dot-flat/source.scm" { sourceName = "golden/read/list-dot-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -43,7 +47,8 @@
] ]
) )
( MkAnn ( MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-dot-flat/source.scm" { sourceName = "golden/read/list-dot-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+17 -9
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -7,9 +8,10 @@
} }
) )
} :< CompoundF } :< CompoundF
( ListF StyleData ( ListF Ordinary
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -19,7 +21,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "가" ) ( SimpleSymbol "가" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -29,7 +32,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "나" ) ( SimpleSymbol "나" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -39,7 +43,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "다" ) ( SimpleSymbol "다" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -49,7 +54,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "라" ) ( SimpleSymbol "라" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -59,7 +65,8 @@
} :< SimpleF } :< SimpleF
( SimpleNumber 1.0 ) ( SimpleNumber 1.0 )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -69,7 +76,8 @@
} :< SimpleF } :< SimpleF
( SimpleNumber 2.0 ) ( SimpleNumber 2.0 )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list-flat/source.scm" { sourceName = "golden/read/list-flat/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+26 -14
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -10,7 +11,8 @@
( DotListF ( DotListF
( (
( MkAnn ( MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -21,7 +23,8 @@
( SimpleSymbol "a" ) ( SimpleSymbol "a" )
) :| ) :|
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -31,7 +34,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "b" ) ( SimpleSymbol "b" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -39,9 +43,10 @@
} }
) )
} :< CompoundF } :< CompoundF
( ListF StyleData ( ListF Ordinary
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -51,7 +56,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "c" ) ( SimpleSymbol "c" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -65,7 +71,8 @@
] ]
) )
( MkAnn ( MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -73,9 +80,10 @@
} }
) )
} :< CompoundF } :< CompoundF
( ListF StyleData ( ListF Ordinary
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -85,7 +93,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "가" ) ( SimpleSymbol "가" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -96,7 +105,8 @@
( DotListF ( DotListF
( (
( MkAnn ( MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -108,7 +118,8 @@
) :| [] ) :| []
) )
( MkAnn ( MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -120,7 +131,8 @@
) )
) )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/list/source.scm" { sourceName = "golden/read/list/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+2 -1
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/meta-expression/source.scm" { sourceName = "golden/read/meta-expression/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+2 -1
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/meta-splice-expression/source.scm" { sourceName = "golden/read/meta-splice-expression/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+2 -1
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/meta-splice-variable/source.scm" { sourceName = "golden/read/meta-splice-variable/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+2 -1
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/meta-variable/source.scm" { sourceName = "golden/read/meta-variable/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+3 -57
View File
@@ -1,61 +1,7 @@
[ MkAnn [ SynNone :< SimpleF
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-dot/source.scm"
, sourceLine = Pos 1
, sourceColumn = Pos 1
}
)
} :< SimpleF
( SimpleSymbol "..." )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-dot/source.scm"
, sourceLine = Pos 2
, sourceColumn = Pos 1
}
)
} :< SimpleF
( SimpleSymbol ".." ) ( SimpleSymbol ".." )
, MkAnn , SynNone :< SimpleF
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-dot/source.scm"
, sourceLine = Pos 3
, sourceColumn = Pos 1
}
)
} :< SimpleF
( SimpleSymbol ".abc" ) ( SimpleSymbol ".abc" )
, MkAnn , SynNone :< SimpleF
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-dot/source.scm"
, sourceLine = Pos 4
, sourceColumn = Pos 1
}
)
} :< SimpleF
( SimpleSymbol "....abcc" ) ( SimpleSymbol "....abcc" )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-dot/source.scm"
, sourceLine = Pos 5
, sourceColumn = Pos 1
}
)
} :< SimpleF
( SimpleSymbol ".++-" )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-dot/source.scm"
, sourceLine = Pos 6
, sourceColumn = Pos 1
}
)
} :< SimpleF
( SimpleSymbol ".-" )
] ]
@@ -1,6 +1 @@
... .. .abc ....abcc
..
.abc
....abcc
.++-
.-
+4 -52
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm" { sourceName = "golden/read/peculiar-identifier-sign/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -9,7 +10,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "+" ) ( SimpleSymbol "+" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm" { sourceName = "golden/read/peculiar-identifier-sign/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -18,54 +20,4 @@
) )
} :< SimpleF } :< SimpleF
( SimpleSymbol "-" ) ( SimpleSymbol "-" )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm"
, sourceLine = Pos 3
, sourceColumn = Pos 1
}
)
} :< SimpleF
( SimpleSymbol "+." )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm"
, sourceLine = Pos 3
, sourceColumn = Pos 4
}
)
} :< SimpleF
( SimpleSymbol "+.." )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm"
, sourceLine = Pos 3
, sourceColumn = Pos 8
}
)
} :< SimpleF
( SimpleSymbol "-." )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm"
, sourceLine = Pos 3
, sourceColumn = Pos 11
}
)
} :< SimpleF
( SimpleSymbol "-...abc" )
, MkAnn
{ position = Just
( SourcePos
{ sourceName = "golden/read/peculiar-identifier-sign/source.scm"
, sourceLine = Pos 3
, sourceColumn = Pos 19
}
)
} :< SimpleF
( SimpleSymbol "-abc.." )
] ]
@@ -1,3 +1 @@
+ - + -
+. +.. -. -...abc -abc..
+2 -1
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/string/source.scm" { sourceName = "golden/read/string/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
+11 -6
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier-token/source.scm" { sourceName = "golden/read/typical-identifier-token/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -9,7 +10,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "abc" ) ( SimpleSymbol "abc" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier-token/source.scm" { sourceName = "golden/read/typical-identifier-token/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -19,7 +21,8 @@
} :< SimpleF } :< SimpleF
( SimpleString "xyz" ) ( SimpleString "xyz" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier-token/source.scm" { sourceName = "golden/read/typical-identifier-token/source.scm"
, sourceLine = Pos 2 , sourceLine = Pos 2
@@ -29,7 +32,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "수학" ) ( SimpleSymbol "수학" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier-token/source.scm" { sourceName = "golden/read/typical-identifier-token/source.scm"
, sourceLine = Pos 2 , sourceLine = Pos 2
@@ -37,9 +41,10 @@
} }
) )
} :< CompoundF } :< CompoundF
( ListF StyleData ( ListF Ordinary
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier-token/source.scm" { sourceName = "golden/read/typical-identifier-token/source.scm"
, sourceLine = Pos 2 , sourceLine = Pos 2
+18 -9
View File
@@ -1,5 +1,6 @@
[ MkAnn [ MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -9,7 +10,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "abc" ) ( SimpleSymbol "abc" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -19,7 +21,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "bala-hwa$" ) ( SimpleSymbol "bala-hwa$" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -29,7 +32,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "x!!!" ) ( SimpleSymbol "x!!!" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -39,7 +43,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "z" ) ( SimpleSymbol "z" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -49,7 +54,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "z123" ) ( SimpleSymbol "z123" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -59,7 +65,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "나는너무졸리다" ) ( SimpleSymbol "나는너무졸리다" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 1 , sourceLine = Pos 1
@@ -69,7 +76,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "學" ) ( SimpleSymbol "學" )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 3 , sourceLine = Pos 3
@@ -79,7 +87,8 @@
} :< SimpleF } :< SimpleF
( SimpleSymbol "車室." ) ( SimpleSymbol "車室." )
, MkAnn , MkAnn
{ position = Just { syntax = SynNone
, position = Just
( SourcePos ( SourcePos
{ sourceName = "golden/read/typical-identifier/source.scm" { sourceName = "golden/read/typical-identifier/source.scm"
, sourceLine = Pos 5 , sourceLine = Pos 5
+8 -17
View File
@@ -26,7 +26,6 @@ common ghcstuffs
ghc-options: ghc-options:
-Wall -fdefer-type-errors -fno-show-valid-hole-fits -Wall -fdefer-type-errors -fno-show-valid-hole-fits
-fdefer-out-of-scope-variables -threaded -fdefer-out-of-scope-variables -threaded
-Wno-name-shadowing -Wno-partial-type-signatures
default-extensions: default-extensions:
BlockArguments BlockArguments
@@ -55,25 +54,20 @@ executable gyehoek
library library
import: ghcstuffs, ghcstuffs-dev import: ghcstuffs, ghcstuffs-dev
ghc-options: -fplugin=Effectful.Plugin ghc-options: -fplugin=Effectful.Plugin
-- build-tool-depends: retrie:retrie
-- cabal-fmt: expand src -- cabal-fmt: expand src
exposed-modules: exposed-modules:
Gyehoek.CPS.Close Gyehoek.CPS.Close
Gyehoek.CPS.Convert Gyehoek.CPS.Convert
Gyehoek.CPS.Eval Gyehoek.CPS.Eval
Gyehoek.CPS.Hoist Gyehoek.CPS.Stackify
Gyehoek.CPS.Syntax Gyehoek.CPS.Syntax
Gyehoek.Driver Gyehoek.Driver
Gyehoek.GenSym Gyehoek.GenSym
Gyehoek.Jalmot Gyehoek.Jalmot
Gyehoek.Language
Gyehoek.Language.Common
Gyehoek.Lift1 Gyehoek.Lift1
Gyehoek.Options Gyehoek.Options
Gyehoek.Prelude Gyehoek.Prelude
Gyehoek.Scheme.Expand
Gyehoek.Scheme.Expand.Old
Gyehoek.Scheme.Syntax Gyehoek.Scheme.Syntax
Gyehoek.Sexp Gyehoek.Sexp
Gyehoek.Sexp.Grammar Gyehoek.Sexp.Grammar
@@ -82,6 +76,9 @@ library
Gyehoek.Sexp.QQ Gyehoek.Sexp.QQ
Gyehoek.Sexp.Read Gyehoek.Sexp.Read
Gyehoek.Sexp.Syntax Gyehoek.Sexp.Syntax
Gyehoek.Stack.Lower
Gyehoek.Stack.Syntax
Gyehoek.Stack.VM
Gyehoek.Wasm Gyehoek.Wasm
build-depends: build-depends:
@@ -102,7 +99,6 @@ library
, hashable , hashable
, invertible-grammar , invertible-grammar
, lens , lens
, lucid
, megaparsec , megaparsec
, mtl , mtl
, optparse-applicative , optparse-applicative
@@ -110,21 +106,16 @@ library
, pretty-simple , pretty-simple
, prettyprinter , prettyprinter
, prettyprinter-ansi-terminal , prettyprinter-ansi-terminal
, prettyprinter-lucid
, process , process
, recursion-schemes , recursion-schemes
, scientific , scientific
, semialign
, string-interpolate , string-interpolate
, tardis
, template-haskell , template-haskell
, text , text
, text-short , text-short
, these
, typed-process , typed-process
, unordered-containers , unordered-containers
, vector , vector
, witherable
hs-source-dirs: src hs-source-dirs: src
default-language: GHC2024 default-language: GHC2024
@@ -139,11 +130,14 @@ test-suite test
-- cabal-fmt: expand test -Main -- cabal-fmt: expand test -Main
other-modules: other-modules:
Gyehoek.Test.CPS.Eval Gyehoek.Test.CPS.Eval
Gyehoek.Test.CPS.Stackify
Gyehoek.Test.CPS.Syntax Gyehoek.Test.CPS.Syntax
Gyehoek.Test.Golden
Gyehoek.Test.Scheme.Syntax Gyehoek.Test.Scheme.Syntax
Gyehoek.Test.Sexp.Print Gyehoek.Test.Sexp.Print
Gyehoek.Test.Sexp.QQ Gyehoek.Test.Sexp.QQ
Gyehoek.Test.Sexp.Read Gyehoek.Test.Sexp.Read
Gyehoek.Test.Stack.VM
Gyehoek.TestUtil Gyehoek.TestUtil
Root Root
@@ -171,10 +165,7 @@ test-suite doctest
import: ghcstuffs, ghcstuffs-dev import: ghcstuffs, ghcstuffs-dev
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
hs-source-dirs: test hs-source-dirs: test
build-depends: build-depends: base
, base
, gyehoek
default-extensions: CPP default-extensions: CPP
main-is: doctest.hs main-is: doctest.hs
-11
View File
@@ -1,11 +0,0 @@
(define-syntax if-not
(syntax-rules ()
((_ c t f) (if (not c) t f))
((_ c t) (if (not c) t))))
(write (macroexpand-1 '(if-not #t 123 456)))
(define (main)
(let loop ((datum (read)))
(unless (eof-object? datum)
())))
+23 -37
View File
@@ -4,52 +4,38 @@ module Gyehoek.CPS.Close
) where ) where
import Gyehoek.CPS.Syntax import Gyehoek.CPS.Syntax
import Data.List (nub)
import Gyehoek.GenSym import Gyehoek.GenSym
import Gyehoek.Prelude import Gyehoek.Prelude
import Debug.Pretty.Simple
import Gyehoek.Sexp qualified as S
import Data.HashSet.Lens
import Data.Traversable
genCodeName :: GenSym :> es => Name -> Eff es Name close :: GenSym :> es => Exp -> Eff es Exp
genCodeName f = gensym' @Name $ f ^. _Wrapped' . to (<> "-code") close = transformM \case
ExpLetRec [(f, AbsLambda lam@(MkLambda bs kb m))] e -> do
bindEnv :: List Name -> Exp -> Exp f_code <- gensym' @Name $ f ^. _Wrapped'. to (<> "-code")
bindEnv frees m = [cps| -- it would probably be most sane to generate a symbol for `env`,
(builtin (get-env) (κ #{frees} #{m})) -- 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})))
|] |]
close1 :: forall es. GenSym :> es => Exp -> Eff es Exp ExpApply f xs ktail -> do
close1 = \case code <- gensym' @Name "code"
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| pure [cps|
(letrec #{bs'} (prim (env-code #{f})
(builtin (make-shared-closure #{codes} #{frees}) (κ (#{code})
(κ #{boundNames} (#{code} #{f} ##{xs} #{ktail})))
#{e})))
|] |]
e -> pure e e -> pure e
close :: forall es. GenSym :> es => Exp -> Eff es Exp
close = transformM close1
closeProgram :: GenSym :> es => Program -> Eff es Program closeProgram :: GenSym :> es => Program -> Eff es Program
closeProgram = traverseOf (#body . #body) close closeProgram = traverseOf #body close
+35 -45
View File
@@ -12,7 +12,6 @@ import Data.List.NonEmpty (NonEmpty((:|)))
import Control.Monad.Cont qualified as Cont import Control.Monad.Cont qualified as Cont
import qualified Data.List.NonEmpty as NE import qualified Data.List.NonEmpty as NE
import Gyehoek.Prelude import Gyehoek.Prelude
import Debug.Pretty.Simple
-- 뻘짓이어라 -- 뻘짓이어라
@@ -24,73 +23,66 @@ 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. -- | Transform an expression with a meta-continuation.
convert convert
:: forall es. (GenSym :> es) :: forall es. (GenSym :> es)
=> Scm.Exp -> (List Val -> Eff es Exp) -> Eff es Exp => Scm.Exp -> (Val -> Eff es Exp) -> Eff es Exp
convert (Scm.ExpVar x) k = k [ValVar x] convert (Scm.ExpVar x) k = k $ ValVar x
convert (Scm.ExpLit l) k = k . one . ValImm $ case l of convert (Scm.ExpLit l) k = k . ValImm $ case l of
LitInt n -> ImmInt n LitInt n -> ImmInt n
LitBool b -> ImmBool b LitBool b -> ImmBool b
_ -> _ _ -> _
convert (Scm.ExpBuiltin p) k = -- special case: call/cc is desugared during cps-conversion...
telescope (convert1 @es) p \p' -> do convert (Scm.ExpPrim (PrimCallCC withcc)) k = do
r_l <- gensym' "r" convert withcc \withcc' -> do
m <- k [ValVar r_l] cc <- gensym' @Name "cc"
r <- gensym' "r"
m <- k $ ValVar r
ccish <- gensym' @Name "cc-ish"
x <- gensym' @Name "x"
pure [cps| pure [cps|
(builtin #{p'} (κ (#{r_l}) #{m})) (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)
convert (Scm.ExpLambda xs e) k = do convert (Scm.ExpLambda xs e) k = do
f <- gensym' "lambda-body" f <- gensym' "lambda-body"
lam <- convertLambda xs e lam <- convertLambda xs e
ke <- k [ValVar f] ke <- k $ ValVar f
pure [cps| pure [cps|
(letrec ((#{f} #{lam})) (letrec ((#{f} #{lam}))
#{ke}) #{ke})
|] |]
convert (Scm.ExpApply f xs) k = convert (Scm.ExpApply f xs) k =
telescope (convert1 @es) (f:|xs) \(f':|xs') -> do telescope (convert @es) (f:|xs) \(f':|xs') -> do
r <- gensym' @Name "r" r <- gensym' "r"
x <- gensym' "x" x <- gensym' "x"
m <- k [ValVar x] m <- k (ValVar x)
pure $ ExpLetRec [(r, AbsKappa' [x] m)] $ pure $ ExpLetRec [(r, AbsKappa' [x] m)] $ ExpApply f' xs' r
ExpApply f' xs' (KexpVar r)
convert (Scm.ExpBegin xs) k = telescope (convert @es) xs (k . NE.last) convert (Scm.ExpBegin xs) k = _
convert (Scm.ExpIf c t (Just f)) k = convert (Scm.ExpIf c t f) k =
convert1 c \c' -> do convert c \c' ->
t_l <- gensym' @Name "truthy-cont" ExpIf c' <$> convert t k <*> convert f k
f_l <- gensym' @Name "falsey-cont"
t' <- convert t k
f' <- convert f k
pure [cps|
(letrec ((#{t_l} (κ () #{t'}))
(#{f_l} (κ () #{f'})))
(if #{c'} #{t_l} #{f_l}))
|]
-- let-bindings are desugared into continuation calls whose parameters -- let-bindings are desugared into continuation calls whose parameters
-- are the left-hand sides and whose arguments are the right-hand -- are the left-hand sides and whose arguments are the right-hand
-- sides. -- sides.
convert (Scm.ExpLet bs e) k = convert (Scm.ExpLet bs e) k =
let rhss = bs ^.. each . _2 let rhss = bs ^.. each . _2
in telescope (convert1 @es) rhss \rhss' -> do in telescope (convert @es) rhss \rhss' -> do
e' <- convert e k e' <- convert e k
kbody <- gensym' @Name "let-body" kbody <- gensym' @Name "let-body"
let bs' = bs ^.. each . _1 let bs' = bs ^.. each . _1
@@ -113,15 +105,13 @@ convertLambda
=> List Name -> Scm.Exp -> Eff es Lambda => List Name -> Scm.Exp -> Eff es Lambda
convertLambda bs m = do convertLambda bs m = do
ktail <- gensym' "lambda-tail" ktail <- gensym' "lambda-tail"
m' <- convert1 m $ pure . ExpContinue (ValVar ktail) . (:[]) m' <- convert m $ pure . ExpContinue (ValVar ktail) . (:[])
pure [cps|(λ (##{bs} #{ktail}) #{m'})|] pure [cps|(λ (##{bs} #{ktail}) #{m'})|]
convertProgram :: forall es. (GenSym :> es) => Scm.Program -> Eff es Program convertProgram :: forall es. (GenSym :> es) => Scm.Program -> Eff es Program
convertProgram p = do convertProgram p = do
ktail <- gensym' "start-ktail" MkProgram <$> telescope (convert @es) (p ^.. each . _Left) (pure . nothalt)
m <- telescope (convert1 @es) (p ^.. each . _Left) where nothalt = ExpContinue (ValVar "main-ktail")
(pure . ExpContinue (ValVar ktail))
pure . MkProgram $ MkLambda [] ktail m
convertExp :: forall es. (GenSym :> es) => Scm.Exp -> Eff es Exp convertExp :: forall es. (GenSym :> es) => Scm.Exp -> Eff es Exp
convertExp e = convert e (pure . Halt) convertExp e = convert e (pure . Halt1)
+55 -279
View File
@@ -1,303 +1,79 @@
{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedLists #-}
module Gyehoek.CPS.Eval module Gyehoek.CPS.Eval
( evalProgram ( evalProgram
, module Gyehoek.CPS.Syntax , module Gyehoek.CPS.Syntax
, evalExp
, eGrammar
) where ) where
import Gyehoek.CPS.Syntax hiding (Hob(..), Obj(..), cont) import Gyehoek.CPS.Syntax
import Gyehoek.Sexp qualified as S import Control.Lens
import Control.Lens hiding (assign) import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe, isJust)
import Text.Show.Functions () import Text.Show.Functions ()
import qualified Data.HashMap.Strict as H import qualified Data.HashMap.Strict as H
import Gyehoek.Prelude hiding (assign) import Gyehoek.Prelude
import Debug.Pretty.Simple
import Gyehoek.Jalmot
import Control.Monad.Cont
import Gyehoek.Sexp qualified as S
import GHC.Generics (Generically(..))
import Gyehoek.Sexp ((:-)(..))
import Data.List (nub, mapAccumR, compareLength)
import Data.HashSet.Lens (setOf)
import Data.IntMap.Strict (IntMap)
import Data.IntMap.Strict qualified as IM
import Data.Monoid
import Control.Monad.State
import Data.Traversable (for)
import Data.Foldable (traverse_, foldrM)
newtype Loc = MkLoc { getLoc :: Int } data Env = MkEnv
deriving stock (Generic, Data) { vars :: HashMap Name Obj
deriving newtype (Show, Eq, Ord, Enum) , labels :: HashMap Name (Env, Abs)
data Store = MkStore
{ nextLoc :: Loc
, heap :: IntMap E
} }
deriving stock (Show, Generic)
type instance Index Store = Loc
type instance IxValue Store = E
instance Ixed Store where ix (MkLoc j) = #heap . ix j
instance At Store where at (MkLoc j) = #heap . at j
emptyStore :: Store
emptyStore = MkStore
{ nextLoc = MkLoc 0
, heap = mempty
}
newtype Env = MkEnv { getEnv :: HashMap Name Loc }
deriving stock (Show, Generic, Data)
deriving newtype (Semigroup, Monoid)
emptyEnv :: Env
emptyEnv = mempty
type instance Index Env = Name
type instance IxValue Env = Loc
instance Ixed Env where ix j = #getEnv . ix j
instance At Env where at j = #getEnv . at j
update :: Loc -> E -> Store -> Store
update (MkLoc loc) v = #heap %~ IM.alter f loc
where
f (Just _) = Just v
f Nothing = error "segfault lol"
updates :: Foldable f => f (Loc, E) -> Store -> Store
updates = alaf Endo foldMap (uncurry update)
fetch :: Loc -> M r E
fetch (MkLoc loc) = gets (^?! #heap . ix loc)
new :: M r Loc
new = state \st -> (st.nextLoc, st & #nextLoc %~ succ)
new' :: E -> M r Loc
new' e = state \st ->
( st.nextLoc
, st & #nextLoc %~ succ & at st.nextLoc ?~ e
)
defines :: Traversable t => t (Name, E) -> M Answer Env
defines = alaf Ap foldMap \(name,e) -> do
l <- new' e
pure $ bind name l
var :: HasCallStack => Env -> Name -> M Answer Loc
var g x = case g ^. at x of
Just l -> pure l
Nothing -> wrong [i|unbound variable #{x}|]
type CmdCont = Store -> Answer
type ExpCont = List E -> CmdCont
type M r = ContT r (State Store)
data Answer
= AnswerValues (List E)
| AnswerError AJalmot
deriving (Show, Generic) deriving (Show, Generic)
data Mutability eval :: Env -> Exp -> List Obj
= Mut
| NoMut
deriving (Show, Generic, Data, Eq)
wrong :: Text -> M Answer a eval g (Halt xs) = evalVal g <$> xs
wrong s = ContT \_ -> pure . AnswerError . EvalError $ s
orWrong eval g (ExpContinue k xs) =
:: Getting (First a) s a case g ^. #labels . at k of
-> Text -> s -> M Answer a Just (h, AbsKappa' bs m) -> eval h' m
orWrong p msg s = case getFirst . getConst $ p (Const . First . Just) s of where h' = h & #vars <>~ envOfBinds bs (evalVal g <$> xs)
Nothing -> wrong msg _ -> error [i|not a kappa: #{k}|]
Just x -> pure x
bind :: Name -> Loc -> Env eval g (ExpApply ((^?! #ValVar) -> f) xs ktail) =
bind k = MkEnv . H.singleton k 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}|]
extends :: Foldable f => f (Name, Loc) -> Env -> Env eval g (ExpLetRec [(b, ab)] e) = eval g' e
extends xs g = g <> foldMap (uncurry bind) xs where g' = g & #labels . at b ?~ (g,ab)
assign :: Loc -> E -> M Answer () eval g (ExpPrim p (MkKappa bs e)) = case evalVal g <$> p of
assign l e = do PrimAdd x y -> arithBinop (+) x y
use (at l) >>= \case PrimMul x y -> arithBinop (*) x y
Just _ -> at l ?= e PrimSub x y -> arithBinop (-) x y
Nothing -> wrong [i|#{e}에서 #{l}이라는 주소는 없다|] PrimDiv x y -> arithBinop div x y
_ -> error [i|unhandled prim: #{p}|]
-- | The denotation of an expressed value.
data E
= ESymbol Text
| ECharacter Char
| EInt Int
| EBool Bool
| EUndefined
| EUnspecified
| ENull
| EPair Mutability Loc Loc
| EVec Mutability (List Loc)
| EString Mutability (List Loc)
| EProcedure Procedure
deriving stock (Show, Generic)
type Procedure = List E -> DynPoints -> M Answer (List E)
eGrammar :: Store -> S.DatumGrammar E
eGrammar st = S.partialOsi (const . Left $ mempty) go
where where
gofetch x = go $ st ^?! ix x ret rs = eval
go = \case (g & #vars <>~ envOfBinds bs rs)
ESymbol s -> S.Symbol s e
ECharacter c -> S.Character c arithBinop f (ObjImm (ImmInt x)) (ObjImm (ImmInt y)) =
EInt n -> S.Number (fromIntegral n) ret [ObjImm . ImmInt $ f x y]
EBool b -> S.Boolean b arithBinop _ x y = error [i|bad arith: #{x}, #{y}|]
EUndefined -> S.Unreadable "#<undefined>"
EUnspecified -> S.Unreadable "#<unspecified>"
EProcedure _ -> S.Unreadable "#<procedure>"
ENull -> S.List []
EPair _mut car cdr -> S.DotList [gofetch car] (gofetch cdr)
EVec _mut xs -> S.Vector . fmap gofetch $ xs
EString _mut xs -> S.String _
data DynPoints = MkDynPoints eval _ e = error [i|unimplemented case: #{e}|]
deriving (Generic, Data)
envOfBinds bs xs = foldMap (uncurry H.singleton) (zip bs xs)
truthy :: E -> Bool evalVal :: Env -> Val -> Obj
truthy (EBool False) = False evalVal g = \case
truthy _ = True ValVar x -> fromMaybe (error [i|unbound: #{x}|]) $ g ^?! #vars . at x
ValImm x -> ObjImm x
emptyEnv :: Env
emptyEnv = MkEnv
{ vars = mempty
-- a kinda silly hack to make sure `halt` is handled correctly when
-- it appears as the tail continuation of an application. the
-- special case of `eval` responsible for `halt` only covers terms
-- of the form `(continue $halt xs …)`; other terms such as
-- `($some-fn xs $halt)` just see an undefined label `$halt`.
, labels = H.singleton "halt"
( emptyEnv
, AbsKappa' ["h0"] $ Halt [ValVar "h0"]
)
}
evalVal :: Env -> Val -> M Answer E evalProgram :: Program -> List Obj
evalProgram (MkProgram e) = eval emptyEnv e
evalVal g (ValVar x) = var g x >>= fetch
evalVal g (ValImm imm) = case imm of
ImmLabel (MkLabel l) -> var g l >>= fetch
ImmInt n -> pure $ EInt n
ImmBool b -> pure $ EBool b
ImmUndefined -> pure EUndefined
evalKexp :: Env -> Kexp -> M Answer E
evalKexp g (KexpVar x) = var g x >>= fetch
evalKexp g (KexpKappa kap) = evalAbs g (AbsKappa kap)
evalAbs :: Env -> Abs -> M Answer E
evalAbs g (MkAbs formals ktail e) = pure . EProcedure $ \xs dps ->
let
formals' = formals ++ foldMap (:[]) ktail
lformals = length formals'
lxs = length xs
in if lformals /= lxs
then wrong [i|함수는 #{lformals}개의 인자를 필요로 하는데 #{lxs}개 받았다.|]
else do
ls <- xs & traverse new'
let g' = g & extends (zip formals' ls)
eval g' dps e
eval :: Env -> DynPoints -> Exp -> M Answer (List E)
eval g dps (ExpJump f xs ktail) = do
f' <- evalVal g f
xs' <- traverse (evalVal g) xs
ktail' <- traverse (evalKexp g) (ktail ^.. _Just)
case f' of
EProcedure p -> p (xs' ++ ktail') dps
_ -> wrong "bad procedure"
eval g dps (ExpLetRec bs e) = do
ls <- for bs . const $ new' EUndefined
let g' = g & extends (zip (bs ^.. each . _1) ls)
bs' <- forOf (each . _2) bs (evalAbs g')
traverse_ (uncurry assign) $ zip ls (bs' ^.. each . _2)
eval g' dps e
eval g dps (ExpBuiltin (BuiltinCallCC withcc) k) = do
withcc' <- evalVal g withcc >>= orWrong #_EProcedure
[i|call/cc: 함수가 아닌 것을 받았다|]
k' <- evalKexp g k
kproc <- orWrong #_EProcedure [i|call/cc: 몰라...|] k'
let cc = EProcedure \xs dps -> case unsnoc xs of
Just (xs',_) -> kproc xs' dps
Nothing -> wrong [i|call/cc: 잘못하는데!|]
withcc' [cc,k'] dps
eval g dps (ExpBuiltin p k) = do
p' <- evalBuiltin g dps =<< traverse (evalVal g) p
evalKexp g k >>= \case
EProcedure fp -> fp p' dps
_ -> wrong [i|prim(#{p})의 계속을 나쁘다|]
eval g dps (ExpIf c t f) = do
c' <- evalVal g c
let b = if truthy c' then t else f
var g b >>= fetch >>= \case
EProcedure fp -> fp [] dps
_ -> wrong [i|if의 계속을 나쁘다|]
eval g dps e = error [i|unimplemented #{e}|]
evalBuiltin :: Env -> DynPoints -> Builtin E -> M Answer (List E)
evalBuiltin g dps = \case
BuiltinAdd x y -> arith2 (+) x y
BuiltinMul x y -> arith2 (*) x y
BuiltinSub x y -> arith2 (-) x y
BuiltinDiv x y -> arith2 div x y
BuiltinZeroP x -> pure1 . EBool . isJust $ x ^? #EInt . only 0
BuiltinCons x y -> pcons x y >>= pure1
BuiltinCar p -> cr p _2
BuiltinCdr p -> cr p _3
BuiltinPairP p -> pure1 . EBool . maybe False (const True) $
p ^? #_EPair
BuiltinValues xs -> pure xs
BuiltinList xs -> foldrM pcons ENull xs >>= pure1
p -> wrong [i|prim(#{p})은 벌써 나지 않다|]
where
pure1 x = pure [x]
pcons x y = do
(x',y') <- traverseOf both new' (x,y)
pure $ EPair Mut x' y'
arith2 f (EInt x) (EInt y) = pure [EInt $ f x y]
arith2 f x y = wrong [i|나쁜 인자: #{x}, #{y}|]
cr p l = orWrong (#_EPair . l) [i|car/cdr는 pair을 받지 않다|] p
>>= fmap (:[]) . fetch
evalExp :: Jalmot :> es => Exp -> Eff es _
evalExp e = _
evalProgram :: Jalmot :> es => Program -> Eff es (List S.Datum)
evalProgram (MkProgram lam) = case run (pure . AnswerValues) of
(AnswerError jm, _) -> throwError jm
(AnswerValues vs, st) -> traverse (S.toDatum $ eGrammar st) vs
where
run f = (`runState` emptyStore) . (`runContT` f) $ do
g <- setup
eval g MkDynPoints (ExpLetRec
[("_start",AbsLambda lam)]
(ExpApply (ValVar "_start") [] (KexpVar "halt")))
setup :: M Answer Env
setup = defines @List
[ ("halt", EProcedure prim_halt)
]
prim_halt :: Procedure
prim_halt xs _dps = ContT \_ -> pure $ AnswerValues xs
-24
View File
@@ -1,24 +0,0 @@
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}
+148
View File
@@ -0,0 +1,148 @@
{-# LANGUAGE OverloadedLists #-}
module Gyehoek.CPS.Stackify
( stackifyExp
, stackifyProgram
, module Gyehoek.CPS.Syntax
) where
import Gyehoek.CPS.Syntax
import Gyehoek.Stack.Syntax qualified as Stk
import Data.Sequence (Seq)
import Data.Sequence qualified as Seq
import Gyehoek.GenSym
import Effectful.Writer.Static.Shared
import Data.Foldable
import qualified Data.HashMap.Strict as H
import Data.List (elemIndex)
import Gyehoek.Prelude
type Stackify = Writer Stk.Program
runStackify :: Eff (Stackify : es) a -> Eff es (a, Stk.Program)
runStackify = runWriter
live :: Free a => Env -> a -> List Name
live g e = free' e & filter \x ->
x `H.member` g.bound
&& not (x `elem` g.contStack)
data BlockBuilder
= Code (List Stk.Instr) BlockBuilder
| Tail Stk.Tail
deriving (Show, Generic)
buildBlock :: BlockBuilder -> Stk.Block
buildBlock = go [] where
go acc (Code xs bb) = go (acc ++ xs) bb
go acc (Tail t) = Stk.MkBlock acc t
emitRoutine :: Stackify :> es => Stk.Routine -> Eff es ()
emitRoutine rt = tell [rt]
stackify
:: (GenSym :> es, Stackify :> es)
=> Env -> Exp -> Eff es BlockBuilder
stackify g (ExpLetRec [(f, kap@(AbsKappa' xs m))] e) = do
let vs = (f, Stk.ValLabel f) : (bindReg <$> xs)
let ls = live g kap
m' <- stackify (g & #bound .~ H.fromList (vs ++ (bindReg <$> ls))) m
emitRoutine $
Stk.MkRoutine f xs . buildBlock $
Code [Stk.Pop x | x <- ls] m'
let g' = g & #bound . at f ?~ Stk.ValLabel f
& #liveness . at f ?~ ls
stackify g' e
stackify g (ExpLetRec [(f, AbsLambda' xs k m)] e) = do
let vs = (k:xs) <&> \x -> (x, Stk.ValReg x)
m' <- stackify (g & #bound .~ H.fromList vs
& #contStack %~ (k:)) m
emitRoutine $ Stk.MkRoutine f xs (buildBlock m')
stackify g e
stackify g (ExpIf c t f) = do
let c' = stackifyVal g c
t' <- buildBlock <$> stackify g t
f' <- buildBlock <$> stackify g f
pure . Tail $ Stk.If c' t' f'
stackify g (ExpApply f xs ktail) = pure $
Code [ Stk.Push (Stk.ValReg l) | l <- ls ] $
Tail (Stk.PushCall k (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) =
pure . Tail $ Stk.TailCall (stackifyVal g k) (stackifyVal g <$> xs)
stackify g (ExpPrim p (MkKappa [x] e)) = do
e' <- stackify (g & #bound . at x ?~ Stk.ValReg x) e
pure $
Code [ Stk.Prim x (stackifyVal g <$> p) ] $
e'
stackify _ e = error [i|unimplemented exp: #{e}|]
stackifyVal :: Env -> Val -> Stk.Val
stackifyVal g = \case
ValImm imm -> Stk.ValImm imm
ValVar v -> var g v
v -> error [i|unimplemented val: #{v}|]
var :: Env -> Name -> Stk.Val
var g v = case g ^. #bound . at v of
Just x -> x
Nothing -> Stk.ValLabel v
bindReg :: Name -> (Name, Stk.Val)
bindReg x = (x, Stk.ValReg x)
data Env = MkEnv
{ bound :: HashMap Name Stk.Val
-- | for each locally-bound continuation @k@, @liveness@ has an
-- entry @(k,ls)@ where @ls@ is the sequence of registers @k@
-- expects to find saved on the stack.
, liveness :: HashMap Name (List Name)
, contStack :: List Name
}
deriving (Show, Generic)
emptyEnv :: Env
emptyEnv = MkEnv mempty mempty ["halt"]
stackifyExp :: GenSym :> es => Name -> Exp -> Eff es Stk.Program
stackifyExp lbl e = do
let g = emptyEnv & #bound . at "main-ktail" ?~ Stk.ValReg "main-ktail"
(code,p) <- runStackify $ stackify g e
pure $ p <> [ Stk.MkRoutine lbl ["main-ktail"] (buildBlock code) ]
stackifyProgram :: GenSym :> es => Program -> Eff es Stk.Program
stackifyProgram (MkProgram e) = stackifyExp "main" e
fac :: Program
fac = [cps|
(letrec ((fac (λ (n ktail)
(prim (zero? n)
(κ (x0)
(if x0
(continue ktail 1)
(prim (- n 1)
(κ (x1)
(letrec ((fac-k0
(κ (x2)
(prim (* n x2)
(κ (x3)
(continue ktail x3))))))
(fac x1 fac-k0))))))))))
(fac 6 halt))
|]
+74 -213
View File
@@ -5,29 +5,23 @@
{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveAnyClass #-}
{- HLINT ignore "Avoid lambda using `infix`" -}
{- HLINT ignore "Redundant $" -}
module Gyehoek.CPS.Syntax module Gyehoek.CPS.Syntax
( Val(..) ( Val(..)
, Kappa(..) , Kappa(..)
, Lambda(..) , Lambda(..)
, Exp(..) , Exp(..)
, Kexp(..)
, ExpF(..) , ExpF(..)
, Name(..) , Name(..)
, Builtin(..) , Prim(..)
, Program(..) , Program(..)
, HoistedProgram(..)
, Lit(..) , Lit(..)
, Imm(..) , Imm(..)
, Obj(..) , Obj(..)
, Hob(..) , Hob(..)
, Label(..)
, Reg(..)
, pattern Halt , pattern Halt
, pattern Halt1 , pattern Halt1
, _MkKappa , _MkKappa
, _ExpBuiltin , _ExpPrim
, _ExpLetRec , _ExpLetRec
, _ExpApply , _ExpApply
, _AbsLambda' , _AbsLambda'
@@ -42,17 +36,11 @@ module Gyehoek.CPS.Syntax
, Abs(..) , Abs(..)
, Free(..) , Free(..)
, pattern ValLabel , pattern ValLabel
, pattern ObjLabel , labelName -- don't like that this is part of the api
, absBody
, pattern MkAbs
, _MkAbs
, unhoist
, pattern ExpJump
, _ExpJump
) )
where where
import Gyehoek.Scheme.Syntax (Name (..), Builtin(..), builtinDatumIso, Lit(..)) import Gyehoek.Scheme.Syntax (Name (..), Prim(..), primDatumIso, Lit(..))
import Gyehoek.Sexp qualified as S import Gyehoek.Sexp qualified as S
import Control.Category import Control.Category
import Prelude hiding ((.), id) import Prelude hiding ((.), id)
@@ -61,12 +49,9 @@ import Data.Monoid (Endo)
import Data.Functor.Foldable.TH import Data.Functor.Foldable.TH
import Data.Data.Lens (uniplate) import Data.Data.Lens (uniplate)
import Gyehoek.Prelude hiding (op) import Gyehoek.Prelude hiding (op)
import Gyehoek.Sexp (G, (:-)(..), Datum) import Gyehoek.Sexp (Datum)
import Gyehoek.Sexp (G, (:-)(..))
import qualified Data.InvertibleGrammar.Base as IG import qualified Data.InvertibleGrammar.Base as IG
import Gyehoek.GenSym (Gen)
import Data.String (IsString)
import Control.Applicative
import qualified Data.HashMap.Strict as H
-- Data types -- Data types
@@ -75,24 +60,13 @@ data Val
| ValVar Name | ValVar Name
deriving (Show, Generic, Data, Eq) deriving (Show, Generic, Data, Eq)
pattern ValLabel :: Label -> Val pattern ValLabel :: Name -> Val
pattern ValLabel x = ValImm (ImmLabel x) 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 data Imm
= ImmInt Int = ImmInt Int
| ImmBool Bool | ImmBool Bool
| ImmLabel Label | ImmLabel Name
| ImmUndefined
deriving stock (Show, Generic, Data, Eq) deriving stock (Show, Generic, Data, Eq)
deriving anyclass (NFData) deriving anyclass (NFData)
@@ -102,15 +76,9 @@ data Obj
deriving stock (Show, Generic, Data, Eq) deriving stock (Show, Generic, Data, Eq)
deriving anyclass (NFData) deriving anyclass (NFData)
pattern ObjLabel :: Label -> Obj
pattern ObjLabel l = ObjImm (ImmLabel l)
-- | a heap object. -- | a heap object.
data Hob data Hob
= HobClosure { label :: Label, env :: List Obj } = HobClosure { label :: Name, 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 stock (Show, Generic, Data, Eq)
deriving anyclass (NFData) deriving anyclass (NFData)
@@ -125,60 +93,24 @@ data Abs
| AbsLambda Lambda | AbsLambda Lambda
deriving (Show, Generic, Data, Eq) deriving (Show, Generic, Data, Eq)
pattern AbsKappa' :: List Name -> Exp -> Abs pattern AbsKappa' :: [Name] -> Exp -> Abs
pattern AbsKappa' xs e = AbsKappa (MkKappa xs e) pattern AbsKappa' xs e = AbsKappa (MkKappa xs e)
pattern AbsLambda' :: List Name -> Name -> Exp -> Abs pattern AbsLambda' :: [Name] -> Name -> Exp -> Abs
pattern AbsLambda' xs e ktail = AbsLambda (MkLambda xs e ktail) pattern AbsLambda' xs e ktail = AbsLambda (MkLambda xs e ktail)
{-# COMPLETE AbsKappa', AbsLambda' #-}
_MkAbs :: Iso' Abs (List Name, Maybe Name, Exp)
_MkAbs = iso
(\case
AbsKappa' xs e -> (xs,Nothing,e)
AbsLambda' xs ktail e -> (xs,Just ktail,e))
(\(xs,ktail,e) -> case ktail of
Just k -> AbsLambda' xs k e
Nothing -> AbsKappa' xs e)
pattern MkAbs :: List Name -> Maybe Name -> Exp -> Abs
pattern MkAbs xs ktail body <- (view _MkAbs -> (xs,ktail,body))
where MkAbs xs ktail body = review _MkAbs (xs,ktail,body)
{-# COMPLETE MkAbs #-}
_ExpJump :: Prism' Exp (Val, List Val, Maybe Kexp)
_ExpJump = prism'
(\(f,xs,ktail) -> case ktail of
Just k -> ExpApply f xs k
Nothing -> ExpContinue f xs)
\case
ExpApply f xs ktail -> Just (f,xs,Just ktail)
ExpContinue f xs -> Just (f,xs,Nothing)
_ -> Nothing
pattern ExpJump :: Val -> List Val -> Maybe Kexp -> Exp
pattern ExpJump f xs ktail <- (preview _ExpJump -> Just (f,xs,ktail))
where ExpJump f xs ktail = review _ExpJump (f,xs,ktail)
data Exp data Exp
= ExpBuiltin (Builtin Val) Kexp = ExpPrim (Prim Val) Kappa
| ExpLetRec { binders :: List (Name, Abs), body :: Exp } | ExpLetRec { binders :: List (Name, Abs), body :: Exp }
| ExpContinue Val (List Val) | ExpContinue Val (List Val)
| ExpIf Val Name Name | ExpIf Val Exp Exp
| ExpApply | ExpApply
{ op :: Val { op :: Val
, args :: List Val , args :: List Val
, cont :: Kexp , cont :: Name
} }
deriving (Show, Generic, Data, Eq) deriving (Show, Generic, Data, Eq)
data Kexp
= KexpVar Name
| KexpKappa Kappa
deriving (Show, Generic, Data, Eq)
pattern Halt :: List Val -> Exp pattern Halt :: List Val -> Exp
pattern Halt xs = ExpContinue (ValLabel "halt") xs pattern Halt xs = ExpContinue (ValLabel "halt") xs
@@ -188,27 +120,11 @@ pattern Halt1 x = ExpContinue (ValLabel "halt") [x]
data Def = DefConstant Name Exp data Def = DefConstant Name Exp
deriving (Show, Generic, Data) deriving (Show, Generic, Data)
newtype Program = MkProgram data Program = MkProgram
{ body :: Lambda { body :: Exp
} }
deriving (Show, Generic, Data) deriving (Show, Generic, Data)
data HoistedProgram = MkHoistedProgram
{ bindings :: HashMap Label Abs
, body :: Lambda
}
deriving stock (Show, Generic, Data)
type instance Index HoistedProgram = Label
type instance IxValue HoistedProgram = Abs
instance Ixed HoistedProgram where ix j = #bindings . ix j
instance At HoistedProgram where at j = #bindings . at j
instance Each HoistedProgram HoistedProgram Abs Abs where
each = #bindings . each
makePrisms ''Kappa makePrisms ''Kappa
makePrisms ''Exp makePrisms ''Exp
makeFieldsId ''Exp makeFieldsId ''Exp
@@ -232,21 +148,6 @@ _AbsLambda' = prism'
instance Plated Exp where plate = uniplate instance Plated Exp where plate = uniplate
absBody :: Lens' Abs Exp
absBody = lens
(\case
AbsLambda lam -> lam.body
AbsKappa kap -> kap.body)
(\cases
(AbsLambda lam) b -> AbsLambda $ lam & #body .~ b
(AbsKappa kap) b -> AbsKappa $ kap & #body .~ b)
unhoist :: HoistedProgram -> Program
unhoist p =
MkProgram $ p.body & body %~ ExpLetRec
(p ^.. #bindings . itraversed . withIndex
. to (\(MkLabel l, ab) -> (l,ab)))
-- DatumIso instances -- DatumIso instances
@@ -266,64 +167,51 @@ instance S.DatumIso Imm where
datumIso = S.match datumIso = S.match
$ S.With (. S.int) $ S.With (. S.int)
$ S.With (. S.datumIso) $ S.With (. S.datumIso)
$ S.With (. S.datumIso) $ S.With (. labelName)
$ S.With (. S.unreadable (const "#<undefined>"))
$ S.End $ S.End
instance S.DatumIso Label where labelName :: S.DatumGrammar Name
datumIso = S.with \g -> S.coproduct labelName = S.coproduct
[ S.datumIso @Name >>> S.prismIso [ S.decorate S.SynConstant >>> S.datumIso @Name >>> S.prismIso
(S.expected "label") (S.expected "label")
(prefixed @Name "$") (prefixed @Name "$")
, S.list $ S.el (S.sym "$") >>> S.el (S.datumIso @Name) , S.list $ S.el (S.sym "$") >>> S.el (S.datumIso @Name)
] ]
>>> g
instance S.DatumIso Reg where
datumIso = S.with \g ->
S.datumIso @Name >>> S.prismIso
(S.expected "register")
(prefixed @Name "%")
>>> g
instance S.DatumIso Hob where instance S.DatumIso Hob where
datumIso = S.match datumIso = S.match
$ S.With (. closure) $ S.With (. closure)
$ S.With (. cont)
$ S.With (. conspair)
$ S.End $ S.End
where where
conspair = S.dottedList (S.el S.datumIso) S.datumIso
-- closures can be printed, but not parsed. -- closures can be printed, but not parsed.
closure :: G (Datum :- t) (List Obj :- Label :- t) closure :: G (Datum :- t) (List Obj :- Name :- t)
closure = IG.Flip $ IG.PartialIso closure = IG.Flip $ IG.PartialIso
(\(env:-code:-t) -> S.Unreadable [i|\#<procedure $#{code}>|] :- t) (\(env:-code:-t) -> [S.sx|(<closure> #{code} ##{env})|] :- t)
(const . Left $ mempty)
cont :: G (Datum :- t) (NonEmpty (List Obj) :- _ :- t)
cont = IG.Flip $ IG.PartialIso
(\(_ :- l :- t) ->
let x = S.encodeOrShow' @Text S.datumIso l
in S.Unreadable [i|\#<continuation #{x}>|] :- t)
(const . Left $ mempty) (const . Left $ mempty)
instance S.DatumIso Lambda where instance S.DatumIso Lambda where
datumIso = S.with (lam >>>) datumIso = S.match
$ S.With (. lambda)
$ S.End
where where
lam :: forall t. G (Datum :- t) (Exp :- Name :- List Name :- t) lambda = S.list $
lam = S.lambdaLike S.el S.lambdaKeyword
S.lambdaKeyword >>> S.el binders
binders >>> S.el S.datumIso
(S.el $ S.datumIso @Exp)
binders :: forall t. G (Datum :- t) (Name :- List Name :- t) binders :: forall t. G (Datum :- t) (Name :- List Name :- t)
binders = binders = S.list $
S.list (S.rest $ S.datumIso @Name) S.rest (S.datumIso @Name)
>>> S.flipped S.snoced >>> S.onTail (S.flipped $ IG.PartialIso
>>> S.swap (\(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")
)
instance S.DatumIso Kappa where instance S.DatumIso Kappa where
datumIso = S.with \g -> datumIso = S.with \g ->
S.lambdaLike S.kappaKeyword S.lambdaLike S.kappaKeyword
(S.datumIso @(List Name)) (S.list $ S.rest (S.datumIso @Name))
(S.el $ S.datumIso @Exp) (S.el $ S.datumIso @Exp)
>>> g >>> g
@@ -335,71 +223,50 @@ instance S.DatumIso Abs where
instance S.DatumIso Exp where instance S.DatumIso Exp where
datumIso = S.match datumIso = S.match
$ S.With (. builtin) $ S.With (. prim)
$ S.With (. letrec) $ S.With (. letrec)
$ S.With (. continue) $ S.With (. continue)
$ S.With (. if_) $ S.With (. if_)
$ S.With (. app) $ S.With (. app)
$ S.End $ S.End
where where
continue = S.listWithStyle (S.StyleSyntax 1) $ continue = S.list $
S.el (S.sym "continue") S.el (S.decorate S.SynBuiltin >>> S.sym "continue")
>>> S.el S.datumIso >>> S.el (S.decorate S.SynProcedure >>> S.datumIso)
>>> S.rest S.datumIso >>> S.rest S.datumIso
letrec = S.letLike "letrec" S.datumIso S.datumIso S.datumIso letrec = S.letLike "letrec" S.datumIso S.datumIso S.datumIso
if_ = S.ifLike "if" if_ = S.ifLike "if"
S.datumIso S.datumIso S.datumIso S.datumIso S.datumIso S.datumIso
app :: forall t. app :: forall t.
G (Datum :- t) (Kexp :- List Val :- Val :- t) G (Datum :- t) (Name :- ([Val] :- (Val :- t)))
app = S.list $ app = S.list $ S.el (S.datumIso @Val)
S.flipped (S.PartialIso -- >>> S.flipped Gyehoek.Datum.nonEmptyGrammar
(\(S.MkListContext ctx :- t) ->
case ctx of
f:kexp:xs -> S.MkListContext (f : snoc xs kexp) :- t
_ -> error "unreachable")
(\(S.MkListContext ctx :- t) ->
case unsnoc ctx of
Just (f:xs,kexp) -> Right $ S.MkListContext (f:kexp:xs) :- t
_ -> Left $ S.expected "continuation arg"))
>>> S.el (S.datumIso @Val)
>>> S.el (S.datumIso @Kexp)
>>> S.rest (S.datumIso @Val) >>> S.rest (S.datumIso @Val)
>>> S.onTail S.swap -- >>> _
builtin = S.listWithStyle (S.StyleSyntax 1) $ >>> S.onTail (S.flipped $ IG.PartialIso
S.el (S.sym "builtin") (\(karg :- args :- op :- t) ->
>>> S.el (builtinDatumIso id (S.datumIso @Val)) (args ++ [ValVar karg]) :- op :- t)
(\(xs :- op :- t) -> case xs ^? _Snoc of
Just (args,preview #ValVar -> Just karg) ->
Right $ karg:- args :- op :- t
_ -> Left $ S.expected "continuation arg"
))
-- prim = S.headTagged2 "prim"
-- (primDatumIso id (S.datumIso @Val))
-- (S.datumIso @Kappa)
prim = S.list $
S.el (S.decorate S.SynBuiltin >>> S.sym "prim")
>>> S.el (primDatumIso id (S.datumIso @Val))
>>> S.el S.datumIso >>> S.el S.datumIso
instance S.DatumIso Kexp where
datumIso = S.match
$ S.With (S.datumIso @Name >>>)
$ S.With (S.datumIso @Kappa >>>)
$ S.End
instance S.DatumIso Program where instance S.DatumIso Program where
datumIso = S.with \prog -> S.datumIso @Lambda >>> prog datumIso = S.with \prog -> S.datumIso @Exp >>> prog
-- the printed representation is pretty dishonest in its current
-- state. consider the following hoisted program:
--
-- (letrec ((k (κ () (continue start-ktail 123))))
-- (λ (start-ktail)
-- (continue k)))
--
-- here, `start-ktail` is bound in `k`, but the printed representation
-- fails to reflect that.
instance S.DatumIso HoistedProgram where
datumIso = S.with \prog ->
S.letLike "letrec"
(S.datumIso @Label) (S.datumIso @Abs) (S.datumIso @Lambda)
>>> S.onTail (S.iso H.fromList H.toList)
>>> prog
-- quasiquoters -- quasiquoters
class Data a => CPS a where class Data a => CPS a where
toCPS :: HasCallStack => Datum -> a toCPS :: Datum -> a
instance CPS Exp where toCPS = S.fromDatumUnsafe S.datumIso instance CPS Exp where toCPS = S.fromDatumUnsafe S.datumIso
instance CPS Val where toCPS = S.fromDatumUnsafe S.datumIso instance CPS Val where toCPS = S.fromDatumUnsafe S.datumIso
@@ -407,16 +274,21 @@ instance CPS Kappa where toCPS = S.fromDatumUnsafe S.datumIso
instance CPS Lambda where toCPS = S.fromDatumUnsafe S.datumIso instance CPS Lambda where toCPS = S.fromDatumUnsafe S.datumIso
instance CPS Abs where toCPS = S.fromDatumUnsafe S.datumIso instance CPS Abs where toCPS = S.fromDatumUnsafe S.datumIso
instance CPS Program where toCPS = S.fromDatumUnsafe S.datumIso instance CPS Program where toCPS = S.fromDatumUnsafe S.datumIso
instance CPS HoistedProgram where toCPS = S.fromDatumUnsafe S.datumIso
cps :: S.QuasiQuoter cps :: S.QuasiQuoter
cps = S.makeSx' [| toCPS |] cps = S.makeSx' [| toCPS |]
deleteFrom :: (Foldable f, Hashable a) => f a -> HashSet a -> HashSet a
deleteFrom = flip $ foldr HS.delete
insertFrom :: (Foldable f, Hashable a) => f a -> HashSet a -> HashSet a insertFrom :: (Foldable f, Hashable a) => f a -> HashSet a -> HashSet a
insertFrom = flip $ foldr HS.insert insertFrom = flip $ foldr HS.insert
toHashSetOf :: Hashable a => Getting (Endo (HashSet a)) s a -> s -> HashSet a
toHashSetOf l = foldrOf l HS.insert mempty
class Free a where class Free a where
free :: a -> HashSet Name free :: a -> HashSet Name
free = freeWithBound mempty free = freeWithBound mempty
@@ -424,8 +296,7 @@ class Free a where
freeWithBound :: HashSet Name -> a -> HashSet Name freeWithBound :: HashSet Name -> a -> HashSet Name
freeWithBound bound = HS.fromList . freeWithBound' bound freeWithBound bound = HS.fromList . freeWithBound' bound
-- | Free variables given in the same left-to-right order they -- | Free variables given in the order of their appearance.
-- appear.
free' :: a -> List Name free' :: a -> List Name
free' = freeWithBound' mempty free' = freeWithBound' mempty
@@ -435,21 +306,11 @@ instance Free Abs where
freeWithBound' bound (AbsKappa kap) = freeWithBound' bound kap freeWithBound' bound (AbsKappa kap) = freeWithBound' bound kap
freeWithBound' bound (AbsLambda lam) = freeWithBound' bound lam freeWithBound' bound (AbsLambda lam) = freeWithBound' bound lam
mif :: Alternative f => (a -> Bool) -> a -> f a
mif p a
| p a = pure a
| otherwise = empty
instance Free Kexp where
freeWithBound' bound = \case
KexpVar x -> mif (`notElem` bound) x
KexpKappa kap -> freeWithBound' bound kap
instance Free Exp where instance Free Exp where
freeWithBound' bound = \case freeWithBound' bound = \case
ExpBuiltin p k -> ExpPrim p k ->
(p ^.. folded . #ValVar . filtered (`notElem` bound)) p & toListOf (folded . #ValVar . filtered (`notElem` bound))
++ freeWithBound' bound k & (<> freeWithBound' bound k)
ExpLetRec bs m -> ExpLetRec bs m ->
foldMapOf (each . _2) (freeWithBound' bound') bs foldMapOf (each . _2) (freeWithBound' bound') bs
<> freeWithBound' bound' m <> freeWithBound' bound' m
@@ -457,10 +318,10 @@ instance Free Exp where
ExpContinue k xs -> filter (`notElem` bound) ((k:xs) ^.. each . #ValVar) ExpContinue k xs -> filter (`notElem` bound) ((k:xs) ^.. each . #ValVar)
ExpIf c t f -> ExpIf c t f ->
(c ^.. #ValVar . filtered (`notElem` bound)) (c ^.. #ValVar . filtered (`notElem` bound))
<> mif (`notElem` bound) t <> mif (`notElem` bound) f <> freeWithBound' bound t <> freeWithBound' bound f
ExpApply f xs k -> ExpApply f xs k ->
(f:xs) ^.. (each . #ValVar . filtered (`notElem` bound)) (f:xs) ^.. (each . #ValVar . filtered (`notElem` bound))
<> freeWithBound' bound k <> (k ^.. filtered (`notElem` bound))
instance Free Kappa where instance Free Kappa where
freeWithBound' bound (MkKappa xs m) = freeWithBound' bound (MkKappa xs m) =
+30 -26
View File
@@ -1,5 +1,5 @@
module Gyehoek.Driver module Gyehoek.Driver
(main, convert_e2e, parse_e2e, readScm, eval_cps1_e2e, eval_cps2_e2e) (main, lower_e2e, convert_e2e, parse_e2e, readScm, eval_e2e)
where where
import Gyehoek.Options import Gyehoek.Options
@@ -17,6 +17,7 @@ import qualified Data.Text.Encoding as T
import System.IO (Handle) import System.IO (Handle)
import System.IO qualified as IO import System.IO qualified as IO
import Gyehoek.CPS.Convert import Gyehoek.CPS.Convert
import Gyehoek.Stack.Lower
import Gyehoek.CPS.Eval qualified as CPS import Gyehoek.CPS.Eval qualified as CPS
import Control.Monad import Control.Monad
import Text.Pretty.Simple (pShowNoColor) import Text.Pretty.Simple (pShowNoColor)
@@ -24,14 +25,16 @@ import System.Process.Typed
import System.Environment.Blank (getEnvDefault) import System.Environment.Blank (getEnvDefault)
import qualified Data.Text.IO as TIO import qualified Data.Text.IO as TIO
import qualified Data.ByteString.Lazy as BS import qualified Data.ByteString.Lazy as BS
import Gyehoek.CPS.Stackify (stackifyProgram)
import Gyehoek.Stack.VM (eval, writeObj, Obj)
import qualified Data.Text as T import qualified Data.Text as T
import Gyehoek.Stack.Syntax qualified as Stk
import Gyehoek.CPS.Close (closeProgram) import Gyehoek.CPS.Close (closeProgram)
import Control.Lens.Extras (is) import Control.Lens.Extras (is)
import Control.Arrow ((>>>)) import Control.Arrow ((>>>))
import Gyehoek.Prelude import Gyehoek.Prelude
import Gyehoek.Jalmot import Gyehoek.Jalmot
import qualified Gyehoek.Sexp as S import qualified Gyehoek.Sexp as S
import Gyehoek.CPS.Hoist (hoistProgram)
main :: IO () main :: IO ()
@@ -115,20 +118,27 @@ driver opts = do
hPutStrLn FS.stdout . view strict . pShowNoColor $ scm hPutStrLn FS.stdout . view strict . pShowNoColor $ scm
cps <- convertProgram scm cps <- convertProgram scm
when opts.dumpCPS do when opts.dumpCPS do
S.writeDatum cps hPutStrLn FS.stdout =<< S.encodeWith S.datumIso cps
closedCps <- closeProgram cps closedCps <- closeProgram cps
when opts.dumpClosed do when opts.dumpClosed do
S.writeDatum closedCps hPutStrLn FS.stdout =<< S.encodeWith S.datumIso closedCps
hoistedCps <- hoistProgram closedCps
when opts.dumpHoisted do
S.writeDatum hoistedCps
let rt_is p = is (_Just . p) opts.runtime let rt_is p = is (_Just . p) opts.runtime
when (rt_is #HigherOrderCPS) do dumpOrRun opts.dumpStackified (rt_is #Stackify)
CPS.evalProgram cps (stackifyProgram closedCps)
>>= S.writeData (hPutStrLn FS.stdout <=< S.encodeDataWith S.dataIso)
(eval >>> fmap writeObj
>>> T.unwords
>>> hPutStrLn FS.stdout)
when (rt_is #CPS) do when (rt_is #CPS) do
CPS.evalProgram closedCps closedCps
>>= S.writeData & CPS.evalProgram
& fmap writeObj
& T.unwords
& hPutStrLn FS.stdout
-- dumpOrRun opts.inspectWasm (rt_is #Wasm)
-- (lowerProgram cps)
-- inspectWasm
-- (\wat -> withFile opts.output FS.WriteMode \h -> hPutStrLn h wat)
parse_e2e :: FilePath -> IO Scm.Program parse_e2e :: FilePath -> IO Scm.Program
parse_e2e = runJalmotIO . runFileSystem . readScm parse_e2e = runJalmotIO . runFileSystem . readScm
@@ -137,18 +147,12 @@ convert_e2e :: FilePath -> IO CPS.Program
convert_e2e = runJalmotIO . runFileSystem . runGenSym convert_e2e = runJalmotIO . runFileSystem . runGenSym
. (closeProgram <=< convertProgram <=< readScm) . (closeProgram <=< convertProgram <=< readScm)
eval_cps1_e2e :: FilePath -> IO Text lower_e2e :: FilePath -> IO Text
eval_cps1_e2e fp = runJalmotIO . runFileSystem . runGenSym $ lower_e2e =
readScm fp runJalmotIO . runFileSystem . runGenSym
>>= convertProgram . (lowerProgram <=< closeProgram <=< convertProgram <=< readScm)
>>= closeProgram
>>= CPS.evalProgram
>>= pure . S.encodeOrShowData' S.dataIso
eval_cps2_e2e :: FilePath -> IO Text eval_e2e :: FilePath -> IO (List Obj)
eval_cps2_e2e fp = runJalmotIO . runFileSystem . runGenSym $ eval_e2e fp = runJalmotIO . runFileSystem . runGenSym $ do
readScm fp stk <- stackifyProgram <=< closeProgram <=< convertProgram <=< readScm $ fp
>>= convertProgram pure . eval $ stk
-- >>= closeProgram
>>= CPS.evalProgram
>>= pure . S.encodeOrShowData' S.dataIso
-8
View File
@@ -8,7 +8,6 @@ module Gyehoek.Jalmot
, runJalmotIO , runJalmotIO
, runJalmotIOE , runJalmotIOE
, runJalmotUnsafe , runJalmotUnsafe
, runJalmotCS
) )
where where
@@ -30,8 +29,6 @@ deriving instance Data p => Data (Grammar.ErrorMessage p)
data AJalmot data AJalmot
= ReaderError (ParseErrorBundle Text Void) = ReaderError (ParseErrorBundle Text Void)
| GrammarError (Grammar.ErrorMessage Ann) | GrammarError (Grammar.ErrorMessage Ann)
| VMError Text
| EvalError Text
deriving (Show, Generic, Data) deriving (Show, Generic, Data)
data AJalmotCS = MkAJalmotCS !CallStack !AJalmot data AJalmotCS = MkAJalmotCS !CallStack !AJalmot
@@ -42,9 +39,6 @@ type Jalmot = Error AJalmot
runJalmot :: Eff (Jalmot : es) a -> Eff es (Either (CallStack, AJalmot) a) runJalmot :: Eff (Jalmot : es) a -> Eff es (Either (CallStack, AJalmot) a)
runJalmot = runError 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 :: IOE :> es => Eff (Jalmot : es) a -> Eff es a
runJalmotIOE eff = runJalmotIOE eff =
runJalmot eff >>= \case runJalmot eff >>= \case
@@ -66,8 +60,6 @@ instance Exception AJalmot where
pretty err pretty err
& layoutPretty defaultLayoutOptions & layoutPretty defaultLayoutOptions
& renderString & renderString
VMError err -> [i|#{err}|]
EvalError err -> [i|#{err}|]
instance Exception AJalmotCS where instance Exception AJalmotCS where
backtraceDesired = const False backtraceDesired = const False
-4
View File
@@ -1,4 +0,0 @@
module Gyehoek.Language
(
) where
-62
View File
@@ -1,62 +0,0 @@
{-# LANGUAGE DeriveAnyClass #-}
module Gyehoek.Language.Common
(
-- * Syntax
Lib(..)
, LibName(..)
, Exports
, Imports
, ExternName(..)
, Name(..)
) where
import Gyehoek.Prelude
import Data.String (IsString)
import Gyehoek.GenSym (Gen)
import qualified Gyehoek.Sexp as S
-- | R⁷RS 라이프러리의 표현.
data Lib name body = MkLib
{ name :: LibName
, exports :: Exports name
, imports :: Imports name
}
deriving (Show)
newtype LibName = MkLibName { getLibName :: NonEmpty Text }
deriving stock (Show, Generic, Data, Eq)
deriving anyclass (NFData, Hashable)
-- | A map whose keys are names of library definitions and whose
-- values are the names they are exported as. An @⟨identifier⟩@ @x@
-- corresponds to an entry @(⟨identifier⟩, ⟨identifier⟩)@, while a
-- @(rename ⟨identifier₁⟩ ⟨identifier₂⟩)@ form corresponds to an entry
-- @(⟨identifier₁⟩, ⟨identifier₂⟩)@.
type Exports name = HashMap name ExternName
-- | A map whose keys are symbols to be brought into the library's
-- environment and whose values are pairs of the library in which the
-- symbol is defined and the name the symbol is exported as.
type Imports name = HashMap name (LibName, ExternName)
-- | Representation of an identifier at the library boundary. While
-- 'Name's are used within a compilation unit and may be decorated
-- with additional structure and metadata, they are exported as
-- 'ExternName's, which are essentially just plain strings.
newtype ExternName = MkExternName { getExternName :: Text }
deriving newtype (Show)
newtype Name = MkName { inner :: Text }
deriving newtype (Show, Eq, Ord, IsString, Gen, Hashable)
deriving stock (Generic, Data)
deriving anyclass (Wrapped, NFData)
instance Prefixed Name where
prefixed (MkName s) = _Wrapped' . prefixed @Text s . from _Wrapped'
--- DatumIsos
instance S.DatumIso Name where
datumIso = S.symbol >>> S.iso coerce coerce
+10 -22
View File
@@ -13,13 +13,14 @@ import Data.Foldable
import Gyehoek.Prelude hiding (argument) import Gyehoek.Prelude hiding (argument)
data Runtime = Wasm | CPS | HigherOrderCPS data Runtime = Stackify | Wasm | CPS
deriving (Show, Generic, Eq) deriving (Show, Generic, Eq)
data Language data Language
= LanguageScheme = LanguageScheme
| LanguageCPS | LanguageCPS
| LanguageClosed | LanguageClosed
| LanguageStackified
| LanguageWasm | LanguageWasm
deriving (Show, Generic, Eq) deriving (Show, Generic, Eq)
@@ -27,30 +28,29 @@ data Options = MkOptions
{ dumpClosed :: Bool { dumpClosed :: Bool
, dumpCPS :: Bool , dumpCPS :: Bool
, dumpParsed :: Bool , dumpParsed :: Bool
, dumpHoisted :: Bool , dumpStackified :: Bool
, noColour :: Bool
, runtime :: Maybe Runtime , runtime :: Maybe Runtime
, inspectWasm :: Bool , inspectWasm :: Bool
, output :: FilePath , output :: FilePath
, sourceFile :: FilePath , sourceFile :: FilePath
, sourceLanguage :: Language , sourceLanguage :: Language
, targetLanguage :: Language
} }
deriving (Show, Generic) deriving (Show, Generic)
languageValues = ["scheme","cps","closed","wasm"] languageValues = ["scheme","cps","closed","stackified","wasm"]
languageReader = maybeReader \case languageReader = maybeReader \case
"scheme" -> Just LanguageScheme "scheme" -> Just LanguageScheme
"cps" -> Just LanguageCPS "cps" -> Just LanguageCPS
"closed" -> Just LanguageClosed "closed" -> Just LanguageClosed
"stackified" -> Just LanguageStackified
"wasm" -> Just LanguageWasm "wasm" -> Just LanguageWasm
_ -> Nothing _ -> Nothing
runtimeValues = ["stackify","wasm","cps","none"] runtimeValues = ["stackify","wasm","cps","none"]
runtimeReader = maybeReader \case runtimeReader = maybeReader \case
"stackify" -> Just (Just Stackify)
"wasm" -> Just (Just Wasm) "wasm" -> Just (Just Wasm)
"cps1" -> Just (Just CPS) "cps" -> Just (Just CPS)
("cps";"higher-order-cps") -> Just (Just HigherOrderCPS)
"none" -> Just Nothing "none" -> Just Nothing
_ -> Nothing _ -> Nothing
@@ -58,19 +58,15 @@ parser :: Parser Options
parser = do parser = do
dumpClosed <- switch (long "dump-closed") dumpClosed <- switch (long "dump-closed")
dumpCPS <- switch (long "dump-cps") dumpCPS <- switch (long "dump-cps")
dumpStackified <- switch (long "dump-stackified")
dumpParsed <- switch (long "dump-parsed") dumpParsed <- switch (long "dump-parsed")
dumpHoisted <- switch (long "dump-hoisted")
noColour <- switch . fold $
[ long "no-colour"
, long "no-color"
]
inspectWasm <- switch $ long "inspect-wasm" <> short 'p' inspectWasm <- switch $ long "inspect-wasm" <> short 'p'
runtime <- option runtimeReader . fold $ runtime <- option runtimeReader . fold $
[ long "runtime" [ long "runtime"
, short 'R' , short 'R'
, value (Just HigherOrderCPS) , value (Just Stackify)
, completeWith runtimeValues , completeWith runtimeValues
, showDefaultWith $ const "higher-order-cps" , showDefaultWith $ const "stackify"
, metavar "RUNTIME" , metavar "RUNTIME"
] ]
sourceLanguage <- option languageReader . fold $ sourceLanguage <- option languageReader . fold $
@@ -81,14 +77,6 @@ parser = do
, showDefaultWith $ const "scheme" , showDefaultWith $ const "scheme"
, metavar "LANGUAGE" , metavar "LANGUAGE"
] ]
targetLanguage <- option languageReader . fold $
[ long "target"
, short 'T'
, value LanguageCPS
, completeWith languageValues
, showDefaultWith $ const "cps"
, metavar "LANGUAGE"
]
output <- strOption . fold $ output <- strOption . fold $
[ long "output" [ long "output"
, short 'o' , short 'o'
-2
View File
@@ -19,7 +19,6 @@ module Gyehoek.Prelude
, (>>>) , (>>>)
, (>=>) , (>=>)
, (<=<) , (<=<)
, wrappedIso
) where ) where
import Control.Lens hiding (List, (:<)) import Control.Lens hiding (List, (:<))
@@ -41,5 +40,4 @@ import Data.List.NonEmpty (NonEmpty((:|)))
import Numeric.Natural (Natural) import Numeric.Natural (Natural)
import Control.Category ((>>>)) import Control.Category ((>>>))
import Control.Monad import Control.Monad
import Data.Generics.Wrapped (Wrapped(..))
-536
View File
@@ -1,536 +0,0 @@
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TypeFamilies #-}
module Gyehoek.Scheme.Expand
(
) where
import Gyehoek.Sexp.Syntax
import Gyehoek.Sexp qualified as S
import Gyehoek.Prelude
import Gyehoek.Scheme.Syntax hiding (Prim(..))
import Gyehoek.Scheme.Syntax qualified as Scm
import qualified Data.HashSet as HS
import qualified Data.HashMap.Strict as H
import Data.Foldable
import Data.These
import Data.Zip
import Prelude hiding (filter, zip, mapMaybe)
import qualified Data.List.NonEmpty as NE
import Data.Monoid (Ap(Ap, getAp))
import Gyehoek.Sexp.Grammar.Base ((:-)(..))
import Control.Lens.Extras (is)
import Control.Applicative (Alternative(..))
import Gyehoek.GenSym
import Data.HashSet.Lens (setOf)
import Data.List (sort)
import GHC.Exts (IsList(..))
import Gyehoek.Jalmot
import Data.Maybe (isNothing)
import Data.Kind (Type)
import Data.Traversable (for, mapAccumR)
import Effectful.State.Dynamic
import Witherable
import Data.Semigroup (Arg(..))
import Data.Ord (Down(..))
import qualified Data.Scientific as Sci
data Bind
= BindLexical { symbol :: Name, identity :: Natural }
| BindGlobal { symbol :: Name }
deriving stock (Generic, Eq, Show)
deriving anyclass (Hashable)
newtype Scope = MkScope { identity :: Natural }
deriving stock (Show, Generic, Eq, Ord)
deriving anyclass (Hashable)
deriving newtype (Gen)
type ScopeSet = HashSet Scope
data Formals
= FormalsFixed (List Name)
| FormalsRest (List Name) Name
deriving (Show, Generic)
data PrimLambda e = MkPrimLambda Formals (List e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data PrimLet e = MkPrimLet (Maybe Name) (List (Name, e)) (List e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data PrimIf e = MkPrimIf e e (Maybe e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data PrimLetSyntax e = MkPrimLetSyntax (List (Name, e)) (List e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data Prim e
= PrimLambda (PrimLambda e)
| PrimLet (PrimLet e)
| PrimIf (PrimIf e)
| PrimLetSyntax (PrimLetSyntax e)
| PrimSyntaxRules Trans
deriving (Show, Generic)
data Key = MkKey Name (HashSet Scope)
deriving stock (Show, Generic, Eq)
deriving anyclass (Hashable)
instance S.DatumIso Scope where
datumIso = S.with (S.datumIso >>>)
hashSetGrammar
:: forall a. (Hashable a, Ord a)
=> S.DatumGrammar a -> S.DatumGrammar (HashSet a)
hashSetGrammar g =
S.list (S.rest g)
>>> S.iso HS.fromList (sort . HS.toList)
instance S.DatumIso Key where
datumIso = S.with \g ->
S.list
( S.el (S.sym "@")
>>> S.el (S.datumIso @Name)
>>> S.el (hashSetGrammar S.datumIso)
)
>>> g
instance S.DatumIso Formals where
datumIso = S.match
$ S.With (fixed >>>)
$ S.With (rest >>>)
$ S.End
where
fixed :: S.G (Datum :- t) (List Name :- t)
fixed = S.list $ S.rest $ S.datumIso @Name
rest :: S.G (Datum :- t) (Name :- List Name :- t)
rest = S.coproduct
[ S.dottedList (S.rest $ S.datumIso @Name) (S.datumIso @Name)
, S.datumIso @Name >>> S.onTail (S.push [] null (const mempty))
]
optEl
:: S.G (S.Datum :- t) (a :- t)
-> S.G (S.ListContext :- t) (S.ListContext :- Maybe a :- t)
optEl g =
S.coproduct
[ S.el g >>> S.onTail (S.partialIso Just \case
Nothing -> Left mempty
Just x -> Right x)
, S.onTail $ S.push Nothing isNothing (const mempty)
]
anykw :: Text -> S.G (Datum :- t) t
anykw s = S.Flip $ S.PartialIso
(\t -> adorn SynBuiltin (Symbol s) :- t)
\case
(Symbol _ :- t) -> Right t
_ -> Left $ S.expected "symbol"
prim_if :: S.DatumIso e => S.DatumGrammar (PrimIf e)
prim_if = S.with \g ->
S.listWithStyle (StyleSyntax 1)
(S.el (anykw "if")
>>> S.el S.datumIso
>>> S.el S.datumIso
>>> optEl S.datumIso)
>>> g
prim_lambda :: S.DatumIso e => S.DatumGrammar (PrimLambda e)
prim_lambda = S.with \g ->
S.lambdaLike (anykw "λ") (S.datumIso @Formals) (S.rest S.datumIso)
>>> g
prim_let
:: forall e. S.DatumIso e => S.DatumGrammar (PrimLet e)
prim_let = S.with \g ->
(S.listWithStyle (StyleSyntax 1) $
S.el (anykw "let")
>>> optEl (S.datumIso @Name)
>>> S.el (S.list $ S.rest $ S.datumIso @(Name,e))
>>> S.rest S.datumIso)
>>> g
prim_let_syntax :: S.DatumIso e => S.DatumGrammar (PrimLetSyntax e)
prim_let_syntax = S.with \g ->
(S.listWithStyle (StyleSyntax 1) $
S.el (anykw "let-syntax")
>>> S.el (S.list $ S.rest $ S.datumIso)
>>> S.rest S.datumIso)
>>> g
instance S.DatumIso Bind where
datumIso = S.match
$ S.With (\g ->
S.list (S.el (S.sym "L") >>> S.el S.datumIso >>> S.el S.datumIso)
>>> g)
$ S.With (\g ->
S.list (S.el (S.sym "G") >>> S.el S.datumIso)
>>> g)
$ S.End
{- |
* Examples
>>> :set -XTemplateHaskellQuotes
>>> pat = S.makeSx [|| S.fromDatumUnsafe @Pat S.datumIso ||]
>>> match [] [pat|(x . y)|] [S.sx|(1 2 . 2)|]
Nothing
>>> match [] [pat|(x . y)|] [S.sx|(1 . 2)|]
Just
...
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 => 2)|]
Just
...
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 -> 2)|]
Nothing
-}
match :: Foldable f
=> f Text
-- ^ Literal keywords
-> Pat
-> Datum
-> Maybe (HashMap Name Datum)
match kws p = getAp . match' p where
match' :: Pat -> Datum -> Ap Maybe (HashMap Name Datum)
match' PatWildcard _ = pure mempty
match' (PatVar x) e
| coerce x `elem` kws = case e of
Symbol x' | coerce x == x' -> pure mempty
_ -> empty
| otherwise = pure $ H.singleton x e
match' (PatList ps Nothing Nothing) (List es) = matches ps es
match' (PatList ps (Just []) Nothing) (List es) = do
let (ps',p) = ps ^?! _Snoc
(r,rest) <- fold $ alignWith f ps' es
rest' <- rest
& fmap (fmap (fmap (:[])) . match' p)
& foldr (liftA2 $ H.unionWith (<>)) mempty
& fmap (fmap List)
pure $ r <> rest'
where
f (These a b) = (,[]) <$> match' a b
f (This a) = empty
f (That b) = pure (mempty,[b])
match' (PatList ps Nothing (Just p)) (DotList es e) =
matches (p:|ps) (NE.cons e es)
match' _ _ = _
matchPrefix
:: (Semialign f, Foldable f)
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum, List Datum)
matchPrefix ps es = fold $ alignWith f ps es
where
f (These a b) = (,[]) <$> match' a b
f (This a) = empty
f (That b) = pure (mempty,[b])
matches
:: (Semialign f, Foldable f)
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum)
matches ps es = fold $ alignWith matchThese ps es
matchThese (These a b) = match' a b
matchThese _ = empty
-- | this hashmap is "curried" since we often want to traverse the
-- entire set of scopes associated with a given symbol.
newtype SymTable = MkSymTable
{ curried :: HashMap Name (HashMap (HashSet Scope) Bind) }
deriving (Show, Generic)
type instance Index SymTable = Name
type instance IxValue SymTable = HashMap (HashSet Scope) Bind
instance Ixed SymTable where ix j = #curried . ix j
instance At SymTable where at j = #curried . at j
instance Semigroup SymTable where
MkSymTable c1 <> MkSymTable c2 = MkSymTable $ H.unionWith (<>) c1 c2
instance Monoid SymTable where mempty = MkSymTable mempty
type Expand es = (State SymTable :> es, GenSym :> es)
runExpand :: SymTable -> Eff (State SymTable : GenSym : es) a -> Eff es (a, SymTable)
runExpand tab = runGenSym . runStateLocal tab
testExpand :: Datum -> IO (CommandOrDef, SymTable)
testExpand = runJalmotIO . runExpand (symTableOfEnv env_scheme_base)
. expand env_scheme_base mempty
-- | denotations
data Denot
= DenotVar
| DenotMacro Trans
| DenotPrim Name
| DenotSyntax Datum
| DenotKeyword Name
deriving stock (Show, Generic)
newtype Env = MkEnv { names :: HashMap Bind Denot }
deriving stock (Show, Generic)
deriving newtype (Semigroup, Monoid)
-- | @(environment '(scheme base))@
env_scheme_base :: Env
env_scheme_base = MkEnv . fromList . fold $
[ [ (BindGlobal p, DenotPrim p) | p <- prims ]
, [ (BindGlobal "λ", DenotPrim "lambda")
]
]
where
prims =
[ "if"
, "lambda"
, "let"
, "let-syntax"
]
symTableOfEnv :: Env -> SymTable
symTableOfEnv = ifoldMapOf (#names . itraversed) \cases
b@(BindGlobal n) d -> binding n mempty b
type instance IxValue Env = Denot
type instance Index Env = Bind
instance Ixed Env where ix j = #names . ix j
instance At Env where at j = #names . at j
err :: Jalmot :> es => Text -> Eff es a
err = throwError . EvalError
run :: S.DatumGrammar a -> Datum -> Maybe a
run g = preview #_Right . runPureEff . runJalmot . S.fromDatum g
parsePrim :: S.DatumIso e => Name -> Datum -> Maybe (Prim e)
parsePrim primname d = case primname of
"let" -> PrimLet <$> run prim_let d
"let-syntax" -> PrimLetSyntax <$> run prim_let_syntax d
"lambda" -> PrimLambda <$> run prim_lambda d
"if" -> PrimIf <$> run prim_if d
emit :: (Monoid m, State m :> es) => m -> Eff es ()
emit x = modify (<> x)
binding :: Name -> HashSet Scope -> Bind -> SymTable
binding sym scopes = MkSymTable . H.singleton sym . H.singleton scopes
envOfVar :: Bind -> Env
envOfVar = MkEnv . flip H.singleton DenotVar
gensymLexical :: GenSym :> es => Name -> Eff es Bind
gensymLexical symbol = do
identity <- gensym
pure $ BindLexical {symbol,identity}
lookupSymbol
:: forall es. (State SymTable :> es, Jalmot :> es)
=> Env -> HashSet Scope -> Name -> Eff es (Bind, Denot)
lookupSymbol g scopes x = do
ss <- fmap fold . preuse @SymTable @(Eff es) $ ix x
case nearest (MkKey x scopes) (H.keys ss) of
[] -> err [i|심벌 #{x}는 정의되지 않다|]
(_:_:_) -> err [i|심벌 #{x}는 모호하다|]
[s] | Just b <- ss ^? ix s
, Just d <- g ^? ix b -> pure (b,d)
| otherwise -> error "unreachable"
{- | Given a 'Key' and a collection of 'ScopeSet's, filter that
collection down to the largest subsets of the 'Key'\'s scope set.
This is our analogue of the lexical scoping rule which chooses the
"nearest" binding of a variable when shadowing occurs.
- If no qualifying subsets are found, the name is not in scope.
- If one subset is found, we're on the happy path!
- If more than one subset is found, we're on the rarest and
saddest path: the reference is ambiguous.
* Examples
> (let ((x {A} 123))
> (λ (x {A,B})
> (let ((y {A,B,C} 456))
> x {A,B,C})))
>>> :seti -XOverloadedLists
>>> :{
nearest
(MkKey "x" [MkScope 0, MkScope 1, MkScope 2])
[ [MkScope 0]
, [MkScope 0, MkScope 1] ]
:}
-}
nearest :: (Traversable f, Filterable f) => Key -> f ScopeSet -> List ScopeSet
nearest (MkKey symbol scopes) =
mapMaybe (\x ->
if x `HS.isSubsetOf` scopes
then Just . Down $ Arg (length x) x
else Nothing)
-- 나쁨. 안 좋다. 안 좋아하야.
>>> Data.Foldable.toList >>> sort
>>> foldr (\cases
x [] -> [x]
x acc@(y:_) -> case x `compare` y of
LT -> acc
EQ -> x:acc
GT -> [x])
[]
>>> fmap (\(Down (Arg _ x)) -> x)
expand
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Datum -> Eff es CommandOrDef
expand g scopes datum@(List (Symbol s : es)) = do
let s' = MkName s
(_,denot) <- lookupSymbol g scopes s'
case denot of
DenotVar -> Command . ExpApply (ExpVar s')
<$> traverse (expandAsExp g scopes) es
DenotPrim x -> expandPrim g scopes x datum
DenotMacro trans -> expandMacro g scopes trans datum
-- 신기하지 않은 경우들
expand g scopes datum = case datum of
List (x:xs) -> Command <$> (ExpApply <$> go x <*> traverse go xs)
Symbol s -> pure . Command . ExpVar . MkName $ s
Boolean b -> lit $ LitBool b
Number (Sci.floatingOrInteger -> Right n) -> lit $ LitInt n
where
go = expandAsExp g scopes
lit = pure . Command . ExpLit
expandMacro
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Trans -> Datum -> Eff es CommandOrDef
expandMacro g scopes trans datum = _
expandPrim
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Name -> Datum -> Eff es CommandOrDef
expandPrim g scopes primName datum
| Just prim <- parsePrim @Datum primName datum
= case prim of
PrimLetSyntax (MkPrimLetSyntax bs [body]) -> do
scopes' <- flip HS.insert scopes <$> gensym
(rhss,g') <- (_2 %~ (g<>) . fold) . Prelude.unzip <$> for bs \(x,trans) ->
expandAsExp g scopes trans >>= \case
ExpSyntaxRules trans' ->
(trans',) <$> bindLexical scopes' x (DenotMacro trans')
e -> err [i|syntax-rules를 원하는데 이것 받는다: #{e}|]
Command <$> (ExpLetSyntax
(zip (bs ^.. each . _1) rhss)
<$> expandAsExp g' scopes' body
)
PrimLet (MkPrimLet Nothing bs [body]) -> do
scopes' <- flip HS.insert scopes <$> gensym
g' <- (g<>) . fold <$> for (bs ^.. each . _1) \x ->
bindLexical scopes' x DenotVar
Command <$> (ExpLet
<$> traverseOf (each . _2) (expandAsExp g scopes) bs
<*> expandAsExp g scopes' body)
PrimLambda (MkPrimLambda (FormalsFixed xs) [body]) -> do
scopes' <- flip HS.insert scopes <$> gensym
g' <- (g<>) . fold <$> for xs \x -> bindLexical scopes' x DenotVar
Command . ExpLambda xs <$> expandAsExp g' scopes' body
PrimIf (MkPrimIf c t f) ->
Command <$> (ExpIf
<$> expandAsExp g scopes c
<*> expandAsExp g scopes t
<*> traverse (expandAsExp g scopes) f
)
| otherwise = err [i|prim #{primName}에 잘못한 신택스: #{datum}|]
expandAsExp
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Datum -> Eff es Exp
expandAsExp g scopes d = expand g scopes d >>= intoExp
bindLexical :: Expand es => ScopeSet -> Name -> Denot -> Eff es Env
bindLexical scopes symbol denot = do
identity <- gensym
let bind = BindLexical {symbol,identity}
modify (<> binding symbol scopes bind)
pure $ MkEnv (H.singleton bind denot)
intoExp :: Jalmot :> es => CommandOrDef -> Eff es Exp
intoExp (Command e) = pure e
intoExp (Begin es) = traverse intoExp es >>= \case
[] -> err "begin expression은 빔"
(x:xs) -> pure . ExpBegin $ x NE.:| xs
trans_when :: Trans
trans_when = MkTrans
{ ellipsis = "..."
, keywords = []
, rules =
[ MkRule
(PatList
[ PatVar "when"
, PatVar "test"
, PatVar "body" ]
(Just [])
Nothing)
(TemList
[ El $ TemVar "if"
, El $ TemVar "test"
, El $ TemList
[ El $ TemVar "begin"
, Ellipsis $ TemVar "body"
]
Nothing
]
Nothing)
]
}
trans_and :: Trans
trans_and = MkTrans
{ ellipsis = "..."
, keywords = []
, rules =
[ MkRule
(PatList
[PatVar "and"]
Nothing
Nothing)
(TemLit (LitBool True))
, MkRule
(PatList
[PatVar "and", PatVar "x"]
Nothing
Nothing)
(TemVar "x")
, MkRule
(PatList
[PatVar "and", PatVar "x", PatVar "y"]
(Just [])
Nothing)
(TemList
[ El (TemVar "if")
, El (TemVar "x")
, El (TemList
[ El (TemVar "and")
, Ellipsis (TemVar "y")
]
Nothing)
, El . TemLit . LitBool $ False
]
Nothing)
]
}
-536
View File
@@ -1,536 +0,0 @@
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TypeFamilies #-}
module Gyehoek.Scheme.Expand.Old
(
) where
import Gyehoek.Sexp.Syntax
import Gyehoek.Sexp qualified as S
import Gyehoek.Prelude
import Gyehoek.Scheme.Syntax hiding (Prim(..))
import Gyehoek.Scheme.Syntax qualified as Scm
import qualified Data.HashSet as HS
import qualified Data.HashMap.Strict as H
import Data.Foldable
import Data.These
import Data.Zip
import Prelude hiding (filter, zip, mapMaybe)
import qualified Data.List.NonEmpty as NE
import Data.Monoid (Ap(Ap, getAp))
import Gyehoek.Sexp.Grammar.Base ((:-)(..))
import Control.Lens.Extras (is)
import Control.Applicative (Alternative(..))
import Gyehoek.GenSym
import Data.HashSet.Lens (setOf)
import Data.List (sort)
import GHC.Exts (IsList(..))
import Gyehoek.Jalmot
import Data.Maybe (isNothing)
import Data.Kind (Type)
import Data.Traversable (for, mapAccumR)
import Effectful.State.Dynamic
import Witherable
import Data.Semigroup (Arg(..))
import Data.Ord (Down(..))
import qualified Data.Scientific as Sci
data Bind
= BindLexical { symbol :: Name, identity :: Natural }
| BindGlobal { symbol :: Name }
deriving stock (Generic, Eq, Show)
deriving anyclass (Hashable)
newtype Scope = MkScope { identity :: Natural }
deriving stock (Show, Generic, Eq, Ord)
deriving anyclass (Hashable)
deriving newtype (Gen)
type ScopeSet = HashSet Scope
data Formals
= FormalsFixed (List Name)
| FormalsRest (List Name) Name
deriving (Show, Generic)
data PrimLambda e = MkPrimLambda Formals (List e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data PrimLet e = MkPrimLet (Maybe Name) (List (Name, e)) (List e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data PrimIf e = MkPrimIf e e (Maybe e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data PrimLetSyntax e = MkPrimLetSyntax (List (Name, e)) (List e)
deriving (Show, Generic, Functor, Foldable, Traversable)
data Prim e
= PrimLambda (PrimLambda e)
| PrimLet (PrimLet e)
| PrimIf (PrimIf e)
| PrimLetSyntax (PrimLetSyntax e)
| PrimSyntaxRules Trans
deriving (Show, Generic)
data Key = MkKey Name (HashSet Scope)
deriving stock (Show, Generic, Eq)
deriving anyclass (Hashable)
instance S.DatumIso Scope where
datumIso = S.with (S.datumIso >>>)
hashSetGrammar
:: forall a. (Hashable a, Ord a)
=> S.DatumGrammar a -> S.DatumGrammar (HashSet a)
hashSetGrammar g =
S.list (S.rest g)
>>> S.iso HS.fromList (sort . HS.toList)
instance S.DatumIso Key where
datumIso = S.with \g ->
S.list
( S.el (S.sym "@")
>>> S.el (S.datumIso @Name)
>>> S.el (hashSetGrammar S.datumIso)
)
>>> g
instance S.DatumIso Formals where
datumIso = S.match
$ S.With (fixed >>>)
$ S.With (rest >>>)
$ S.End
where
fixed :: S.G (Datum :- t) (List Name :- t)
fixed = S.list $ S.rest $ S.datumIso @Name
rest :: S.G (Datum :- t) (Name :- List Name :- t)
rest = S.coproduct
[ S.dottedList (S.rest $ S.datumIso @Name) (S.datumIso @Name)
, S.datumIso @Name >>> S.onTail (S.push [] null (const mempty))
]
optEl
:: S.G (S.Datum :- t) (a :- t)
-> S.G (S.ListContext :- t) (S.ListContext :- Maybe a :- t)
optEl g =
S.coproduct
[ S.el g >>> S.onTail (S.partialIso Just \case
Nothing -> Left mempty
Just x -> Right x)
, S.onTail $ S.push Nothing isNothing (const mempty)
]
anykw :: Text -> S.G (Datum :- t) t
anykw s = S.Flip $ S.PartialIso
(\t -> adorn SynBuiltin (Symbol s) :- t)
\case
(Symbol _ :- t) -> Right t
_ -> Left $ S.expected "symbol"
prim_if :: S.DatumIso e => S.DatumGrammar (PrimIf e)
prim_if = S.with \g ->
S.listWithStyle (StyleSyntax 1)
(S.el (anykw "if")
>>> S.el S.datumIso
>>> S.el S.datumIso
>>> optEl S.datumIso)
>>> g
prim_lambda :: S.DatumIso e => S.DatumGrammar (PrimLambda e)
prim_lambda = S.with \g ->
S.lambdaLike (anykw "λ") (S.datumIso @Formals) (S.rest S.datumIso)
>>> g
prim_let
:: forall e. S.DatumIso e => S.DatumGrammar (PrimLet e)
prim_let = S.with \g ->
(S.listWithStyle (StyleSyntax 1) $
S.el (anykw "let")
>>> optEl (S.datumIso @Name)
>>> S.el (S.list $ S.rest $ S.datumIso @(Name,e))
>>> S.rest S.datumIso)
>>> g
prim_let_syntax :: S.DatumIso e => S.DatumGrammar (PrimLetSyntax e)
prim_let_syntax = S.with \g ->
(S.listWithStyle (StyleSyntax 1) $
S.el (anykw "let-syntax")
>>> S.el (S.list $ S.rest $ S.datumIso)
>>> S.rest S.datumIso)
>>> g
instance S.DatumIso Bind where
datumIso = S.match
$ S.With (\g ->
S.list (S.el (S.sym "L") >>> S.el S.datumIso >>> S.el S.datumIso)
>>> g)
$ S.With (\g ->
S.list (S.el (S.sym "G") >>> S.el S.datumIso)
>>> g)
$ S.End
{- |
* Examples
>>> :set -XTemplateHaskellQuotes
>>> pat = S.makeSx [|| S.fromDatumUnsafe @Pat S.datumIso ||]
>>> match [] [pat|(x . y)|] [S.sx|(1 2 . 2)|]
Nothing
>>> match [] [pat|(x . y)|] [S.sx|(1 . 2)|]
Just
...
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 => 2)|]
Just
...
>>> match ["=>"] [pat|(x => y)|] [S.sx|(1 -> 2)|]
Nothing
-}
match :: Foldable f
=> f Text
-- ^ Literal keywords
-> Pat
-> Datum
-> Maybe (HashMap Name Datum)
match kws p = getAp . match' p where
match' :: Pat -> Datum -> Ap Maybe (HashMap Name Datum)
match' PatWildcard _ = pure mempty
match' (PatVar x) e
| coerce x `elem` kws = case e of
Symbol x' | coerce x == x' -> pure mempty
_ -> empty
| otherwise = pure $ H.singleton x e
match' (PatList ps Nothing Nothing) (List es) = matches ps es
match' (PatList ps (Just []) Nothing) (List es) = do
let (ps',p) = ps ^?! _Snoc
(r,rest) <- fold $ alignWith f ps' es
rest' <- rest
& fmap (fmap (fmap (:[])) . match' p)
& foldr (liftA2 $ H.unionWith (<>)) mempty
& fmap (fmap List)
pure $ r <> rest'
where
f (These a b) = (,[]) <$> match' a b
f (This a) = empty
f (That b) = pure (mempty,[b])
match' (PatList ps Nothing (Just p)) (DotList es e) =
matches (p:|ps) (NE.cons e es)
match' _ _ = _
matchPrefix
:: (Semialign f, Foldable f)
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum, List Datum)
matchPrefix ps es = fold $ alignWith f ps es
where
f (These a b) = (,[]) <$> match' a b
f (This a) = empty
f (That b) = pure (mempty,[b])
matches
:: (Semialign f, Foldable f)
=> f Pat -> f Datum -> Ap Maybe (HashMap Name Datum)
matches ps es = fold $ alignWith matchThese ps es
matchThese (These a b) = match' a b
matchThese _ = empty
-- | this hashmap is "curried" since we often want to traverse the
-- entire set of scopes associated with a given symbol.
newtype SymTable = MkSymTable
{ curried :: HashMap Name (HashMap (HashSet Scope) Bind) }
deriving (Show, Generic)
type instance Index SymTable = Name
type instance IxValue SymTable = HashMap (HashSet Scope) Bind
instance Ixed SymTable where ix j = #curried . ix j
instance At SymTable where at j = #curried . at j
instance Semigroup SymTable where
MkSymTable c1 <> MkSymTable c2 = MkSymTable $ H.unionWith (<>) c1 c2
instance Monoid SymTable where mempty = MkSymTable mempty
type Expand es = (State SymTable :> es, GenSym :> es)
runExpand :: SymTable -> Eff (State SymTable : GenSym : es) a -> Eff es (a, SymTable)
runExpand tab = runGenSym . runStateLocal tab
testExpand :: Datum -> IO (CommandOrDef, SymTable)
testExpand = runJalmotIO . runExpand (symTableOfEnv env_scheme_base)
. expand env_scheme_base mempty
-- | denotations
data Denot
= DenotVar
| DenotMacro Trans
| DenotPrim Name
| DenotSyntax Datum
| DenotKeyword Name
deriving stock (Show, Generic)
newtype Env = MkEnv { names :: HashMap Bind Denot }
deriving stock (Show, Generic)
deriving newtype (Semigroup, Monoid)
-- | @(environment '(scheme base))@
env_scheme_base :: Env
env_scheme_base = MkEnv . fromList . fold $
[ [ (BindGlobal p, DenotPrim p) | p <- prims ]
, [ (BindGlobal "λ", DenotPrim "lambda")
]
]
where
prims =
[ "if"
, "lambda"
, "let"
, "let-syntax"
]
symTableOfEnv :: Env -> SymTable
symTableOfEnv = ifoldMapOf (#names . itraversed) \cases
b@(BindGlobal n) d -> binding n mempty b
type instance IxValue Env = Denot
type instance Index Env = Bind
instance Ixed Env where ix j = #names . ix j
instance At Env where at j = #names . at j
err :: Jalmot :> es => Text -> Eff es a
err = throwError . EvalError
run :: S.DatumGrammar a -> Datum -> Maybe a
run g = preview #_Right . runPureEff . runJalmot . S.fromDatum g
parsePrim :: S.DatumIso e => Name -> Datum -> Maybe (Prim e)
parsePrim primname d = case primname of
"let" -> PrimLet <$> run prim_let d
"let-syntax" -> PrimLetSyntax <$> run prim_let_syntax d
"lambda" -> PrimLambda <$> run prim_lambda d
"if" -> PrimIf <$> run prim_if d
emit :: (Monoid m, State m :> es) => m -> Eff es ()
emit x = modify (<> x)
binding :: Name -> HashSet Scope -> Bind -> SymTable
binding sym scopes = MkSymTable . H.singleton sym . H.singleton scopes
envOfVar :: Bind -> Env
envOfVar = MkEnv . flip H.singleton DenotVar
gensymLexical :: GenSym :> es => Name -> Eff es Bind
gensymLexical symbol = do
identity <- gensym
pure $ BindLexical {symbol,identity}
lookupSymbol
:: forall es. (State SymTable :> es, Jalmot :> es)
=> Env -> HashSet Scope -> Name -> Eff es (Bind, Denot)
lookupSymbol g scopes x = do
ss <- fmap fold . preuse @SymTable @(Eff es) $ ix x
case nearest (MkKey x scopes) (H.keys ss) of
[] -> err [i|심벌 #{x}는 정의되지 않다|]
(_:_:_) -> err [i|심벌 #{x}는 모호하다|]
[s] | Just b <- ss ^? ix s
, Just d <- g ^? ix b -> pure (b,d)
| otherwise -> error "unreachable"
{- | Given a 'Key' and a collection of 'ScopeSet's, filter that
collection down to the largest subsets of the 'Key'\'s scope set.
This is our analogue of the lexical scoping rule which chooses the
"nearest" binding of a variable when shadowing occurs.
- If no qualifying subsets are found, the name is not in scope.
- If one subset is found, we're on the happy path!
- If more than one subset is found, we're on the rarest and
saddest path: the reference is ambiguous.
* Examples
> (let ((x {A} 123))
> (λ (x {A,B})
> (let ((y {A,B,C} 456))
> x {A,B,C})))
>>> :seti -XOverloadedLists
>>> :{
nearest
(MkKey "x" [MkScope 0, MkScope 1, MkScope 2])
[ [MkScope 0]
, [MkScope 0, MkScope 1] ]
:}
-}
nearest :: (Traversable f, Filterable f) => Key -> f ScopeSet -> List ScopeSet
nearest (MkKey symbol scopes) =
mapMaybe (\x ->
if x `HS.isSubsetOf` scopes
then Just . Down $ Arg (length x) x
else Nothing)
-- 나쁨. 안 좋다. 안 좋아하야.
>>> Data.Foldable.toList >>> sort
>>> foldr (\cases
x [] -> [x]
x acc@(y:_) -> case x `compare` y of
LT -> acc
EQ -> x:acc
GT -> [x])
[]
>>> fmap (\(Down (Arg _ x)) -> x)
expand
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Datum -> Eff es CommandOrDef
expand g scopes datum@(List (Symbol s : es)) = do
let s' = MkName s
(_,denot) <- lookupSymbol g scopes s'
case denot of
DenotVar -> Command . ExpApply (ExpVar s')
<$> traverse (expandAsExp g scopes) es
DenotPrim x -> expandPrim g scopes x datum
DenotMacro trans -> expandMacro g scopes trans datum
-- 신기하지 않은 경우들
expand g scopes datum = case datum of
List (x:xs) -> Command <$> (ExpApply <$> go x <*> traverse go xs)
Symbol s -> pure . Command . ExpVar . MkName $ s
Boolean b -> lit $ LitBool b
Number (Sci.floatingOrInteger -> Right n) -> lit $ LitInt n
where
go = expandAsExp g scopes
lit = pure . Command . ExpLit
expandMacro
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Trans -> Datum -> Eff es CommandOrDef
expandMacro g scopes trans datum = _
expandPrim
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Name -> Datum -> Eff es CommandOrDef
expandPrim g scopes primName datum
| Just prim <- parsePrim @Datum primName datum
= case prim of
PrimLetSyntax (MkPrimLetSyntax bs [body]) -> do
scopes' <- flip HS.insert scopes <$> gensym
(rhss,g') <- (_2 %~ (g<>) . fold) . Prelude.unzip <$> for bs \(x,trans) ->
expandAsExp g scopes trans >>= \case
ExpSyntaxRules trans' ->
(trans',) <$> bindLexical scopes' x (DenotMacro trans')
e -> err [i|syntax-rules를 원하는데 이것 받는다: #{e}|]
Command <$> (ExpLetSyntax
(zip (bs ^.. each . _1) rhss)
<$> expandAsExp g' scopes' body
)
PrimLet (MkPrimLet Nothing bs [body]) -> do
scopes' <- flip HS.insert scopes <$> gensym
g' <- (g<>) . fold <$> for (bs ^.. each . _1) \x ->
bindLexical scopes' x DenotVar
Command <$> (ExpLet
<$> traverseOf (each . _2) (expandAsExp g scopes) bs
<*> expandAsExp g scopes' body)
PrimLambda (MkPrimLambda (FormalsFixed xs) [body]) -> do
scopes' <- flip HS.insert scopes <$> gensym
g' <- (g<>) . fold <$> for xs \x -> bindLexical scopes' x DenotVar
Command . ExpLambda xs <$> expandAsExp g' scopes' body
PrimIf (MkPrimIf c t f) ->
Command <$> (ExpIf
<$> expandAsExp g scopes c
<*> expandAsExp g scopes t
<*> traverse (expandAsExp g scopes) f
)
| otherwise = err [i|prim #{primName}에 잘못한 신택스: #{datum}|]
expandAsExp
:: (Expand es, Jalmot :> es)
=> Env -> ScopeSet -> Datum -> Eff es Exp
expandAsExp g scopes d = expand g scopes d >>= intoExp
bindLexical :: Expand es => ScopeSet -> Name -> Denot -> Eff es Env
bindLexical scopes symbol denot = do
identity <- gensym
let bind = BindLexical {symbol,identity}
modify (<> binding symbol scopes bind)
pure $ MkEnv (H.singleton bind denot)
intoExp :: Jalmot :> es => CommandOrDef -> Eff es Exp
intoExp (Command e) = pure e
intoExp (Begin es) = traverse intoExp es >>= \case
[] -> err "begin expression은 빔"
(x:xs) -> pure . ExpBegin $ x NE.:| xs
trans_when :: Trans
trans_when = MkTrans
{ ellipsis = "..."
, keywords = []
, rules =
[ MkRule
(PatList
[ PatVar "when"
, PatVar "test"
, PatVar "body" ]
(Just [])
Nothing)
(TemList
[ El $ TemVar "if"
, El $ TemVar "test"
, El $ TemList
[ El $ TemVar "begin"
, Ellipsis $ TemVar "body"
]
Nothing
]
Nothing)
]
}
trans_and :: Trans
trans_and = MkTrans
{ ellipsis = "..."
, keywords = []
, rules =
[ MkRule
(PatList
[PatVar "and"]
Nothing
Nothing)
(TemLit (LitBool True))
, MkRule
(PatList
[PatVar "and", PatVar "x"]
Nothing
Nothing)
(TemVar "x")
, MkRule
(PatList
[PatVar "and", PatVar "x", PatVar "y"]
(Just [])
Nothing)
(TemList
[ El (TemVar "if")
, El (TemVar "x")
, El (TemList
[ El (TemVar "and")
, Ellipsis (TemVar "y")
]
Nothing)
, El . TemLit . LitBool $ False
]
Nothing)
]
}
+51 -332
View File
@@ -10,24 +10,16 @@
{-# LANGUAGE OrPatterns #-} {-# LANGUAGE OrPatterns #-}
{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE ViewPatterns #-}
{- HLINT ignore "Avoid lambda using `infix`" -}
{- HLINT ignore "Redundant $" -}
module Gyehoek.Scheme.Syntax module Gyehoek.Scheme.Syntax
( Name(..) ( Name(..)
, Builtin(..) , Prim(..)
, Lit(..) , Lit(..)
, Def(..) , Def(..)
, Exp(..) , Exp(..)
, ExpF(..) , ExpF(..)
, Program(..) , Program(..)
, CommandOrDef(..) , CommandOrDef(..)
, Trans(..) , primDatumIso
, Rule(..)
, Pat(..)
, Tem(..)
, El(..)
, builtinDatumIso
, free , free
, subst , subst
, getName , getName
@@ -38,6 +30,8 @@ module Gyehoek.Scheme.Syntax
) )
where where
import Data.List (intersperse)
import Effectful
import Prelude hiding ((.), id) import Prelude hiding ((.), id)
import Control.Category import Control.Category
import Gyehoek.Sexp qualified as GS import Gyehoek.Sexp qualified as GS
@@ -50,12 +44,15 @@ import Data.Functor.Foldable hiding (fold)
import qualified Data.HashSet as HS import qualified Data.HashSet as HS
import Data.Foldable (fold, toList) import Data.Foldable (fold, toList)
import Language.Haskell.TH.Quote (QuasiQuoter) import Language.Haskell.TH.Quote (QuasiQuoter)
import Effectful.FileSystem (runFileSystem)
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 qualified Data.Set.Ordered as O
import Gyehoek.Sexp.Grammar qualified as Sexp
import Gyehoek.Sexp.Grammar qualified as S import Gyehoek.Sexp.Grammar qualified as S
import Gyehoek.Sexp.Grammar (DatumIso, G, DataIso, (:-)((:-))) import Gyehoek.Sexp.Grammar (DatumIso, DataIso)
import Gyehoek.Prelude import Gyehoek.Prelude
import Control.Lens.Extras (is)
import qualified Data.Scientific as Sci
newtype Name = MkName { inner :: Text } newtype Name = MkName { inner :: Text }
@@ -69,36 +66,28 @@ instance Prefixed Name where
getName :: Name -> Text getName :: Name -> Text
getName (MkName x) = x getName (MkName x) = x
data Builtin e data Prim e
= BuiltinAdd e e = PrimAdd e e
| BuiltinSub e e | PrimSub e e
| BuiltinMul e e | PrimMul e e
| BuiltinDiv e e | PrimDiv e e
| BuiltinCons e e | PrimCons e e
| BuiltinCar e | PrimCar e
| BuiltinCdr e | PrimCdr e
| BuiltinImmediateP e | PrimImmediateP e
| BuiltinConsP e | PrimConsP e
| BuiltinIntegerP e | PrimIntegerP e
| BuiltinWrite e | PrimWrite e
| BuiltinZeroP e | PrimZeroP e
| BuiltinNewline | PrimNewline
| BuiltinMakeClosure { code :: e, env :: List e } | PrimMakeClosure { code :: e, env :: List e }
| BuiltinMakeSharedClosure { codes :: List e, env :: List e } | PrimEnvRef e Int
| BuiltinGetEnv | PrimEnvCode e
| BuiltinEnv | PrimCallCC e
| BuiltinEnvRef Int
| BuiltinCallCC e
| BuiltinCaptureCC
| BuiltinInvokeCC e (List e)
| BuiltinValues (List e)
| BuiltinCallWithValues e e
| BuiltinPairP e
| BuiltinList (List e)
deriving stock (Show, Generic, Functor, Foldable, Traversable, Data, Eq) deriving stock (Show, Generic, Functor, Foldable, Traversable, Data, Eq)
deriving anyclass (NFData) deriving anyclass (NFData)
instance Each (Builtin e) (Builtin e') e e' instance Each (Prim e) (Prim e') e e'
data Lit data Lit
= LitInt Int = LitInt Int
@@ -113,79 +102,19 @@ data Def
deriving stock (Show, Generic, Data) deriving stock (Show, Generic, Data)
deriving anyclass (NFData) deriving anyclass (NFData)
data Formals a
= FormalsFixed (List a)
| FormalsVar (List a) a
deriving stock (Show, Generic, Data, Functor, Foldable, Traversable)
deriving anyclass (NFData)
data Exp data Exp
= ExpLet (List (Name, Exp)) Exp = ExpLet (List (Name, Exp)) Exp
| ExpLetSyntax (List (Name, Trans)) Exp
| ExpLetRec (List (Name, Exp)) Exp | ExpLetRec (List (Name, Exp)) Exp
| ExpBuiltin (Builtin Exp) | ExpPrim (Prim Exp)
| ExpBegin (NonEmpty Exp) | ExpBegin (List Exp)
| ExpIf Exp Exp (Maybe Exp) | ExpIf Exp Exp Exp
| ExpLit Lit | ExpLit Lit
| ExpLambda (List Name) Exp | ExpLambda (List Name) Exp
| ExpVar Name | ExpVar Name
| ExpSyntaxRules Trans
| ExpApply Exp (List Exp) | ExpApply Exp (List Exp)
deriving stock (Show, Generic, Data) deriving stock (Show, Generic, Data)
deriving anyclass (NFData) deriving anyclass (NFData)
data Trans = MkTrans
{ ellipsis :: Name
, keywords :: List Name
, rules :: List Rule
}
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data Rule = MkRule
{ rhs :: Pat
, lhs :: Tem
}
deriving (Show, Generic, Data)
deriving anyclass (NFData)
data Pat
= PatWildcard
| PatVar Name
| PatList
{ init :: List Pat
, ellipsis :: Maybe (List Pat)
, tail :: Maybe Pat
}
-- | PatVec
-- { init :: List Pat
-- , ellipsis :: Maybe (List Pat)
-- }
deriving (Show, Generic, Data)
deriving anyclass (NFData)
data Tem
-- | @(⟨element⟩ …)@
-- @(⟨element⟩ ⟨element⟩ … . ⟨element⟩)@
= TemList
{ init :: List El
, tail :: Maybe Tem
}
-- | @(⟨ellipsis⟩ ⟨template⟩)@
| TemTrail Tem
| TemLit Lit
| TemVar Name
deriving (Show, Generic, Data)
deriving anyclass (NFData)
data El
-- | @⟨template⟩ ⟨ellipsis⟩@
= Ellipsis Tem
-- | @⟨template⟩@
| El Tem
deriving (Show, Generic, Data)
deriving anyclass (NFData)
data CommandOrDef data CommandOrDef
= Command Exp = Command Exp
| Definition Def | Definition Def
@@ -193,37 +122,8 @@ data CommandOrDef
deriving stock (Show, Generic, Data) deriving stock (Show, Generic, Data)
deriving anyclass (NFData) deriving anyclass (NFData)
newtype LibName = MkLibName { inner :: NonEmpty Name } newtype Program = MkProgram
deriving stock (Show, Generic, Data) { commandsAndDefs :: List CommandOrDef
deriving anyclass (NFData)
data ImportSet
= ImportLib LibName
| ImportOnly ImportSet (NonEmpty Name)
| ImportExcept ImportSet (NonEmpty Name)
| ImportPrefix ImportSet Name
| ImportRename ImportSet (NonEmpty (Name, Name))
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
newtype ImportDecl = MkImportDecl (NonEmpty ImportSet)
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data LibDecl
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data Lib = MkLib
{ name :: LibName
, decls :: List LibDecl
}
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data Program = MkProgram
{ imports :: List ImportDecl
, commandsAndDefs :: List CommandOrDef
} }
deriving stock (Show, Generic, Data) deriving stock (Show, Generic, Data)
deriving anyclass (NFData) deriving anyclass (NFData)
@@ -242,12 +142,14 @@ makeBaseFunctor ''Exp
instance DatumIso Name where instance DatumIso Name where
datumIso = S.symbol >>> S.iso coerce coerce datumIso = S.decorate S.SynVariable
>>> S.symbol
>>> S.iso coerce coerce
builtinDatumIso primDatumIso
:: (Text -> Text) :: (Text -> Text)
-> S.DatumGrammar a -> S.DatumGrammar (Builtin a) -> S.DatumGrammar a -> S.DatumGrammar (Prim a)
builtinDatumIso namefn a = S.match primDatumIso namefn a = S.match
$ S.With (. ht2 "+") $ S.With (. ht2 "+")
$ S.With (. ht2 "-") $ S.With (. ht2 "-")
$ S.With (. ht2 "*") $ S.With (. ht2 "*")
@@ -260,32 +162,22 @@ builtinDatumIso namefn a = S.match
$ S.With (. ht1 "integer?") $ S.With (. ht1 "integer?")
$ S.With (. ht1 "write") $ S.With (. ht1 "write")
$ S.With (. ht1 "zero?") $ S.With (. ht1 "zero?")
$ S.With (. ht0 "newline") $ S.With (. nullop "newline")
$ S.With (. ht1' "make-closure") $ S.With (. ht1' "make-closure")
$ S.With (. S.headTagged2 (namefn "make-shared-closure") $ S.With (. S.headTagged2 (namefn "env-ref") a S.int)
(S.list $ S.rest a) $ S.With (. ht1 "env-code")
(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 (. ht1 "call/cc")
$ S.With (. ht0 "capture/cc")
$ S.With (. ht1' "invoke/cc")
$ S.With (. ht0' "values")
$ S.With (. ht2 "call-with-values")
$ S.With (. ht1 "pair?")
$ S.With (. ht0' "list")
$ S.End $ S.End
where where
idn = S.el . S.sym . namefn idn = S.el . S.sym . namefn
ht0 s = S.list $ idn s nullop s = S.list $ idn s
ht1 s = S.headTagged1 (namefn s) a ht1 s = S.headTagged1 (namefn s) a
ht2 s = S.headTagged2 (namefn s) a a ht2 s = S.headTagged2 (namefn s) a a
ht1' s = S.headTagged1' (namefn s) a a ht1' s = S.headTagged1' (namefn s) a a
ht0' s = S.headTagged0' (namefn s) a
instance DatumIso a => DatumIso (Builtin a) where instance DatumIso a => DatumIso (Prim a) where
datumIso = builtinDatumIso id S.datumIso -- datumIso = primDatumIso ("prim:"<>) datumIso
datumIso = primDatumIso id S.datumIso
instance DatumIso Lit where instance DatumIso Lit where
datumIso = S.match datumIso = S.match
@@ -306,203 +198,30 @@ instance DatumIso Def where
>>> S.el args >>> S.rest S.datumIso >>> S.el args >>> S.rest S.datumIso
args = S.list $ S.el S.datumIso >>> S.rest S.datumIso args = S.list $ S.el S.datumIso >>> S.rest S.datumIso
instance DatumIso Trans where
datumIso = S.with \g ->
S.listWithStyle
(S.StyleSyntax 1)
( S.el (S.sym "syntax-rules")
>>> S.Iso (\(ctx:-t) -> ctx:-"...":-t) (\(ctx:-_:-t) -> ctx:-t)
>>> S.el (S.list $ S.rest (S.datumIso @Name))
>>> S.rest (S.datumIso @Rule)
)
>>> g
instance DatumIso Exp where instance DatumIso Exp where
datumIso = S.match datumIso = S.match
$ S.With (. S.letLike "let" S.datumIso S.datumIso S.datumIso) $ S.With (. S.letLike "let" S.datumIso S.datumIso S.datumIso)
$ S.With (letsyntax >>>)
$ S.With (. S.letLike "letrec" S.datumIso S.datumIso S.datumIso) $ S.With (. S.letLike "letrec" S.datumIso S.datumIso S.datumIso)
$ S.With (. S.datumIso) $ S.With (. S.datumIso)
$ S.With (. begin) $ S.With (. S.beginLike "begin" S.datumIso)
$ S.With (. if_) $ S.With (. S.ifLike "if" S.datumIso S.datumIso S.datumIso)
$ S.With (. S.datumIso) $ S.With (. S.datumIso)
$ S.With (. lam) $ S.With (. lam)
$ S.With (. S.datumIso) $ S.With (. S.datumIso)
$ S.With (. S.datumIso)
$ S.With (\app -> app . S.list (S.el S.datumIso >>> S.rest S.datumIso)) $ S.With (\app -> app . S.list (S.el S.datumIso >>> S.rest S.datumIso))
$ S.End $ S.End
where where
letsyntax = S.listWithStyle (S.StyleSyntax 1) $
S.el (S.sym "let-syntax")
>>> S.el (S.list $ S.rest $ S.datumIso)
>>> S.el S.datumIso
lam = S.lambdaLike S.lambdaKeyword S.datumIso (S.el S.datumIso) lam = S.lambdaLike S.lambdaKeyword S.datumIso (S.el S.datumIso)
if_ = S.ifLike "if" S.datumIso S.datumIso $ S.datumIso @Exp >>> S.iso
Just
\case
Just x -> x
Nothing -> error "안 괜찮다ㅠㅠ"
begin :: forall t. G (S.Datum :- t) (NonEmpty Exp :- t)
begin = 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 S.DatumIso Rule where
datumIso = S.with \g ->
S.list (S.el S.datumIso >>> S.el S.datumIso) >>> g
instance S.DatumIso Pat where
datumIso = S.match
$ S.With (S.sym "_" >>>)
$ S.With (S.datumIso >>>)
$ S.With (lst >>>)
$ S.End
where
lst :: S.G (S.Datum :- t) (Maybe Pat :- Maybe (List Pat) :- List Pat :- t)
lst = S.coproduct
[ S.list $
ellipsis
>>> S.onTail (S.push Nothing (is _Nothing) (const mempty))
, S.dottedList
ellipsis
( S.datumIso @Pat
>>> S.partialIso Just (maybe (Left mempty) Right) )
]
ellipsis =
S.restData split
>>> S.onTail
( S.onHead (S.traversed . S.traversed . S.sealed $
S.datumIso @Pat)
>>> S.onTail (S.onHead . S.traversed . S.sealed $
S.datumIso @Pat)
)
split
:: forall t. S.G (List S.Datum :- t)
(Maybe (List S.Datum) :- List S.Datum :- t)
split = S.Iso
(\(ps0:-t) ->
let (ps,ell) = splitEllipsis ps0
in ell :- ps :- t)
(\(ell:-ps:-t) -> (ps ++ foldMap ([S.Symbol "..."]++) ell) :- t)
splitEllipsis :: List S.Datum -> (List S.Datum, Maybe (List S.Datum))
splitEllipsis [] = ([], Nothing)
splitEllipsis (S.Symbol "..." : xs) = ([], Just xs)
splitEllipsis (x:xs) = splitEllipsis xs & _1 %~ (x:)
instance S.DataIso El where
dataIso = S.match
$ S.With (ellipsis >>>)
$ S.With (noellipsis >>>)
$ S.End
where
ellipsis = S.recontextualise $
S.el (S.datumIso @Tem) >>> S.el (S.sym "...")
noellipsis = S.recontextualise $ S.el (S.datumIso @Tem)
instance S.DatumIso Tem where
datumIso = S.match
$ S.With (lst >>>)
$ S.With (trail >>>)
$ S.With (S.datumIso @Lit >>>)
$ S.With (S.datumIso @Name >>>)
$ S.End
where
trail = S.list $ S.el (S.sym "...") >>> S.el (S.datumIso @Tem)
lst :: S.G (S.Datum :- t) (Maybe Tem :- List El :- t)
lst = S.coproduct
[ S.list els
>>> (S.push Nothing (is _Nothing) (const mempty))
, S.dottedList els $
S.datumIso @Tem
>>> S.partialIso Just (maybe (Left mempty) Right)
]
els :: S.G (S.ListContext :- t) (S.ListContext :- List El :- t)
els =
S.iso
(\(S.MkListContext ds) -> affixEllipses ds)
(S.MkListContext . foldMap \(d,b) ->
d : if b then [S.Symbol "..."] else [])
>>> S.onHead (S.traversed . S.sealed $
S.flipped S.pair
>>> S.onTail (S.datumIso @Tem)
>>> S.pair
>>> S.iso
(\(t,b) -> if b then Ellipsis t else El t)
(\case
Ellipsis t -> (t,True)
El t -> (t,False)))
>>> S.push (S.MkListContext [])
(\(S.MkListContext xs) -> null xs)
(const mempty)
affixEllipses :: List S.Datum -> List (S.Datum, Bool)
affixEllipses (x : S.Symbol "..." : xs) = (x,True) : affixEllipses xs
affixEllipses (x : xs) = (x,False) : affixEllipses xs
affixEllipses [] = []
instance DatumIso CommandOrDef where instance DatumIso CommandOrDef where
datumIso = S.match datumIso = S.match
$ S.With (\_Command -> _Command . S.datumIso) $ S.With (\_Command -> _Command . S.datumIso)
$ S.With (\_Definition -> _Definition . S.datumIso) $ S.With (\_Definition -> _Definition . S.datumIso)
$ S.With (\_Begin -> _Begin . S.beginLike "begin" (S.rest S.datumIso)) $ S.With (\_Begin -> _Begin . S.beginLike "begin" S.datumIso)
$ S.End $ S.End
instance DatumIso LibName where
datumIso = S.with \g ->
S.list (S.restData $ S.nonEmptyData comp)
>>> g
where
comp = S.partialOsi
(\case
S.Symbol s -> Right $ MkName s
S.Number (Sci.floatingOrInteger @Double @Int -> Right n)
| n > 0 -> Right $ MkName [i|#{n}|]
_ -> Left $ S.expected "library name part"
)
\(MkName s) -> S.Symbol s
instance DatumIso ImportSet where
datumIso = S.match
$ S.With (S.datumIso @LibName >>>)
$ S.With (imp "only" >>>)
$ S.With (imp "except" >>>)
$ S.With (imp' "prefix" >>>)
$ S.With (imp "rename" >>>)
$ S.End
where
imp s = S.list $ S.el (S.sym s)
>>> S.el S.datumIso >>> S.restData S.dataIso
imp' s = S.list $
S.el (S.sym s)
>>> S.el S.datumIso
>>> S.el S.datumIso
instance DatumIso ImportDecl where
datumIso = S.with \decl ->
S.list (S.el (S.sym "import") >>> S.restData S.dataIso)
>>> decl
instance DataIso Program where instance DataIso Program where
dataIso = S.with \g -> dataIso = S.dataIso @(List CommandOrDef) >>> S.iso coerce coerce
splitG
>>> S.onHead (S.sealed S.dataIso)
>>> S.onTail (S.onHead . S.sealed $ S.dataIso)
>>> g
where
isImport = \case
S.List (S.Symbol "import" : _) -> True
_ -> False
splitG :: G (List S.Datum :- t) (List S.Datum :- List S.Datum :- t)
splitG = S.Iso
(\(xs:-t) ->
let (ys,zs) = span isImport xs
in zs :- ys :- t
)
\(zs:-ys:-t) -> (ys ++ zs) :- t
-- utilities -- utilities
+4 -55
View File
@@ -15,7 +15,6 @@ module Gyehoek.Sexp.Grammar
, encodeDataTest , encodeDataTest
, encodeDataTestColour , encodeDataTestColour
, encodeOrShow' , encodeOrShow'
, encodeOrShowData'
, decodeDataWith , decodeDataWith
, encodeDataWith' , encodeDataWith'
, decodeTest , decodeTest
@@ -29,8 +28,6 @@ module Gyehoek.Sexp.Grammar
, fromDatumUnsafe , fromDatumUnsafe
, Control.Category.id , Control.Category.id
, fromDataUnsafe , fromDataUnsafe
, writeDatum
, writeData
) )
where where
@@ -48,8 +45,6 @@ import qualified Control.Category
import qualified Data.Vector as V import qualified Data.Vector as V
import Data.String (IsString (fromString)) import Data.String (IsString (fromString))
import qualified Data.Text as T import qualified Data.Text as T
import System.Environment (lookupEnv)
import Data.Foldable (toList)
toDatum :: Jalmot :> es => DatumGrammar a -> a -> Eff es Datum toDatum :: Jalmot :> es => DatumGrammar a -> a -> Eff es Datum
@@ -64,21 +59,19 @@ toData g =
>>> runGrammar noAnn >>> runGrammar noAnn
>>> either (throwError . GrammarError) pure >>> either (throwError . GrammarError) pure
fromDatum :: (HasCallStack, Jalmot :> es) => DatumGrammar a -> Datum -> Eff es a fromDatum :: Jalmot :> es => DatumGrammar a -> Datum -> Eff es a
fromDatum g = fromDatum g =
forward (sealed g) forward (sealed g)
>>> runGrammar noAnn >>> runGrammar noAnn
>>> either (throwError . GrammarError) pure >>> either (throwError . GrammarError) pure
fromDatumUnsafe :: HasCallStack => DatumGrammar a -> Datum -> a fromDatumUnsafe :: DatumGrammar a -> Datum -> a
fromDatumUnsafe g = runJalmotUnsafe . fromDatum g fromDatumUnsafe g = runJalmotUnsafe . fromDatum g
fromDataUnsafe :: HasCallStack => DataGrammar a -> List Datum -> a fromDataUnsafe :: DataGrammar a -> List Datum -> a
fromDataUnsafe g = runJalmotUnsafe . fromData g fromDataUnsafe g = runJalmotUnsafe . fromData g
fromData fromData :: Jalmot :> es => DataGrammar a -> List Datum -> Eff es a
:: (HasCallStack, Jalmot :> es)
=> DataGrammar a -> List Datum -> Eff es a
fromData g = fromData g =
forward (sealed g) forward (sealed g)
>>> runGrammar noAnn >>> runGrammar noAnn
@@ -131,39 +124,6 @@ encodeOrShow' g x = fromString $
Left _ -> show x Left _ -> show x
Right t -> T.unpack t Right t -> T.unpack t
encodeOrShow :: (IsString s, Show a) => DatumGrammar a -> a -> s
encodeOrShow g x = fromString $
case runPureEff . runJalmot . encodeWith g $ x of
Left _ -> show x
Right t -> T.unpack t
encodeOrShowData' :: (IsString s, Show a) => DataGrammar a -> a -> s
encodeOrShowData' g x = fromString $
case runPureEff . runJalmot . encodeDataWith' g $ x of
Left _ -> show x
Right t -> T.unpack t
encodeOrShowData :: (IsString s, Show a) => DataGrammar a -> a -> s
encodeOrShowData g x = fromString $
case runPureEff . runJalmot . encodeDataWith g $ x of
Left _ -> show x
Right t -> T.unpack t
useColour :: IO Bool
useColour = maybe True (const False) <$> lookupEnv "NO_COLOR"
writeDatum :: (Show a, DatumIso a, MonadIO m) => a -> m ()
writeDatum x = do
c <- liftIO useColour
let f = if c then encodeOrShow else encodeOrShow'
liftIO . TIO.putStrLn . f datumIso $ x
writeData :: (Show a, DataIso a, MonadIO m) => a -> m ()
writeData x = do
c <- liftIO useColour
let f = if c then encodeOrShowData else encodeOrShowData'
liftIO . TIO.putStrLn . f dataIso $ x
class DatumIso a where class DatumIso a where
datumIso :: DatumGrammar a datumIso :: DatumGrammar a
@@ -179,14 +139,6 @@ instance DatumIso Bool where datumIso = boolean
instance DatumIso Int where datumIso = int instance DatumIso Int where datumIso = int
instance DatumIso Natural where
datumIso = int
>>> partialOsi
(\x -> if x < 0
then Left $ expected "non-negative integer" <> unexpected [i|#{x}|]
else Right $ fromIntegral x)
fromIntegral
instance DatumIso Datum where datumIso = Control.Category.id instance DatumIso Datum where datumIso = Control.Category.id
instance DatumIso a => DataIso (List a) where instance DatumIso a => DataIso (List a) where
@@ -196,8 +148,5 @@ instance DatumIso a => DataIso (V.Vector a) where
dataIso = iso fromList V.toList dataIso = iso fromList V.toList
>>> (onHead . traversed . sealed $ datumIso @a) >>> (onHead . traversed . sealed $ datumIso @a)
instance DatumIso a => DataIso (NonEmpty a) where
dataIso = nonEmptyData datumIso
instance (DatumIso a, DatumIso b) => DatumIso (a, b) where instance (DatumIso a, DatumIso b) => DatumIso (a, b) where
datumIso = with \tup2 -> list (el datumIso >>> el datumIso) >>> tup2 datumIso = with \tup2 -> list (el datumIso >>> el datumIso) >>> tup2
+47 -118
View File
@@ -1,4 +1,3 @@
{- HLINT ignore "Avoid lambda" -}
-- | cribbed from sexp-grammar:Language.SexpGrammar.Base -- | cribbed from sexp-grammar:Language.SexpGrammar.Base
module Gyehoek.Sexp.Grammar.Base module Gyehoek.Sexp.Grammar.Base
( module Gyehoek.Sexp.Syntax ( module Gyehoek.Sexp.Syntax
@@ -9,21 +8,20 @@ module Gyehoek.Sexp.Grammar.Base
, Grammar(..) , Grammar(..)
, DatumGrammar , DatumGrammar
, DataGrammar , DataGrammar
, ListContext(..) , Grammar
, ListContext
, (:-)((:-)) , (:-)((:-))
-- * lists -- * lists
, list , list
, listWithStyle , listWithIndentation
, el , el
, rest , rest
, restData , restData
, nonEmptyData
, headTagged0' , headTagged0'
, headTagged0 , headTagged0
, headTagged1' , headTagged1'
, headTagged1 , headTagged1
, headTagged2 , headTagged2
, headTagged2'
-- * atoms -- * atoms
, simple , simple
, string , string
@@ -33,32 +31,30 @@ module Gyehoek.Sexp.Grammar.Base
, number , number
, integer , integer
, int , int
, unreadable
-- * TODO: sort lol -- * TODO: sort lol
, prismIso , prismIso
, isoIso , isoIso, decorate
, snoced , snoced
, letLike , letLike
, ifLike , ifLike
, lambdaLike , lambdaLike
, lambdaKeyword , lambdaKeyword
, kappaKeyword , kappaKeyword
, beginLike , beginLike, headTagged2'
, dottedList
, reifyContext, recontextualise, decontextualise, redecorate
) where ) where
import Data.InvertibleGrammar import Data.InvertibleGrammar
import Data.InvertibleGrammar.Base import Data.InvertibleGrammar.Base
import Data.InvertibleGrammar.Base as Re
( Grammar(..))
import Data.InvertibleGrammar.Combinators import Data.InvertibleGrammar.Combinators
import Gyehoek.Prelude hiding (flipped, traversed, iso, cons, coerced, Iso, Simple, simple) import Gyehoek.Prelude hiding (iso, cons, coerced, Iso, Simple, simple)
import Gyehoek.Sexp.Syntax hiding (position) import Gyehoek.Sexp.Syntax hiding (position)
import Gyehoek.Sexp.Print (printDatum') import Gyehoek.Sexp.Print (printDatum')
import Data.Scientific (Scientific) import Data.Scientific (Scientific)
import qualified Data.Scientific as Sci import qualified Data.Scientific as Sci
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.List.NonEmpty as NE import Control.Monad.RWS (modify)
import Data.Foldable (toList)
-- $setup -- $setup
@@ -87,6 +83,14 @@ locate =
(\(_ :- t) -> t) (\(_ :- t) -> t)
(\t -> noAnn :- t) (\t -> noAnn :- t)
modifyAnn :: (Ann -> Ann) -> G (Datum :- t) (Datum :- t)
modifyAnn f = Iso
(\(d:-t) -> (d & ann %~ f) :- t)
(\(d:-t) -> (d & ann %~ f) :- t)
decorate :: Syn -> G (Datum :- t) (Datum :- t)
decorate s = modifyAnn $ #syntax .~ s
newtype ListContext = MkListContext { inner :: List Datum } newtype ListContext = MkListContext { inner :: List Datum }
unexpectedSimple :: Simple -> Mismatch unexpectedSimple :: Simple -> Mismatch
@@ -98,46 +102,13 @@ unexpectedDatum = unexpected . printDatum'
list list
:: G (ListContext :- t) (ListContext :- t') :: G (ListContext :- t) (ListContext :- t')
-> G (Datum :- t) t' -> G (Datum :- t) t'
list = listWithStyle StyleData list = listWithIndentation Ordinary
-- | listWithIndentation
-- >>> let grammar = with \g -> dottedList (el int) int >>> g :: Indentation
-- >>> decodeTest @(Int,Int) grammar "(1 . 2)"
-- ( 1
-- , 2
-- )
-- >>> let grammar = with \g -> dottedList (el int >>> el int) int >>> g
-- >>> decodeTest @(Int,Int,Int) grammar "(1 2 . 3)"
-- ( 1
-- , 2
-- , 3
-- )
dottedList
:: forall t t' t''. G (ListContext :- t) (ListContext :- t')
-> G (Datum :- t') t''
-> G (Datum :- t) t''
dottedList g final = begin >>> Dive (onTail (g >>> end) >>> final)
where
begin = locate >>> Flip (PartialIso
(\(x:-MkListContext xs:-t) -> case NE.nonEmpty xs of
Just xs' -> DotList xs' x :- t
Nothing -> error "fuck")
(\case
DotList xs x :- t -> Right $ x :- MkListContext (NE.toList xs) :- t
_ -> Left $ expected "dotted list"))
end :: Grammar Ann (ListContext :- t') t'
end = Flip $ PartialIso
(\t -> MkListContext [] :- t)
(\(MkListContext lst :- t) ->
case lst of
[] -> Right t
d:_ -> Left $ unexpectedDatum d)
listWithStyle
:: Style
-> G (ListContext :- t) (ListContext :- t') -> G (ListContext :- t) (ListContext :- t')
-> G (Datum :- t) t' -> G (Datum :- t) t'
listWithStyle ind g = begin >>> Dive (g >>> end) listWithIndentation ind g = begin >>> Dive (g >>> end)
where where
begin = locate >>> partialOsi begin = locate >>> partialOsi
(\case (\case
@@ -159,33 +130,6 @@ el
-> G (ListContext :- t) (ListContext :- t') -> G (ListContext :- t) (ListContext :- t')
el g = coerced (Flip cons >>> onTail g >>> Step) el g = coerced (Flip cons >>> onTail g >>> Step)
reifyContext :: G (ListContext :- t) (List Datum :- t)
reifyContext = iso coerce coerce
decontextualise
:: G (List Datum :- t) (List Datum :- t')
-> G (ListContext :- t) t'
decontextualise g = reifyContext >>> g >>> end
where
end = Flip $ PartialIso
(\t -> [] :- t)
(\(lst :- t) ->
case lst of
[] -> Right t
d:_ -> Left $ unexpectedDatum d)
recontextualise
:: G (ListContext :- t) (ListContext :- t')
-> G (List Datum :- t) t'
recontextualise g = flipped reifyContext >>> g >>> end
where
end = Flip $ PartialIso
(\t -> MkListContext [] :- t)
(\(MkListContext lst :- t) ->
case lst of
[] -> Right t
d:_ -> Left $ unexpectedDatum d)
-- | matches the remainder of a list as repetition of a given -- | matches the remainder of a list as repetition of a given
-- grammar. -- grammar.
-- --
@@ -252,21 +196,13 @@ rest g =
-- >>> encodeTest dataRestGrammar $ MkExample [1,2,3] "end" -- >>> encodeTest dataRestGrammar $ MkExample [1,2,3] "end"
-- (1 2 3 end) -- (1 2 3 end)
restData restData
:: G (List Datum :- t) t' :: G (List Datum :- t) (a :- t)
-> G (ListContext :- t) (ListContext :- t') -> G (ListContext :- t) (ListContext :- a :- t)
restData g = restData g =
iso coerce coerce iso coerce coerce
>>> g >>> g
>>> push (MkListContext []) (const True) mempty >>> push (MkListContext []) (const True) mempty
nonEmptyData :: DatumGrammar a -> DataGrammar (NonEmpty a)
nonEmptyData g = partialOsi
(\case
[] -> Left $ expected "non-empty sequence"
x:xs -> Right $ x:|xs)
toList
>>> (onHead . traversed . sealed $ g)
snoced snoced
:: Snoc s s a a :: Snoc s s a a
=> Grammar p (s :- a :- t) (s :- t) => Grammar p (s :- a :- t) (s :- t)
@@ -368,36 +304,33 @@ int = integer >>> iso fromIntegral fromIntegral
-- high-level combinators -- high-level combinators
redecorate :: Style -> G (Datum :- t) t' -> G (Datum :- t) t'
redecorate sty g = iso (styleWith sty) (styleWith sty) >>> g
headTagged0 :: Text -> G (Datum :- t) t headTagged0 :: Text -> G (Datum :- t) t
headTagged0 s = listWithStyle StyleCode $ el (sym s) headTagged0 s = list $ el (symProcedure s)
headTagged0' :: Text -> DatumGrammar a -> G (Datum :- t) (List a :- t) headTagged0' :: Text -> DatumGrammar a -> G (Datum :- t) (List a :- t)
headTagged0' s gt = listWithStyle StyleCode $ el (sym s) >>> rest gt headTagged0' s gt = list $ el (symProcedure s) >>> rest gt
headTagged1 :: Text -> DatumGrammar a -> G (Datum :- t) (a :- t) headTagged1 :: Text -> DatumGrammar a -> G (Datum :- t) (a :- t)
headTagged1 s g1 = listWithStyle StyleCode $ el (sym s) >>> el g1 headTagged1 s g1 = list $ el (symProcedure s) >>> el g1
headTagged1' headTagged1'
:: Text :: Text
-> DatumGrammar a -> DatumGrammar b -> DatumGrammar a -> DatumGrammar b
-> G (Datum :- t) (List b :- a :- t) -> G (Datum :- t) (List b :- a :- t)
headTagged1' s g1 gt = list $ el (sym s) >>> el g1 >>> rest gt headTagged1' s g1 gt = list $ el (symProcedure s) >>> el g1 >>> rest gt
headTagged2 headTagged2
:: Text :: Text
-> DatumGrammar a -> DatumGrammar b -> DatumGrammar a -> DatumGrammar b
-> G (Datum :- t) (b :- a :- t) -> G (Datum :- t) (b :- a :- t)
headTagged2 s g1 g2 = listWithStyle StyleCode $ el (sym s) >>> el g1 >>> el g2 headTagged2 s g1 g2 = list $ el (symProcedure s) >>> el g1 >>> el g2
headTagged2' headTagged2'
:: Text :: Text
-> DatumGrammar a -> DatumGrammar b -> DatumGrammar c -> DatumGrammar a -> DatumGrammar b -> DatumGrammar c
-> G (Datum :- t) (List c :- b :- a :- t) -> G (Datum :- t) (List c :- b :- a :- t)
headTagged2' s g1 g2 gt = headTagged2' s g1 g2 gt =
listWithStyle StyleCode $ el (sym s) >>> el g1 >>> el g2 >>> rest gt list $ el (symProcedure s) >>> el g1 >>> el g2 >>> rest gt
ifLike ifLike
-- | keyword -- | keyword
@@ -410,17 +343,21 @@ ifLike
-> DatumGrammar c -> DatumGrammar c
-> G (Datum :- t) (c :- b :- a :- t) -> G (Datum :- t) (c :- b :- a :- t)
ifLike kw c t f = ifLike kw c t f =
listWithStyle (StyleSyntax 1) $ listWithIndentation (NSpecial 1) $
el (sym kw) >>> el c >>> el t >>> el f el (symBuiltin kw) >>> el c >>> el t >>> el f
symBuiltin, symProcedure :: Text -> G (Datum :- t) t
symBuiltin s = decorate SynBuiltin >>> sym s
symProcedure s = decorate SynProcedure >>> sym s
letLike letLike
:: Text :: Text
-> (forall t. G (Datum :- t) (a :- t)) -> (forall t. G (Datum :- t) (a :- t))
-> (forall t. G (Datum :- t) (b :- t)) -> (forall t. G (Datum :- t) (b :- t))
-> G (Datum :- List (a, b) :- t1) t2 -> G (Datum :- (List (a, b) :- t1)) t2
-> G (Datum :- t1) t2 -> G (Datum :- t1) t2
letLike kw name rhs e = listWithStyle (StyleSyntax 1) $ letLike kw name rhs e = listWithIndentation (NSpecial 1) $
el (sym kw) >>> el bindings >>> el e el (symBuiltin kw) >>> el bindings >>> el e
where where
bindings = list $ rest binding bindings = list $ rest binding
binding :: G (Datum :- t) ((_, _) :- t) binding :: G (Datum :- t) ((_, _) :- t)
@@ -428,11 +365,11 @@ letLike kw name rhs e = listWithStyle (StyleSyntax 1) $
lambdaLike lambdaLike
:: (forall t. G (Datum :- t) t) :: (forall t. G (Datum :- t) t)
-> G (Datum :- t1) (a :- t2) -> DatumGrammar a
-> G (ListContext :- a :- t2) (ListContext :- t3) -> G (ListContext :- a :- t) (ListContext :- t')
-> G (Datum :- t1) t3 -> G (Datum :- t) t'
lambdaLike kw formals body = listWithStyle (StyleSyntax 1) $ lambdaLike kw formals body = listWithIndentation (NSpecial 1) $
el kw el (decorate SynBuiltin >>> kw)
>>> el formals >>> el formals
>>> body >>> body
@@ -444,19 +381,11 @@ kappaKeyword = coproduct [ sym "κ", sym "kappa" ]
beginLike beginLike
:: Text :: Text
-> G (ListContext :- t) (ListContext :- t') -> DatumGrammar a
-> G (Datum :- t) t' -> G (Datum :- t) (List a :- t)
beginLike kw g = beginLike kw g =
listWithStyle (StyleSyntax 0) $ listWithIndentation (NSpecial 0) $
el (sym kw) >>> g el (symBuiltin kw) >>> rest g
-- | define a printed syntax for an object which cannot be read.
unreadable
:: (t -> Text)
-> G (Datum :- t) t
unreadable f = Flip $ PartialIso
(\t -> Unreadable (f t) :- t)
(const $ Left mempty)
isoIso :: Iso' s a -> Grammar p (s :- t) (a :- t) isoIso :: Iso' s a -> Grammar p (s :- t) (a :- t)
isoIso l = iso (view l) (review l) isoIso l = iso (view l) (review l)
+24 -81
View File
@@ -1,40 +1,26 @@
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
module Gyehoek.Sexp.Print module Gyehoek.Sexp.Print
( printDatum ( printDatum
, printDatumW , printDatumW
, printDatum' , printDatum'
, printData , printData
, printData' , printData'
, htmlDatum
, htmlData
, putDoc
) where ) where
import Gyehoek.Sexp.Syntax import Gyehoek.Sexp.Syntax
import Prettyprinter import Data.Text.Prettyprint.Doc
import Gyehoek.Prelude hiding (Simple) import Data.Functor.Foldable
import qualified Control.Comonad.Trans.Cofree as F
import Prettyprinter.Util
import Gyehoek.Prelude hiding (Simple, (:<))
import Data.Foldable (traverse_)
import qualified Prettyprinter.Render.Terminal as ANSI import qualified Prettyprinter.Render.Terminal as ANSI
import System.IO (stdout) import System.IO (stdout)
import Prettyprinter.Render.Terminal (Color(..), color, italicized, AnsiStyle, bold, colorDull) import Prettyprinter.Render.Terminal (Color(..), color, italicized, AnsiStyle, bold, colorDull)
import Prettyprinter.Render.Text (renderStrict) import Prettyprinter.Render.Text (renderStrict)
import Data.List (intersperse)
import Lucid
import Prettyprinter.Render.Util.SimpleDocTree (treeForm)
import Prettyprinter.Lucid (renderHtml)
import Data.Foldable (toList)
import qualified Data.Scientific as Sci import qualified Data.Scientific as Sci
import Data.List (intersperse)
data Syn
= SynSyntax
| SynProcedure
| SynParen Int
| SynString
| SynConstant
| SynVariable
| SynNone
deriving (Show, Read, Data, Generic, Eq)
printDatum' :: Datum -> Text printDatum' :: Datum -> Text
printDatum' = printDatum' =
prettyDatum 0 prettyDatum 0
@@ -45,31 +31,6 @@ printDatum' =
{ layoutPageWidth = AvailablePerLine 80 1.0 { layoutPageWidth = AvailablePerLine 80 1.0
} }
htmlDatum :: Datum -> Html ()
htmlDatum =
prettyDatum 0
>>> layoutPretty opts
>>> treeForm
>>> fmap highlightHtml
>>> renderHtml
where
opts = LayoutOptions
{ layoutPageWidth = AvailablePerLine 80 1.0
}
htmlData :: Foldable f => f Datum -> Html ()
htmlData =
foldr f mempty
>>> layoutPretty opts
>>> treeForm
>>> fmap highlightHtml
>>> renderHtml
where
f x y = prettyDatum 0 x <> hardline <> hardline <> y
opts = LayoutOptions
{ layoutPageWidth = AvailablePerLine 80 1.0
}
printDatum :: Datum -> Text printDatum :: Datum -> Text
printDatum = printDatumW 80 printDatum = printDatumW 80
@@ -83,7 +44,7 @@ printDatumW :: Int -> Datum -> Text
printDatumW w = printDatumW w =
prettyDatum 0 prettyDatum 0
>>> layoutSmart opts >>> layoutSmart opts
>>> reAnnotateS highlightAnsi >>> reAnnotateS highlight
>>> ANSI.renderStrict >>> ANSI.renderStrict
where where
opts = LayoutOptions opts = LayoutOptions
@@ -92,29 +53,20 @@ printDatumW w =
prettyDatum :: Int -> Datum -> Doc Syn prettyDatum :: Int -> Datum -> Doc Syn
prettyDatum depth datum = case datum of prettyDatum depth datum = case datum of
Simple simp -> prettySimple depth simp Simple simp -> annotate (datum ^. syntax) $ prettySimple depth simp
DotList xs x -> List' indent xs ->
pparen depth . group . align $ case indent of
vsep [ vsep (prettyDatum (depth+1) <$> toList xs) NSpecial n | keyword:args <- xs ->
, "."
, prettyDatum (depth+1) x
]
List' sty xs -> case sty of
StyleSyntax n | keyword:args <- xs ->
let (specialArgs,body) = splitAt n args let (specialArgs,body) = splitAt n args
in pparen depth . nest 2 . vsep $ in pparen depth . nest 2 . vsep $
[ group . nest 2 . hcat $ [ group . nest 2 . hcat $
[ annotate SynSyntax $ prettyDatum (depth+1) keyword [ prettyDatum (depth+1) keyword
, if null specialArgs then mempty else softline , if null specialArgs then mempty else softline
, hsep $ prettyDatum (depth+1) <$> specialArgs , hsep $ prettyDatum (depth+1) <$> specialArgs
] ]
, vsep $ prettyDatum (depth+1) <$> body , vsep $ prettyDatum (depth+1) <$> body
] ]
StyleCode | f:args <- xs -> pparen depth $ Ordinary; NSpecial _ -> pparen depth $
group . align . vsep . (_head %~ annotate SynProcedure) $
prettyDatum (depth+1) <$> xs
StyleData; StyleSyntax _; StyleCode -> pparen depth $
group . align . vsep $ group . align . vsep $
prettyDatum (depth+1) <$> xs prettyDatum (depth+1) <$> xs
_ -> error [i|unimplemented: #{datum}|] _ -> error [i|unimplemented: #{datum}|]
@@ -122,39 +74,30 @@ prettyDatum depth datum = case datum of
pparen depth = enclose (delim depth "(") (delim depth ")") pparen depth = enclose (delim depth "(") (delim depth ")")
delim depth = annotate (SynParen depth) delim depth = annotate (SynParen depth)
delimited :: Int -> Doc Syn -> Doc Syn -> List (Doc Syn) -> Doc Syn
delimited depth open close =
encloseSep (delim depth open) (delim depth close) softline
prettySimple :: Int -> Simple -> Doc Syn prettySimple :: Int -> Simple -> Doc Syn
prettySimple depth = \case prettySimple depth = \case
SimpleBoolean b -> annotate SynConstant $ if b then "#t" else "#f" SimpleBoolean b -> annotate SynConstant $ if b then "#t" else "#f"
SimpleNumber n -> n SimpleNumber n ->
& Sci.floatingOrInteger @Double @Integer Sci.floatingOrInteger n
& either viaShow viaShow & either viaShow viaShow
& annotate SynConstant & annotate SynConstant
SimpleString s -> annotate SynString $ viaShow s SimpleString s -> annotate SynString $ viaShow s
SimpleSymbol s -> pretty s SimpleSymbol s -> pretty s
SimpleUnreadable s -> pretty s
putDoc :: Doc Syn -> IO () putDoc :: Doc Syn -> IO ()
putDoc = ANSI.renderIO stdout putDoc = ANSI.renderIO stdout
. reAnnotateS highlightAnsi . layoutSmart defaultLayoutOptions . (<>"\n") . reAnnotateS highlight . layoutSmart defaultLayoutOptions . (<>"\n")
highlightAnsi :: Syn -> AnsiStyle highlight :: Syn -> AnsiStyle
highlightAnsi = \case highlight = \case
SynSyntax -> color Magenta <> italicized <> bold (SynBuiltin; SynMacro) -> color Magenta <> italicized <> bold
SynProcedure -> color Blue SynProcedure -> color Blue
SynConstant -> color Yellow SynConstant -> color Yellow
SynParen n -> colorDull $ rainbow ^?! ix n SynParen n -> colorDull $ rainbow ^?! ix n
_ -> mempty _ -> mempty
where where
rainbow = cycle [Red,Yellow,Green,Blue,Magenta,Cyan] rainbow = cycle [Red,Yellow,Green,Blue,Magenta,Cyan]
highlightHtml :: Syn -> Html () -> Html ()
highlightHtml syn = span_ [class_ synClass]
where
synClass = case syn of
SynSyntax -> "syn-builtin"
SynConstant -> "syn-constant"
SynString -> "syn-string"
SynProcedure -> "syn-procedure"
SynVariable -> "syn-variable"
SynNone -> "syn-none"
SynParen n -> [i|syn-paren-#{mod n 5}|]
+6 -41
View File
@@ -1,4 +1,3 @@
{-# LANGUAGE ApplicativeDo #-}
module Gyehoek.Sexp.Read module Gyehoek.Sexp.Read
( readFile ( readFile
, readString , readString
@@ -21,7 +20,6 @@ import qualified Data.Text as T
import Data.Char (GeneralCategory(..), generalCategory) import Data.Char (GeneralCategory(..), generalCategory)
import Data.Scientific (Scientific) import Data.Scientific (Scientific)
import Gyehoek.Jalmot import Gyehoek.Jalmot
import Data.Foldable
readFile :: (Jalmot :> es, IOE :> es) => FilePath -> Eff es (List Datum) readFile :: (Jalmot :> es, IOE :> es) => FilePath -> Eff es (List Datum)
@@ -106,51 +104,18 @@ verb = L.symbol sc
identifier :: P Text identifier :: P Text
identifier = label "identifier" . lexeme . choice $ identifier = label "identifier" . lexeme . choice $
[ typical [ typical-- , delimited, peculiar
-- , delimited
, peculiar
] ]
where where
typical = T.cons <$> initial <*> subsequent typical = T.cons <$> initial <*> subsequent
where
subsequent = takeWhileP Nothing \c -> subsequent = takeWhileP Nothing \c ->
isInitial c || isInitial c ||
c `hasCategory` [SpacingCombiningMark, EnclosingMark, DecimalNumber] c `hasCategory` [SpacingCombiningMark, EnclosingMark, DecimalNumber]
|| c == '.' || c == '@' || c == '+' || c == '-' || c == '.' || c == '@' || c == '+' || c == '-'
initial = satisfy isInitial initial = satisfy isInitial
delimited = _ delimited = _
peculiar = peculiarSign <|> peculiarDot peculiar = _
-- peculiarSign과 R⁷RS의 이 production 새 개들은 같음:
-- ⟨explicit sign⟩
-- ⟨explicit sign⟩ ⟨sign subsequent⟩ ⟨subsequent⟩*
-- ⟨explicit sign⟩ . ⟨dot subsequent⟩ ⟨subsequent⟩*
-- 같음:
-- ⟨explicit sign⟩
-- ((⟨sign subsequent⟩ | . ⟨dot subsequent⟩) ⟨subsequent⟩*)?
peculiarSign = do
sign <- explicitSign
r <- fold <$> optional do
neck <- choice
[ T.singleton <$> signSubsequent
, T.cons <$> single '.' <*> (T.singleton <$> dotSubsequent)
]
subs <- subsequent
pure $ neck <> subs
pure $ T.cons sign r
-- . ⟨dot subsequent⟩ ⟨subsequent⟩*
peculiarDot = do
dot <- single '.'
dotSub <- dotSubsequent
subs <- subsequent
pure $ T.cons dot $ T.cons dotSub subs
dotSubsequent = single '.' <|> signSubsequent
<?> "dot subsequent"
explicitSign = (satisfy \c -> c == '+' || c == '-')
<?> "explicit sign"
signSubsequent = initial <|> explicitSign <|> satisfy (=='@')
<?> "sign subsequent"
hasCategory c xs = generalCategory c `elem` xs hasCategory c xs = generalCategory c `elem` xs
isInitial c = (c `hasCategory` isInitial c = (c `hasCategory`
@@ -242,7 +207,7 @@ simpleDatum = choice
, SimpleNumber <$> try number , SimpleNumber <$> try number
-- , SimpleCharacter <$> character -- , SimpleCharacter <$> character
, SimpleString <$> string , SimpleString <$> string
, SimpleSymbol <$> try symbol , SimpleSymbol <$> symbol
-- , SimpleBytevector <$> bytevector -- , SimpleBytevector <$> bytevector
] ]
@@ -254,9 +219,9 @@ compoundDatum = choice
list :: P Compound list :: P Compound
list = label "list" . between lparen rparen $ do list = label "list" . between lparen rparen $ do
optional datum >>= \case optional datum >>= \case
Nothing -> pure $ ListF StyleData [] Nothing -> pure $ ListF Ordinary []
Just x -> do Just x -> do
xs <- many datum xs <- many datum
optional (dot *> datum) >>= \case optional (dot *> datum) >>= \case
Nothing -> pure $ ListF StyleData (x:xs) Nothing -> pure $ ListF Ordinary (x:xs)
Just y -> pure $ DotListF (x:|xs) y Just y -> pure $ DotListF (x:|xs) y
+40 -48
View File
@@ -1,7 +1,6 @@
{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ApplicativeDo #-} {-# LANGUAGE ApplicativeDo #-}
{- HLINT ignore "Use newtype instead of data" -}
module Gyehoek.Sexp.Syntax module Gyehoek.Sexp.Syntax
( DatumF(..) ( DatumF(..)
, Simple(..) , Simple(..)
@@ -14,7 +13,8 @@ module Gyehoek.Sexp.Syntax
, Cofree((:<)) , Cofree((:<))
, Fix(..) , Fix(..)
, Compound , Compound
, Style(..) , Indentation(..)
, Syn(..)
, pattern Simple , pattern Simple
, pattern Compound , pattern Compound
, pattern Labeled , pattern Labeled
@@ -25,9 +25,10 @@ module Gyehoek.Sexp.Syntax
, pattern Vector , pattern Vector
, pattern DotList , pattern DotList
, pattern Gyehoek.Sexp.Syntax.List , pattern Gyehoek.Sexp.Syntax.List
, style , syntax
, styleWith , indentation
, pattern Unreadable , adorn
, indentWith
, pattern Bytevector , pattern Bytevector
, pattern Symbol , pattern Symbol
, pattern String , pattern String
@@ -37,16 +38,15 @@ module Gyehoek.Sexp.Syntax
, Ann(..) , Ann(..)
, noAnn , noAnn
, ann , ann
, dat
, pattern List' , pattern List'
, position , position
, stripAnn , stripAnn
) where ) where
import Language.Haskell.TH.Syntax (Lift (lift)) import Language.Haskell.TH.Syntax (Lift (lift), liftData)
import Data.Scientific (Scientific) import Data.Scientific (Scientific)
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Gyehoek.Prelude hiding (Simple) import Gyehoek.Prelude hiding ((:<), Simple)
import Text.Megaparsec.Pos (SourcePos(..), sourcePosPretty) import Text.Megaparsec.Pos (SourcePos(..), sourcePosPretty)
import Control.Comonad.Cofree (Cofree((:<)), _extract, _unwrap) import Control.Comonad.Cofree (Cofree((:<)), _extract, _unwrap)
import Data.Fix (Fix (..)) import Data.Fix (Fix (..))
@@ -67,9 +67,7 @@ data DatumF a
| CompoundF (CompoundF a) | CompoundF (CompoundF a)
| LabeledF Label a | LabeledF Label a
| LabelRefF Label | LabelRefF Label
-- | Should not be used outside of the "Gyehoek.Sexp.QQ" implementation.
| MetaF Text | MetaF Text
-- | Should not be used outside of the "Gyehoek.Sexp.QQ" implementation.
| MetaSpliceF Text | MetaSpliceF Text
deriving stock (Show, Eq, Data, Generic, Lift, Functor, Foldable, Traversable) deriving stock (Show, Eq, Data, Generic, Lift, Functor, Foldable, Traversable)
deriving anyclass (NFData) deriving anyclass (NFData)
@@ -81,12 +79,11 @@ data Simple
| SimpleString Text | SimpleString Text
| SimpleSymbol Text | SimpleSymbol Text
| SimpleBytevector ByteString | SimpleBytevector ByteString
| SimpleUnreadable Text
deriving stock (Show, Eq, Data, Generic, Lift) deriving stock (Show, Eq, Data, Generic, Lift)
deriving anyclass (NFData) deriving anyclass (NFData)
data CompoundF a data CompoundF a
= ListF Style (List a) = ListF Indentation (List a)
| DotListF (NonEmpty a) a | DotListF (NonEmpty a) a
| VectorF (List a) | VectorF (List a)
| AbbrevF Prefix a | AbbrevF Prefix a
@@ -94,12 +91,7 @@ data CompoundF a
deriving anyclass (NFData) deriving anyclass (NFData)
data Prefix data Prefix
= Quote -- ^ @'@ = Quote | Backtick | Comma | CommaAt
| Backtick -- ^ @`@
| Comma -- ^ @,@
| CommaAt -- ^ @,\@@
| PoundQuote -- ^ @#'@
| PoundBacktick -- ^ @#`@
deriving stock (Show, Eq, Data, Generic, Lift) deriving stock (Show, Eq, Data, Generic, Lift)
deriving anyclass (NFData) deriving anyclass (NFData)
@@ -120,21 +112,33 @@ newtype Label = MkLabel Natural
type Datum = Cofree DatumF Ann type Datum = Cofree DatumF Ann
type Compound = CompoundF Datum type Compound = CompoundF Datum
data Style data Indentation
= StyleSyntax Int = NSpecial Int
| StyleCode | Ordinary
| StyleData
deriving stock (Data, Eq, Generic, Show, Lift, Read) deriving stock (Data, Eq, Generic, Show, Lift, Read)
deriving anyclass (NFData) deriving anyclass (NFData)
data Syn
= SynMacro
| SynBuiltin
| SynProcedure
| SynParen Int
| SynString
| SynConstant
| SynVariable
| SynNone
deriving (Show, Read, Data, Generic, Eq, Lift)
data Ann = MkAnn data Ann = MkAnn
{ position :: Maybe SourcePos { syntax :: Syn
, position :: Maybe SourcePos
} }
deriving (Show, Data, Eq, Generic) deriving (Show, Data, Eq, Generic)
noAnn :: Ann noAnn :: Ann
noAnn = MkAnn noAnn = MkAnn
{ position = Nothing { syntax = SynNone
, position = Nothing
} }
-- requisite of the Pretty instance for invertible-grammar's error type. -- requisite of the Pretty instance for invertible-grammar's error type.
@@ -152,21 +156,23 @@ deriveEq1 ''DatumF
ann :: Lens' Datum Ann ann :: Lens' Datum Ann
ann = _extract ann = _extract
dat :: Lens' Datum (DatumF Datum) syntax :: Lens' Datum Syn
dat = _unwrap syntax = ann . #syntax
position :: Lens' Datum (Maybe SourcePos) position :: Lens' Datum (Maybe SourcePos)
position = ann . #position position = ann . #position
-- affine indentation :: Traversal' Datum Indentation
style :: Traversal' Datum Style indentation k (syn :< CompoundF (ListF ind xs)) = do
style k (syn :< CompoundF (ListF ind xs)) = do
ind' <- k ind ind' <- k ind
pure $ syn :< CompoundF (ListF ind' xs) pure $ syn :< CompoundF (ListF ind' xs)
style k a = pure a indentation k a = pure a
styleWith :: Style -> Datum -> Datum adorn :: Syn -> Datum -> Datum
styleWith = set style adorn = set syntax
indentWith :: Indentation -> Datum -> Datum
indentWith = set indentation
stripAnn :: Datum -> Fix DatumF stripAnn :: Datum -> Fix DatumF
stripAnn = hoist tailF stripAnn = hoist tailF
@@ -200,9 +206,9 @@ pattern Meta x <- _ :< MetaF x
pattern List :: List Datum -> Datum pattern List :: List Datum -> Datum
pattern List a <- _ :< CompoundF (ListF _ a) pattern List a <- _ :< CompoundF (ListF _ a)
where List a = noAnn :< CompoundF (ListF StyleData a) where List a = noAnn :< CompoundF (ListF Ordinary a)
pattern List' :: Style -> List Datum -> Datum pattern List' :: Indentation -> List Datum -> Datum
pattern List' ind a <- _ :< CompoundF (ListF ind a) pattern List' ind a <- _ :< CompoundF (ListF ind a)
where List' ind a = noAnn :< CompoundF (ListF ind a) where List' ind a = noAnn :< CompoundF (ListF ind a)
@@ -218,27 +224,13 @@ pattern Abbrev :: Prefix -> Datum -> Datum
pattern Abbrev p a <- _ :< CompoundF (AbbrevF p a) pattern Abbrev p a <- _ :< CompoundF (AbbrevF p a)
where Abbrev p a = noAnn :< CompoundF (AbbrevF p a) where Abbrev p a = noAnn :< CompoundF (AbbrevF p a)
pattern Boolean :: Bool -> Datum
pattern Boolean a = Simple (SimpleBoolean a) pattern Boolean a = Simple (SimpleBoolean a)
pattern Number :: Scientific -> Datum
pattern Number a = Simple (SimpleNumber a) pattern Number a = Simple (SimpleNumber a)
pattern Character :: Char -> Datum
pattern Character a = Simple (SimpleCharacter a) pattern Character a = Simple (SimpleCharacter a)
pattern String :: Text -> Datum
pattern String a = Simple (SimpleString a) pattern String a = Simple (SimpleString a)
pattern Symbol :: Text -> Datum
pattern Symbol a = Simple (SimpleSymbol a) pattern Symbol a = Simple (SimpleSymbol a)
pattern Bytevector :: ByteString -> Datum
pattern Bytevector a = Simple (SimpleBytevector a) pattern Bytevector a = Simple (SimpleBytevector a)
pattern Unreadable :: Text -> Datum
pattern Unreadable a = Simple (SimpleUnreadable a)
--- Lift1 instances --- Lift1 instances
+29
View File
@@ -0,0 +1,29 @@
module Gyehoek.Stack.Lower
( lowerProgram
) where
import Gyehoek.Stack.Syntax
import Gyehoek.Wasm qualified as Wasm
import Gyehoek.Prelude
import Gyehoek.Wasm (wat, watM)
lowerRoutine :: Routine -> Wasm.Function
lowerRoutine rt = _
lowerBlock :: Block -> Wasm.Expr
lowerBlock = _
lowerInstr :: Instr -> Wasm.Expr
lowerInstr = \case
-- PopCont ktail -> [wat|
-- |]
lowerProgram :: Program -> Eff es Wasm.Module
lowerProgram p = pure [watM|
(module
##{rs})
|]
where
rs = p ^.. #routines . each . to lowerRoutine
+143
View File
@@ -0,0 +1,143 @@
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveAnyClass #-}
module Gyehoek.Stack.Syntax
( Program(..)
, Routine(..)
, Instr(..)
, Block(..)
, Tail(..)
, Val(..)
, Lit(..)
, Obj(..)
, Imm(..)
, Hob(..)
, Prim(..)
, Name
, pattern ValLabel
, stkP
) where
import Control.Lens
import qualified Gyehoek.Sexp as S
import Gyehoek.Scheme.Syntax (Name(..), Lit(..), Prim(..))
import GHC.Exts (IsList(..))
import Data.List (intersperse)
import Gyehoek.CPS.Syntax (Imm(..), Obj(..), Hob(..), labelName)
import Gyehoek.Prelude
import Gyehoek.Sexp ((:-)((:-)))
newtype Program = MkProgram
{ routines :: HashMap Name Routine
}
deriving stock (Show, Generic, Data)
deriving newtype (Semigroup, Monoid)
deriving anyclass (NFData)
instance IsList Program where
type Item Program = Routine
fromList rs = MkProgram
{ routines = fromList [ (r.label, r) | r <- rs ]
}
toList = toListOf $ #routines . each
data Routine = MkRoutine
{ label :: Name
, params :: List Name
, start :: Block
}
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data Block = MkBlock
{ code :: List Instr
, tail :: Tail
}
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data Tail
= TailCall Val (List Val)
| PushCall Val Val (List Val)
| If Val Block Block
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data Instr
= Pop Name
| Push Val
| Prim Name (Prim Val)
deriving stock (Show, Generic, Data)
deriving anyclass (NFData)
data Val
= ValReg Name
| ValImm Imm
deriving stock (Show, Generic, Data, Eq)
deriving anyclass (NFData)
pattern ValLabel :: Name -> Val
pattern ValLabel x = ValImm (ImmLabel x)
--- sexp work
pure []
instance S.DatumIso Instr where
datumIso = S.match
$ S.With (S.headTagged1 "pop!" regName >>>)
$ S.With (S.headTagged1 "push!" S.datumIso >>>)
$ S.With (S.headTagged2 "prim" regName S.datumIso >>>)
$ S.End
where
instance S.DataIso Block where
dataIso = S.with \g ->
S.flipped S.snoced
>>> S.onHead (S.traversed $ S.sealed S.datumIso)
>>> S.onTail (S.datumIso @Tail)
>>> S.swap
>>> g
instance S.DatumIso Tail where
datumIso = S.match
$ S.With (S.headTagged1' "tail-call" S.datumIso S.datumIso >>>)
$ S.With (S.headTagged2' "push-call" S.datumIso S.datumIso S.datumIso >>>)
$ S.With (if_ >>>)
$ S.End
where
if_ = S.ifLike "if" (S.datumIso @Val) (branch "then") (branch "else")
branch :: Text -> S.DatumGrammar Block
branch s =
S.listWithIndentation (S.NSpecial 0) $
S.el (S.decorate S.SynBuiltin >>> S.sym s)
>>> S.restData (S.dataIso @Block)
instance S.DatumIso Val where
datumIso = S.match
$ S.With (regName >>>)
$ S.With (S.datumIso >>>)
$ S.End
instance S.DatumIso Routine where
datumIso = S.with \rout ->
S.listWithIndentation (S.NSpecial 1)
( S.el (S.decorate S.SynBuiltin >>> S.sym "define")
>>> S.el (S.list $ S.el labelName >>> S.rest regName)
>>> S.restData (S.dataIso @Block)
)
>>> rout
regName :: S.DatumGrammar Name
regName = S.decorate S.SynVariable >>> S.datumIso @Name >>> S.prismIso
(S.expected "register")
(prefixed @Name "%")
instance S.DataIso Program where
dataIso = S.dataIso @(List Routine) >>> S.iso fromList toList
stkP :: S.QuasiQuoter
stkP = S.makeSxs [|| S.fromDataUnsafe (S.dataIso @Program) ||]
+150
View File
@@ -0,0 +1,150 @@
{-# LANGUAGE ViewPatterns #-}
module Gyehoek.Stack.VM
( VM(..)
, Env(..)
, eval
, trace
, module Gyehoek.Stack.Syntax
, writeObj
) where
import Gyehoek.Stack.Syntax
import Control.Lens
import qualified Data.HashMap.Strict as H
import Data.List (unfoldr)
import Gyehoek.Prelude
data VM = MkVM
{ stack :: List Obj
, code :: List Instr
, tail :: Tail
, registers :: HashMap Name Obj
, stdout :: Text
, result :: Maybe (List Obj)
}
deriving (Show, Generic)
data Env = MkEnv
{ labels :: HashMap Name Routine
}
deriving (Show, Generic)
step :: Env -> VM -> VM
step g vm = case vm ^. #code of
c:cs -> stepI g (vm & #code .~ cs) c
[] -> stepT g vm vm.tail
stepI :: Env -> VM -> Instr -> VM
stepI e vm (Push v) = vm & #stack %~ (evalVal e vm v :)
stepI e vm (Prim r p) = case evalVal e vm <$> p of
PrimZeroP x -> case x of
ObjImm (ImmInt n) -> ret . ObjImm . ImmBool $ n == 0
_ -> error [i|bad arg to zero?: #{x}|]
PrimAdd x y -> arith_binop (+) x y
PrimMul x y -> arith_binop (*) x y
PrimSub x y -> arith_binop (-) x y
PrimDiv x y -> arith_binop div x y
PrimMakeClosure f env ->
case f of
ObjImm (ImmLabel l) -> ret . ObjHob $ HobClosure l env
_ -> error [i|expected label, got #{f}|]
PrimEnvCode env ->
case env of
ObjHob (HobClosure l _) -> ret . ObjImm . ImmLabel $ l
_ -> error [i|expected closure, got #{env}|]
PrimEnvRef env n ->
case env of
ObjHob (HobClosure _ xs) -> ret $ xs ^?! ix n
_ -> error [i|expected closure, got #{env}|]
x -> error [i|unimplemented prim: #{p}|]
where
ret v = vm & #registers . at r ?~ v
arith_binop op (ObjImm (ImmInt x)) (ObjImm (ImmInt y)) =
ret $ ObjImm (ImmInt (op x y))
arith_binop _ x y = error [i|bad arith: #{x}, #{y}|]
stepI e vm (Pop r) = case vm ^. #stack of
[] -> error "empty stack"
(x:xs) -> vm & #registers . at r ?~ x
& #stack .~ xs
stepI e vm ins = error [i|unimplemented instruction: #{ins}|]
stepT :: Env -> VM -> Tail -> VM
stepT g vm (TailCall f xs) =
case evalToLabel g vm f of
"halt" -> vm & #result ?~ fmap (evalVal g vm) xs
l -> vm & #code .~ rt.start.code
& #tail .~ rt.start.tail
& #registers .~
fmap (evalVal g vm) (H.fromList $ rt.params `zip` xs)
where
rt = case g ^. #labels . at l of
Nothing -> error [i|undefined label: #{l}|]
Just x -> x
stepT g vm (PushCall k f xs) =
_
stepT g vm (If c t f) = vm & #code .~ branch.code & #tail .~ branch.tail
where
branch = case evalVal g vm c of
ObjImm (ImmBool False) -> f
_ -> t
evalToLabel e vm v =
case evalVal e vm v of
ObjImm (ImmLabel x) -> x
x -> error [i|not a label: #{x}|]
evalVal :: Env -> VM -> Val -> Obj
evalVal e vm = \case
ValImm imm -> ObjImm imm
ValReg r -> case vm ^. #registers . at r of
Just x -> x
Nothing -> error [i|undefined register: #{r}|]
initialVM :: VM
initialVM = MkVM
{ stack = []
, code = []
, tail = TailCall (ValLabel "main") [ValLabel "halt"]
, registers = mempty
, stdout = ""
, result = Nothing
}
initialEnv :: Program -> Env
initialEnv p = MkEnv
{ labels = p.routines
}
loop :: (a -> Either b a) -> a -> b
loop f a = case f a of
Right a' -> loop f a'
Left b -> b
eval :: Program -> List Obj
eval p = initialVM & loop \vm -> case vm ^. #result of
Nothing -> Right $ step (initialEnv p) vm
Just rs -> Left rs
trace :: Program -> List VM
trace p = initialVM & unfoldr \vm ->
case vm.result of
Just _ -> Nothing
Nothing -> Just (vm, step e vm)
where e = initialEnv p
writeObj :: Obj -> Text
writeObj (ObjImm im) = case im of
ImmInt n -> [i|#{n}|]
ImmBool True -> "#t"
ImmBool False -> "#f"
ImmLabel l -> "#<procedure>"
writeObj (ObjHob h) = case h of
HobClosure code env -> "#<procedure>"
+1
View File
@@ -1,3 +1,4 @@
{- HLINT ignore "Use newtype instead of data" -}
{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE TemplateHaskellQuotes #-} {-# LANGUAGE TemplateHaskellQuotes #-}
BIN
View File
Binary file not shown.
-4
View File
@@ -1,4 +0,0 @@
(import (scheme eval))
(eval '(λ (x) x)
(environment))
+42 -67
View File
@@ -5,75 +5,50 @@ import Test.Tasty.HUnit
import Gyehoek.CPS.Syntax (cps, Obj(..), Hob(..), Imm(..)) import Gyehoek.CPS.Syntax (cps, Obj(..), Hob(..), Imm(..))
import Gyehoek.CPS.Eval qualified as Sut import Gyehoek.CPS.Eval qualified as Sut
import Data.List (List) import Data.List (List)
import Test.Tasty.ExpectedFailure (ignoreTestBecause, expectFail)
import System.Directory (listDirectory)
import Test.Tasty.Silver
import System.FilePath
import Control.Exception
import qualified Gyehoek.Driver as Driver
import Control.DeepSeq (($!!))
import System.Exit (ExitCode(..))
import qualified Data.Text as T
import Data.Function (applyWhen)
import Gyehoek.Prelude
brokenEvalTests :: List String test_cpsInterpreter = testGroup "cps interpreter" $
brokenEvalTests = [ primitives
[] , testCase "halt with constant" do
-- [ "adder" evalsTo [ObjImm (ImmInt 123)] [cps|
-- , "apply2" (continue halt 123)
-- , "apply-twice" |]
-- , "arith" , testCase "identity cont" do
-- , "begin-1" evalsTo [ObjImm (ImmInt 154)] [cps|
-- , "callcc-constant" (letrec ((id (κ (x)
-- , "callcc-discard" (continue halt x))))
-- , "callcc-early-exit-1" (continue id 154))
-- , "callcc-early-exit-2" |]
-- , "callcc-early-exit-3" , testCase "identity function" do
-- , "callcc-early-exit-4" evalsTo [ObjImm (ImmInt 456)] [cps|
-- , "callcc-early-exit-5" (letrec ((id (λ (x ktail)
-- , "callcc-early-exit-6" (continue ktail x))))
-- , "callcc-nested-1" (id 456 halt))
-- , "callcc-nested-2" |]
-- , "complicated-1" , testCase "square" do
-- , "cons-1" evalsTo [ObjImm (ImmInt 81)] [cps|
-- , "factorial" (letrec ((square (λ (x ktail)
-- , "false" (prim (* x x)
-- , "fn-of-fn" (κ (r) (continue ktail r))))))
-- , "if-false" (square 9 halt))
-- , "if-number" |]
-- , "if-true"
-- , "lambda"
-- , "letrec-fn"
-- , "let-fn"
-- , "lit-int"
-- , "square"
-- , "true"
-- ]
test_eval :: IO TestTree
test_eval = do
cs <- listDirectory "golden/exec"
<&> fmap ("golden/exec" </>)
pure $ testGroup "cps interpreter"
[ testGroup "higher-order" $ cpsCase Driver.eval_cps2_e2e <$> cs
-- , testGroup "first-order" $ cpsCase Driver.eval_cps1_e2e <$> cs
] ]
maybeBroken name broken = applyWhen (name `elem` broken) expectFail evalsTo :: HasCallStack => List Obj -> Sut.Program -> Assertion
evalsTo rs p = Sut.evalProgram p @?= rs
cpsCase :: (FilePath -> IO Text) -> FilePath -> TestTree primitives = testGroup "primitives"
cpsCase f test = [ testGroup "arith"
maybeBroken testName brokenEvalTests $ [ testCase "basic 1" do
goldenVsAction testName resultFile action printProcResult evalsTo [ObjImm (ImmInt 20)] [cps|
where (prim (* 4 5)
testName = takeFileName test (κ (x) (continue halt x)))
resultFile = test </> "exec" |]
sourceFile = test </> "source.scm" , testCase "basic 2" do
action = catch @SomeException evalsTo [ObjImm (ImmInt 35)] [cps|
(do r <- f sourceFile (prim (* 2 16)
pure $!! ( ExitSuccess (κ (x) (prim (+ x 3)
, r (κ (r) (continue halt r)))))
, "" )) |]
\e -> pure (ExitFailure 1, "", T.pack $ displayException e) ]
]
+86
View File
@@ -0,0 +1,86 @@
module Gyehoek.Test.CPS.Stackify where
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit
import qualified Gyehoek.CPS.Stackify as Sut
import Gyehoek.Stack.VM as Stk
import Data.List (List)
import Gyehoek.CPS.Syntax (cps)
import Gyehoek.GenSym (runGenSym)
import Effectful
test_stackify =
[ trivialReturn
, tailCall
, prim
, condition
, procedure
]
evalsTo :: List Obj -> Sut.Exp -> Assertion
evalsTo rs e =
Stk.eval e' @?= rs
where e' = runPureEff . runGenSym $ Sut.stackifyExp "main" e
trivialReturn = testGroup "trivial return"
[ testCase "return int" do
evalsTo [ObjImm (ImmInt 4)]
[cps|(continue halt 4)|]
, testCase "return bool" do
evalsTo [ObjImm (ImmBool True)]
[cps|(continue halt #t)|]
evalsTo [ObjImm (ImmBool False)]
[cps|(continue halt #f)|]
]
tailCall = testGroup "tail call"
[ testCase "square" do
evalsTo [ObjImm (ImmInt 16)]
[cps|(letrec ((square (λ (x ktail)
(prim (* x x)
(κ (x0) (continue ktail x0))))))
(square 4 halt))|]
]
prim = testGroup "prim"
[ testCase "multiply" do
evalsTo [ObjImm (ImmInt 20)]
[cps|(prim (* 4 5)
(κ (x) (continue halt x)))|]
, testCase "add" do
evalsTo [ObjImm (ImmInt 9)]
[cps|(prim (+ 4 5)
(κ (x) (continue halt x)))|]
-- , testGroup "call/cc"
-- [ testCase "trivial" do
-- evalsTo [ObjImm (ImmInt 123)]
-- [cps|(letrec ((f (λ (cc ktail) (continue cc 123))))
-- (prim (call/cc f)))|]
-- ]
]
condition = testCase "if" do
evalsTo [ObjImm (ImmInt 123)]
[cps|(if #t (continue halt 123) (continue halt 456))|]
evalsTo [ObjImm (ImmInt 456)]
[cps|(if #f (continue halt 123) (continue halt 456))|]
procedure = testGroup "procedure"
[ testCase "factorial" do
evalsTo [ObjImm (ImmInt 720)]
[cps|(letrec ((fac (λ (n ktail)
(prim (zero? n)
(κ (x0)
(if x0
(continue ktail 1)
(prim (- n 1)
(κ (x1)
(letrec ((fac-k0
(κ (x2)
(prim (* n x2)
(κ (x3)
(continue ktail x3))))))
(fac x1 fac-k0))))))))))
(fac 6 halt))|]
]
+8 -13
View File
@@ -28,27 +28,22 @@ free = testGroup "free"
qq :: TestTree qq :: TestTree
qq = testGroup "parser" qq = testGroup "parser"
[ testCase "lambda" do [ testCase "lambda" do
assertEqual "" assertEqual "" (Sut.MkLambda ["x","y"] "ktail"
(Sut.MkLambda ["x","y"] "ktail" (Sut.ExpContinue (Sut.ValLabel "ktail") [Sut.ValVar "x"]))
(Sut.ExpContinue (Sut.ValVar "ktail") [Sut.ValVar "x"]))
[cps|(λ (x y ktail) (continue ktail x))|] [cps|(λ (x y ktail) (continue ktail x))|]
assertEqual "" assertEqual "" (Sut.MkLambda [] "ktail"
(Sut.MkLambda [] "ktail" (Sut.ExpContinue (Sut.ValLabel "ktail") [Sut.ValVar "x"]))
(Sut.ExpContinue (Sut.ValVar "ktail") [Sut.ValVar "x"]))
[cps|(λ (ktail) (continue ktail x))|] [cps|(λ (ktail) (continue ktail x))|]
, testCase "kappa" do , testCase "kappa" do
assertEqual "" assertEqual "" (Sut.MkKappa ["x","y"]
(Sut.MkKappa ["x","y"] (Sut.ExpContinue (Sut.ValLabel "k123") [Sut.ValVar "x", Sut.ValVar "y"]))
(Sut.ExpContinue
(Sut.ValVar "k123")
[Sut.ValVar "x", Sut.ValVar "y"]))
[cps|(κ (x y) (continue k123 x y))|] [cps|(κ (x y) (continue k123 x y))|]
, testCase "application" do , testCase "application" do
assertEqual "" (Sut.ExpApply (Sut.ValVar "f") assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
[Sut.ValVar "x",Sut.ValVar "y"] [Sut.ValVar "x",Sut.ValVar "y"]
(Sut.KexpVar "k")) "k")
[cps|(f x y k)|] [cps|(f x y k)|]
assertEqual "" (Sut.ExpApply (Sut.ValVar "f") assertEqual "" (Sut.ExpApply (Sut.ValVar "f")
[] (Sut.KexpVar "k")) [] "k")
[cps|(f k)|] [cps|(f k)|]
] ]
+87
View File
@@ -0,0 +1,87 @@
module Gyehoek.Test.Golden where
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.Silver
import Gyehoek.Driver qualified as Driver
import System.FilePath
import Data.List (List)
import Data.Functor ((<&>))
import System.Directory
import Data.Function
import System.Environment.Blank (getEnvDefault)
import qualified System.Process.Text as PT
import Control.Exception (SomeException (SomeException), Exception (..), catch)
import Gyehoek.Stack.VM (writeObj)
import Data.Text qualified as T
import System.Exit (ExitCode(..))
import Test.Tasty.ExpectedFailure (expectFail, ignoreTestBecause)
import Control.DeepSeq (($!!))
import Text.Pretty.Simple (pShow, pShowNoColor)
import Control.Lens (strict, view)
import Gyehoek.Sexp.Read qualified as Read
import Effectful
brokenWasmTests :: List String
brokenWasmTests =
[
]
brokenStackifyTests :: List String
brokenStackifyTests =
[]
-- [ "adder"
-- , "let-fn"
-- , "callcc-nested1" -- requires closure-conversion
-- ]
test_root :: IO TestTree
test_root = do
all_cases <- listDirectory "golden/exec"
let tests = all_cases
& fmap ("golden/exec"</>)
testGroup "execution" <$> sequenceA
[ ignoreTestBecause "wasm codegen is on the backburner"
<$> wasmTests tests
, stackifyTests tests
]
maybeBroken name broken = applyWhen (name `elem` broken) expectFail
wasmTests :: List FilePath -> IO TestTree
wasmTests files = do
cmd <- getEnvDefault "GYEHOEK_WASM_RUNTIME"
"runtime/target/debug/gyehoek-wasm-runtime"
pure $ testGroup "wasm" $ files <&> \test ->
let testname = takeFileName test
scmfile = test </> "source.scm"
resultfile = test </> "exec"
action = do
t <- Driver.lower_e2e scmfile
PT.readProcessWithExitCode cmd ["-"] t
in maybeBroken testname brokenWasmTests $
goldenVsAction
testname
resultfile
action
printProcResult
stackifyTests :: List FilePath -> IO TestTree
stackifyTests files = do
pure $ testGroup "stackified" $ files <&> \test ->
let testname = takeFileName test
scmfile = test </> "source.scm"
resultfile = test </> "exec"
action =
catch @SomeException
(do rs <- Driver.eval_e2e scmfile
pure $!! ( ExitSuccess
, T.unwords . fmap writeObj $ rs
, "" ))
\e -> pure (ExitFailure 1, "", T.pack $ displayException e)
in maybeBroken testname brokenStackifyTests $
goldenVsAction
testname
resultfile
action
printProcResult
+6 -6
View File
@@ -24,19 +24,19 @@ thinWide name x = testGroup name
, tcaseW 4 (name <> "-thin") x , tcaseW 4 (name <> "-thin") x
] ]
datumBegin xs = S.styleWith (S.StyleSyntax 0) . S.List $ datumBegin xs = S.indentWith (S.NSpecial 0) . S.List $
S.Symbol "begin" : xs (S.adorn S.SynBuiltin . S.Symbol $ "begin") : xs
datumLambda formals body = datumLambda formals body =
S.styleWith (S.StyleSyntax 1) . S.List $ S.indentWith (S.NSpecial 1) . S.List $
S.Symbol "lambda" : formals : body (S.adorn S.SynBuiltin . S.Symbol $ "lambda") : formals : body
test_print = testGroup "sexp pretty printer" test_print = testGroup "sexp pretty printer" $
[ tcase "null" $ S.List [] [ tcase "null" $ S.List []
, thinWide "simple-list" $ , thinWide "simple-list" $
S.List [ S.Symbol s | s <- ["가","나","다","라"] ] S.List [ S.Symbol s | s <- ["가","나","다","라"] ]
, thinWide "begin-nonempty" $ , thinWide "begin-nonempty" $
S.styleWith (S.StyleSyntax 0) $ S.indentWith (S.NSpecial 0) $
datumBegin [ S.Symbol "책을" datumBegin [ S.Symbol "책을"
, S.Symbol "더" , S.Symbol "더"
, S.Symbol "먹으세요~!" , S.Symbol "먹으세요~!"
+1
View File
@@ -20,6 +20,7 @@ brokenReaderTests :: List String
brokenReaderTests = brokenReaderTests =
[ "delimited-identifier" [ "delimited-identifier"
, "string-line-continuation" , "string-line-continuation"
, "peculiar-identifier-dot"
, "meta-splice-expression-interior-brace" , "meta-splice-expression-interior-brace"
, "datum-comment" , "datum-comment"
] ]
+75
View File
@@ -0,0 +1,75 @@
{-# LANGUAGE OverloadedLists #-}
module Gyehoek.Test.Stack.VM where
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit
import Gyehoek.Stack.Syntax
import Gyehoek.Stack.VM qualified as Sut
import Data.List (List)
evalsTo :: List Obj -> Program -> Assertion
evalsTo rs p = Sut.eval p @?= rs
test_root = testGroup "stack machine"
[ testCase "lit int" do
evalsTo [ObjImm (ImmInt 3)] [stkP|
(define ($main)
(pop-cont! %ktail)
(tail-call %ktail 3))
|]
, testCase "return constant" do
evalsTo [ObjImm (ImmInt 123)] [stkP|
(define ($main)
(tail-call $silly))
(define ($silly)
(pop-cont! %ktail)
(tail-call %ktail 123))
|]
, testCase "identity function" do
evalsTo [ObjImm (ImmInt 45)] [stkP|
(define ($main)
(tail-call $id 45))
(define ($id %x)
(pop-cont! %ktail)
(tail-call %ktail %x))
|]
-- , testCase "square" do
-- evalsTo [ObjImm (ImmInt 16)] [stkP|
-- (define ($main))
-- |]
, testCase "square" do
evalsTo [ObjImm (ImmInt 16)] [stkP|
(define ($main)
(tail-call $square 4))
(define ($square %x)
(prim %x2 (* %x %x))
(pop-cont! %ktail)
(tail-call %ktail %x2))
|]
, testCase "factorial" do
let hsfac (n :: Int) = foldr (*) (1) [1..n]
let fac (n :: Int) = [stkP|
(define ($fac %n)
(prim %x0 (zero? %n))
(if %x0
(then (pop-cont! %ktail)
(tail-call %ktail 1))
(else (push! %n)
(prim %x1 (- %n 1))
(push-cont! $fac-k0)
(tail-call $fac %x1))))
(define ($fac-k0 %x2)
(pop! %n)
(prim %x3 (* %x2 %n))
(pop-cont! %ktail)
(tail-call %ktail %x3))
(define ($main)
(tail-call $fac #{n}))
|]
evalsTo [ObjImm (ImmInt 1)] $ fac 0
evalsTo [ObjImm (ImmInt 1)] $ fac 1
evalsTo [ObjImm (ImmInt 720)] $ fac 6
-- 20 is the greatest `n` for which n! ≤ maxBount @Int
evalsTo [ObjImm (ImmInt 2432902008176640000)] $ fac 20
]