Push interpretation functions to CoreHelpers for later use in TO et al.

This commit is contained in:
Starfflame 2021-07-16 16:56:21 -05:00
parent ecd663320a
commit b92df463f1
2 changed files with 50 additions and 51 deletions

View File

@ -172,6 +172,54 @@ function CoreHelpers.searchDictionaries(e: Environment, token: string): WordInfo
end end
end end
function selectRecXT(env: Environment): function(Environment)
local dataStack = env.activeDataStack
if env.compileState then
dataStack:pop()
local selected = dataStack:pop()
dataStack:pop()
return selected as function(Environment)
else
dataStack:pop()
dataStack:pop()
return dataStack:pop() as function(Environment)
end
end
function CoreHelpers.interpreters_outer(env: Environment)
local dataStack = env.activeDataStack
CoreHelpers.skipWhitespace(env)
local token: string | nil = CoreHelpers.parseToken(env)
if not token then
env.running = false
return
end
for _, rec in ipairs(env.recognizers) do
dataStack:push(token)
rec(env)
local result = dataStack:pop() as boolean
if not result then
token = dataStack:pop() as string
end
if result then
(selectRecXT(env))(env)
return
end
end
io.write("\""..token.."\"?\n")
end
function CoreHelpers.interpreters_inner(env: Environment)
while(env.running) do
env.instructionPointer:deref() as function(Environment)(env)
env.instructionPointer:inc()
end
end
function CoreHelpers.recWord(env: Environment) function CoreHelpers.recWord(env: Environment)
local dataStack = CoreHelpers.getActiveDataStack(env) local dataStack = CoreHelpers.getActiveDataStack(env)

View File

@ -13,62 +13,13 @@ end
function Interpreters.start(env: Environment) function Interpreters.start(env: Environment)
table.insert(env.dictionaries, CoreWords) table.insert(env.dictionaries, CoreWords)
local interpreterInstructions = {} local interpreterInstructions = {}
table.insert(interpreterInstructions, Interpreters.outer) table.insert(interpreterInstructions, helpers.interpreters_outer)
table.insert(interpreterInstructions, helpers.reset) table.insert(interpreterInstructions, helpers.reset)
table.insert(env.recognizers, helpers.recNumber) table.insert(env.recognizers, helpers.recNumber)
table.insert(env.recognizers, helpers.recWord) table.insert(env.recognizers, helpers.recWord)
local startPointer = Pointer:new() local startPointer = Pointer:new()
startPointer.referant = interpreterInstructions startPointer.referant = interpreterInstructions
env.instructionPointer = startPointer env.instructionPointer = startPointer
Interpreters.inner(env) helpers.interpreters_inner(env)
end end
function selectRecXT(env: Environment): function(Environment)
local dataStack = env.activeDataStack
if env.compileState then
dataStack:pop()
local selected = dataStack:pop()
dataStack:pop()
return selected as function(Environment)
else
dataStack:pop()
dataStack:pop()
return dataStack:pop() as function(Environment)
end
end
function Interpreters.outer(env: Environment)
local dataStack = env.activeDataStack
skipWhitespace(env)
local token: string | nil = parseToken(env)
if not token then
env.running = false
return
end
for _, rec in ipairs(env.recognizers) do
dataStack:push(token)
rec(env)
local result = dataStack:pop() as boolean
if not result then
token = dataStack:pop() as string
end
if result then
(selectRecXT(env))(env)
return
end
end
-- We did not recognize the token.
io.write("\""..token.."\"?\n")
end
function Interpreters.inner(env: Environment)
while(env.running) do
env.instructionPointer:deref() as function(Environment)(env)
env.instructionPointer:inc()
end
end
return Interpreters return Interpreters