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
|
#!/usr/bin/awk -f
BEGIN {
# Initialize stacks and dictionaries
stack_ptr = 0
dict_size = 0
# Built-in words, and some documentation (I could use stack comments, but I find those sort of unintuitive)
dict["+"] = "+ : Adds the top two numbers on the stack."
dict["-"] = "- : Subtracts the top number from the second top number on the stack."
dict["*"] = "* : Multiplies the top two numbers on the stack."
dict["/"] = "/ : Divides the second top number by the top number on the stack."
dict["."] = ". : Prints the top of the stack."
dict[".s"] = ".s : Shows all values on the stack."
dict["dup"] = "dup : Duplicates the top value on the stack."
dict["drop"] = "drop : Removes the top value from the stack."
dict["swap"] = "swap : Swaps the top two values on the stack."
dict["over"] = "over : Copies the second top value to the top of the stack."
dict["rot"] = "rot : Rotates the top three values on the stack."
dict["="] = "= : Compares the top two values for equality."
dict["<"] = "< : Checks if the second top value is less than the top value."
dict[">"] = "> : Checks if the second top value is greater than the top value."
dict["bye"] = "bye : Exits the interpreter."
dict["words"] = "words : Lists all available words and their documentation."
# State flags
compiling = 0
current_def = ""
def_name = ""
# If an input file isn't specified, enter REPL mode
if (ARGC == 1) {
repl()
}
}
# Handle file input
{
if (FILENAME ~ /\.forth$/) {
interpret($0)
}
}
function repl() {
print "f.awk! A forth interpreter.\nUse 'bye' to exit.\nUse 'words' to list all available words.\n"
while (1) {
printf "f> "
if (getline input < "/dev/tty" <= 0) break
interpret(input)
}
}
function interpret(line) {
gsub(/\(.*\)/, "", line) # Remove everything from ( to )
n = split(line, words, /[ \t]+/)
for (i = 1; i <= n; i++) {
word = words[i]
if (word == "") continue
if (word == ":") {
compiling = 1
i++
def_name = words[i]
current_def = ""
continue
}
if (compiling) {
if (word == ";") {
# Store user-defined word with its name and definition
dict[def_name] = "word " current_def
compiling = 0
continue
}
current_def = current_def " " word
continue
}
execute_word(word)
}
}
function execute_word(word) {
if (word ~ /^-?[0-9]+$/) {
push(word + 0)
} else if (word in dict) {
if (dict[word] ~ /^word /) {
# User-defined word
sequence = substr(dict[word], 6)
split(sequence, subwords, " ")
for (sw in subwords) {
if (subwords[sw] != "") {
execute_word(subwords[sw])
}
}
} else {
# Built-in words
if (dict[word] == "math_add") math_add()
else if (dict[word] == "math_sub") math_sub()
else if (dict[word] == "math_mul") math_mul()
else if (dict[word] == "math_div") math_div()
else if (dict[word] == "stack_print") stack_print()
else if (dict[word] == "stack_show") stack_show()
else if (dict[word] == "stack_dup") stack_dup()
else if (dict[word] == "stack_drop") stack_drop()
else if (dict[word] == "stack_swap") stack_swap()
else if (dict[word] == "stack_over") stack_over()
else if (dict[word] == "stack_rot") stack_rot()
else if (dict[word] == "compare_eq") compare_eq()
else if (dict[word] == "compare_lt") compare_lt()
else if (dict[word] == "compare_gt") compare_gt()
else if (word == "bye") exit_program()
else if (word == "words") list_words()
}
} else {
print "Error: Unknown word '" word "'"
}
}
function push(val) {
stack[stack_ptr++] = val
}
function pop() {
if (stack_ptr <= 0) {
print "Error: Stack underflow"
return 0
}
return stack[--stack_ptr]
}
function math_add() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(a + b)
}
function math_sub() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(a - b)
}
function math_mul() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(a * b)
}
function math_div() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
if (b == 0) {
print "Error: Division by zero"
return
}
a = pop()
push(int(a / b))
}
function stack_print() {
if (stack_ptr < 1) {
print "Error: Stack underflow"
return
}
print pop()
}
function stack_show() {
print "<", stack_ptr, "> "
for (i = 0; i < stack_ptr; i++) {
printf "%s ", stack[i]
}
print ""
}
function stack_dup() {
if (stack_ptr < 1) {
print "Error: Stack underflow"
return
}
val = stack[stack_ptr - 1]
push(val)
}
function stack_drop() {
if (stack_ptr < 1) {
print "Error: Stack underflow"
return
}
pop()
}
function stack_swap() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(b)
push(a)
}
function stack_over() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(a)
push(b)
push(a)
}
function stack_rot() {
if (stack_ptr < 3) {
print "Error: Stack underflow"
return
}
c = pop()
b = pop()
a = pop()
push(b)
push(c)
push(a)
}
function compare_eq() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(a == b ? -1 : 0)
}
function compare_lt() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(a < b ? -1 : 0)
}
function compare_gt() {
if (stack_ptr < 2) {
print "Error: Stack underflow"
return
}
b = pop()
a = pop()
push(a > b ? -1 : 0)
}
function exit_program() {
print "Exiting program."
exit 0
}
function list_words() {
print "Available words:"
# Separate arrays to hold built-in and user-defined words
split("", built_in_words)
split("", user_defined_words)
for (w in dict) {
split(dict[w], parts, ": ")
if (parts[1] ~ /^word /) {
user_defined_words[w] = parts[2]
} else {
built_in_words[w] = parts[2]
}
}
# Sort built-in words manually because I'm picky
n = 0
for (w in built_in_words) {
sorted_words[n++] = w
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (sorted_words[i] > sorted_words[j]) {
temp = sorted_words[i]
sorted_words[i] = sorted_words[j]
sorted_words[j] = temp
}
}
}
# First print the built-in words
for (i = 0; i < n; i++) {
print sorted_words[i] ": " built_in_words[sorted_words[i]]
}
# Then print the user-defined words
for (w in user_defined_words) {
print w ": " user_defined_words[w] " ( User-defined )"
}
}
|