57 lines
1.6 KiB
Plaintext
57 lines
1.6 KiB
Plaintext
local CoreWords = require("CoreWords")
|
|
local ds = require("DataStructures")
|
|
local istream = require("InputStream")
|
|
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 reset = helpers.reset
|
|
local machineEnvironment = Environment:new()
|
|
local Pointer = ds.Pointer
|
|
function standardInputRefill(): string
|
|
local input = io.read().."\n"
|
|
return input
|
|
end
|
|
|
|
local standardInputStream = istream:new(standardInputRefill)
|
|
|
|
machineEnvironment.activeInputStream = standardInputStream
|
|
machineEnvironment.activeDataStack = Stack:new()
|
|
table.insert(machineEnvironment.dictionaries, CoreWords)
|
|
|
|
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
|
|
print(string.format('%q ?', token))
|
|
::continue::
|
|
end
|
|
end
|
|
end
|
|
|
|
function innerInterpreter(env: Environment)
|
|
while(true) do
|
|
(env.instructionPointer:deref() as function(Environment))(env)
|
|
env.instructionPointer:inc()
|
|
end
|
|
end
|
|
|
|
local interpreterInstructions = {outerInterpreter, reset}
|
|
local startPointer = Pointer:new()
|
|
startPointer.referant = interpreterInstructions
|
|
startPointer.index = 0
|
|
|
|
machineEnvironment.instructionPointer = startPointer
|
|
|
|
|
|
outerInterpreter(machineEnvironment)
|