-
Notifications
You must be signed in to change notification settings - Fork 1
/
QQ.hs
295 lines (269 loc) · 8.61 KB
/
QQ.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
-- |
-- Present compile time lisp (zuramaru) literals as quasi-quotes.
--
-- The compile time inline lisp is contained :sunny:
--
-- You can use 'Maru.QQ.ShortName' instead of these functions,
-- if you don't worry name conflictions.
--
-- These requires to
-- `
-- import Maru.Type.SExpr (SExpr(..), MaruSymbol(..))
-- `
-- and add `text` to your package.yaml or {project}.cabal (below is a package.yaml example)
-- `
-- dependencies:
-- - text
-- `
--
-- and optionally requirements are existent, please see below.
--
-- If you want to use as patterns
-- (e.g. `let ![parse|123|] = AtomInt 123`)
-- `
-- {-# LANGUAGE OverloadedStrings #-}
-- `
--
-- If you want to use as types
-- (e.g. `f :: [parse|(1 2)|]`)
-- `
-- {-# LANGUAGE DataKinds #-}
-- `
module Maru.QQ
( parse
, parsePreprocess
, zura
, p
, pp
, z
) where
import Control.Arrow ((>>>))
import Data.Text (Text)
import Language.Haskell.TH
import Language.Haskell.TH.Quote (QuasiQuoter(..))
import Maru.Type (SExpr(..), MaruSymbol(..), CallowSExpr(..))
import qualified Data.Text as T
import qualified Maru.Eval as Maru
import qualified Maru.Parser as Maru
import qualified Maru.Preprocessor as Maru
-- $setup
-- >>> :set -XQuasiQuotes
-- >>> :set -XDataKinds
-- >>> :set -XOverloadedStrings
-- >>> import Data.Singletons (sing, fromSing)
-- >>> import Maru.Type.Parser (ParseErrorResult)
-- >>> import Maru.Type.SExpr (SourceCode)
-- >>> import Maru.Type.TypeLevel (HighSExpr(..), Sing(..))
-- >>> import System.IO.Silently (silence)
--TODO: Implement tests to check the compiler errors (See 'Maru.QQ.parse', 'Maru.QQ.zura' documentation for conditions of the compile error)
-- (What can I implement it ?)
textE :: Text -> Exp
textE t =
let x = T.unpack t
in VarE (mkName "Data.Text.pack") `AppE` LitE (StringL x)
intL :: Int -> Lit
intL = IntegerL . fromIntegral
boolP :: Bool -> Pat
boolP x = ConP (mkName $ show x) []
-- | A value `x :: Int` as a type `x :: 'Nat'`
intT :: Int -> Type
intT = LitT . NumTyLit . fromIntegral
-- | A value `x :: Bool` as a type `x :: 'Bool`
boolT :: Bool -> Type
boolT = PromotedT . mkName . show
-- | A value 'x :: Text' as a type `x :: 'Symbol'`
textSym :: Text -> Type
textSym = LitT . StrTyLit . T.unpack
toExp :: SExpr -> Exp
toExp (Cons x y) = ConE (mkName "Cons") `AppE` ParensE (toExp x) `AppE` ParensE (toExp y)
toExp (Quote x) = ConE (mkName "Quote") `AppE` ParensE (toExp x)
toExp Nil = ConE (mkName "Nil")
toExp (AtomInt x) = ConE (mkName "AtomInt") `AppE` LitE (intL x)
toExp (AtomBool x) = ConE (mkName "AtomBool") `AppE` ConE (mkName $ show x)
toExp (AtomSymbol (MaruSymbol x)) = ConE (mkName "AtomSymbol") `AppE`
ParensE (ConE (mkName "MaruSymbol") `AppE` textE x)
toPat :: SExpr -> Pat
toPat (Cons x y) = ConP (mkName "Cons") [toPat x, toPat y]
toPat (Quote x) = ConP (mkName "Quote") [toPat x]
toPat Nil = ConP (mkName "Nil") []
toPat (AtomInt x) = ConP (mkName "AtomInt") [LitP $ intL x]
toPat (AtomBool x) = ConP (mkName "AtomBool") [boolP x]
-- This requires OverloadedStrings
toPat (AtomSymbol (MaruSymbol "__")) = WildP
toPat (AtomSymbol (MaruSymbol x)) = ConP (mkName "AtomSymbol") [ConP (mkName "MaruSymbol") [LitP . StringL $ T.unpack x]]
toType :: SExpr -> Type
toType (Cons x y) = PromotedT (mkName "HCons") `AppT` ParensT (toType x) `AppT` ParensT (toType y)
toType (Quote x) = PromotedT (mkName "HQuote") `AppT` ParensT (toType x)
toType Nil = PromotedT (mkName "HNil")
--NOTE: Should specify a branch when `x < 0` ? or NumTyLit satisfies ?
toType (AtomInt x) = PromotedT (mkName "HAtomInt") `AppT` intT x
toType (AtomBool x) = PromotedT (mkName "HAtomBool") `AppT` boolT x
toType (AtomSymbol (MaruSymbol x)) = PromotedT (mkName "HAtomSymbol") `AppT` textSym x
-- |
-- Expand a code to 'SExpr'
-- if it can be parsed by 'Maru.Parser.parse'
-- (or to be compiled error).
--
-- Occure the compile error if it cannot be parsed.
--
-- >>> :{
-- >>> let p :: SourceCode -> Either ParseErrorResult SExpr
-- >>> p x = growUp <$> Maru.parse x -- with the empty preprocessor (growUp)
-- >>> :}
--
-- As expressions
--
-- >>> Right [parse|123|] == p "123"
-- True
-- >>> Right [parse|abc|] == p "abc"
-- True
-- >>> Right [parse|(123 456)|] == p "(123 456)"
-- True
--
-- (multi line)
--
-- >>> :{
-- >>> Right [parse|
-- >>> (do
-- >>> (def! x 10)
-- >>> nil)
-- >>> |] == p "(do (def! x 10) nil)"
-- >>> :}
-- True
--
-- >>> :{
-- >>> Right [parse|
-- >>> (let* (x 10)
-- >>> (let* (y 20) nil))
-- >>> |] == p "(let* (x 10) (let* (y 20) nil))"
-- >>> :}
-- True
--
-- As patterns
--
-- >>> case AtomInt 123 of; [parse|123|] -> "good"
-- "good"
-- >>> case AtomInt 000 of; [parse|123|] -> "bad"; AtomInt _ -> "good"
-- "good"
-- >>> case AtomSymbol "x" of; [parse|x|] -> "good"
-- "good"
-- >>> case Quote (AtomInt 10) of; [parse|'10|] -> "good"
-- "good"
-- >>> case Cons (AtomSymbol "x") (Cons (AtomInt 10) Nil) of; [parse|(x 10)|] -> "good"
-- "good"
--
-- and `__` to be a wild pattern
--
-- >>> case AtomInt 10 of; [parse|__|] -> "good"
-- "good"
-- >>> case AtomSymbol "konoko" of; [parse|__|] -> "good"
-- "good"
-- >>> case Cons (AtomInt 1) (Cons (AtomInt 2) Nil) of; [parse|(1 __)|] -> "good"
-- "good"
--
-- As types (compile time calculations)
--
-- >>> fromSing (sing :: Sing [parse|10|])
-- AtomInt 10
-- >>> fromSing (sing :: Sing [parse|konoko|])
-- AtomSymbol "konoko"
-- >>> fromSing (sing :: Sing [parse|'aiya000|])
-- Quote (AtomSymbol "aiya000")
-- >>> fromSing (sing :: Sing [parse|(1 2 3)|])
-- Cons (AtomInt 1) (Cons (AtomInt 2) (Cons (AtomInt 3) Nil))
parse :: QuasiQuoter
parse = QuasiQuoter
{ quoteExp = power toExp
, quotePat = power toPat
, quoteType = power toType
, quoteDec = error "Maru.QQ.parse: the expansion to `[Dec]` (`quoteDec`) isn't support supported"
}
where
power :: (SExpr -> a) -> String -> Q a
power f = T.pack >>> Maru.parse >>> \case
Left e -> fail $ Maru.parseErrorPretty e
Right a -> return . f $ growUp a
-- |
-- Similar to 'parse' but 'Maru.preprocessor.preprocess' is appended
--
-- >>> :{
-- >>> let pp :: SourceCode -> Either ParseErrorResult SExpr
-- >>> pp code = Maru.preprocess <$> Maru.parse code
-- >>> :}
--
-- >>> Right [parsePreprocess|sugar|] == pp "sugar"
-- True
-- >>> Right [parsePreprocess|10|] == pp "10"
-- True
-- >>> Right [parsePreprocess|(1 2 3)|] == pp "(1 2 3)"
-- True
-- >>> Right [parsePreprocess|'10|] == pp "'10"
-- True
parsePreprocess :: QuasiQuoter
parsePreprocess = QuasiQuoter
{ quoteExp = power toExp
, quotePat = power toPat
, quoteType = power toType
, quoteDec = error "Maru.QQ.parsePreprocess: the expansion to `[Dec]` (`quoteDec`) isn't support supported"
}
where
power :: (SExpr -> a) -> String -> Q a
power f = T.pack >>> Maru.parse >>> \case
Left e -> fail $ Maru.parseErrorPretty e
Right a -> return . f $ Maru.preprocess a
-- |
-- Similar to 'parsePreprocess' but it is ran in the compile time,
-- Occure the side effects,
-- and Return the evaluated result.
--
-- Occure the compile error if the code throws exceptions.
--
-- NOTICE:
-- Please be careful to the halting problem.
-- The halting safety is abandon
-- because this ('Maru.Eval.eval') is turing-complete in the compile time :D
--
-- >>> :{
-- >>> let force :: SourceCode -> IO SExpr
-- >>> force code = do
-- >>> let (Right sexpr) = Maru.preprocess <$> Maru.parse code
-- >>> Right (result, _, _) <- silence $ Maru.eval Maru.initialEnv sexpr
-- >>> return result
-- >>> :}
--
-- >>> (==) <$> return [zura|10|] <*> force "10"
-- True
-- >>> (==) <$> return [zura|'sugar|] <*> force "'sugar"
-- True
-- >>> (==) <$> return [zura|'(1 2 3)|] <*> force "'(1 2 3)"
-- True
-- >>> (==) <$> return [zura|(print 10)|] <*> force "(print 10)"
-- 10True
zura :: QuasiQuoter
zura = QuasiQuoter
{ quoteExp = power toExp
, quotePat = power toPat
, quoteType = power toType
, quoteDec = error "Maru.QQ.zura: the expansion to `[Dec]` (`quoteDec`) isn't support supported"
}
where
power :: (SExpr -> a) -> String -> Q a
power f = T.pack >>> Maru.parse >>> fmap Maru.preprocess >>> \case
Left e -> fail $ Maru.parseErrorPretty e
Right sexpr -> do
result <- runIO $ Maru.eval Maru.initialEnv sexpr
case result of
Left e -> fail $ "Maru.QQ.zura: an error is occured in the compile time: " ++ show e
Right (sexpr, _, _) -> return $ f sexpr
-- | The shorthand for 'parse'
p :: QuasiQuoter
p = parse
-- | The shorthand for 'parsePreprocess'
pp :: QuasiQuoter
pp = parsePreprocess
-- | The shorthand for 'zura'
z :: QuasiQuoter
z = zura