-
Notifications
You must be signed in to change notification settings - Fork 0
/
Macros.fold
357 lines (229 loc) · 5.39 KB
/
Macros.fold
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
--
-- Fold Macros
--
type Parser a
{- The type for parser producing value [a]. -}
val syntax :: Expr -> Parser (List Expr)
{- [syntax form...] describes the parser using the elements from [form] and
returning a list of all non-terminal expressions. -}
val argument = syntax expression
syntax `val name `= value
syntax `def name (args <- argument*) `= value
syntax `if t `then a `else b
val expression = syntax
val if :: Parser (List Expr) =
sytnax `if t `then a `else b
parser
| xs <- if =
match t
| True -> a
| False -> b
end
| `if t `then a `end =
match t
| True -> a
| False -> ()
end
if Random.int () > 100 then
print "Too big"
else
print "Too small"
-- * --
-- For-expression
syntax `for x `in xs body =
List.iter (x -> body) xs)
`for x `in xs body =>
Form [Atom (Symbol "for"), ]
for i in [1, 2, 3] do
print i
end
syntax
| `define name = `(var $name)
| `define name `= value = `(var $name = $value)
def hello name =
print "hello"
val x = 1
def factorial 0 = 1
| factorial n = n * factorial (n - 1)
def a + b =
Int.add a b
-- * --
-- To create a macro a syntax rule is defined:
macro `unless (cond & body)...
`(if (not %cond) then: %body...)
end
unless (User.authorized? usr)
log "Unauthorized user: %(usr)..."
redirect to: `Login_page
end
(unless (User.authorized? usr)
(do (log "Unauthorized user: %(usr)...")
(redirect :to `Login_page)))
(defmacro unless [arg & body]
`(if (not ~arg)
(do ~@body))) ; Remember the do!
macro `time exp
quote
t = Time.now!
x = %(exp)
print "elapsed time: %(t - Time.now!) seconds"
x
end
end
=> `(2 + %(4 + 2))
:: Code Int = `(2 + 6)
---
{ define-method ?class:name . ?name:name (?params:*)
?:body
end } =>
{ define method ?name (?=self :: ?class, ?params)
?body
end }
macro (class::Symbol) . (attribute::Symbol) ()
if_then_else_ : ∀ {a} {A : Set a} → Bool → A → A → A
if true then t else f = t
if false then t else f = f
if: Bool -> then: a -> else: a -> a
if-then-else- : Bool → A → A → A
if : Bool -> then : A -> else : A -> A
if <Comparable A, B> :: Code A -> then: Code A ->
if condition consequence alternative =
`if {not condition}
then: consequence
else: alternative
if cond::`Bool -> then::`a -> else::`a -> `a
`if %(not cond) then:
end
unless (a > 3) do:
3
else:
4
(+) :: Code A -> Code B -> Code C
x + y = `(+, [x, y])
=> `(2 + {4 + 2})
:: Code Int = `(2 + 6)
=> function_name = "fold"
=> List.{{function_name}}
:: Code Int = `(2 + 6)
---
macro time expr =
```
let before = Time.now ()
let result = $exp
let after = Time.now ()
in
print "Elapsed time: $(after - before) seconds";
result
```
(print (format t "I have ~D apples." 4))
(let ((num 4))
(quasiquote (I have (unquote num) apples)))
print ("I have %s apples." 4)
let num = 4
in quote (I have (unquote num) apples)
let num = 4
in `(I have ~(num) apples)
--
-- If then else, macro definition syntax
--
if_then_else_ : ∀ {a} {A : Set a} → Bool → A → A → A
if true then t else f = t
if false then t else f = f
macro `if condition a `else b {
...
}
macro `do expr {
}
macro print to: (file = IO.stdout) :(flush = False) :(sep = " ") :(end = "\n") args... =
let output_str =
String.join with: sep (map show args) ++ end
in
IO.write to: file output_str;
if flush then
use IO in
flush stdout
end
end
print to: IO.stderr flush: true sep: "\n\t" end: "\n---"
"warning: invalid input format:" row
---
-- Declaration of conditional
-- if true then TrueClause else FalseClause -> TrueClause
-- if false then TrueClause else FalseClause -> FalseClause
if true then A else _ = A
if false then _ else B = B
---
-- https://en.wikipedia.org/wiki/Lithe_(programming_language)
-- rule "|" <i:int> "|" return int;
-- {if i<0 then return -i else return i end}
syntax "|" n "|" =
quote
if (unquote n) < 0 then -(unquote n) else (unquote n)
end
macro `| n `| =
```
if (unquote n) < 0 then -(unquote n) else (unquote n)
```
-- * --
-- Nemerle Macros
-- https://github.com/rsdn/nemerle/wiki/Syntax-extensions
macro while_macro (cond, body)
syntax ("while", "(", cond, ")", body) {
<[
def loop () {
when ($cond) {
$body;
loop ()
}
}
loop ()
]>
}
macro while :: Expr -> Expr -> Expr
macro while condition body =
```
let loop () =
if $c then $a else loop ()
in
loop ()
```
-- ## Quoting/Unquoting
-- Just a list with expressions: [2, `+, 2]
quote (2 + 2)
-- Operator for quoting.
`(2 + 2)
-- Values can be unquoted (during compile-time) into the quoted expression:
x = 7
quote (2 + (unquote x)) -- produces a list [2, +, 7]
`(2 + $x)
-- Quoted expression blocks are also supported
x = 7
```
a = 2 + $x
print a
```
macro if $t then [a] else (b)
...
end
macro $k => $v
...
end
-- * --
-- Idris – Syntax Extensions
-- http://docs.idris-lang.org/en/latest/tutorial/syntax.html
-- syntax for {x} in [xs] ":" [body] = forLoop xs (\x => body)
macro for $x in $xs $body
`(List.iter (x -> $body) $xs)
end
-- * --
table = '{' table-items '}' | '{' '}'
table-items = item | item ',' table-items
comma_list form =
case form
| [a, ',', b, rest...] ->
end
(item | item ',') +
Reuse the sedlex syntax
macro table
{ } ->
end