Compare commits
15
Commits
309f722712
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bc5f25984 | ||
|
|
38b4047f5d | ||
|
|
71a6dda189 | ||
|
|
2fdc1382ae | ||
|
|
f143df400a | ||
|
|
94ab42007a | ||
|
|
5a030b66b7 | ||
|
|
b136737a49 | ||
|
|
9ab371ba01 | ||
|
|
daf21ce3f5 | ||
|
|
fc931ca77c | ||
|
|
4a6a15ae39 | ||
|
|
a16a4a764f | ||
|
|
8aee7c9a58 | ||
|
|
1a93f168a9 |
No files matched your search
@@ -1,5 +1,7 @@
|
|||||||
#+title: on libraries
|
#+title: on libraries
|
||||||
|
|
||||||
|
* libraries and the file system
|
||||||
|
|
||||||
R⁷RS leaves it unspecified how exactly libraries correspond to files:
|
R⁷RS leaves it unspecified how exactly libraries correspond to files:
|
||||||
|
|
||||||
#+begin_quote
|
#+begin_quote
|
||||||
@@ -7,3 +9,11 @@ Programs and libraries are typically stored in files, although in some implement
|
|||||||
#+end_quote
|
#+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.
|
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
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
nodejs
|
nodejs
|
||||||
wasm-tools
|
wasm-tools
|
||||||
wac-cli
|
wac-cli
|
||||||
guile
|
gauche
|
||||||
rust-analyzer
|
rust-analyzer
|
||||||
wasmtime
|
wasmtime
|
||||||
# bashInteractive is necessary to work around an
|
# bashInteractive is necessary to work around an
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
[0;31m([0m[0;95;1;3mbegin[0m
|
[0;31m([0m[0;95;1;3mbegin[0m
|
||||||
[0m책을[0m
|
책을
|
||||||
[0m더[0m
|
더
|
||||||
[0m먹으세요~![0m[0;31m)[0m
|
먹으세요~![0;31m)[0m
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
[0;31m([0m[0;95;1;3mbegin[0m
|
[0;31m([0m[0;95;1;3mbegin[0m
|
||||||
[0m책을[0m
|
책을
|
||||||
[0m더[0m
|
더
|
||||||
[0m먹으세요~![0m[0;31m)[0m
|
먹으세요~![0;31m)[0m
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
[0;31m([0m[0;95;1;3mlambda[0m
|
[0;31m([0m[0;95;1;3mlambda[0m
|
||||||
[0;33m([0m[0m어간[0m
|
[0;33m([0m어간
|
||||||
[0m어미[0m[0;33m)[0m
|
어미[0;33m)[0m
|
||||||
[0;33m([0m[0mdisplay[0m
|
[0;33m([0mdisplay
|
||||||
[0m꾸깃[0m[0;33m)[0m[0;31m)[0m
|
꾸깃[0;33m)[0m[0;31m)[0m
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
[0;31m([0m[0;95;1;3mlambda[0m [0;33m([0m[0m어간[0m [0m어미[0m[0;33m)[0m
|
[0;31m([0m[0;95;1;3mlambda[0m [0;33m([0m어간 어미[0;33m)[0m
|
||||||
[0;33m([0m[0mdisplay[0m [0m꾸깃[0m[0;33m)[0m[0;31m)[0m
|
[0;33m([0mdisplay 꾸깃[0;33m)[0m[0;31m)[0m
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
[0;31m([0m[0m가[0m
|
[0;31m([0m가
|
||||||
[0m나[0m
|
나
|
||||||
[0m다[0m
|
다
|
||||||
[0m라[0m[0;31m)[0m
|
라[0;31m)[0m
|
||||||
@@ -1 +1 @@
|
|||||||
[0;31m([0m[0m가[0m [0m나[0m [0m다[0m [0m라[0m[0;31m)[0m
|
[0;31m([0m가 나 다 라[0;31m)[0m
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/bool/source.scm"
|
{ sourceName = "golden/read/bool/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -9,8 +8,7 @@
|
|||||||
)
|
)
|
||||||
} :< SimpleF ( SimpleBoolean True )
|
} :< SimpleF ( SimpleBoolean True )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/bool/source.scm"
|
{ sourceName = "golden/read/bool/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -19,8 +17,7 @@
|
|||||||
)
|
)
|
||||||
} :< SimpleF ( SimpleBoolean True )
|
} :< SimpleF ( SimpleBoolean True )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/bool/source.scm"
|
{ sourceName = "golden/read/bool/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -29,8 +26,7 @@
|
|||||||
)
|
)
|
||||||
} :< SimpleF ( SimpleBoolean False )
|
} :< SimpleF ( SimpleBoolean False )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/bool/source.scm"
|
{ sourceName = "golden/read/bool/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/decimal/source.scm"
|
{ sourceName = "golden/read/decimal/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -10,8 +9,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleNumber 45.0 )
|
( SimpleNumber 45.0 )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/decimal/source.scm"
|
{ sourceName = "golden/read/decimal/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -21,8 +19,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleNumber 5667.0 )
|
( SimpleNumber 5667.0 )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/decimal/source.scm"
|
{ sourceName = "golden/read/decimal/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -11,8 +10,7 @@
|
|||||||
( DotListF
|
( DotListF
|
||||||
(
|
(
|
||||||
( MkAnn
|
( MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -23,8 +21,7 @@
|
|||||||
( SimpleSymbol "가" )
|
( SimpleSymbol "가" )
|
||||||
) :|
|
) :|
|
||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -34,8 +31,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "나" )
|
( SimpleSymbol "나" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -47,8 +43,7 @@
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
( MkAnn
|
( MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -8,10 +7,9 @@
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
} :< CompoundF
|
} :< CompoundF
|
||||||
( ListF Ordinary
|
( ListF StyleData
|
||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -21,8 +19,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "가" )
|
( SimpleSymbol "가" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -32,8 +29,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "나" )
|
( SimpleSymbol "나" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -43,8 +39,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "다" )
|
( SimpleSymbol "다" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -54,8 +49,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "라" )
|
( SimpleSymbol "라" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -65,8 +59,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleNumber 1.0 )
|
( SimpleNumber 1.0 )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -76,8 +69,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleNumber 2.0 )
|
( SimpleNumber 2.0 )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list-flat/source.scm"
|
{ sourceName = "golden/read/list-flat/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
|
|||||||
+14
-26
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -11,8 +10,7 @@
|
|||||||
( DotListF
|
( DotListF
|
||||||
(
|
(
|
||||||
( MkAnn
|
( MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -23,8 +21,7 @@
|
|||||||
( SimpleSymbol "a" )
|
( SimpleSymbol "a" )
|
||||||
) :|
|
) :|
|
||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -34,8 +31,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "b" )
|
( SimpleSymbol "b" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -43,10 +39,9 @@
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
} :< CompoundF
|
} :< CompoundF
|
||||||
( ListF Ordinary
|
( ListF StyleData
|
||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -56,8 +51,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "c" )
|
( SimpleSymbol "c" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -71,8 +65,7 @@
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
( MkAnn
|
( MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -80,10 +73,9 @@
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
} :< CompoundF
|
} :< CompoundF
|
||||||
( ListF Ordinary
|
( ListF StyleData
|
||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -93,8 +85,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "가" )
|
( SimpleSymbol "가" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -105,8 +96,7 @@
|
|||||||
( DotListF
|
( DotListF
|
||||||
(
|
(
|
||||||
( MkAnn
|
( MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -118,8 +108,7 @@
|
|||||||
) :| []
|
) :| []
|
||||||
)
|
)
|
||||||
( MkAnn
|
( MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -131,8 +120,7 @@
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/list/source.scm"
|
{ sourceName = "golden/read/list/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/meta-expression/source.scm"
|
{ sourceName = "golden/read/meta-expression/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/meta-variable/source.scm"
|
{ sourceName = "golden/read/meta-variable/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
|
|||||||
@@ -1,7 +1,61 @@
|
|||||||
[ SynNone :< SimpleF
|
[ MkAnn
|
||||||
|
{ 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 ".." )
|
||||||
, SynNone :< SimpleF
|
, MkAnn
|
||||||
|
{ position = Just
|
||||||
|
( SourcePos
|
||||||
|
{ sourceName = "golden/read/peculiar-identifier-dot/source.scm"
|
||||||
|
, sourceLine = Pos 3
|
||||||
|
, sourceColumn = Pos 1
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} :< SimpleF
|
||||||
( SimpleSymbol ".abc" )
|
( SimpleSymbol ".abc" )
|
||||||
, SynNone :< SimpleF
|
, MkAnn
|
||||||
|
{ 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 +1,6 @@
|
|||||||
.. .abc ....abcc
|
...
|
||||||
|
..
|
||||||
|
.abc
|
||||||
|
....abcc
|
||||||
|
.++-
|
||||||
|
.-
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -10,8 +9,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "+" )
|
( SimpleSymbol "+" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -20,4 +18,54 @@
|
|||||||
)
|
)
|
||||||
} :< 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 +1,3 @@
|
|||||||
+ -
|
+ -
|
||||||
|
|
||||||
|
+. +.. -. -...abc -abc..
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/string/source.scm"
|
{ sourceName = "golden/read/string/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -10,8 +9,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "abc" )
|
( SimpleSymbol "abc" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -21,8 +19,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleString "xyz" )
|
( SimpleString "xyz" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -32,8 +29,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "수학" )
|
( SimpleSymbol "수학" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
@@ -41,10 +37,9 @@
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
} :< CompoundF
|
} :< CompoundF
|
||||||
( ListF Ordinary
|
( ListF StyleData
|
||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, 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
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[ MkAnn
|
[ MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -10,8 +9,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "abc" )
|
( SimpleSymbol "abc" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -21,8 +19,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "bala-hwa$" )
|
( SimpleSymbol "bala-hwa$" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -32,8 +29,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "x!!!" )
|
( SimpleSymbol "x!!!" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -43,8 +39,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "z" )
|
( SimpleSymbol "z" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -54,8 +49,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "z123" )
|
( SimpleSymbol "z123" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -65,8 +59,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "나는너무졸리다" )
|
( SimpleSymbol "나는너무졸리다" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 1
|
, sourceLine = Pos 1
|
||||||
@@ -76,8 +69,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "學" )
|
( SimpleSymbol "學" )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 3
|
, sourceLine = Pos 3
|
||||||
@@ -87,8 +79,7 @@
|
|||||||
} :< SimpleF
|
} :< SimpleF
|
||||||
( SimpleSymbol "車室." )
|
( SimpleSymbol "車室." )
|
||||||
, MkAnn
|
, MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Just
|
||||||
, position = Just
|
|
||||||
( SourcePos
|
( SourcePos
|
||||||
{ sourceName = "golden/read/typical-identifier/source.scm"
|
{ sourceName = "golden/read/typical-identifier/source.scm"
|
||||||
, sourceLine = Pos 5
|
, sourceLine = Pos 5
|
||||||
|
|||||||
+9
-1
@@ -26,6 +26,7 @@ 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
|
||||||
@@ -54,6 +55,7 @@ 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:
|
||||||
@@ -66,9 +68,12 @@ library
|
|||||||
Gyehoek.GenSym
|
Gyehoek.GenSym
|
||||||
Gyehoek.Jalmot
|
Gyehoek.Jalmot
|
||||||
Gyehoek.Language
|
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
|
||||||
@@ -109,14 +114,17 @@ library
|
|||||||
, 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
|
||||||
, tardis
|
, witherable
|
||||||
|
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
default-language: GHC2024
|
default-language: GHC2024
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
(define-syntax if-not
|
||||||
|
(syntax-rules ()
|
||||||
|
((_ c t f) (if (not c) t f))
|
||||||
|
((_ c t) (if (not c) t))))
|
||||||
|
|
||||||
|
(write (macroexpand-1 '(if-not #t 123 456)))
|
||||||
|
|
||||||
|
(define (main)
|
||||||
|
(let loop ((datum (read)))
|
||||||
|
(unless (eof-object? datum)
|
||||||
|
())))
|
||||||
@@ -18,7 +18,7 @@ genCodeName f = gensym' @Name $ f ^. _Wrapped' . to (<> "-code")
|
|||||||
|
|
||||||
bindEnv :: List Name -> Exp -> Exp
|
bindEnv :: List Name -> Exp -> Exp
|
||||||
bindEnv frees m = [cps|
|
bindEnv frees m = [cps|
|
||||||
(prim (get-env) (κ #{frees} #{m}))
|
(builtin (get-env) (κ #{frees} #{m}))
|
||||||
|]
|
|]
|
||||||
|
|
||||||
close1 :: forall es. GenSym :> es => Exp -> Eff es Exp
|
close1 :: forall es. GenSym :> es => Exp -> Eff es Exp
|
||||||
@@ -41,7 +41,7 @@ close1 = \case
|
|||||||
let codes = bs' ^.. each . _1 . to MkLabel
|
let codes = bs' ^.. each . _1 . to MkLabel
|
||||||
pure [cps|
|
pure [cps|
|
||||||
(letrec #{bs'}
|
(letrec #{bs'}
|
||||||
(prim (make-shared-closure #{codes} #{frees})
|
(builtin (make-shared-closure #{codes} #{frees})
|
||||||
(κ #{boundNames}
|
(κ #{boundNames}
|
||||||
#{e})))
|
#{e})))
|
||||||
|]
|
|]
|
||||||
|
|||||||
@@ -46,18 +46,13 @@ convert (Scm.ExpLit l) k = k . one . ValImm $ case l of
|
|||||||
LitBool b -> ImmBool b
|
LitBool b -> ImmBool b
|
||||||
_ -> _
|
_ -> _
|
||||||
|
|
||||||
convert (Scm.ExpPrim p) k =
|
convert (Scm.ExpBuiltin p) k =
|
||||||
telescope (convert1 @es) p \p' -> do
|
telescope (convert1 @es) p \p' -> do
|
||||||
r_l <- gensym' "r"
|
r_l <- gensym' "r"
|
||||||
-- k_l <- gensym' @Name "prim-k"
|
|
||||||
m <- k [ValVar r_l]
|
m <- k [ValVar r_l]
|
||||||
pure [cps|
|
pure [cps|
|
||||||
(prim #{p'} (κ (#{r_l}) #{m}))
|
(builtin #{p'} (κ (#{r_l}) #{m}))
|
||||||
|]
|
|]
|
||||||
-- pure [cps|
|
|
||||||
-- (letrec ((#{k_l} (κ (#{r_l}) #{m})))
|
|
||||||
-- (prim #{p'} #{k_l}))
|
|
||||||
-- |]
|
|
||||||
|
|
||||||
convert (Scm.ExpLambda xs e) k = do
|
convert (Scm.ExpLambda xs e) k = do
|
||||||
f <- gensym' "lambda-body"
|
f <- gensym' "lambda-body"
|
||||||
@@ -78,7 +73,7 @@ convert (Scm.ExpApply f xs) k =
|
|||||||
|
|
||||||
convert (Scm.ExpBegin xs) k = telescope (convert @es) xs (k . NE.last)
|
convert (Scm.ExpBegin xs) k = telescope (convert @es) xs (k . NE.last)
|
||||||
|
|
||||||
convert (Scm.ExpIf c t f) k =
|
convert (Scm.ExpIf c t (Just f)) k =
|
||||||
convert1 c \c' -> do
|
convert1 c \c' -> do
|
||||||
t_l <- gensym' @Name "truthy-cont"
|
t_l <- gensym' @Name "truthy-cont"
|
||||||
f_l <- gensym' @Name "falsey-cont"
|
f_l <- gensym' @Name "falsey-cont"
|
||||||
|
|||||||
+16
-16
@@ -226,7 +226,7 @@ eval g dps (ExpLetRec bs e) = do
|
|||||||
traverse_ (uncurry assign) $ zip ls (bs' ^.. each . _2)
|
traverse_ (uncurry assign) $ zip ls (bs' ^.. each . _2)
|
||||||
eval g' dps e
|
eval g' dps e
|
||||||
|
|
||||||
eval g dps (ExpPrim (PrimCallCC withcc) k) = do
|
eval g dps (ExpBuiltin (BuiltinCallCC withcc) k) = do
|
||||||
withcc' <- evalVal g withcc >>= orWrong #_EProcedure
|
withcc' <- evalVal g withcc >>= orWrong #_EProcedure
|
||||||
[i|call/cc: 함수가 아닌 것을 받았다|]
|
[i|call/cc: 함수가 아닌 것을 받았다|]
|
||||||
k' <- evalKexp g k
|
k' <- evalKexp g k
|
||||||
@@ -236,8 +236,8 @@ eval g dps (ExpPrim (PrimCallCC withcc) k) = do
|
|||||||
Nothing -> wrong [i|call/cc: 잘못하는데!|]
|
Nothing -> wrong [i|call/cc: 잘못하는데!|]
|
||||||
withcc' [cc,k'] dps
|
withcc' [cc,k'] dps
|
||||||
|
|
||||||
eval g dps (ExpPrim p k) = do
|
eval g dps (ExpBuiltin p k) = do
|
||||||
p' <- evalPrim g dps =<< traverse (evalVal g) p
|
p' <- evalBuiltin g dps =<< traverse (evalVal g) p
|
||||||
evalKexp g k >>= \case
|
evalKexp g k >>= \case
|
||||||
EProcedure fp -> fp p' dps
|
EProcedure fp -> fp p' dps
|
||||||
_ -> wrong [i|prim(#{p})의 계속을 나쁘다|]
|
_ -> wrong [i|prim(#{p})의 계속을 나쁘다|]
|
||||||
@@ -251,20 +251,20 @@ eval g dps (ExpIf c t f) = do
|
|||||||
|
|
||||||
eval g dps e = error [i|unimplemented #{e}|]
|
eval g dps e = error [i|unimplemented #{e}|]
|
||||||
|
|
||||||
evalPrim :: Env -> DynPoints -> Prim E -> M Answer (List E)
|
evalBuiltin :: Env -> DynPoints -> Builtin E -> M Answer (List E)
|
||||||
evalPrim g dps = \case
|
evalBuiltin g dps = \case
|
||||||
PrimAdd x y -> arith2 (+) x y
|
BuiltinAdd x y -> arith2 (+) x y
|
||||||
PrimMul x y -> arith2 (*) x y
|
BuiltinMul x y -> arith2 (*) x y
|
||||||
PrimSub x y -> arith2 (-) x y
|
BuiltinSub x y -> arith2 (-) x y
|
||||||
PrimDiv x y -> arith2 div x y
|
BuiltinDiv x y -> arith2 div x y
|
||||||
PrimZeroP x -> pure1 . EBool . isJust $ x ^? #EInt . only 0
|
BuiltinZeroP x -> pure1 . EBool . isJust $ x ^? #EInt . only 0
|
||||||
PrimCons x y -> pcons x y >>= pure1
|
BuiltinCons x y -> pcons x y >>= pure1
|
||||||
PrimCar p -> cr p _2
|
BuiltinCar p -> cr p _2
|
||||||
PrimCdr p -> cr p _3
|
BuiltinCdr p -> cr p _3
|
||||||
PrimPairP p -> pure1 . EBool . maybe False (const True) $
|
BuiltinPairP p -> pure1 . EBool . maybe False (const True) $
|
||||||
p ^? #_EPair
|
p ^? #_EPair
|
||||||
PrimValues xs -> pure xs
|
BuiltinValues xs -> pure xs
|
||||||
PrimList xs -> foldrM pcons ENull xs >>= pure1
|
BuiltinList xs -> foldrM pcons ENull xs >>= pure1
|
||||||
p -> wrong [i|prim(#{p})은 벌써 나지 않다|]
|
p -> wrong [i|prim(#{p})은 벌써 나지 않다|]
|
||||||
where
|
where
|
||||||
pure1 x = pure [x]
|
pure1 x = pure [x]
|
||||||
|
|||||||
+19
-25
@@ -5,6 +5,8 @@
|
|||||||
{-# 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(..)
|
||||||
@@ -13,7 +15,7 @@ module Gyehoek.CPS.Syntax
|
|||||||
, Kexp(..)
|
, Kexp(..)
|
||||||
, ExpF(..)
|
, ExpF(..)
|
||||||
, Name(..)
|
, Name(..)
|
||||||
, Prim(..)
|
, Builtin(..)
|
||||||
, Program(..)
|
, Program(..)
|
||||||
, HoistedProgram(..)
|
, HoistedProgram(..)
|
||||||
, Lit(..)
|
, Lit(..)
|
||||||
@@ -25,7 +27,7 @@ module Gyehoek.CPS.Syntax
|
|||||||
, pattern Halt
|
, pattern Halt
|
||||||
, pattern Halt1
|
, pattern Halt1
|
||||||
, _MkKappa
|
, _MkKappa
|
||||||
, _ExpPrim
|
, _ExpBuiltin
|
||||||
, _ExpLetRec
|
, _ExpLetRec
|
||||||
, _ExpApply
|
, _ExpApply
|
||||||
, _AbsLambda'
|
, _AbsLambda'
|
||||||
@@ -50,7 +52,7 @@ module Gyehoek.CPS.Syntax
|
|||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
import Gyehoek.Scheme.Syntax (Name (..), Prim(..), primDatumIso, Lit(..))
|
import Gyehoek.Scheme.Syntax (Name (..), Builtin(..), builtinDatumIso, 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)
|
||||||
@@ -59,15 +61,12 @@ 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 (Datum)
|
import Gyehoek.Sexp (G, (:-)(..), Datum)
|
||||||
import Gyehoek.Sexp (G, (:-)(..))
|
|
||||||
import qualified Data.InvertibleGrammar.Base as IG
|
import qualified Data.InvertibleGrammar.Base as IG
|
||||||
import Gyehoek.GenSym (Gen)
|
import Gyehoek.GenSym (Gen)
|
||||||
import Data.String (IsString)
|
import Data.String (IsString)
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import qualified Data.HashMap.Strict as H
|
import qualified Data.HashMap.Strict as H
|
||||||
import GHC.Records (HasField (..))
|
|
||||||
import Data.Bifunctor
|
|
||||||
|
|
||||||
-- Data types
|
-- Data types
|
||||||
|
|
||||||
@@ -103,6 +102,7 @@ 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)
|
pattern ObjLabel l = ObjImm (ImmLabel l)
|
||||||
|
|
||||||
-- | a heap object.
|
-- | a heap object.
|
||||||
@@ -163,7 +163,7 @@ pattern ExpJump f xs ktail <- (preview _ExpJump -> Just (f,xs,ktail))
|
|||||||
where ExpJump f xs ktail = review _ExpJump (f,xs,ktail)
|
where ExpJump f xs ktail = review _ExpJump (f,xs,ktail)
|
||||||
|
|
||||||
data Exp
|
data Exp
|
||||||
= ExpPrim (Prim Val) Kexp
|
= ExpBuiltin (Builtin Val) Kexp
|
||||||
| ExpLetRec { binders :: List (Name, Abs), body :: Exp }
|
| ExpLetRec { binders :: List (Name, Abs), body :: Exp }
|
||||||
| ExpContinue Val (List Val)
|
| ExpContinue Val (List Val)
|
||||||
| ExpIf Val Name Name
|
| ExpIf Val Name Name
|
||||||
@@ -188,7 +188,7 @@ 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)
|
||||||
|
|
||||||
data Program = MkProgram
|
newtype Program = MkProgram
|
||||||
{ body :: Lambda
|
{ body :: Lambda
|
||||||
}
|
}
|
||||||
deriving (Show, Generic, Data)
|
deriving (Show, Generic, Data)
|
||||||
@@ -272,7 +272,7 @@ instance S.DatumIso Imm where
|
|||||||
|
|
||||||
instance S.DatumIso Label where
|
instance S.DatumIso Label where
|
||||||
datumIso = S.with \g -> S.coproduct
|
datumIso = S.with \g -> S.coproduct
|
||||||
[ S.decorate S.SynConstant >>> S.datumIso @Name >>> S.prismIso
|
[ 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)
|
||||||
@@ -281,7 +281,7 @@ instance S.DatumIso Label where
|
|||||||
|
|
||||||
instance S.DatumIso Reg where
|
instance S.DatumIso Reg where
|
||||||
datumIso = S.with \g ->
|
datumIso = S.with \g ->
|
||||||
S.decorate S.SynVariable >>> S.datumIso @Name >>> S.prismIso
|
S.datumIso @Name >>> S.prismIso
|
||||||
(S.expected "register")
|
(S.expected "register")
|
||||||
(prefixed @Name "%")
|
(prefixed @Name "%")
|
||||||
>>> g
|
>>> g
|
||||||
@@ -335,16 +335,16 @@ instance S.DatumIso Abs where
|
|||||||
|
|
||||||
instance S.DatumIso Exp where
|
instance S.DatumIso Exp where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (. prim)
|
$ S.With (. builtin)
|
||||||
$ 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.list $
|
continue = S.listWithStyle (S.StyleSyntax 1) $
|
||||||
S.el (S.decorate S.SynBuiltin >>> S.sym "continue")
|
S.el (S.sym "continue")
|
||||||
>>> S.el (S.decorate S.SynProcedure >>> S.datumIso)
|
>>> S.el 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"
|
||||||
@@ -365,9 +365,9 @@ instance S.DatumIso Exp where
|
|||||||
>>> S.el (S.datumIso @Kexp)
|
>>> S.el (S.datumIso @Kexp)
|
||||||
>>> S.rest (S.datumIso @Val)
|
>>> S.rest (S.datumIso @Val)
|
||||||
>>> S.onTail S.swap
|
>>> S.onTail S.swap
|
||||||
prim = S.list $
|
builtin = S.listWithStyle (S.StyleSyntax 1) $
|
||||||
S.el (S.decorate S.SynBuiltin >>> S.sym "prim")
|
S.el (S.sym "builtin")
|
||||||
>>> S.el (primDatumIso id (S.datumIso @Val))
|
>>> S.el (builtinDatumIso id (S.datumIso @Val))
|
||||||
>>> S.el S.datumIso
|
>>> S.el S.datumIso
|
||||||
|
|
||||||
instance S.DatumIso Kexp where
|
instance S.DatumIso Kexp where
|
||||||
@@ -414,15 +414,9 @@ 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
|
||||||
@@ -453,7 +447,7 @@ instance Free Kexp where
|
|||||||
|
|
||||||
instance Free Exp where
|
instance Free Exp where
|
||||||
freeWithBound' bound = \case
|
freeWithBound' bound = \case
|
||||||
ExpPrim p k ->
|
ExpBuiltin p k ->
|
||||||
(p ^.. folded . #ValVar . filtered (`notElem` bound))
|
(p ^.. folded . #ValVar . filtered (`notElem` bound))
|
||||||
++ freeWithBound' bound k
|
++ freeWithBound' bound k
|
||||||
ExpLetRec bs m ->
|
ExpLetRec bs m ->
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
{-# 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
|
||||||
@@ -0,0 +1,536 @@
|
|||||||
|
{-# 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)
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,536 @@
|
|||||||
|
{-# 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)
|
||||||
|
]
|
||||||
|
}
|
||||||
+225
-50
@@ -11,16 +11,23 @@
|
|||||||
{-# LANGUAGE PatternSynonyms #-}
|
{-# LANGUAGE PatternSynonyms #-}
|
||||||
{-# LANGUAGE DeriveAnyClass #-}
|
{-# LANGUAGE DeriveAnyClass #-}
|
||||||
{-# LANGUAGE ViewPatterns #-}
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
|
{- HLINT ignore "Avoid lambda using `infix`" -}
|
||||||
|
{- HLINT ignore "Redundant $" -}
|
||||||
module Gyehoek.Scheme.Syntax
|
module Gyehoek.Scheme.Syntax
|
||||||
( Name(..)
|
( Name(..)
|
||||||
, Prim(..)
|
, Builtin(..)
|
||||||
, Lit(..)
|
, Lit(..)
|
||||||
, Def(..)
|
, Def(..)
|
||||||
, Exp(..)
|
, Exp(..)
|
||||||
, ExpF(..)
|
, ExpF(..)
|
||||||
, Program(..)
|
, Program(..)
|
||||||
, CommandOrDef(..)
|
, CommandOrDef(..)
|
||||||
, primDatumIso
|
, Trans(..)
|
||||||
|
, Rule(..)
|
||||||
|
, Pat(..)
|
||||||
|
, Tem(..)
|
||||||
|
, El(..)
|
||||||
|
, builtinDatumIso
|
||||||
, free
|
, free
|
||||||
, subst
|
, subst
|
||||||
, getName
|
, getName
|
||||||
@@ -31,8 +38,6 @@ 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
|
||||||
@@ -45,12 +50,7 @@ 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, G, DataIso, (:-)((:-)))
|
||||||
import Gyehoek.Prelude
|
import Gyehoek.Prelude
|
||||||
@@ -69,36 +69,36 @@ instance Prefixed Name where
|
|||||||
getName :: Name -> Text
|
getName :: Name -> Text
|
||||||
getName (MkName x) = x
|
getName (MkName x) = x
|
||||||
|
|
||||||
data Prim e
|
data Builtin e
|
||||||
= PrimAdd e e
|
= BuiltinAdd e e
|
||||||
| PrimSub e e
|
| BuiltinSub e e
|
||||||
| PrimMul e e
|
| BuiltinMul e e
|
||||||
| PrimDiv e e
|
| BuiltinDiv e e
|
||||||
| PrimCons e e
|
| BuiltinCons e e
|
||||||
| PrimCar e
|
| BuiltinCar e
|
||||||
| PrimCdr e
|
| BuiltinCdr e
|
||||||
| PrimImmediateP e
|
| BuiltinImmediateP e
|
||||||
| PrimConsP e
|
| BuiltinConsP e
|
||||||
| PrimIntegerP e
|
| BuiltinIntegerP e
|
||||||
| PrimWrite e
|
| BuiltinWrite e
|
||||||
| PrimZeroP e
|
| BuiltinZeroP e
|
||||||
| PrimNewline
|
| BuiltinNewline
|
||||||
| PrimMakeClosure { code :: e, env :: List e }
|
| BuiltinMakeClosure { code :: e, env :: List e }
|
||||||
| PrimMakeSharedClosure { codes :: List e, env :: List e }
|
| BuiltinMakeSharedClosure { codes :: List e, env :: List e }
|
||||||
| PrimGetEnv
|
| BuiltinGetEnv
|
||||||
| PrimEnv
|
| BuiltinEnv
|
||||||
| PrimEnvRef Int
|
| BuiltinEnvRef Int
|
||||||
| PrimCallCC e
|
| BuiltinCallCC e
|
||||||
| PrimCaptureCC
|
| BuiltinCaptureCC
|
||||||
| PrimInvokeCC e (List e)
|
| BuiltinInvokeCC e (List e)
|
||||||
| PrimValues (List e)
|
| BuiltinValues (List e)
|
||||||
| PrimCallWithValues e e
|
| BuiltinCallWithValues e e
|
||||||
| PrimPairP e
|
| BuiltinPairP e
|
||||||
| PrimList (List 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 (Prim e) (Prim e') e e'
|
instance Each (Builtin e) (Builtin e') e e'
|
||||||
|
|
||||||
data Lit
|
data Lit
|
||||||
= LitInt Int
|
= LitInt Int
|
||||||
@@ -113,19 +113,79 @@ 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
|
||||||
| ExpPrim (Prim Exp)
|
| ExpBuiltin (Builtin Exp)
|
||||||
| ExpBegin (NonEmpty Exp)
|
| ExpBegin (NonEmpty Exp)
|
||||||
| ExpIf Exp Exp Exp
|
| ExpIf Exp Exp (Maybe 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
|
||||||
@@ -182,14 +242,12 @@ makeBaseFunctor ''Exp
|
|||||||
|
|
||||||
|
|
||||||
instance DatumIso Name where
|
instance DatumIso Name where
|
||||||
datumIso = S.decorate S.SynVariable
|
datumIso = S.symbol >>> S.iso coerce coerce
|
||||||
>>> S.symbol
|
|
||||||
>>> S.iso coerce coerce
|
|
||||||
|
|
||||||
primDatumIso
|
builtinDatumIso
|
||||||
:: (Text -> Text)
|
:: (Text -> Text)
|
||||||
-> S.DatumGrammar a -> S.DatumGrammar (Prim a)
|
-> S.DatumGrammar a -> S.DatumGrammar (Builtin a)
|
||||||
primDatumIso namefn a = S.match
|
builtinDatumIso namefn a = S.match
|
||||||
$ S.With (. ht2 "+")
|
$ S.With (. ht2 "+")
|
||||||
$ S.With (. ht2 "-")
|
$ S.With (. ht2 "-")
|
||||||
$ S.With (. ht2 "*")
|
$ S.With (. ht2 "*")
|
||||||
@@ -226,8 +284,8 @@ primDatumIso namefn a = S.match
|
|||||||
ht1' s = S.headTagged1' (namefn s) a a
|
ht1' s = S.headTagged1' (namefn s) a a
|
||||||
ht0' s = S.headTagged0' (namefn s) a
|
ht0' s = S.headTagged0' (namefn s) a
|
||||||
|
|
||||||
instance DatumIso a => DatumIso (Prim a) where
|
instance DatumIso a => DatumIso (Builtin a) where
|
||||||
datumIso = primDatumIso id S.datumIso
|
datumIso = builtinDatumIso id S.datumIso
|
||||||
|
|
||||||
instance DatumIso Lit where
|
instance DatumIso Lit where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
@@ -248,20 +306,42 @@ 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 (. begin)
|
||||||
$ S.With (. S.ifLike "if" S.datumIso S.datumIso S.datumIso)
|
$ S.With (. if_)
|
||||||
$ 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 :: forall t. G (S.Datum :- t) (NonEmpty Exp :- t)
|
||||||
begin = S.beginLike "begin" $
|
begin = S.beginLike "begin" $
|
||||||
S.el (S.datumIso @Exp) >>> S.rest (S.datumIso @Exp)
|
S.el (S.datumIso @Exp) >>> S.rest (S.datumIso @Exp)
|
||||||
@@ -269,6 +349,101 @@ instance DatumIso Exp where
|
|||||||
(\(xs:-x:-t) -> (x:|xs):-t)
|
(\(xs:-x:-t) -> (x:|xs):-t)
|
||||||
(\((x:|xs):-t) -> xs:-x:-t))
|
(\((x:|xs):-t) -> xs:-x:-t))
|
||||||
|
|
||||||
|
instance S.DatumIso Rule where
|
||||||
|
datumIso = S.with \g ->
|
||||||
|
S.list (S.el S.datumIso >>> S.el S.datumIso) >>> g
|
||||||
|
|
||||||
|
instance S.DatumIso Pat where
|
||||||
|
datumIso = S.match
|
||||||
|
$ S.With (S.sym "_" >>>)
|
||||||
|
$ S.With (S.datumIso >>>)
|
||||||
|
$ S.With (lst >>>)
|
||||||
|
$ S.End
|
||||||
|
where
|
||||||
|
lst :: S.G (S.Datum :- t) (Maybe Pat :- Maybe (List Pat) :- List Pat :- t)
|
||||||
|
lst = S.coproduct
|
||||||
|
[ S.list $
|
||||||
|
ellipsis
|
||||||
|
>>> S.onTail (S.push Nothing (is _Nothing) (const mempty))
|
||||||
|
, S.dottedList
|
||||||
|
ellipsis
|
||||||
|
( S.datumIso @Pat
|
||||||
|
>>> S.partialIso Just (maybe (Left mempty) Right) )
|
||||||
|
]
|
||||||
|
ellipsis =
|
||||||
|
S.restData split
|
||||||
|
>>> S.onTail
|
||||||
|
( S.onHead (S.traversed . S.traversed . S.sealed $
|
||||||
|
S.datumIso @Pat)
|
||||||
|
>>> S.onTail (S.onHead . S.traversed . S.sealed $
|
||||||
|
S.datumIso @Pat)
|
||||||
|
)
|
||||||
|
split
|
||||||
|
:: forall t. S.G (List S.Datum :- t)
|
||||||
|
(Maybe (List S.Datum) :- List S.Datum :- t)
|
||||||
|
split = S.Iso
|
||||||
|
(\(ps0:-t) ->
|
||||||
|
let (ps,ell) = splitEllipsis ps0
|
||||||
|
in ell :- ps :- t)
|
||||||
|
(\(ell:-ps:-t) -> (ps ++ foldMap ([S.Symbol "..."]++) ell) :- t)
|
||||||
|
|
||||||
|
splitEllipsis :: List S.Datum -> (List S.Datum, Maybe (List S.Datum))
|
||||||
|
splitEllipsis [] = ([], Nothing)
|
||||||
|
splitEllipsis (S.Symbol "..." : xs) = ([], Just xs)
|
||||||
|
splitEllipsis (x:xs) = splitEllipsis xs & _1 %~ (x:)
|
||||||
|
|
||||||
|
instance S.DataIso El where
|
||||||
|
dataIso = S.match
|
||||||
|
$ S.With (ellipsis >>>)
|
||||||
|
$ S.With (noellipsis >>>)
|
||||||
|
$ S.End
|
||||||
|
where
|
||||||
|
ellipsis = S.recontextualise $
|
||||||
|
S.el (S.datumIso @Tem) >>> S.el (S.sym "...")
|
||||||
|
noellipsis = S.recontextualise $ S.el (S.datumIso @Tem)
|
||||||
|
|
||||||
|
instance S.DatumIso Tem where
|
||||||
|
datumIso = S.match
|
||||||
|
$ S.With (lst >>>)
|
||||||
|
$ S.With (trail >>>)
|
||||||
|
$ S.With (S.datumIso @Lit >>>)
|
||||||
|
$ S.With (S.datumIso @Name >>>)
|
||||||
|
$ S.End
|
||||||
|
where
|
||||||
|
trail = S.list $ S.el (S.sym "...") >>> S.el (S.datumIso @Tem)
|
||||||
|
|
||||||
|
lst :: S.G (S.Datum :- t) (Maybe Tem :- List El :- t)
|
||||||
|
lst = S.coproduct
|
||||||
|
[ S.list els
|
||||||
|
>>> (S.push Nothing (is _Nothing) (const mempty))
|
||||||
|
, S.dottedList els $
|
||||||
|
S.datumIso @Tem
|
||||||
|
>>> S.partialIso Just (maybe (Left mempty) Right)
|
||||||
|
]
|
||||||
|
els :: S.G (S.ListContext :- t) (S.ListContext :- List El :- t)
|
||||||
|
els =
|
||||||
|
S.iso
|
||||||
|
(\(S.MkListContext ds) -> affixEllipses ds)
|
||||||
|
(S.MkListContext . foldMap \(d,b) ->
|
||||||
|
d : if b then [S.Symbol "..."] else [])
|
||||||
|
>>> S.onHead (S.traversed . S.sealed $
|
||||||
|
S.flipped S.pair
|
||||||
|
>>> S.onTail (S.datumIso @Tem)
|
||||||
|
>>> S.pair
|
||||||
|
>>> S.iso
|
||||||
|
(\(t,b) -> if b then Ellipsis t else El t)
|
||||||
|
(\case
|
||||||
|
Ellipsis t -> (t,True)
|
||||||
|
El t -> (t,False)))
|
||||||
|
>>> S.push (S.MkListContext [])
|
||||||
|
(\(S.MkListContext xs) -> null xs)
|
||||||
|
(const mempty)
|
||||||
|
affixEllipses :: List S.Datum -> List (S.Datum, Bool)
|
||||||
|
affixEllipses (x : S.Symbol "..." : xs) = (x,True) : affixEllipses xs
|
||||||
|
affixEllipses (x : xs) = (x,False) : affixEllipses xs
|
||||||
|
affixEllipses [] = []
|
||||||
|
|
||||||
|
|
||||||
instance DatumIso CommandOrDef where
|
instance DatumIso CommandOrDef where
|
||||||
datumIso = S.match
|
datumIso = S.match
|
||||||
$ S.With (\_Command -> _Command . S.datumIso)
|
$ S.With (\_Command -> _Command . S.datumIso)
|
||||||
@@ -299,16 +474,16 @@ instance DatumIso ImportSet where
|
|||||||
$ S.With (imp "rename" >>>)
|
$ S.With (imp "rename" >>>)
|
||||||
$ S.End
|
$ S.End
|
||||||
where
|
where
|
||||||
imp s = S.list $ S.el (S.symBuiltin s)
|
imp s = S.list $ S.el (S.sym s)
|
||||||
>>> S.el S.datumIso >>> S.restData S.dataIso
|
>>> S.el S.datumIso >>> S.restData S.dataIso
|
||||||
imp' s = S.list $
|
imp' s = S.list $
|
||||||
S.el (S.symBuiltin s)
|
S.el (S.sym s)
|
||||||
>>> S.el S.datumIso
|
>>> S.el S.datumIso
|
||||||
>>> S.el S.datumIso
|
>>> S.el S.datumIso
|
||||||
|
|
||||||
instance DatumIso ImportDecl where
|
instance DatumIso ImportDecl where
|
||||||
datumIso = S.with \decl ->
|
datumIso = S.with \decl ->
|
||||||
S.list (S.el (S.symBuiltin "import") >>> S.restData S.dataIso)
|
S.list (S.el (S.sym "import") >>> S.restData S.dataIso)
|
||||||
>>> decl
|
>>> decl
|
||||||
|
|
||||||
instance DataIso Program where
|
instance DataIso Program where
|
||||||
|
|||||||
@@ -179,6 +179,14 @@ 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
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
{- 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
|
||||||
@@ -8,12 +9,11 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, Grammar(..)
|
, Grammar(..)
|
||||||
, DatumGrammar
|
, DatumGrammar
|
||||||
, DataGrammar
|
, DataGrammar
|
||||||
, Grammar
|
|
||||||
, ListContext(..)
|
, ListContext(..)
|
||||||
, (:-)((:-))
|
, (:-)((:-))
|
||||||
-- * lists
|
-- * lists
|
||||||
, list
|
, list
|
||||||
, listWithIndentation
|
, listWithStyle
|
||||||
, el
|
, el
|
||||||
, rest
|
, rest
|
||||||
, restData
|
, restData
|
||||||
@@ -23,6 +23,7 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, headTagged1'
|
, headTagged1'
|
||||||
, headTagged1
|
, headTagged1
|
||||||
, headTagged2
|
, headTagged2
|
||||||
|
, headTagged2'
|
||||||
-- * atoms
|
-- * atoms
|
||||||
, simple
|
, simple
|
||||||
, string
|
, string
|
||||||
@@ -33,32 +34,29 @@ module Gyehoek.Sexp.Grammar.Base
|
|||||||
, integer
|
, integer
|
||||||
, int
|
, int
|
||||||
, unreadable
|
, unreadable
|
||||||
-- ** symbols
|
|
||||||
, symBuiltin
|
|
||||||
-- * TODO: sort lol
|
-- * TODO: sort lol
|
||||||
, prismIso
|
, prismIso
|
||||||
, isoIso, decorate
|
, isoIso
|
||||||
, snoced
|
, snoced
|
||||||
, letLike
|
, letLike
|
||||||
, ifLike
|
, ifLike
|
||||||
, lambdaLike
|
, lambdaLike
|
||||||
, lambdaKeyword
|
, lambdaKeyword
|
||||||
, kappaKeyword
|
, kappaKeyword
|
||||||
, beginLike, headTagged2', dottedList
|
, beginLike
|
||||||
|
, 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 (traversed, iso, cons, coerced, Iso, Simple, simple)
|
import Gyehoek.Prelude hiding (flipped, traversed, iso, cons, coerced, Iso, Simple, simple)
|
||||||
import Gyehoek.Sexp.Syntax hiding (position)
|
import Gyehoek.Sexp.Syntax hiding (position)
|
||||||
import Gyehoek.Sexp.Print (printDatum')
|
import Gyehoek.Sexp.Print (printDatum')
|
||||||
import Data.Scientific (Scientific)
|
import Data.Scientific (Scientific)
|
||||||
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 Control.Monad.RWS (modify)
|
|
||||||
import qualified Data.List.NonEmpty as NE
|
import qualified Data.List.NonEmpty as NE
|
||||||
import Data.Foldable (toList)
|
import Data.Foldable (toList)
|
||||||
|
|
||||||
@@ -89,14 +87,6 @@ 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
|
||||||
@@ -108,7 +98,7 @@ unexpectedDatum = unexpected . printDatum'
|
|||||||
list
|
list
|
||||||
:: G (ListContext :- t) (ListContext :- t')
|
:: G (ListContext :- t) (ListContext :- t')
|
||||||
-> G (Datum :- t) t'
|
-> G (Datum :- t) t'
|
||||||
list = listWithIndentation Ordinary
|
list = listWithStyle StyleData
|
||||||
|
|
||||||
-- |
|
-- |
|
||||||
-- >>> let grammar = with \g -> dottedList (el int) int >>> g
|
-- >>> let grammar = with \g -> dottedList (el int) int >>> g
|
||||||
@@ -143,11 +133,11 @@ dottedList g final = begin >>> Dive (onTail (g >>> end) >>> final)
|
|||||||
[] -> Right t
|
[] -> Right t
|
||||||
d:_ -> Left $ unexpectedDatum d)
|
d:_ -> Left $ unexpectedDatum d)
|
||||||
|
|
||||||
listWithIndentation
|
listWithStyle
|
||||||
:: Indentation
|
:: Style
|
||||||
-> G (ListContext :- t) (ListContext :- t')
|
-> G (ListContext :- t) (ListContext :- t')
|
||||||
-> G (Datum :- t) t'
|
-> G (Datum :- t) t'
|
||||||
listWithIndentation ind g = begin >>> Dive (g >>> end)
|
listWithStyle ind g = begin >>> Dive (g >>> end)
|
||||||
where
|
where
|
||||||
begin = locate >>> partialOsi
|
begin = locate >>> partialOsi
|
||||||
(\case
|
(\case
|
||||||
@@ -169,6 +159,33 @@ 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.
|
||||||
--
|
--
|
||||||
@@ -235,8 +252,8 @@ 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) (a :- t)
|
:: G (List Datum :- t) t'
|
||||||
-> G (ListContext :- t) (ListContext :- a :- t)
|
-> G (ListContext :- t) (ListContext :- t')
|
||||||
restData g =
|
restData g =
|
||||||
iso coerce coerce
|
iso coerce coerce
|
||||||
>>> g
|
>>> g
|
||||||
@@ -351,33 +368,36 @@ 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 = list $ el (symProcedure s)
|
headTagged0 s = listWithStyle StyleCode $ el (sym s)
|
||||||
|
|
||||||
headTagged0' :: Text -> DatumGrammar a -> G (Datum :- t) (List a :- t)
|
headTagged0' :: Text -> DatumGrammar a -> G (Datum :- t) (List a :- t)
|
||||||
headTagged0' s gt = list $ el (symProcedure s) >>> rest gt
|
headTagged0' s gt = listWithStyle StyleCode $ el (sym s) >>> rest gt
|
||||||
|
|
||||||
headTagged1 :: Text -> DatumGrammar a -> G (Datum :- t) (a :- t)
|
headTagged1 :: Text -> DatumGrammar a -> G (Datum :- t) (a :- t)
|
||||||
headTagged1 s g1 = list $ el (symProcedure s) >>> el g1
|
headTagged1 s g1 = listWithStyle StyleCode $ el (sym 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 (symProcedure s) >>> el g1 >>> rest gt
|
headTagged1' s g1 gt = list $ el (sym 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 = list $ el (symProcedure s) >>> el g1 >>> el g2
|
headTagged2 s g1 g2 = listWithStyle StyleCode $ el (sym 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 =
|
||||||
list $ el (symProcedure s) >>> el g1 >>> el g2 >>> rest gt
|
listWithStyle StyleCode $ el (sym s) >>> el g1 >>> el g2 >>> rest gt
|
||||||
|
|
||||||
ifLike
|
ifLike
|
||||||
-- | keyword
|
-- | keyword
|
||||||
@@ -390,21 +410,17 @@ 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 =
|
||||||
listWithIndentation (NSpecial 1) $
|
listWithStyle (StyleSyntax 1) $
|
||||||
el (symBuiltin kw) >>> el c >>> el t >>> el f
|
el (sym 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 = listWithIndentation (NSpecial 1) $
|
letLike kw name rhs e = listWithStyle (StyleSyntax 1) $
|
||||||
el (symBuiltin kw) >>> el bindings >>> el e
|
el (sym 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)
|
||||||
@@ -415,8 +431,8 @@ lambdaLike
|
|||||||
-> G (Datum :- t1) (a :- t2)
|
-> G (Datum :- t1) (a :- t2)
|
||||||
-> G (ListContext :- a :- t2) (ListContext :- t3)
|
-> G (ListContext :- a :- t2) (ListContext :- t3)
|
||||||
-> G (Datum :- t1) t3
|
-> G (Datum :- t1) t3
|
||||||
lambdaLike kw formals body = listWithIndentation (NSpecial 1) $
|
lambdaLike kw formals body = listWithStyle (StyleSyntax 1) $
|
||||||
el (decorate SynBuiltin >>> kw)
|
el kw
|
||||||
>>> el formals
|
>>> el formals
|
||||||
>>> body
|
>>> body
|
||||||
|
|
||||||
@@ -431,8 +447,8 @@ beginLike
|
|||||||
-> G (ListContext :- t) (ListContext :- t')
|
-> G (ListContext :- t) (ListContext :- t')
|
||||||
-> G (Datum :- t) t'
|
-> G (Datum :- t) t'
|
||||||
beginLike kw g =
|
beginLike kw g =
|
||||||
listWithIndentation (NSpecial 0) $
|
listWithStyle (StyleSyntax 0) $
|
||||||
el (symBuiltin kw) >>> g
|
el (sym kw) >>> g
|
||||||
|
|
||||||
-- | define a printed syntax for an object which cannot be read.
|
-- | define a printed syntax for an object which cannot be read.
|
||||||
unreadable
|
unreadable
|
||||||
|
|||||||
+27
-21
@@ -1,3 +1,4 @@
|
|||||||
|
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
|
||||||
module Gyehoek.Sexp.Print
|
module Gyehoek.Sexp.Print
|
||||||
( printDatum
|
( printDatum
|
||||||
, printDatumW
|
, printDatumW
|
||||||
@@ -6,26 +7,34 @@ module Gyehoek.Sexp.Print
|
|||||||
, printData'
|
, printData'
|
||||||
, htmlDatum
|
, htmlDatum
|
||||||
, htmlData
|
, htmlData
|
||||||
|
, putDoc
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Gyehoek.Sexp.Syntax
|
import Gyehoek.Sexp.Syntax
|
||||||
import Prettyprinter
|
import Prettyprinter
|
||||||
import Data.Functor.Foldable
|
import Gyehoek.Prelude hiding (Simple)
|
||||||
import qualified Control.Comonad.Trans.Cofree as F
|
|
||||||
import Prettyprinter.Util
|
|
||||||
import Gyehoek.Prelude hiding (Simple, (:<))
|
|
||||||
import Data.Foldable (traverse_, toList)
|
|
||||||
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 qualified Data.Scientific as Sci
|
|
||||||
import Data.List (intersperse)
|
import Data.List (intersperse)
|
||||||
import Lucid
|
import Lucid
|
||||||
import Prettyprinter.Render.Util.SimpleDocTree (treeForm)
|
import Prettyprinter.Render.Util.SimpleDocTree (treeForm)
|
||||||
import Prettyprinter.Lucid (renderHtml)
|
import Prettyprinter.Lucid (renderHtml)
|
||||||
|
import Data.Foldable (toList)
|
||||||
|
import qualified Data.Scientific as Sci
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
@@ -83,7 +92,7 @@ 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 -> annotate (datum ^. syntax) $ prettySimple depth simp
|
Simple simp -> prettySimple depth simp
|
||||||
DotList xs x ->
|
DotList xs x ->
|
||||||
pparen depth . group . align $
|
pparen depth . group . align $
|
||||||
vsep [ vsep (prettyDatum (depth+1) <$> toList xs)
|
vsep [ vsep (prettyDatum (depth+1) <$> toList xs)
|
||||||
@@ -91,19 +100,21 @@ prettyDatum depth datum = case datum of
|
|||||||
, prettyDatum (depth+1) x
|
, prettyDatum (depth+1) x
|
||||||
]
|
]
|
||||||
|
|
||||||
List' indent xs ->
|
List' sty xs -> case sty of
|
||||||
case indent of
|
StyleSyntax n | keyword:args <- xs ->
|
||||||
NSpecial 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 $
|
||||||
[ prettyDatum (depth+1) keyword
|
[ annotate SynSyntax $ 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
|
||||||
]
|
]
|
||||||
Ordinary; NSpecial _ -> pparen depth $
|
StyleCode | f:args <- xs -> 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}|]
|
||||||
@@ -111,15 +122,11 @@ 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 ->
|
SimpleNumber n -> n
|
||||||
Sci.floatingOrInteger n
|
& Sci.floatingOrInteger @Double @Integer
|
||||||
& either viaShow viaShow
|
& either viaShow viaShow
|
||||||
& annotate SynConstant
|
& annotate SynConstant
|
||||||
SimpleString s -> annotate SynString $ viaShow s
|
SimpleString s -> annotate SynString $ viaShow s
|
||||||
@@ -132,7 +139,7 @@ putDoc = ANSI.renderIO stdout
|
|||||||
|
|
||||||
highlightAnsi :: Syn -> AnsiStyle
|
highlightAnsi :: Syn -> AnsiStyle
|
||||||
highlightAnsi = \case
|
highlightAnsi = \case
|
||||||
(SynBuiltin; SynMacro) -> color Magenta <> italicized <> bold
|
SynSyntax -> 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
|
||||||
@@ -144,8 +151,7 @@ highlightHtml :: Syn -> Html () -> Html ()
|
|||||||
highlightHtml syn = span_ [class_ synClass]
|
highlightHtml syn = span_ [class_ synClass]
|
||||||
where
|
where
|
||||||
synClass = case syn of
|
synClass = case syn of
|
||||||
SynBuiltin -> "syn-builtin"
|
SynSyntax -> "syn-builtin"
|
||||||
SynMacro -> "syn-macro"
|
|
||||||
SynConstant -> "syn-constant"
|
SynConstant -> "syn-constant"
|
||||||
SynString -> "syn-string"
|
SynString -> "syn-string"
|
||||||
SynProcedure -> "syn-procedure"
|
SynProcedure -> "syn-procedure"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
{-# LANGUAGE ApplicativeDo #-}
|
||||||
module Gyehoek.Sexp.Read
|
module Gyehoek.Sexp.Read
|
||||||
( readFile
|
( readFile
|
||||||
, readString
|
, readString
|
||||||
@@ -20,6 +21,7 @@ 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)
|
||||||
@@ -104,18 +106,51 @@ verb = L.symbol sc
|
|||||||
|
|
||||||
identifier :: P Text
|
identifier :: P Text
|
||||||
identifier = label "identifier" . lexeme . choice $
|
identifier = label "identifier" . lexeme . choice $
|
||||||
[ typical-- , delimited, peculiar
|
[ typical
|
||||||
|
-- , 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 = _
|
peculiar = peculiarSign <|> peculiarDot
|
||||||
|
|
||||||
|
-- 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`
|
||||||
@@ -207,7 +242,7 @@ simpleDatum = choice
|
|||||||
, SimpleNumber <$> try number
|
, SimpleNumber <$> try number
|
||||||
-- , SimpleCharacter <$> character
|
-- , SimpleCharacter <$> character
|
||||||
, SimpleString <$> string
|
, SimpleString <$> string
|
||||||
, SimpleSymbol <$> symbol
|
, SimpleSymbol <$> try symbol
|
||||||
-- , SimpleBytevector <$> bytevector
|
-- , SimpleBytevector <$> bytevector
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -219,9 +254,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 Ordinary []
|
Nothing -> pure $ ListF StyleData []
|
||||||
Just x -> do
|
Just x -> do
|
||||||
xs <- many datum
|
xs <- many datum
|
||||||
optional (dot *> datum) >>= \case
|
optional (dot *> datum) >>= \case
|
||||||
Nothing -> pure $ ListF Ordinary (x:xs)
|
Nothing -> pure $ ListF StyleData (x:xs)
|
||||||
Just y -> pure $ DotListF (x:|xs) y
|
Just y -> pure $ DotListF (x:|xs) y
|
||||||
+45
-40
@@ -1,6 +1,7 @@
|
|||||||
{-# 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(..)
|
||||||
@@ -13,8 +14,7 @@ module Gyehoek.Sexp.Syntax
|
|||||||
, Cofree((:<))
|
, Cofree((:<))
|
||||||
, Fix(..)
|
, Fix(..)
|
||||||
, Compound
|
, Compound
|
||||||
, Indentation(..)
|
, Style(..)
|
||||||
, Syn(..)
|
|
||||||
, pattern Simple
|
, pattern Simple
|
||||||
, pattern Compound
|
, pattern Compound
|
||||||
, pattern Labeled
|
, pattern Labeled
|
||||||
@@ -25,10 +25,8 @@ module Gyehoek.Sexp.Syntax
|
|||||||
, pattern Vector
|
, pattern Vector
|
||||||
, pattern DotList
|
, pattern DotList
|
||||||
, pattern Gyehoek.Sexp.Syntax.List
|
, pattern Gyehoek.Sexp.Syntax.List
|
||||||
, syntax
|
, style
|
||||||
, indentation
|
, styleWith
|
||||||
, adorn
|
|
||||||
, indentWith
|
|
||||||
, pattern Unreadable
|
, pattern Unreadable
|
||||||
, pattern Bytevector
|
, pattern Bytevector
|
||||||
, pattern Symbol
|
, pattern Symbol
|
||||||
@@ -39,15 +37,16 @@ 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), liftData)
|
import Language.Haskell.TH.Syntax (Lift (lift))
|
||||||
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 (..))
|
||||||
@@ -68,7 +67,9 @@ 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)
|
||||||
@@ -85,7 +86,7 @@ data Simple
|
|||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
data CompoundF a
|
data CompoundF a
|
||||||
= ListF Indentation (List a)
|
= ListF Style (List a)
|
||||||
| DotListF (NonEmpty a) a
|
| DotListF (NonEmpty a) a
|
||||||
| VectorF (List a)
|
| VectorF (List a)
|
||||||
| AbbrevF Prefix a
|
| AbbrevF Prefix a
|
||||||
@@ -93,7 +94,12 @@ data CompoundF a
|
|||||||
deriving anyclass (NFData)
|
deriving anyclass (NFData)
|
||||||
|
|
||||||
data Prefix
|
data Prefix
|
||||||
= Quote | Backtick | Comma | CommaAt
|
= Quote -- ^ @'@
|
||||||
|
| 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)
|
||||||
|
|
||||||
@@ -114,33 +120,21 @@ newtype Label = MkLabel Natural
|
|||||||
type Datum = Cofree DatumF Ann
|
type Datum = Cofree DatumF Ann
|
||||||
type Compound = CompoundF Datum
|
type Compound = CompoundF Datum
|
||||||
|
|
||||||
data Indentation
|
data Style
|
||||||
= NSpecial Int
|
= StyleSyntax Int
|
||||||
| Ordinary
|
| StyleCode
|
||||||
|
| 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
|
||||||
{ syntax :: Syn
|
{ position :: Maybe SourcePos
|
||||||
, position :: Maybe SourcePos
|
|
||||||
}
|
}
|
||||||
deriving (Show, Data, Eq, Generic)
|
deriving (Show, Data, Eq, Generic)
|
||||||
|
|
||||||
noAnn :: Ann
|
noAnn :: Ann
|
||||||
noAnn = MkAnn
|
noAnn = MkAnn
|
||||||
{ syntax = SynNone
|
{ position = Nothing
|
||||||
, position = Nothing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
-- requisite of the Pretty instance for invertible-grammar's error type.
|
-- requisite of the Pretty instance for invertible-grammar's error type.
|
||||||
@@ -158,23 +152,21 @@ deriveEq1 ''DatumF
|
|||||||
ann :: Lens' Datum Ann
|
ann :: Lens' Datum Ann
|
||||||
ann = _extract
|
ann = _extract
|
||||||
|
|
||||||
syntax :: Lens' Datum Syn
|
dat :: Lens' Datum (DatumF Datum)
|
||||||
syntax = ann . #syntax
|
dat = _unwrap
|
||||||
|
|
||||||
position :: Lens' Datum (Maybe SourcePos)
|
position :: Lens' Datum (Maybe SourcePos)
|
||||||
position = ann . #position
|
position = ann . #position
|
||||||
|
|
||||||
indentation :: Traversal' Datum Indentation
|
-- affine
|
||||||
indentation k (syn :< CompoundF (ListF ind xs)) = do
|
style :: Traversal' Datum Style
|
||||||
|
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)
|
||||||
indentation k a = pure a
|
style k a = pure a
|
||||||
|
|
||||||
adorn :: Syn -> Datum -> Datum
|
styleWith :: Style -> Datum -> Datum
|
||||||
adorn = set syntax
|
styleWith = set style
|
||||||
|
|
||||||
indentWith :: Indentation -> Datum -> Datum
|
|
||||||
indentWith = set indentation
|
|
||||||
|
|
||||||
stripAnn :: Datum -> Fix DatumF
|
stripAnn :: Datum -> Fix DatumF
|
||||||
stripAnn = hoist tailF
|
stripAnn = hoist tailF
|
||||||
@@ -208,9 +200,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 Ordinary a)
|
where List a = noAnn :< CompoundF (ListF StyleData a)
|
||||||
|
|
||||||
pattern List' :: Indentation -> List Datum -> Datum
|
pattern List' :: Style -> 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)
|
||||||
|
|
||||||
@@ -226,12 +218,25 @@ 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)
|
pattern Unreadable a = Simple (SimpleUnreadable a)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
((λ ()
|
(import (scheme eval))
|
||||||
(* 2 (call/cc
|
|
||||||
(λ (k)
|
(eval '(λ (x) x)
|
||||||
(begin (k 6)
|
(environment))
|
||||||
3))))))
|
|
||||||
@@ -24,19 +24,19 @@ thinWide name x = testGroup name
|
|||||||
, tcaseW 4 (name <> "-thin") x
|
, tcaseW 4 (name <> "-thin") x
|
||||||
]
|
]
|
||||||
|
|
||||||
datumBegin xs = S.indentWith (S.NSpecial 0) . S.List $
|
datumBegin xs = S.styleWith (S.StyleSyntax 0) . S.List $
|
||||||
(S.adorn S.SynBuiltin . S.Symbol $ "begin") : xs
|
S.Symbol "begin" : xs
|
||||||
|
|
||||||
datumLambda formals body =
|
datumLambda formals body =
|
||||||
S.indentWith (S.NSpecial 1) . S.List $
|
S.styleWith (S.StyleSyntax 1) . S.List $
|
||||||
(S.adorn S.SynBuiltin . S.Symbol $ "lambda") : formals : body
|
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.indentWith (S.NSpecial 0) $
|
S.styleWith (S.StyleSyntax 0) $
|
||||||
datumBegin [ S.Symbol "책을"
|
datumBegin [ S.Symbol "책을"
|
||||||
, S.Symbol "더"
|
, S.Symbol "더"
|
||||||
, S.Symbol "먹으세요~!"
|
, S.Symbol "먹으세요~!"
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ 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"
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in new issue
Block a user