forth-stuff/Interpreters.tl

39 lines
999 B
Plaintext

local ds = require("DataStructures")
local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment
local helpers = require("CoreHelpers")
local isNumber, skipWhitespace, parseToken = helpers.isNumber, helpers.skipWhitespace, helpers.parseToken
local type Interpreters = record
inner: function(Environment)
outer: function(Environment)
end
function Interpreters.outer(env: Environment)
skipWhitespace(env)
local token: string = parseToken(env)
if isNumber(token) then
env.activeDataStack:push(tonumber(token))
else
for _, dictionary in ipairs(env.dictionaries) do
local wordinfo = dictionary:lookup(token)
if wordinfo then
(wordinfo as WordInfo).func(env)
goto continue
end
end
print(string.format('%q ?', token))
::continue::
end
end
function Interpreters.inner(env: Environment)
while(true) do
env.instructionPointer:deref() as function(Environment)(env)
env.instructionPointer:inc()
end
end
return Interpreters