-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforthcore.lua
326 lines (271 loc) · 7.43 KB
/
forthcore.lua
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
-- Love2D 'middleware' to use Love2D with Forth language.
-- Copyright 2021 Rob Probin, released under MIT license, see file LICENSE
--
-- NOTES on operation and Word-set - see file README.md
local dict = { ['base'] = 10 }
local dict_ptr = 1
-- maybe make this a table then collapse into string when finished for speed?
-- (to avoid string concatention cost)
local current_word = ""
local current_word_name = nil
-- maybe we should put the stack in the dictionary?
-- then @ would work
dstack = {}
dstack_ptr = 0 -- no top of stack
local return_stack = {}
local return_stack_ptr = 0 -- no top of stack
input_buffer_index = 1
input_buffer = ""
state_compile = false
-- print function can be replaced
local function fprint(s)
end
local fprint_x = 0
local fprint_y = 0
function here()
return dict_ptr
end
function allot(n)
dict_ptr = dict_ptr + n
end
function push(p)
dstack_ptr = dstack_ptr + 1
dstack[dstack_ptr] = p
end
function pop()
local tos = dstack[dstack_ptr]
dstack_ptr = dstack_ptr - 1
return tos
end
function tos()
return dstack[dstack_ptr]
end
function drop()
dstack_ptr = dstack_ptr - 1
end
local function rpush(p)
return_stack_ptr = return_stack_ptr + 1
return_stack[return_stack_ptr] = p
end
local function rpop()
local tos = dstack[return_stack_ptr]
return_stack = return_stack_ptr - 1
return tos
end
function get_next_word()
-- capture all non-whitespace characters (%S is opposite of %s) after whitespace
local starti, endi, pattern = input_buffer:find("%s*(%S+)", input_buffer_index)
if starti == nil then
return nil
end
input_buffer_index = endi + 1
return pattern
end
function get_delimited_string(delimiter)
local starti, endi = input_buffer:find(delimiter, input_buffer_index)
if starti == nil then
return nil
end
local s = input_buffer:sub(input_buffer_index, starti-1)
input_buffer_index = endi + 1
return s
end
function compile_raw(s)
current_word = current_word .. s .. "\n"
end
compiled_word_list = {
["."] = function() print(pop()) end,
["dup"] = function() push(dstack[dstack_ptr]) end,
-- drop
["drop"] = function() dstack_ptr = dstack_ptr - 1 end,
["swap"] = function()
local tos = dstack[dstack_ptr]
dstack[dstack_ptr] = dstack[dstack_ptr-1]
dstack[dstack_ptr-1] = tos
end,
["nip"] = function()
local tos = dstack[dstack_ptr]
dstack_ptr = dstack_ptr - 1
dstack[dstack_ptr] = tos
end,
["over"] = function()
push(dstack[dstack_ptr-1])
end,
["tuck"] = function()
-- ( n1 n2 -- n2 n1 n2 ) basically swap over
-- swap part
local tos = dstack[dstack_ptr]
dstack[dstack_ptr] = dstack[dstack_ptr-1]
dstack[dstack_ptr-1] = tos
-- over part
push(tos)
end,
["+"] = function()
local tos = pop()
dstack[dstack_ptr] = tos + dstack[dstack_ptr]
end,
-- 5 3 -
["-"] = function()
local tos = pop()
dstack[dstack_ptr] = dstack[dstack_ptr] - tos
end,
["base"] = function()
push('base')
end,
["decimal"] = function()
dict.base = 10
end,
["["] = function()
state_compile = false
end,
["]"] = function()
state_compile = true
end,
[":"] = function()
local word = get_next_word()
if word then
current_word_name = word
current_word = ""
state_compile = true
else
error("No name for function")
end
end,
['S"'] = function()
input_buffer_index = input_buffer_index + 1
local s = get_delimited_string('"')
if not s then
error("Problem with string")
end
if state_compile then
compile_raw(string.format("push(%q) push(%f)", s, s:len()))
else
push(s)
push(s:len())
end
end,
--['(S")'] = function()
--end,
['depth'] = function() push(dstack_ptr) end,
['execute'] = function() pop()() end,
}
local immediate_word_list = {
['S"'] = true,
}
-- this isolate downstream word definition from core storage method
function add_new_word(wordname, func, immediate)
compiled_word_list[wordname] = func
immediate_word_list[wordname] = immediate
end
-- this one 'compiles' the Lua
function finalise_word()
local func, err = loadstring(current_word, current_word_name)
if func == nil then
error(err)
end
compiled_word_list[current_word_name] = func
end
add_new_word(";", function()
finalise_word()
state_compile = false
end, true)
-- ( comments )
add_new_word("(", function()
get_delimited_string(')')
end, true)
-- \ comment to end of line
add_new_word("\\", function()
local starti, endi = input_buffer:find('\r', input_buffer_index)
local startj, endj = input_buffer:find('\n', input_buffer_index)
if starti == nil and startj == nil then
input_buffer_index = len(input_buffer) + 1
end
if starti and startj then
endi = math.min(endi, endj)
elseif startj then
endi = endj
end
input_buffer_index = endi + 1
end, true)
-- find word in dictionary search order?
function find_word_in_dict(word)
end
-- these compile Forth to Lua
function compile_xt(word, xt)
-- this is a terrible way of doing it... but leave it like this for the moment
current_word = current_word .. string.format("compiled_word_list['%s']()\n", word)
end
function compile_number(n)
current_word = current_word .. string.format("push(%f)\n", n)
end
-- Normally quit is the outer interpreter
function raw_quit()
state_compile = false
return_stack_ptr = 0
end
function raw_abort()
dstack_ptr = 0
raw_quit()
end
function convert_number(s)
-- Why function?
-- 1. we might want to ensure whole string is number
-- 2. we also might want to handle negative numbers
-- 3. tonumber only handles unsigned integers in bases other than 10
-- 4. we might want to handle % $ # operators of FlashForth
return tonumber(s, dict.base )
end
function compile_num(n)
compile_raw(string.format("push(%f)", n))
end
function interpret(s)
input_buffer = s
input_buffer_index = 1
repeat
local word = get_next_word()
if word == nil then
return true
end
-- notice this doesn't follow forth dictionary order
local xt = compiled_word_list[word]
if xt then
if state_compile and not immediate_word_list[word] then
compile_xt(word, xt)
else
xt()
end
else
local number = convert_number(word)
if number then
if state_compile then
compile_num(number)
else
push(number)
end
else
raw_quit()
return false, "Unknown word or number " .. word
end
end
until false
end
-- should be FAST
function get_word_xt(word)
--find_word_in_dict(word)
return compiled_word_list[word]
end
-- could this be removed, since it's just a lua function?
function execute_xt(xt)
if xt then
xt()
end
end
-- ( addr -- n )
add_new_word("@", function()
push(dict[pop()])
end)
-- ( n addr -- )
add_new_word("!", function()
local addr = pop()
dict[addr] = pop()
end)