aboutsummaryrefslogtreecommitdiff
path: root/core/control_flow.nom
blob: 8e7306d4f41aaeea091600776bab8c6d7b65b8d5 (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#
    This file contains compile-time actions that define basic control flow structures
    like "if" statements and loops.

use "core/metaprogramming.nom"
use "core/text.nom"
use "core/operators.nom"

# No-Op
immediately
    compile [do nothing] to: Lua ""

# Conditionals
immediately
    compile [if %condition %if_body] to
        Lua ".."
            if \(%condition as lua expr) then
                \(%if_body as lua statements)
            end
    parse [unless %condition %unless_body] as: if (not %condition) %unless_body

    compile [if %condition %if_body else %else_body, unless %condition %else_body else %if_body] to
        Lua ".."
            if \(%condition as lua expr) then
                \(%if_body as lua statements)
            else
                \(%else_body as lua statements)
            end

# Conditional expression (ternary operator)
#   Note: this uses a function instead of "(condition and if_expr or else_expr)"
    because that breaks if %if_expr is falsey, e.g. "x < 5 and false or 99"
immediately
    compile [..]
        %when_true_expr if %condition else %when_false_expr
        %when_true_expr if %condition otherwise %when_false_expr
        %when_false_expr unless %condition else %when_true_expr
        %when_false_expr unless %condition then %when_true_expr
    ..to
        #   If %when_true_expr is guaranteed to be truthy, we can use Lua's idiomatic
            equivalent of a conditional expression: (cond and if_true or if_false)
        if: %when_true_expr.type in {Text:yes, List:yes, Dict:yes, Number:yes}
            return
                Lua value ".."
                    (\(%condition as lua expr) and \(%when_true_expr as lua expr) or \(%when_false_expr as lua expr))
        ..else
            #   Otherwise, need to do an anonymous inline function (yuck, too bad lua 
                doesn't have a proper ternary operator!)
                To see why this is necessary consider: (random()<.5 and false or 99)
            return
                Lua value ".."
                    (function()
                        if \(%condition as lua expr) then
                            return \(%when_true_expr as lua expr);
                        else
                            return \(%when_false_expr as lua expr);
                        end
                    end)()

# GOTOs
immediately
    compile [=== %label ===, --- %label ---, *** %label ***] to
        Lua "::label_\(%label as lua identifier)::;"
    compile [go to %label] to
        Lua "goto label_\(%label as lua identifier);"

# Basic loop control
immediately
    compile [do next] to: Lua "continue;"
    compile [stop] to: Lua "break;"

# Helper function
immediately
    compile [%tree has subtree %subtree where %condition] to
        Lua value ".."
            (function()
                for \(%subtree as lua expr) in coroutine.wrap(function() \(%tree as lua expr):map(coroutine.yield) end) do
                    if \(%condition as lua expr) then
                        return true;
                    end
                end
                return false;
            end)()

# While loops
immediately
    compile [do next repeat] to: Lua "goto continue_repeat;"
    compile [stop repeating] to: Lua "goto stop_repeat;"
    compile [repeat while %condition %body] to
        %lua <-
            Lua ".."
                while \(%condition as lua expr) do
                    \(%body as lua statements)
        if
            %body has subtree % where: (%.type = "Action") and ((%'s stub) is "do next repeat")
        ..: to %lua write "\n    ::continue_repeat::;"
        to %lua write "\nend --while-loop"
        if
            %body has subtree % where: (%.type = "Action") and ((%'s stub) is "stop repeating")
        ..
            %lua <-
                Lua ".."
                    do -- scope of "stop repeating" label
                        \%lua
                        ::stop_repeat::;
                    end -- end of "stop repeating" label scope
        return %lua
    parse [repeat %body] as: repeat while (yes) %body
    parse [repeat until %condition %body] as: repeat while (not %condition) %body

    compile [..]
        repeat %n times %body
    ..to
        %lua <-
            Lua ".."
                for i=1,\(%n as lua expr) do
                    \(%body as lua statements)
        if
            %body has subtree % where: (%.type = "Action") and ((%'s stub) is "do next repeat")
        ..: to %lua write "\n    ::continue_repeat::;"
        to %lua write "\nend --numeric for-loop"
        if
            %body has subtree % where: (%.type = "Action") and ((%'s stub) is "stop repeating")
        ..
            %lua <-
                Lua ".."
                    do -- scope of "stop repeating" label
                        \%lua
                        ::stop_repeat::;
                    end -- end of "stop repeating" label scope
        return %lua

# For loop control flow
immediately
    compile [stop %var] to
        Lua "goto stop_\(%var as lua identifier);"
    compile [do next %var] to
        Lua "goto continue_\(%var as lua identifier);"

# Numeric range for loops
immediately
    compile [..]
        for %var in %start to %stop by %step %body
        for %var in %start to %stop via %step %body
    ..to
        # This uses Lua's approach of only allowing loop-scoped variables in a loop
        assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
        %lua <-
            Lua ".."
                for \(%var as lua expr)=\(%start as lua expr),\(%stop as lua expr),\(%step as lua expr) do
                    \(%body as lua statements)
        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "do next %") and 
                        %.value.3.value is %var.value
        ..: to %lua write "\n    ::continue_\(%var as lua identifier)::;"
        to %lua write "\nend --numeric for-loop"

        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "stop %") and
                        %.value.2.value is %var.value
        ..
            %lua <-
                Lua ".."
                    do -- scope for stopping for-loop
                        \%lua
                        ::stop_\(%var as lua identifier)::;
                    end -- end of scope for stopping for-loop

        return %lua

immediately
    parse [for %var in %start to %stop %body] as: for %var in %start to %stop via 1 %body

# For-each loop (lua's "ipairs()")
immediately
    compile [for %var in %iterable %body] to
        # This uses Lua's approach of only allowing loop-scoped variables in a loop
        assume (%var.type is "Var") or barf "Loop expected variable, not: \(%var's source code)"
        %lua <-
            Lua ".."
                for i,\(%var as lua identifier) in ipairs(\(%iterable as lua expr)) do
                    \(%body as lua statements)
        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "do next %") and
                        %.value.3.value is %var.value
        ..: to %lua write (Lua "\n    ::continue_\(%var as lua identifier)::;")
        to %lua write "\nend --foreach-loop"
        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "stop %") and
                        %.value.2.value is %var.value
        ..
            %lua <-
                Lua ".."
                    do -- scope for stopping for-loop
                        \%lua
                        ::stop_\(%var as lua identifier)::;
                    end -- end of scope for stopping for-loop
        return %lua

# Dict iteration (lua's "pairs()")
immediately
    compile [for %key = %value in %iterable %body] to
        # This uses Lua's approach of only allowing loop-scoped variables in a loop
        assume (%key.type is "Var") or barf "Loop expected variable, not: \(%key's source code)"
        assume (%value.type is "Var") or barf "Loop expected variable, not: \(%value's source code)"
        %lua <-
            Lua ".."
                for \(%key as lua identifier),\(%value as lua identifier) in pairs(\(%iterable as lua expr)) do
                    \(%body as lua statements)
        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "do next %") and
                        %.value.3.value is %key.value
        ..: to %lua write (Lua "\n    ::continue_\(%key as lua identifier)::;")

        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "do next %") and
                        %.value.3.value is %value.value
        ..: to %lua write (Lua "\n    ::continue_\(%value as lua identifier)::;")
        to %lua write "\nend --foreach-loop"
        
        %stop_labels <- (Lua "")
        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "stop %") and
                        %.value.2.value is %key.value
        ..: to %stop_labels write "\n::stop_\(%key as lua identifier)::;"

        if
            %body has subtree % where
                (%.type = "Action") and
                    ((%'s stub) is "stop %") and
                        %.value.2.value is %value.value
        ..: to %stop_labels write "\n::stop_\(%value as lua identifier)::;"

        if: (length of %stop_labels) > 0
            %lua <-
                Lua ".."
                    do -- scope for stopping for % = % loop
                        \%lua
                        \%stop_labels
                    end
        return %lua

# Switch statement/multi-branch if
immediately
    compile [when %body] to
        %code <- (Lua "")
        %fallthroughs <- []
        %is_first <- (yes)
        %seen_else <- (no)
        %branches <-
            %body.value if (%body.type = "Block") else [%body]
        for %func_call in %branches
            assume (%func_call.type is "Action") or barf ".."
                Invalid format for 'when' statement. Only '*' blocks are allowed.
            %tokens <- %func_call.value
            with {..}
                %star: %tokens.1
                %condition: %tokens.2
                %action: %tokens.3
            ..
                assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".."
                    Invalid format for 'when' statement. Lines must begin with '*'
                assume %condition or barf ".."
                    Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else"
                if: %action is (nil)
                    lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
                    do next %func_call

                if: (%condition.type is "Word") and (%condition.value is "else")
                    assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
                    to %code write "\nelse\n    "
                    to %code write: %action as lua statements
                    %seen_else <- (yes)
                ..else
                    assume (not %seen_else) or barf "'else' clause needs to be last in 'when' block"
                    lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
                    to %code write "\("if" if %is_first else "\nelseif") "
                    for %i=%condition in %fallthroughs
                        if (%i > 1): to %code write " or "
                        to %code write %condition
                    to %code write " then\n    "
                    to %code write (%action as lua statements)

                %fallthroughs <- []
                %is_first <- (no)

        assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block"
        assume ((length of %code) > 0) or barf "Empty body for 'when' block"
        to %code write "\nend --when"
        return %code

# Switch statement
immediately
    compile [when %branch_value = ? %body, when %branch_value is ? %body] to
        %code <- (Lua "")
        %fallthroughs <- []
        %is_first <- (yes)
        %seen_else <- (no)
        %branches <-
            %body.value if (%body.type = "Block") else [%body]
        for %func_call in %branches
            assume (%func_call.type is "Action") or barf ".."
                Invalid format for 'when' statement. Only '*' blocks are allowed.
            %tokens <- %func_call.value
            with {%star:%tokens.1, %condition:%tokens.2, %action:%tokens.3}
                assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".."
                    Invalid format for 'when' statement. Lines must begin with '*'
                assume %condition or barf ".."
                    Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else"
                if: %action is (nil)
                    lua> "table.insert(\%fallthroughs, \(%condition as lua expr))"
                    do next %func_call

                if: (%condition.type is "Word") and (%condition.value is "else")
                    assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block"
                    to %code write "\nelse\n    "
                    to %code write: %action as lua statements
                ..else
                    assume (not %seen_else) or barf "'else' clause needs to be last in 'when % = ?' block"
                    to %code write "\("if" if %is_first else "\nelseif") "
                    lua> "table.insert(\%fallthroughs, \(%condition as lua expr));"
                    for %i = % in %fallthroughs
                        if: %i > 1
                            to %code write " or "
                        if: (%.type is "Text") or (%.type is "Number")
                            to %code write "branch_value == \%"
                        ..else
                            to %code write "utils.equivalent(branch_value, \%)"
                    to %code write "then\n    "
                    to %code write (%action as lua statements)

            %fallthroughs <- []
            %is_first <- (no)
        
        assume (%fallthroughs = []) or barf "Unfinished fallthrough conditions in 'when' block"
        assume ((length of %code) > 0) or barf "No body for 'when % = ?' block!"
        to %code write "\nend"
        %code <-
            Lua ".."
                do --when % = ?
                    local branch_value = \(%branch_value as lua expr);
                    \%code
                end --when % = ?
        return %code

# Try/except
immediately
    compile [..]
        try %action and if it succeeds %success or if it barfs %fallback
        try %action and if it barfs %fallback or if it succeeds %success
    ..to
        Lua ".."
            do
                local fell_through = false;
                local ok, ret = pcall(function()
                    \(%action as lua statements)
                    fell_through = true;
                end);
                if ok then
                    \(%success as lua statements)
                end
                if not ok then
                    \(%fallback as lua statements)
                elseif not fell_through then
                    return ret;
                end
            end
    parse [try %action] as
        try %action and if it succeeds: do nothing
        ..or if it barfs: do nothing
    parse [try %action and if it barfs %fallback] as
        try %action and if it succeeds: do nothing
        ..or if it barfs %fallback
    parse [try %action and if it succeeds %success] as
        try %action and if it succeeds %success or if it barfs: do nothing

# Do/finally
immediately
    compile [do %action] to
        Lua ".."
            do
                \(%action as lua statements)
            end --do

    compile [do %action then always %final_action] to
        Lua ".."
            do
                local fell_through = false;
                local ok, ret1 = pcall(function()
                    \(%action as lua statements)
                    fell_through = true;
                end);
                local ok2, ret2 = pcall(function()
                    \(%final_action as lua statements)
                end);
                if not ok then error(ret1); end
                if not ok2 then error(ret2); end
                if not fell_through then
                    return ret1;
                end
            end --do-then-always

# Inline thunk:
immediately
    compile [result of %body] to
        Lua value ".."
            (function()
                \(%body as lua statements)
            end)()

# Coroutines:
immediately
    compile [values %body, coroutine %body, generator %body] to
        Lua value ".."
            (function()
                \(%body as lua statements)
            end)
    compile [->] to: Lua "coroutine.yield(true);"
    compile [-> %] to: Lua "coroutine.yield(true, \(% as lua expr));"
    compile [-> %k = %v] to: Lua "coroutine.yield(\(%k as lua expr), \(%v as lua expr));"