-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitespace.reb
485 lines (390 loc) · 13 KB
/
whitespace.reb
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
Rebol [
Title: "Whitespace Interpreter"
Purpose: "Whitespace Language Written as a Rebol 3 Parse Dialect"
Author: "Hostile Fork"
Home: http://github.com/hostilefork/whitespacers/
License: mit
File: %whitespace.reb
Date: 31-Jan-2019
Version: 0.3.0
; Header conventions: http://www.rebol.org/one-click-submission-help.r
Type: fun
Level: intermediate
Description: {
This is an interpreter for the Whitespace language in the Ren-C branch
of the Rebol 3 language:
http://compsoc.dur.ac.uk/whitespace/
What makes it somewhat unique is that it does some fairly accessible
bending of the the language itself so that it acts like a language
that was designed for making interpreters for languages that are
"Whitespace-like".
Due to being specification-driven in a homoiconic language (where data
is code and code is data), it gains advantages in things like
documentation generation.
}
Usage: {
Run with the argument of a file that you wish to process. The
extension determines the handling:
.ws - An "official" whitespace program, where the instructions are the
actual ASCII codes for SPACE, TAB, and LF.
.wsw - Format based on the actual *words* `space`, `tab` and `lf`.
Semicolons are used for comments to end of line.
(.wsa for Whitespace Assembler format is not yet supported)
}
History: [
0.1.0 [8-Oct-2009 {Private release to R3 Chat Group for commentary}]
0.2.0 [10-Jul-2010 {Public release as part of a collection of
whitespace interpreters in various languages}]
0.3.0 [31-Jan-2019 {Converted to Ren-C with more ambitious concept
of dialecting the spec blended with the instruction handling code.}]
]
]
import %ws-common.reb
import %ws-dialect.reb ; defines the CATEGORY and OPERATION used below
vm: import %ws-runtime.reb ; runtime stack, program counters, etc.
=== CONTROL SEQUENCE DEFINITIONS ===
; http://compsoc.dur.ac.uk/whitespace/tutorial.php
Stack-Manipulation: category [
IMP: [space]
description: {
Stack manipulation is one of the more common operations, hence the
shortness of the IMP [space].
}
push: operation [
{Push the number onto the stack}
space [value: Number]
][
insert stack value
]
duplicate-top: operation [
{Duplicate the top item on the stack}
lf space
][
insert stack first stack
]
duplicate-indexed: operation [
{Copy Nth item on the stack (given by the arg) to top of stack}
tab space [index: Number]
][
insert stack pick stack index + 1
]
swap-top-2: operation [
{Swap the top two items on the stack}
lf tab
][
move/part stack 1 1
]
discard-top: operation [
{Discard the top item on the stack}
lf lf
][
take stack
]
slide-n-values: operation [
{Slide n items off the stack, keeping the top item}
tab lf [n: Number]
][
take/part next stack n
]
]
do-arithmetic: func [
operator [word!]
][
; note the first item pushed is the left of the operation.
let right: take stack
let left: take stack
insert stack do reduce [ ; we could also `reeval operator left right`
operator left right
]
]
Arithmetic: category [
IMP: [tab space]
description: {
Arithmetic commands operate on the top two items on the stack, and
replace them with the result of the operation. The first item pushed
is considered to be left of the operator.
The copy and slide instructions are an extension implemented in
Whitespace 0.3 and are designed to facilitate the implementation of
recursive functions. The idea is that local variables are referred to
using [space tab space], then on return, you can push the return
value onto the top of the stack and use [space tab lf] to discard the
local variables.
}
add: operation [
{Addition}
space space
][
do-arithmetic $add
]
subtract: operation [
{Subtraction}
space tab
][
do-arithmetic $subtract
]
multiply: operation [
{Multiplication}
space lf
][
do-arithmetic $multiply
]
divide: operation [
{Integer Division}
tab space
][
do-arithmetic $divide
]
modulo: operation [
{Modulo}
tab tab
][
do-arithmetic $modulo
]
]
Heap-Access: category [
IMP: [tab tab]
description: {
Heap access commands look at the stack to find the address of items
to be stored or retrieved. To store an item, push the address then the
value and run the store command. To retrieve an item, push the address
and run the retrieve command, which will place the value stored in
the location at the top of the stack.
}
store: operation [
{Store}
space
][
let value: take stack ; spec does not explicitly specify removal
let address: ensure integer! take stack ; (same)
heap.(address): value
]
retrieve: operation [
{Retrieve}
tab
][
let address: take stack ; spec does not explicitly specify removal
let value: select heap address
insert stack value
]
]
Flow-Control: category [
IMP: [lf]
description: {
Flow control operations are also common. Subroutines are marked by
labels, as well as the targets of conditional and unconditional jumps,
by which loops can be implemented. Programs must be ended by means of
[lf lf lf] so that the interpreter can exit cleanly.
}
mark-location: operation [
{Mark a location in the program}
space space [label: Label]
<local> address ; could use LET, but test expanded spec feature
][
; Capture the position *after* this instruction. We calculate
; relative to program-start in case the whitespace data did not start
; right at the beginning. Must add 1 to be in the Redbol 1-based
; series indexing mode (what PARSE's SEEK expects to use)
;
address: 1 + offset? program-start instruction-end
labels.(label): address
]
call-subroutine: operation [
{Call a subroutine}
space tab [label: Label]
][
; Call subroutine must be able to find the current parse location
; (a.k.a. program counter) so it can put it in the callstack.
;
let current-offset: 1 + offset? program-start instruction-end
insert callstack current-offset
return lookup-label-offset label
]
jump-to-label: operation [
{Jump unconditionally to a Label}
space lf [label: Label]
][
return lookup-label-offset label
]
jump-if-zero: operation [
{Jump to a Label if the top of the stack is zero}
tab space [label: Label]
][
; must pop stack to make example work
if zero? take stack [
return lookup-label-offset label
]
]
jump-if-negative: operation [
{Jump to a Label if the top of the stack is negative}
tab tab [label: Label]
][
; must pop stack to make example work
if 0 > take stack [
return lookup-label-offset label
]
]
return-from-subroutine: operation [
{End a subroutine and transfer control back to the caller}
tab lf
][
return take callstack else [
fail "RUNTIME ERROR: return with no callstack!"
]
]
end-program: operation [
{End the program}
lf lf
][
; Requesting to jump to the address at the end of the program will be
; the same as reaching it normally, terminating the PARSE interpreter.
;
return length of program-start + 1
]
]
IO: category [
IMP: [tab lf]
description: {
Finally, we need to be able to interact with the user. There are IO
instructions for reading and writing numbers and individual characters.
With these, string manipulation routines can be written (see examples
to see how this may be done).
The read instructions take the heap address in which to store the
result from the top of the stack.
Note: spec didn't say we should pop the stack when we output, but
the sample proves we must!
}
output-character-on-stack: operation [
{Output the character at the top of the stack}
space space
][
; Note: ISSUE! is being merged with CHAR! to be something called TOKEN!
; So `#a` acts as what `#"a"` was in historical Rebol. This is a work
; in progress and the renaming has not yet been done.
;
write-stdout make issue! first stack
take stack
]
output-number-on-stack: operation [
{Output the number at the top of the stack}
space tab
][
; When a number is written out, there is no newline. So we use
; WRITE-STDOUT of the string conversion instead of PRINT.
;
write-stdout to text! first stack
take stack
]
read-character-to-location: operation [
{Read a character to the location given by the top of the stack}
tab space
][
; !!! see notes above about ISSUE!/CHAR! duality, work-in-progress
;
let char: ask issue! else [fail "Character Input Was Required"]
let address: ensure integer! take stack
heap.(address): codepoint of char
]
read-number-to-location: operation [
{Read a number to the location given by the top of the stack}
tab tab
][
let address: ensure integer! take stack
let num: ask integer! else [fail "Integer Input Was Required"]
heap.(address): num
]
]
=== PROCESS COMMAND-LINE ARGUMENTS ===
strict: false
filename: null
; Note that system.script.args is the arguments given to the script, e.g. if
; you ran it from an interpreter with:
;
; >> do/args %whitespace.reb ["--verbose" "1" "examples/tutorial.ws"]
;
; The system.options.args would reflect the arguments the interpreter itself
; had been started up with. These are the same when running the script from
; the command line.
;
parse system.script.args [while [not <end>] [
["-v" | "--verbose"]
vm.verbose: [
"0" (0) | "1" (1) | "2" (2) | "3" (3)
| fail @["--verbose must be 0, 1, 2, or 3"]
]
|
"--strict" (strict: true)
|
"--max-steps" vm.max-steps: subparse text! [integer!]
|
subparse text! [
ahead "--"
fail @["Unknown command line option"]
]
|
(if filename [
fail "Only one filename allowed on command line at present"
])
filename: [
subparse text! [file! | url!] ; try decoding as FILE! or URL! first
| to-file/ text! ; fall back to converting string TO-FILE
]
]]
except [
fail "Invalid command line parameter"
]
if not filename [
fail "No input file given"
]
if vm.verbose > 0 [
===/visibility true ; show the `=== xxxx ===` lines
]
=== LOAD THE SOURCE INTO PROGRAM VARIABLE ===
program: parse filename [
thru [
".ws" <end> (as text! read filename)
| ".wsw" <end> (unspaced load filename) ; "whitespace words"
| ".wsa" <end> (fail "WSA support not implemented yet")
]
] except [
if strict [
fail "Only `.ws`, `.wsa`, and `.wsw` formats supported in strict mode"
]
as text! read filename ; tolerate
]
=== REMOVE NON-WHITESPACE CHARACTERS FROM PROGRAM ===
; The spec of the whitespace language is supposed to skip over non-whitespace.
; If those characters are left in the program, it is a fairly high overhead
; for all the code which does decoding to filter them out. Pre-filter.
remove-each ch program [
all [ch != space, ch != tab, ch != lf]
]
=== OUTPUT PROGRAM SOURCE IF (VERBOSE >= 1) ===
; There should be more options to decompile and save the source.
if vm.verbose >= 1 [
print mold program
]
=== LABEL SCANNING PASS ===
; We have to scan the program for labels before we run it. This also lets us
; know if all the constructions are valid before we start running.
vm.pass: 1
parse program vm.interpreter-rule except [
fail "INVALID INPUT"
]
if vm.verbose >= 1 [
print mold labels
]
=== PROGRAM EXECUTION PASS ===
; The Rebol parse dialect has the flexibility to do arbitrary seeks to
; locations in the input. This makes it possible to apply it to a language
; like whitespace, where the parse position acts as the program counter.
vm.pass: 2
parse program vm.interpreter-rule except [
fail "UNEXPECTED TERMINATION (Internal Error)"
]
=== PROGRAM END ENCOUNTERED ===
if vm.verbose >= 1 [
print ["stack:" mold stack]
print ["callstack:" mold callstack]
print ["heap:" mold heap]
]
=== EXIT TO SHELL WITH 0 STATUS CODE (DEFAULT) INDICATING SUCCESS TO TESTS ===