-
Notifications
You must be signed in to change notification settings - Fork 1
/
cond_loop.cljc
616 lines (496 loc) · 18.7 KB
/
cond_loop.cljc
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
(ns fif.stdlib.cond-loop
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.sub-stack :as sub-stack]
[fif.stack-machine.words :refer [set-global-word-defn]]
[fif.stack-machine.exceptions :as exceptions]
[fif.utils.token :as token]
[fif.stdlib.conditional :refer [condition-true?]]
[fif.stack-machine.processor :as stack.processor]))
(def arg-do-token 'do)
(def arg-loop-token 'loop)
(def arg-loopend-token 'loopend)
(def arg-plus-loop-token '+loop)
(def arg-plus-loopend-token '+loopend)
(def arg-leave-token 'leave)
;; end start do ... loop
;; end start do ... +loop
(def arg-begin-token 'begin)
(def arg-until-token 'until)
(def arg-untilend-token 'untilend)
;; begin ... ? until
(def arg-while-token 'while)
(def arg-repeat-token 'repeat)
(def arg-repeatend-token 'repeatend)
;; begin ? while ... repeat
(def do-mode-flag :do-mode)
(def inner-do-mode-flag :inner-do-mode)
(def loop-mode-flag :loop-mode)
(def loop-leave-mode-flag :loop-leave-mode)
(def loop-plus-mode-flag :loop-plus-mode)
(def begin-mode-flag :begin-mode)
(def inner-begin-mode-flag :inner-begin-mode)
(def begin-until-mode-flag :begin-until-mode)
(def begin-until-leave-mode-flag :begin-until-leave-mode)
(def begin-while-mode-flag :begin-while-mode)
(def begin-while-leave-mode-flag :begin-while-leave-mode)
(def begin-dump-mode-flag :begin-dump-mode)
(defn get-loop-start-index [sm]
(-> sm stack/get-ret peek first))
(defn get-loop-end-index [sm]
(-> sm stack/get-ret peek second))
(defn increment-loop-index [sm i]
(let [[start end] (-> sm stack/get-ret peek)]
(-> sm stack/pop-ret (stack/push-ret [(+ start i) end]))))
(defn loop-leave-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(or (= arg arg-loopend-token)
(= arg arg-plus-loopend-token))
(-> sm
stack/pop-flag
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/pop-ret
stack/dequeue-code)
:else
(-> sm stack/dequeue-code))))
(defn loop-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
;; Finished processing the current loop iteration
(= arg arg-loopend-token)
(let [start-idx (get-loop-start-index sm)
end-idx (get-loop-end-index sm)]
(if-not (>= start-idx end-idx)
;; Prepare the next loop iteration
(let [loop-body (sub-stack/get-sub-stack stash)
new-code (concat (reverse loop-body)
(list arg-loopend-token)
(-> sm stack/dequeue-code stack/get-code))]
(-> sm
(increment-loop-index 1)
(stack/set-code new-code)))
;; Clean up the return stack, remove our loop content from
;; the stash, get out of loop mode and continue execution
(-> sm
stack/pop-flag
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/pop-ret
stack/dequeue-code)))
;; Finished processing the current loop iteration
(= arg arg-plus-loopend-token)
(let [start-idx (get-loop-start-index sm)
end-idx (get-loop-end-index sm)]
(if-not (>= start-idx end-idx)
;; Prepare the next loop iteration
(let [[loop-step] (stack/get-stack sm)
loop-body (sub-stack/get-sub-stack stash)
new-code (concat (reverse loop-body)
(list arg-plus-loopend-token)
(-> sm stack/dequeue-code stack/get-code))]
(-> sm
stack/pop-stack
(increment-loop-index loop-step)
(stack/set-code new-code)))
;; Clean up the return stack, remove our loop content from
;; the stash, get out of loop mode and continue execution
(-> sm
stack/pop-stack
stack/pop-flag
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/pop-ret
stack/dequeue-code)))
:else
(-> sm stack.processor/process-arg))))
(defn inner-do-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(or (= arg arg-loop-token)
(= arg arg-plus-loop-token))
(-> sm
stack/pop-flag
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code)
(= arg arg-do-token)
(-> sm
(stack/push-flag inner-do-mode-flag)
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code)
:else
(-> sm
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code))))
(defn do-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(= arg arg-do-token)
(-> sm
(stack/push-flag inner-do-mode-flag)
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code)
;; Go into loop-mode
(or (= arg arg-loop-token)
(= arg arg-plus-loop-token))
(let [loop-body (sub-stack/get-sub-stack stash)
end-token (cond (= arg arg-loop-token)
arg-loopend-token
(= arg arg-plus-loop-token)
arg-plus-loopend-token)
new-code (concat (reverse loop-body)
(list end-token)
(-> sm stack/dequeue-code stack/get-code))]
(-> sm
;; Leave do-mode and enter loop-mode
stack/pop-flag
(stack/push-flag loop-mode-flag)
;; The stashed away loop content can now be placed back on
;; the code queue for the first loop iteration
(stack/set-code new-code)))
;; Push the loop body into the stash sub-stack
:else
(-> sm
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code))))
(defn start-do
"Do structure is <start> <end> do (<body> loop)|(<body ... step> +loop)"
[sm]
(let [[start end] (-> sm stack/get-stack)
stash (stack/get-stash sm)]
(-> sm
;; pop the start and end values off of the stack
stack/pop-stack
stack/pop-stack
;; push the start and end values onto the return stack
(stack/push-ret [start end])
;; start do-mode
(stack/push-flag do-mode-flag)
;; create and stash a sub-stack to hold our loop body
(stack/set-stash (sub-stack/create-sub-stack stash))
;; remove code token for next iteration
stack/dequeue-code)))
(defn get-loop-index-1
[sm]
(let [idx (-> sm stack/get-ret first first)]
(-> sm (stack/push-stack idx) stack/dequeue-code)))
(defn get-loop-index-2
[sm]
(let [idx (-> sm stack/get-ret second first)]
(-> sm (stack/push-stack idx) stack/dequeue-code)))
(defn get-loop-index-3
[sm]
(let [idx (-> sm stack/get-ret (nth 2) first)]
(-> sm (stack/push-stack idx) stack/dequeue-code)))
(defn begin-until-leave-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(= arg arg-untilend-token)
(-> sm
stack/pop-flag
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/dequeue-code)
:else
(-> sm stack/dequeue-code))))
(defn begin-while-leave-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(= arg arg-repeatend-token)
(-> sm
stack/pop-flag
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/dequeue-code)
:else
(-> sm stack/dequeue-code))))
(defn begin-dump-mode
[sm]
(let [arg (-> sm stack/get-code first)]
(cond
(= arg arg-begin-token)
(-> sm
(stack/push-flag begin-dump-mode-flag)
stack/dequeue-code)
(or (= arg arg-until-token)
(= arg arg-repeat-token)
(= arg arg-repeatend-token))
(-> sm
stack/pop-flag
stack/dequeue-code)
:else
(-> sm stack/dequeue-code))))
(defn begin-while-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
;; Determine if the rest of the loop content should be
;; processed. If it can be processed, continue on until we
;; reach the repeat clause If we are not continuing, then we
;; need to dump the loop content and continue.
(= arg arg-while-token)
(let [[flag] (-> sm stack/get-stack)]
(if (condition-true? flag)
(-> sm
stack/pop-stack
stack/dequeue-code)
(-> sm
stack/pop-stack
stack/pop-flag
(stack/push-flag begin-dump-mode-flag)
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/dequeue-code)))
;; Finished processing the current loop iteration
(= arg arg-repeatend-token)
;; Prepare the next loop iteration
(let [loop-body (sub-stack/get-sub-stack stash)
new-code (concat (reverse loop-body)
(list arg-repeatend-token)
(-> sm stack/dequeue-code stack/get-code))]
(-> sm
(stack/set-code new-code)))
:else
(-> sm stack.processor/process-arg))))
(defn begin-until-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
;; Finished processing the current loop iteration
(= arg arg-untilend-token)
(let [[flag] (-> sm stack/get-stack)]
(if-not (condition-true? flag)
;; Prepare the next loop iteration
(let [loop-body (sub-stack/get-sub-stack stash)
new-code (concat (reverse loop-body)
(list arg-untilend-token)
(-> sm stack/dequeue-code stack/get-code))]
(-> sm
stack/pop-stack
(stack/set-code new-code)))
;; Clean up the return stack, remove our loop content from
;; the stash, get out of begin-until-mode and continue execution
(-> sm
stack/pop-stack
stack/pop-flag
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/dequeue-code)))
:else
(-> sm stack.processor/process-arg))))
(defn inner-begin-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(or (= arg arg-until-token)
(= arg arg-repeat-token))
(-> sm
stack/pop-flag
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code)
(= arg arg-begin-token)
(-> sm
(stack/push-flag inner-do-mode-flag)
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code)
:else
(-> sm
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code))))
(defn begin-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(= arg arg-begin-token)
(-> sm
(stack/push-flag inner-begin-mode-flag)
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code)
;; Go into begin-until-mode
(= arg arg-until-token)
(let [loop-body (sub-stack/get-sub-stack stash)
new-code (concat (reverse loop-body)
(list arg-untilend-token)
(-> sm stack/dequeue-code stack/get-code))]
(-> sm
;; Leave begin-mode and enter begin-until-mode
stack/pop-flag
(stack/push-flag begin-until-mode-flag)
;; The stashed away loop content can now be placed back on
;; the code queue for the first loop iteration
(stack/set-code new-code)))
;; Go into begin-until-mode
(= arg arg-repeat-token)
(let [loop-body (sub-stack/get-sub-stack stash)
new-code (concat (reverse loop-body)
(list arg-repeatend-token)
(-> sm stack/dequeue-code stack/get-code))]
(-> sm
;; Leave begin-mode and enter begin-while-mode
stack/pop-flag
(stack/push-flag begin-while-mode-flag)
;; The stashed away loop content can now be placed back on
;; the code queue for the first loop iteration
(stack/set-code new-code)))
;; Push the loop body into the stash sub-stack
:else
(-> sm
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code))))
(defn start-begin
"Begin structure begin <body ... ?> until
begin <?> while <body> repeat"
[sm]
(let [[start end] (-> sm stack/get-stack)
stash (stack/get-stash sm)]
(-> sm
;; start begin-mode
(stack/push-flag begin-mode-flag)
;; create and stash a sub-stack to hold our loop body
(stack/set-stash (sub-stack/create-sub-stack stash))
;; remove code token for next iteration
stack/dequeue-code)))
(defn leave-recent-loop
"Find the most recent loop flag and replace it with a loop leave
mode.
Notes:
- Note that any additional conditional modes that are passed need to
be also be replaced with a dump to preserve the stash."
[sm]
(let [flags (-> sm stack/get-flags reverse)
;;FIXME: does not handle situations where there is no loop flags.
recent-begin-until (concat (token/take-to-token flags begin-until-mode-flag)
[begin-until-mode-flag])
recent-begin-while (concat (token/take-to-token flags begin-while-mode-flag)
[begin-while-mode-flag])
recent-loop (concat (token/take-to-token flags loop-mode-flag)
[loop-mode-flag])
recent-listing
(->> (sort-by count [recent-begin-until recent-begin-while recent-loop])
first)
leave-loop-tag
(condp = (last recent-listing)
loop-mode-flag loop-leave-mode-flag
begin-until-mode-flag begin-until-leave-mode-flag
begin-while-mode-flag begin-while-leave-mode-flag)
new-flags
(as-> flags $
(drop (count recent-listing) $)
(reverse $)
(concat $ [leave-loop-tag])
(vec $))]
(-> sm (stack/set-flags new-flags))))
#_(-> (stack/new-stack-machine)
(stack/push-flag :test)
(stack/push-flag :another-value)
(stack/push-flag begin-until-mode-flag)
(stack/push-flag begin-while-mode-flag)
(stack/push-flag :test2)
(stack/push-flag :test3)
(stack/push-flag loop-mode-flag)
(leave-recent-loop)
(stack/get-flags))
(defn start-leave
"Leaves whatever the current loop is.
Goes through the flags, and replaces the most recent loop with a dump-mode"
[sm]
(let []
(-> sm leave-recent-loop stack/dequeue-code)))
(def doc-string-do "<end> <start> do <body> loop|+loop -- Loop conditional")
(def doc-string-begin "begin <body> <flag> until | begin <flag> while <body> repeat -- Loop conditional -- Loop conditional")
(defn import-stdlib-cond-loop-mode [sm]
(-> sm
(set-global-word-defn
arg-do-token start-do
:stdlib? true
:doc doc-string-do
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-loop-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-do
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-loopend-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-do
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-plus-loop-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-do
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-plus-loopend-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-do
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
'i get-loop-index-1
:stdlib? true
:doc "( -- n ) Retrieve the index of the innermost loop."
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
'j get-loop-index-2
:stdlib? true
:doc "( -- n ) Retrieve the index of the second from the innermost loop.")
(set-global-word-defn
'k get-loop-index-3
:stdlib? true
:doc "( -- n ) Retrieve the index of the third from the innermost loop.")
(stack/set-mode do-mode-flag do-mode)
(stack/set-mode inner-do-mode-flag inner-do-mode)
(stack/set-mode loop-mode-flag loop-mode)
(stack/set-mode loop-leave-mode-flag loop-leave-mode)
(set-global-word-defn
arg-begin-token start-begin
:stdlib? true
:doc doc-string-begin
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-until-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-begin
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-untilend-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-begin
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-while-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-begin
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-repeat-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-begin
:group :stdlib.mode.conditional-loop)
(set-global-word-defn
arg-repeatend-token exceptions/raise-unbounded-mode-argument
:stdlib? true
:doc doc-string-begin
:group :stdlib.mode.conditional-loop)
(stack/set-mode begin-mode-flag begin-mode)
(stack/set-mode inner-begin-mode-flag inner-begin-mode)
(stack/set-mode begin-until-mode-flag begin-until-mode)
(stack/set-mode begin-while-mode-flag begin-while-mode)
(stack/set-mode begin-dump-mode-flag begin-dump-mode)
(stack/set-mode begin-while-leave-mode-flag begin-while-leave-mode)
(stack/set-mode begin-until-leave-mode-flag begin-until-leave-mode)
(set-global-word-defn
arg-leave-token start-leave
:stdlib? true
:doc "Used within a conditional loop to leave early"
:group :stdlib.mode.conditional-loop)))