forth-stuff/Interpreters.tl

56 lines
1.5 KiB
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 istream = require("InputStream")
local CoreWords = require("CoreWords")
local Pointer = ds.Pointer
local type Interpreters = record
inner: function(Environment)
outer: function(Environment)
end
function Interpreters.start(env: Environment)
table.insert(env.dictionaries, CoreWords)
local interpreterInstructions = {}
table.insert(interpreterInstructions, Interpreters.outer)
table.insert(interpreterInstructions, helpers.reset)
local startPointer = Pointer:new()
startPointer.referant = interpreterInstructions
env.instructionPointer = startPointer
Interpreters.inner(env)
end
function Interpreters.outer(env: Environment)
skipWhitespace(env)
local token: string | nil = parseToken(env)
if not token then
env.running = false
return
end
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(env.running) do
env.instructionPointer:deref() as function(Environment)(env)
env.instructionPointer:inc()
end
end
return Interpreters