forth-stuff/ForthInterpreter.tl

57 lines
1.6 KiB
Plaintext
Raw Normal View History

local CoreWords = require("CoreWords")
local ds = require("DataStructures")
local istream = require("InputStream")
2021-05-14 08:29:54 +00:00
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
2021-05-18 01:59:46 +00:00
local reset = helpers.reset
2021-05-14 08:29:54 +00:00
local machineEnvironment = Environment:new()
2021-05-18 01:59:46 +00:00
local Pointer = ds.Pointer
function standardInputRefill(): string
local input = io.read().."\n"
return input
2021-05-12 08:32:25 +00:00
end
local standardInputStream = istream:new(standardInputRefill)
2021-05-14 08:29:54 +00:00
machineEnvironment.activeInputStream = standardInputStream
machineEnvironment.activeDataStack = Stack:new()
table.insert(machineEnvironment.dictionaries, CoreWords)
2021-05-12 08:32:25 +00:00
2021-05-18 01:59:46 +00:00
function outerInterpreter(env: Environment)
while(true) do
skipWhitespace(env)
local token: string = parseToken(env)
if isNumber(token) then
env.activeDataStack:push(tonumber(token))
else
for i, dictionary in ipairs(env.dictionaries) do
local wordinfo = dictionary:lookup(token)
if wordinfo then
(wordinfo as WordInfo).func(env)
goto continue
end
end
2021-05-18 01:59:46 +00:00
print(string.format('%q ?', token))
::continue::
end
2021-05-18 01:59:46 +00:00
end
end
function innerInterpreter(env: Environment)
while(true) do
(env.instructionPointer:deref() as function(Environment))(env)
env.instructionPointer:inc()
end
2021-05-12 08:32:25 +00:00
end
2021-05-18 01:59:46 +00:00
local interpreterInstructions = {outerInterpreter, reset}
local startPointer = Pointer:new()
startPointer.referant = interpreterInstructions
startPointer.index = 0
machineEnvironment.instructionPointer = startPointer
2021-05-18 01:59:46 +00:00
outerInterpreter(machineEnvironment)