about summary refs log tree commit diff stats
path: root/test_apps
blob: a7a708300e1a317b7edf2d14a7a70c084d71a53c (plain) (blame)
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
#!/bin/sh
# Build and test all included SubX programs on Linux:
#   translate them into ELF binaries
#   compare the generated binaries with what's already in git
#   run/test the ELF binaries in emulated mode
#   run/test the ELF binaries in native mode
#
# Example usage:
#   test_apps
#   test_apps record  # don't compare with what's in git

set -e
cd `dirname $0`

./build

echo "== translating using the bootstrap C++ translator"

# example programs

echo ex1
./bootstrap_bin translate init.linux apps/ex1.subx  -o apps/ex1
test "$1" = 'record'  ||  git diff --exit-code apps/ex1
{
  ./bootstrap_bin run apps/ex1  ||  ret=$?
  test $ret -eq 42  # life, the universe and everything
}
{
  apps/ex1  ||  ret=$?
  test $ret -eq 42  # life, the universe and everything
}

echo ex2
./bootstrap_bin translate init.linux apps/ex2.subx  -o apps/ex2
test "$1" = 'record'  ||  git diff --exit-code apps/ex2
{
  ./bootstrap_bin run apps/ex2  ||  ret=$?
  test $ret -eq 7  # 3 + 4
}
{
  apps/ex2  ||  ret=$?
  test $ret -eq 7  # 3 + 4
}

echo ex3
./bootstrap_bin translate init.linux apps/ex3.subx  -o apps/ex3
test "$1" = 'record'  ||  git diff --exit-code apps/ex3
{
  ./bootstrap_bin run apps/ex3  ||  ret=$?
  test $ret -eq 55  # 1 + 2 + ... + 10
}
{
  apps/ex3  ||  ret=$?
  test $ret -eq 55  # 1 + 2 + ... + 10
}

echo ex4
./bootstrap_bin translate init.linux apps/ex4.subx  -o apps/ex4
test "$1" = 'record'  ||  git diff --exit-code apps/ex4
{
  echo a | ./bootstrap_bin run apps/ex4 >ex4.out  ||  true
  test `cat ex4.out` = 'a'
}
{
  echo a | apps/ex4 >ex4.out  ||  true
  test `cat ex4.out` = 'a'
}

echo ex5
./bootstrap_bin translate init.linux apps/ex5.subx  -o apps/ex5
test "$1" = 'record'  ||  git diff --exit-code apps/ex5
{
  echo a | ./bootstrap_bin run apps/ex5 >ex5.out  ||  true
  test `cat ex5.out` = 'a'
}
{
  echo a | apps/ex5 >ex5.out  ||  true
  test `cat ex5.out` = 'a'
}

echo ex6
./bootstrap_bin translate init.linux apps/ex6.subx  -o apps/ex6
test "$1" = 'record'  ||  git diff --exit-code apps/ex6
{
  ./bootstrap_bin run apps/ex6 >ex6.out  ||  true
  test "`cat ex6.out`" = 'Hello, world!'
}
{
  apps/ex6 >ex6.out  ||  true
  test "`cat ex6.out`" = 'Hello, world!'
}

echo ex7
./bootstrap_bin translate init.linux apps/ex7.subx  -o apps/ex7
test "$1" = 'record'  ||  git diff --exit-code apps/ex7
{
  ./bootstrap_bin run apps/ex7  ||  ret=$?
  test $ret -eq 97  # 'a'
}
{
  apps/ex7  ||  ret=$?
  test $ret -eq 97  # 'a'
}

echo ex8
./bootstrap_bin translate init.linux apps/ex8.subx  -o apps/ex8
test "$1" = 'record'  || git diff --exit-code apps/ex8
{
  ./bootstrap_bin run apps/ex8 abcd  ||  ret=$?
  test $ret -eq 4  # length('abcd')
}
{
  apps/ex8 abcd  ||  ret=$?
  test $ret -eq 4  # length('abcd')
}

echo ex9
./bootstrap_bin translate init.linux apps/ex9.subx  -o apps/ex9
test "$1" = 'record'  || git diff --exit-code apps/ex9
{
  ./bootstrap_bin run apps/ex9 z x  ||  ret=$?
  test $ret -eq 2  # 'z' - 'x'
}
{
  apps/ex9 z x  ||  ret=$?
  test $ret -eq 2  # 'z' - 'x'
}

echo ex10
./bootstrap_bin translate init.linux apps/ex10.subx  -o apps/ex10
test "$1" = 'record'  || git diff --exit-code apps/ex10
{
  ./bootstrap_bin run apps/ex10 abc abc  ||  ret=$?
  test $ret -eq 1  # equal
  ./bootstrap_bin run apps/ex10 abc abcd  # 0; not equal
}
{
  apps/ex10 abc abc  ||  ret=$?
  test $ret -eq 1  # equal
  apps/ex10 abc abcd  # 0; not equal
}

echo ex11
./bootstrap_bin translate init.linux apps/ex11.subx  -o apps/ex11
test "$1" = 'record'  || git diff --exit-code apps/ex11
{
  ./bootstrap_bin run apps/ex11
  echo
}
{
  apps/ex11
  echo
}

echo ex12
./bootstrap_bin translate init.linux apps/ex12.subx  -o apps/ex12
test "$1" = 'record'  || git diff --exit-code apps/ex12
test $EMULATED  &&  ./bootstrap_bin run apps/ex12  # final byte of mmap'd address is well-nigh guaranteed to be 0
test $NATIVE  &&  apps/ex12

echo ex13
./bootstrap_bin translate init.linux apps/ex13.subx  -o apps/ex13
test "$1" = 'record'  ||  git diff --exit-code apps/ex13
{
  ./bootstrap_bin run apps/ex13  ||  ret=$?
  test $ret -eq 1  # 3 == 3
}
{
  apps/ex13  ||  ret=$?
  test $ret -eq 1  # 3 == 3
}

# Larger apps that use the standard library.

echo factorial
./bootstrap_bin translate init.linux [01]*.subx apps/factorial.subx  -o apps/factorial
test "$1" = 'record'  ||  git diff --exit-code apps/factorial
{
  ./bootstrap_bin run apps/factorial  ||  ret=$?
  test $ret -eq 120  # factorial(5)
  ./bootstrap_bin run apps/factorial test
  echo
}
{
  apps/factorial  ||  ret=$?
  test $ret -eq 120  # factorial(5)
  apps/factorial test
  echo
}

echo crenshaw2-1
./bootstrap_bin translate init.linux [01]*.subx apps/crenshaw2-1.subx  -o apps/crenshaw2-1
test "$1" = 'record'  ||  git diff --exit-code apps/crenshaw2-1
{
  ./bootstrap_bin run apps/crenshaw2-1 test
  echo
}
{
  apps/crenshaw2-1 test
  echo
}

echo crenshaw2-1b
./bootstrap_bin translate init.linux [01]*.subx apps/crenshaw2-1b.subx  -o apps/crenshaw2-1b
test "$1" = 'record'  ||  git diff --exit-code apps/crenshaw2-1b
{
  ./bootstrap_bin run apps/crenshaw2-1b test
  echo
}
{
  apps/crenshaw2-1b test
  echo
}

echo random
./bootstrap_bin translate init.linux [01]*.subx apps/random.subx  -o apps/random
test "$1" = 'record'  ||  git diff --exit-code apps/random
# don't run

# Phases of the self-hosted SubX translator.

for phase in hex survey_elf pack assort dquotes tests
do
  echo $phase
  ./bootstrap_bin translate init.linux [01]*.subx apps/subx-params.subx apps/$phase.subx -o apps/$phase
  test "$1" = 'record'  ||  git diff --exit-code apps/$phase
  {
    ./bootstrap_bin run apps/$phase test
    echo
  }
  {
    apps/$phase test
    echo
  }
done

# One phase variant for translating SubX to baremetal. (Other phases are reused.)

for phase in survey_baremetal
do
  echo $phase
  ./bootstrap_bin translate init.linux [01]*.subx apps/subx-params.subx apps/$phase.subx -o apps/$phase
  test "$1" = 'record'  ||  git diff --exit-code apps/$phase
  {
    ./bootstrap_bin run apps/$phase test
    echo
  }
  {
    apps/$phase test
    echo
  }
done

# Higher-level syntax.

# Certain phases of translation run native beyond this point. We're starting
# to go beyond functionality of the C++ bootstrap_bin.

echo sigils
./bootstrap_bin translate init.linux [012]*.subx apps/subx-params.subx apps/sigils.subx  -o apps/sigils
test "$1" = 'record'  ||  git diff --exit-code apps/sigils
{
  ./bootstrap_bin run apps/sigils test
  echo
}
{
  apps/sigils test
  echo
}

echo calls
cat init.linux [012]*.subx apps/subx-params.subx apps/calls.subx  |  apps/sigils  > a.sigils
./bootstrap_bin translate a.sigils -o apps/calls
test "$1" = 'record'  ||  git diff --exit-code apps/calls
{
  ./bootstrap_bin run apps/calls test
  echo
}
{
  apps/calls test
  echo
}

echo braces
cat init.linux [012]*.subx apps/subx-params.subx apps/braces.subx  |  apps/calls  |  apps/sigils  > a.sigils
./bootstrap_bin translate a.sigils -o apps/braces
test "$1" = 'record'  ||  git diff --exit-code apps/braces
{
  ./bootstrap_bin run apps/braces test
  echo
}
{
  apps/braces test
  echo
}

echo mu
cat init.linux [0-2]*.subx apps/mu.subx  |  apps/braces  |  apps/calls  |  apps/sigils  > a.sigils
./bootstrap_bin translate a.sigils -o apps/mu
test "$1" = 'record'  ||  git diff --exit-code apps/mu
{
  ./bootstrap_bin run apps/mu test
  echo
}
{
  apps/mu test
  echo
}

echo "== translating using the self-hosted translator"

# example programs

for n in `seq 1 12`
do
  echo ex$n
  ./translate_subx init.linux apps/ex$n.subx
  diff apps/ex$n a.elf
done

# Larger apps that use the standard library.

for app in factorial crenshaw2-1 crenshaw2-1b
do
  echo $app
  ./translate_subx init.linux [01]*.subx apps/$app.subx
  diff apps/$app a.elf
done

# Phases of the self-hosted SubX translator.

for app in hex survey_elf pack assort dquotes tests
do
  echo $app
  ./translate_subx init.linux [01]*.subx apps/subx-params.subx apps/$app.subx
  diff apps/$app a.elf
done

# One phase variant for translating SubX to baremetal. (Other phases are reused.)

for app in survey_baremetal
do
  echo $app
  ./translate_subx init.linux [01]*.subx apps/subx-params.subx apps/$app.subx
  diff apps/$app a.elf
done

for app in sigils calls braces
do
  echo $app
  ./translate_subx init.linux [012]*.subx apps/subx-params.subx apps/$app.subx
  diff apps/$app a.elf
done

# Mu translator
echo mu
./translate_subx init.linux [0-2]*.subx apps/mu.subx
diff apps/mu a.elf

# Mu programs

echo ex1.mu
./translate_mu apps/ex1.mu
{
  ./bootstrap_bin run a.elf  ||  ret=$?
  test $ret -eq 42  # life, the universe and everything
}
{
  ./a.elf  ||  ret=$?
  test $ret -eq 42  # life, the universe and everything
}

echo ex2.mu
./translate_mu apps/ex2.mu
{
  ./bootstrap_bin run a.elf  ||  ret=$?
  test $ret -eq 7
}
{
  ./a.elf  ||  ret=$?
  test $ret -eq 7
}

echo ex3.mu
./translate_mu apps/ex3.mu
{
  ./bootstrap_bin run a.elf  ||  ret=$?
  test $ret -eq 55
}
{
  ./a.elf  ||  ret=$?
  test $ret -eq 55
}

echo ex3.2.mu
./translate_mu apps/ex3.2.mu
{
  ./bootstrap_bin run a.elf  ||  ret=$?
  test $ret -eq 55
}
{
  ./a.elf  ||  ret=$?
  test $ret -eq 55
}

echo factorial.mu
./translate_mu apps/factorial.mu
{
  ./bootstrap_bin run a.elf  ||  ret=$?
  test $ret -eq 120
  ./bootstrap_bin run a.elf test
  echo
}
{
  ./a.elf  ||  ret=$?
  test $ret -eq 120
  ./a.elf test
  echo
}

exit 0
effect { begin (at-history-start?:boolean <- lesser-or-equal current-history-index:integer/space:1 0:literal) (break-unless at-history-start?:boolean) (reply nil:literal) } ; then update history index, copy into current buffer ; test: 34<enter><up><enter> up restores previous command ; test todo: 34<enter>23<up>34<down><enter> up doesn't mess up typing on current line ; test todo: 34<enter><up>5<enter><up><up> commands don't modify history ; test todo: multi-line expressions ; identify the history item (current-history-index:integer/space:1 <- subtract current-history-index:integer/space:1 1:literal) (switch-to-history 0:space-address screen:terminal-address) ; <enter> is trimmed in the history expression, so wait for the human to ; hit <enter> again or backspace to make edits (reply nil:literal) } ; down arrow; switch to next item in history { begin (down-arrow?:boolean <- equal c:character ((down literal))) (break-unless down-arrow?:boolean) ; if history exists ; test: <down><enter> down without history has no effect { begin (empty-history?:boolean <- lesser-or-equal history-length:integer/space:1 0:literal) (break-unless empty-history?:boolean) (reply nil:literal) } ; if pointer not already at end of history ; test: 34<enter><down><down><enter> up past history has no effect { begin (x:integer <- subtract history-length:integer/space:1 1:literal) (before-history-end?:boolean <- greater-or-equal current-history-index:integer/space:1 x:integer) (break-unless before-history-end?:boolean) (reply nil:literal) } ; then update history index, copy into current buffer ; test: 34<enter><up><enter> up restores previous command ; test todo: 34<enter>23<up>34<down><enter> up doesn't mess up typing on current line ; test todo: 34<enter><up>5<enter><up><up> commands don't modify history ; test todo: multi-line expressions ; identify the history item (current-history-index:integer/space:1 <- add current-history-index:integer/space:1 1:literal) (switch-to-history 0:space-address screen:terminal-address) ; <enter> is trimmed in the history expression, so wait for the human to ; hit <enter> again or backspace to make edits (reply nil:literal) } ; if it's a newline, decide whether to return ; test: <enter>34<enter> { begin (newline?:boolean <- equal c:character ((#\newline literal))) (break-unless newline?:boolean) (print-character screen:terminal-address c:character/newline) (at-top-level?:boolean <- lesser-or-equal open-parens:integer/space:1 0:literal) (end-expression?:boolean <- and at-top-level?:boolean not-empty?:boolean/space:1) (reply end-expression?:boolean) } ; printable character; save ;? ($print (("append\n" literal))) ;? 2 (result:buffer-address/space:1 <- append result:buffer-address/space:1 c:character) ;? ($print (("done\n" literal))) ;? 2 ; if it's backslash, read, save and print one additional character ; test: (prn #\() { begin (backslash?:boolean <- equal c:character ((#\\ literal))) (break-unless backslash?:boolean) (print-character screen:terminal-address c:character/backslash 7:literal/white) (result:buffer-address/space:1 escapes:buffer-address/space:1 <- slurp-escaped-character result:buffer-address/space:1 7:literal/white escapes:buffer-address/space:1 abort:continuation/space:1 k:keyboard-address screen:terminal-address) (reply nil:literal) } ; if it's a semi-colon, parse a comment { begin (comment?:boolean <- equal c:character ((#\; literal))) (break-unless comment?:boolean) (print-character screen:terminal-address c:character/semi-colon 4:literal/fg/blue) (comment-read?:boolean <- slurp-comment result:buffer-address/space:1 escapes:buffer-address/space:1 abort:continuation/space:1 k:keyboard-address screen:terminal-address) ; return if comment was read (i.e. consumed a newline) ; test: ;a<backspace><backspace> (shouldn't end command until <enter>) { begin (break-if comment-read?:boolean) (reply nil:literal) } ; and we're not within parens ; test: (+ 1 2) ; comment<enter> ; test: (+ 1<enter>; abc<enter>2)<enter> ; test: ; comment<enter>(+ 1 2)<enter> ; too expensive to build: 3<backspace>; comment<enter>(+ 1 2)<enter> (at-top-level?:boolean <- lesser-or-equal open-parens:integer/space:1 0:literal) (end-expression?:boolean <- and at-top-level?:boolean not-empty?:boolean/space:1) (reply end-expression?:boolean) } ; if it's not whitespace, set not-empty? and continue { begin (space?:boolean <- equal c:character ((#\space literal))) (break-if space?:boolean) (newline?:boolean <- equal c:character ((#\newline literal))) (break-if newline?:boolean) (tab?:boolean <- equal c:character ((tab literal))) (break-if tab?:boolean) (not-empty?:boolean/space:1 <- copy t:literal) ; fall through } ; if it's a quote, parse a string { begin (string-started?:boolean <- equal c:character ((#\" literal))) ; for vim: " (break-unless string-started?:boolean) (print-character screen:terminal-address c:character/open-quote 6:literal/fg/cyan) (slurp-string result:buffer-address/space:1 escapes:buffer-address/space:1 abort:continuation/space:1 k:keyboard-address screen:terminal-address) (reply nil:literal) } ; color parens by depth, so they're easy to balance ; test: (+ 1 1)<enter> ; test: (def foo () (+ 1 (* 2 3)))<enter> { begin (open-paren?:boolean <- equal c:character ((#\( literal))) (break-unless open-paren?:boolean) (_ color-code:integer <- divide-with-remainder open-parens:integer/space:1 3:literal) ; 3 distinct colors for parens (color-code:integer <- add color-code:integer 1:literal) (print-character screen:terminal-address c:character/open-paren color-code:integer) (open-parens:integer/space:1 <- add open-parens:integer/space:1 1:literal) ;? ($print open-parens:integer/space:1) ;? 2 (reply nil:literal) } { begin (close-paren?:boolean <- equal c:character ((#\) literal))) (break-unless close-paren?:boolean) (open-parens:integer/space:1 <- subtract open-parens:integer/space:1 1:literal) (_ color-code:integer <- divide-with-remainder open-parens:integer/space:1 3:literal) ; 3 distinct colors for parens (color-code:integer <- add color-code:integer 1:literal) (print-character screen:terminal-address c:character/close-paren color-code:integer) ;? ($print open-parens:integer/space:1) ;? 2 (reply nil:literal) } ; if all else fails, print the character without color (print-character screen:terminal-address c:character/regular) ; todo: error on space outside parens, like python ; todo: [] ; todo: history on up/down (reply nil:literal) ]) (function switch-to-history [ (default-space:space-address <- new space:literal 30:literal) (0:space-address/names:read-expression <- next-input) (screen:terminal-address <- next-input) (clear-repl-state 0:space-address) (curr-history:string-address <- buffer-index history:buffer-address/space:1 current-history-index:integer/space:1) (curr-history-len:integer <- length curr-history:string-address/deref) ; and retype it into the current expression (hist:keyboard-address <- init-keyboard curr-history:string-address) (hist-index:integer-address <- get-address hist:keyboard-address/deref index:offset) { begin (done?:boolean <- greater-or-equal hist-index:integer-address/deref curr-history-len:integer) (break-if done?:boolean) (sub-return:boolean <- process-key 0:space-address hist:keyboard-address screen:terminal-address) (assert-false sub-return:boolean (("recursive call to process keys thought it was done" literal))) (loop) } ]) (function clear-repl-state [ (default-space:space-address/names:read-expression <- next-input) ; clear result (len:integer-address <- get-address result:buffer-address/deref length:offset) (backspace-over len:integer-address/deref screen:terminal-address) (len:integer-address/deref <- copy 0:literal) ; clear other state accumulated for the existing expression (open-parens:integer <- copy 0:literal) (escapes:buffer-address <- init-buffer 5:literal) (not-empty?:boolean <- copy nil:literal) ]) (function backspace-over [ (default-space:space-address <- new space:literal 30:literal) (len:integer <- next-input) (screen:terminal-address <- next-input) { begin (done?:boolean <- lesser-or-equal len:integer 0:literal) (break-if done?:boolean) (print-character screen:terminal-address ((#\backspace literal))) (len:integer <- subtract len:integer 1:literal) (loop) } ]) ; list of characters, list of indices of escaped characters, abort continuation ; -> whether a comment was consumed (can also return by backspacing past comment leader ';') (function slurp-comment [ (default-space:space-address <- new space:literal 30:literal) (in:buffer-address <- next-input) (escapes:buffer-address <- next-input) (abort:continuation <- next-input) (k:keyboard-address <- next-input) (screen:terminal-address <- next-input) ; test: ; abc<enter> { begin next-key-in-comment (c:character <- wait-for-key k:keyboard-address silent:literal/terminal) (maybe-cancel-this-expression c:character abort:continuation screen:terminal-address) ; test: check needs to come before print (print-character screen:terminal-address c:character 4:literal/fg/blue) ; handle backspace ; test: ; abc<backspace><backspace>def<enter> ; todo: how to exit comment? { begin (backspace?:boolean <- equal c:character ((#\backspace literal))) (break-unless backspace?:boolean) (len:integer-address <- get-address in:buffer-address/deref length:offset) ; buffer has to have at least the semi-colon so can't be empty (len:integer-address/deref <- subtract len:integer-address/deref 1:literal) ; if we erase start of comment, return (comment-deleted?:boolean <- backspaced-over-unescaped? in:buffer-address ((#\; literal)) escapes:buffer-address) ; " (jump-unless comment-deleted?:boolean next-key-in-comment:offset) ; loop (reply nil:literal/read-comment?) } (in:buffer-address <- append in:buffer-address c:character) (newline?:boolean <- equal c:character ((#\newline literal))) (loop-unless newline?:boolean) } (reply t:literal/read-comment?) ]) (function slurp-string [ (default-space:space-address <- new space:literal 30:literal) (in:buffer-address <- next-input) (escapes:buffer-address <- next-input) (abort:continuation <- next-input) (k:keyboard-address <- next-input) (screen:terminal-address <- next-input) ; test: "abc" { begin next-key-in-string (c:character <- wait-for-key k:keyboard-address silent:literal/terminal) (maybe-cancel-this-expression c:character abort:continuation screen:terminal-address) ; test: check needs to come before print (print-character screen:terminal-address c:character 6:literal/fg/cyan) ; handle backspace ; test: "abc<backspace>d" ; todo: how to exit string? { begin (backspace?:boolean <- equal c:character ((#\backspace literal))) (break-unless backspace?:boolean) (len:integer-address <- get-address in:buffer-address/deref length:offset) ; typed a quote before calling slurp-string, so can't be empty (len:integer-address/deref <- subtract len:integer-address/deref 1:literal) ; if we erase start of string, return ; test: "<backspace>34 (string-deleted?:boolean <- backspaced-over-unescaped? in:buffer-address ((#\" literal)) escapes:buffer-address) ; " ;? ($print string-deleted?:boolean) ;? 1 (jump-if string-deleted?:boolean end:offset) ; break (jump next-key-in-string:offset) ; loop } (in:buffer-address <- append in:buffer-address c:character) ; break on quote -- unless escaped by backslash ; test: "abc\"ef" { begin (backslash?:boolean <- equal c:character ((#\\ literal))) (break-unless backslash?:boolean) (in:buffer-address escapes:buffer-address <- slurp-escaped-character in:buffer-address 6:literal/cyan escapes:buffer-address abort:continuation k:keyboard-address screen:terminal-address) (jump next-key-in-string:offset) ; loop } ; if not backslash (end-quote?:boolean <- equal c:character ((#\" literal))) ; for vim: " (loop-unless end-quote?:boolean) } end ]) ; buffer to add character to, color to print it in to the screen, abort continuation (function slurp-escaped-character [ (default-space:space-address <- new space:literal 30:literal) (in:buffer-address <- next-input) (color-code:integer <- next-input) (escapes:buffer-address <- next-input) (abort:continuation <- next-input) (k:keyboard-address <- next-input) (screen:terminal-address <- next-input) (c:character <- wait-for-key k:keyboard-address silent:literal/terminal) (maybe-cancel-this-expression c:character abort:continuation screen:terminal-address) ; test: check needs to come before print (print-character screen:terminal-address c:character color-code:integer) (len:integer-address <- get-address in:buffer-address/deref length:offset) (escapes:buffer-address <- append escapes:buffer-address len:integer-address/deref) ;? ($print (("+" literal))) ;? 1 ; handle backspace ; test: "abc\<backspace>def" ; test: #\<backspace> { begin (backspace?:boolean <- equal c:character ((#\backspace literal))) (break-unless backspace?:boolean) ; just typed a backslash, so buffer can't be empty (len:integer-address/deref <- subtract len:integer-address/deref 1:literal) (elen:integer-address <- get-address escapes:buffer-address/deref length:offset) (elen:integer-address/deref <- subtract elen:integer-address/deref 1:literal) ;? ($print (("-" literal))) ;? 1 (reply in:buffer-address/same-as-arg:0 escapes:buffer-address/same-as-arg:2) } ; if not backspace, save and return (in:buffer-address <- append in:buffer-address c:character) (reply in:buffer-address/same-as-arg:0 escapes:buffer-address/same-as-arg:2) ]) (function backspaced-over-unescaped? [ (default-space:space-address <- new space:literal 30:literal) (in:buffer-address <- next-input) (expected:character <- next-input) (escapes:buffer-address <- next-input) ; char just backspaced over matches { begin (c:character <- past-last in:buffer-address) (char-match?:boolean <- equal c:character expected:character) (break-if char-match?:boolean) (reply nil:literal) } ; and char before cursor is not an escape { begin (most-recent-escape:integer <- last escapes:buffer-address) (last-idx:integer <- get in:buffer-address/deref length:offset) ;? ($print most-recent-escape:integer) ;? 1 ;? ($print last-idx:integer) ;? 1 (was-unescaped?:boolean <- not-equal last-idx:integer most-recent-escape:integer) (break-if was-unescaped?:boolean) (reply nil:literal) } (reply t:literal) ]) ; return the character past the end of the buffer, if there's room (function past-last [ (default-space:space-address <- new space:literal 30:literal) (in:buffer-address <- next-input) (n:integer <- get in:buffer-address/deref length:offset) (s:string-address <- get in:buffer-address/deref data:offset) (capacity:integer <- length s:string-address/deref) { begin (no-space?:boolean <- greater-or-equal n:integer capacity:integer) (break-unless no-space?:boolean) (reply ((#\null literal))) } (result:character <- index s:string-address/deref n:integer) (reply result:character) ]) (function maybe-cancel-this-expression [ ; check for ctrl-g and abort (default-space:space-address <- new space:literal 30:literal) (c:character <- next-input) (abort:continuation <- next-input) (screen:terminal-address <- next-input) { begin (interrupt?:boolean <- equal c:character ((ctrl-g literal))) (break-unless interrupt?:boolean) (print-character screen:terminal-address ((#\^ literal))) (print-character screen:terminal-address ((#\G literal))) (print-character screen:terminal-address ((#\newline literal))) (continue-from abort:continuation) } ]) (function main [ (default-space:space-address <- new space:literal 30:literal) (cursor-mode) ($print (("connected to anarki! type in an expression, then hit enter. ctrl-d exits. ctrl-g clears the current expression." literal))) (print-character nil:literal/terminal ((#\newline literal))) ; todo: ctrl-g shouldn't clear history (abort:continuation <- current-continuation) (history:buffer-address <- init-buffer 5:literal) ; buffer of buffers of strings, one per expression typed in { begin (s:string-address <- read-expression nil:literal/keyboard nil:literal/terminal abort:continuation history:buffer-address) (break-unless s:string-address) ;? (x:integer <- length s:string-address/deref) ;? 1 ;? ($print x:integer) ;? 1 ;? ($print ((#\newline literal))) ;? 1 (history:buffer-address <- append history:buffer-address s:string-address) ;? (len:integer <- get history:buffer-address/deref length:offset) ;? 1 ;? ($print len:integer) ;? 1 ;? ($print ((#\newline literal))) ;? 1 (retro-mode) ; print errors cleanly ;? (print-string nil:literal/terminal s:string-address) ;? 1 (t:string-address <- $eval s:string-address) (cursor-mode) ($print (("=> " literal))) (print-string nil:literal/terminal t:string-address) (print-character nil:literal/terminal ((#\newline literal))) (print-character nil:literal/terminal ((#\newline literal))) ; empty line separates each expression and result (loop) } ])