-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforthwords.lua
62 lines (52 loc) · 1.77 KB
/
forthwords.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
-- 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
-- n variable name ( n <name> -- )
add_new_word("variable", function()
local name = get_next_word()
local addr = here()
allot(1)
if name then
compiled_word_list[name] = function() push(addr) end
else
error("No name for function")
end
end)
local unique_do_index = 1
add_new_word('do', function()
compile_raw(string.format("local initial%i = pop() local limit%i=(pop()-1) for i%i = initial%i, limit%i do", unique_do_index, unique_do_index, unique_do_index, unique_do_index, unique_do_index))
end, true)
add_new_word('i', function()
compile_raw(string.format('push(i%i)', unique_do_index))
end, true);
add_new_word('loop', function()
compile_raw('end')
unique_do_index = unique_do_index + 1
end, true)
add_new_word('sinterpret', function()
local old_buffer = input_buffer
local old_index = input_buffer_index
local str_to_intepret = pop()
local state, err
if type(str_to_intepret) ~= "string" then
err = "Tried to interpret "..type(str_to_intepret)
state = false
else
state, err = interpret(str_to_intepret)
end
if not state then
push(err)
end
push(convert_to_forth_bool(state))
input_buffer = old_buffer
input_buffer_index = old_index
end)
add_new_word("count", function()
push(tos().len)
end)
add_new_word("dstack@", function()
local offset = pop()
local index = dstack_ptr-offset
push(dstack[index])
end)