@@ -34,6 +34,10 @@ import Text.Blaze.Html
34
34
import Text.Blaze.Html.Renderer.Text
35
35
import qualified Text.Blaze.Html5 as H
36
36
import qualified Text.Blaze.Html5.Attributes as A
37
+ import qualified Paths_trypurescript as Paths
38
+
39
+ getPreludeFilename :: IO FilePath
40
+ getPreludeFilename = Paths. getDataFileName " prelude.purs"
37
41
38
42
data Compiled = Compiled { js :: String
39
43
, externs :: String
@@ -42,16 +46,23 @@ data Compiled = Compiled { js :: String
42
46
data Response = Response (Either String Compiled )
43
47
44
48
options :: P. Options
45
- options = P. defaultOptions { P. optionsTco = True }
49
+ options = P. defaultOptions { P. optionsTco = True
50
+ , P. optionsMagicDo = True
51
+ , P. optionsModules = [" Main" ] }
52
+
53
+ loadModule :: FilePath -> IO (Either String [P. Module ])
54
+ loadModule moduleFile = do
55
+ moduleText <- readFile moduleFile
56
+ return . either (Left . show ) Right $ P. runIndentParser " " P. parseModules moduleText
46
57
47
- compile :: String -> IO Response
48
- compile input | length input > 5000 = return $ Response $ Left " Please limit your input to 5000 characters"
49
- compile input = do
50
- case P. runIndentParser P. parseModules input of
58
+ compile :: [ P. Module ] -> String -> IO Response
59
+ compile _ input | length input > 5000 = return $ Response $ Left " Please limit your input to 5000 characters"
60
+ compile prelude input = do
61
+ case P. runIndentParser " " P. parseModules input of
51
62
Left parseError -> do
52
63
return $ Response $ Left $ show parseError
53
64
Right modules -> do
54
- case P. compile options modules of
65
+ case P. compile options (prelude ++ modules) of
55
66
Left error ->
56
67
return $ Response $ Left error
57
68
Right (js, externs, _) ->
@@ -135,7 +146,9 @@ examples :: [(String, (String, String))]
135
146
examples =
136
147
[ (" adt" ,
137
148
(" Algebraic Data Types" ,
138
- unlines [ " module ADTs where"
149
+ unlines [ " module Main where"
150
+ , " "
151
+ , " import Prelude"
139
152
, " "
140
153
, " data Person = Person { name :: String, age :: Number }"
141
154
, " "
@@ -146,21 +159,25 @@ examples =
146
159
]))
147
160
, (" ops" ,
148
161
(" Operators" ,
149
- unlines [ " module Operators where"
162
+ unlines [ " module Main where"
163
+ , " "
164
+ , " import Prelude"
150
165
, " "
151
- , " infixl 5 | >"
166
+ , " infixl 5 >> >"
152
167
, " "
153
- , " (| >) :: forall a b c. (a -> b) -> (b -> c) -> a -> c"
154
- , " (| >) f g a = g (f a)"
168
+ , " (>> >) :: forall a b c. (a -> b) -> (b -> c) -> a -> c"
169
+ , " (>> >) f g a = g (f a)"
155
170
, " "
156
171
, " foreign import foo :: String -> Number"
157
172
, " foreign import bar :: Number -> Boolean"
158
173
, " "
159
- , " test = foo | > bar"
174
+ , " test = foo >> > bar"
160
175
]))
161
176
, (" arrays" ,
162
177
(" Arrays" ,
163
- unlines [ " module Arrays where"
178
+ unlines [ " module Main where"
179
+ , " "
180
+ , " import Prelude"
164
181
, " "
165
182
, " sum (x:xs) = x + sum xs"
166
183
, " sum _ = 0"
@@ -170,13 +187,15 @@ examples =
170
187
]))
171
188
, (" rows" ,
172
189
(" Row Polymorphism" ,
173
- unlines [ " module RowPolymorphism where"
190
+ unlines [ " module Main where"
191
+ , " "
192
+ , " import Prelude"
174
193
, " "
175
194
, " showPerson o = o.lastName ++ \" , \" ++ o.firstName"
176
195
]))
177
196
, (" ffi" ,
178
197
(" FFI" ,
179
- unlines [ " module FFI where"
198
+ unlines [ " module Main where"
180
199
, " "
181
200
, " foreign import data IO :: * -> *"
182
201
, " "
@@ -186,7 +205,9 @@ examples =
186
205
]))
187
206
, (" blocks" ,
188
207
(" Mutable Variables" ,
189
- unlines [ " module Mutable where"
208
+ unlines [ " module Main where"
209
+ , " "
210
+ , " import Prelude"
190
211
, " "
191
212
, " collatz :: Number -> Number"
192
213
, " collatz n ="
@@ -207,17 +228,19 @@ examples =
207
228
, (" modules" ,
208
229
(" Modules" ,
209
230
unlines [ " module M1 where"
231
+ , " "
232
+ , " import Prelude"
210
233
, " "
211
234
, " incr :: Number -> Number"
212
235
, " incr x = x + 1"
213
236
, " "
214
- , " module M2 where"
237
+ , " module Main where"
215
238
, " "
216
239
, " test = M1.incr 10"
217
240
]))
218
241
, (" rank2" ,
219
242
(" Rank N Types" ,
220
- unlines [ " module RankNTypes where"
243
+ unlines [ " module Main where"
221
244
, " "
222
245
, " type Nat = forall a. a -> (a -> a) -> a"
223
246
, " "
@@ -234,7 +257,9 @@ examples =
234
257
]))
235
258
, (" recursion" ,
236
259
(" Recursion" ,
237
- unlines [ " module Recursion where"
260
+ unlines [ " module Main where"
261
+ , " "
262
+ , " import Prelude"
238
263
, " "
239
264
, " isOdd :: Number -> Boolean"
240
265
, " isOdd 0 = false"
@@ -246,13 +271,7 @@ examples =
246
271
]))
247
272
, (" do" ,
248
273
(" Do Notation" ,
249
- unlines [ " module Prelude where"
250
- , " "
251
- , " class Monad m where"
252
- , " ret :: forall a. a -> m a"
253
- , " (>>=) :: forall a b. m a -> (a -> m b) -> m b"
254
- , " "
255
- , " module DoNotation where"
274
+ unlines [ " module Main where"
256
275
, " "
257
276
, " import Prelude"
258
277
, " "
@@ -275,7 +294,9 @@ examples =
275
294
]))
276
295
, (" tco" ,
277
296
(" Tail-Call Elimination" ,
278
- unlines [ " module TailCall where"
297
+ unlines [ " module Main where"
298
+ , " "
299
+ , " import Prelude"
279
300
, " "
280
301
, " factHelper prod 0 = prod"
281
302
, " factHelper prod n = factHelper (prod * n) (n - 1)"
@@ -284,7 +305,9 @@ examples =
284
305
]))
285
306
, (" typeclasses" ,
286
307
(" Type Classes" ,
287
- unlines [ " module TypeClasses where"
308
+ unlines [ " module Main where"
309
+ , " "
310
+ , " import Prelude"
288
311
, " "
289
312
, " class Show a where"
290
313
, " show :: a -> String"
@@ -365,20 +388,23 @@ responseToJs (Just (Response (Right (Compiled "" "")))) = (False, "Please enter
365
388
responseToJs (Just (Response (Right (Compiled js _)))) = (True , js)
366
389
367
390
server :: Int -> IO ()
368
- server port = scotty port $ do
369
- get " /" $ do
370
- page Nothing (Just " -- Type PureScript code here and click 'Compile' ...\r\n -- \r\n -- Or select an example from the list at the top right of the page" ) Nothing
371
- get " /example/:name" $ do
372
- name <- param " name"
373
- case lookup name examples of
374
- Nothing -> raise " No such example"
375
- Just (_, code) -> do
376
- response <- lift $ compile code
377
- page (Just name) (Just code) (Just response)
378
- post " /compile" $ do
379
- code <- param " code"
380
- response <- lift $ compile code
381
- page Nothing (Just code) (Just response)
391
+ server port = do
392
+ preludeFilename <- getPreludeFilename
393
+ Right prelude <- loadModule preludeFilename
394
+ scotty port $ do
395
+ get " /" $ do
396
+ page Nothing (Just " -- Type PureScript code here and click 'Compile' ...\r\n -- \r\n -- Or select an example from the list at the top right of the page" ) Nothing
397
+ get " /example/:name" $ do
398
+ name <- param " name"
399
+ case lookup name examples of
400
+ Nothing -> raise " No such example"
401
+ Just (_, code) -> do
402
+ response <- lift $ compile prelude code
403
+ page (Just name) (Just code) (Just response)
404
+ post " /compile" $ do
405
+ code <- param " code"
406
+ response <- lift $ compile prelude code
407
+ page Nothing (Just code) (Just response)
382
408
383
409
term :: Term (IO () )
384
410
term = server <$> port
0 commit comments