@@ -44,11 +44,11 @@ data Response = Response (Either String Compiled)
44
44
compile :: String -> IO Response
45
45
compile input | length input > 5000 = return $ Response $ Left " Please limit your input to 5000 characters"
46
46
compile input = do
47
- case P. runIndentParser P. parseDeclarations input of
47
+ case P. runIndentParser P. parseModules input of
48
48
Left parseError -> do
49
49
return $ Response $ Left $ show parseError
50
- Right decls -> do
51
- case P. compile decls of
50
+ Right modules -> do
51
+ case P. compile modules of
52
52
Left error ->
53
53
return $ Response $ Left error
54
54
Right (js, externs, _) ->
@@ -92,7 +92,9 @@ examples :: [(String, (String, String))]
92
92
examples =
93
93
[ (" adt" ,
94
94
(" Algebraic Data Types" ,
95
- unlines [ " data Person = Person { name :: String, age :: Number }"
95
+ unlines [ " module ADTs where"
96
+ , " "
97
+ , " data Person = Person { name :: String, age :: Number }"
96
98
, " "
97
99
, " foreign import numberToString :: Number -> String"
98
100
, " "
@@ -101,7 +103,9 @@ examples =
101
103
]))
102
104
, (" ops" ,
103
105
(" Operators" ,
104
- unlines [ " infixl 5 |>"
106
+ unlines [ " module Operators where"
107
+ , " "
108
+ , " infixl 5 |>"
105
109
, " "
106
110
, " (|>) :: forall a b c. (a -> b) -> (b -> c) -> a -> c"
107
111
, " (|>) f g a = g (f a)"
@@ -113,27 +117,35 @@ examples =
113
117
]))
114
118
, (" arrays" ,
115
119
(" Arrays" ,
116
- unlines [ " sum (x:xs) = x + sum xs"
120
+ unlines [ " module Arrays where"
121
+ , " "
122
+ , " sum (x:xs) = x + sum xs"
117
123
, " sum [] = 0"
118
124
, " "
119
125
, " sumOfProducts (x : y : xs) = x * y + sum xs"
120
126
, " sumOfProducts _ = 0"
121
127
]))
122
128
, (" rows" ,
123
129
(" Row Polymorphism" ,
124
- unlines [ " showPerson o = o.lastName ++ \" , \" ++ o.firstName"
130
+ unlines [ " module RowPolymorphism where"
131
+ , " "
132
+ , " showPerson o = o.lastName ++ \" , \" ++ o.firstName"
125
133
]))
126
134
, (" ffi" ,
127
135
(" FFI" ,
128
- unlines [ " foreign import data IO :: * -> *"
136
+ unlines [ " module FFI where"
137
+ , " "
138
+ , " foreign import data IO :: * -> *"
129
139
, " "
130
140
, " foreign import console :: { log :: String -> IO { } }"
131
141
, " "
132
142
, " main = console.log \" Hello World!\" "
133
143
]))
134
144
, (" blocks" ,
135
145
(" Mutable Variables" ,
136
- unlines [ " collatz :: Number -> Number"
146
+ unlines [ " module Mutable where"
147
+ , " "
148
+ , " collatz :: Number -> Number"
137
149
, " collatz n ="
138
150
, " { "
139
151
, " var m = n;"
@@ -151,16 +163,20 @@ examples =
151
163
]))
152
164
, (" modules" ,
153
165
(" Modules" ,
154
- unlines [ " module Test where"
166
+ unlines [ " module M1 where"
155
167
, " "
156
- , " incr :: Number -> Number"
157
- , " incr x = x + 1"
168
+ , " incr :: Number -> Number"
169
+ , " incr x = x + 1"
158
170
, " "
159
- , " test = Test.incr 10"
171
+ , " module M2 where"
172
+ , " "
173
+ , " test = M1.incr 10"
160
174
]))
161
175
, (" rank2" ,
162
176
(" Rank N Types" ,
163
- unlines [ " type Nat = forall a. a -> (a -> a) -> a"
177
+ unlines [ " module RankNTypes where"
178
+ , " "
179
+ , " type Nat = forall a. a -> (a -> a) -> a"
164
180
, " "
165
181
, " zero :: Nat"
166
182
, " zero a _ = a"
@@ -173,6 +189,17 @@ examples =
173
189
, " compose :: forall a b c. Lens a b -> Lens b c -> Lens a c"
174
190
, " compose l1 l2 f = l2 (l1 f)"
175
191
]))
192
+ , (" recursion" ,
193
+ (" Recursion" ,
194
+ unlines [ " module Recursion where"
195
+ , " "
196
+ , " isOdd :: Number -> Boolean"
197
+ , " isOdd n = !isEven (n - 1)"
198
+ , " "
199
+ , " isEven :: Number -> Boolean"
200
+ , " isEven 0 = true"
201
+ , " isEven n = !isOdd (n - 1)"
202
+ ]))
176
203
]
177
204
178
205
page :: Maybe String -> Maybe Response -> ActionM ()
0 commit comments