turtle/tlang: add run builtin

This changes how code is executed a bit, code from the stack is popped to an execution stack before.
This makes global executed functions and anonymous ones identical.
Not super well tested as always :]
This commit is contained in:
cron 2020-11-03 07:06:59 +00:00
parent 0a3d331680
commit 957d425609
2 changed files with 33 additions and 15 deletions

View File

@ -64,10 +64,10 @@ function tlang.get_state(code)
v__lex__ = tlang.valconv(lexed),
v__ast__ = {type = "code", value = parsed}}},
stack = {},
code_stack = {},
builtins = tlang.builtins,
wait_target = nil,
nextpop = false,
tree = parse_state
nextpop = false
}
end
@ -80,21 +80,20 @@ end
local complex = [[{dup *} `square =
-5.42 square
"Hello, world!" print
[ 1 2 3 str:"String" ]
[1 2 3 str:"String"]
]]
local number = [[-4.2123
]]
local number = "-4.2123"
local simple = [[{dup *}
]]
local simple = "{dup *}"
local map = [[
[ "thing":1 ]
]]
local map = "[this:2 that:3]"
tlang.exec([[{dup *} `square =
5 square print
]])
local square = [[{dup *} `square =
5 square print]]
local square_run = "5 {dup *} run print"
tlang.exec(square_run)
return tlang

View File

@ -22,7 +22,10 @@ end
{
locals = {},
stack = {},
tree = {}
builtins = {},
code_stack = {},
wait_target = int,
nextpop = f/t
}
--]]
@ -44,6 +47,12 @@ local literals = {
local function call(state, target)
if target.sg == 0 then
state.code_stack[#state.code_stack + 1] = state.stack[target.pos]
table.remove(state.stack, target.pos)
target.pos = #state.code_stack
end
state.locals[#state.locals + 1] = {pc = target}
end
@ -71,7 +80,7 @@ end
local function accesspc(state, pc)
local code
if pc.sg == 0 then -- stack
code = state.stack[pc.pos]
code = state.code_stack[pc.pos]
elseif pc.sg == 1 then -- global
code = access(state, pc.pos)
end
@ -95,6 +104,12 @@ local function getnext(state)
if #state.locals == 0 then
return nil
end
-- pop code stack
local pc = getpc(state)
if pc.sg == 0 then
state.code_stack[pc.pos] = nil
end
state.nextpop = false
end
@ -138,6 +153,10 @@ end
local builtins = {}
function builtins.run(state)
call(state, {sg = 0, pos = #state.stack, elem = 1})
end
builtins["="] = function(state)
local name = statepop_type(state, "quote")
local value = statepop(state)