46 lines
1.5 KiB
Plaintext
46 lines
1.5 KiB
Plaintext
local CoreWords = require("CoreWords")
|
|
local ds = require("DataStructures")
|
|
local istream = require("InputStream")
|
|
local Stack, Dictionary, WordInfo, State = ds.Stack, ds.Dictionary, ds.WordInfo, ds.State
|
|
|
|
local machineState = State:new()
|
|
|
|
function standardInputRefill(): string
|
|
local input = io.read().."\n"
|
|
return input
|
|
end
|
|
|
|
local standardInputStream = istream:new(standardInputRefill)
|
|
|
|
machineState.activeInputStream = standardInputStream
|
|
machineState.activeDataStack = Stack:new()
|
|
table.insert(machineState.dictionaries, CoreWords)
|
|
|
|
local getActiveDataStacks = (CoreWords:lookup("GetActiveData") as WordInfo).func
|
|
local areNumbers = (CoreWords:lookup("areNumbers") as WordInfo).func
|
|
local isNumber = (CoreWords:lookup("isNumber") as WordInfo).func
|
|
local isWhitespace = (CoreWords:lookup("isWhitespace") as WordInfo).func
|
|
local popTwoOperands = (CoreWords:lookup("popTwo") as WordInfo).func
|
|
local skipWhitespace = (CoreWords:lookup("skipWhitespace") as WordInfo).func
|
|
local parseToken = (CoreWords:lookup("parseToken") as WordInfo).func
|
|
|
|
while(true) do
|
|
skipWhitespace(machineState)
|
|
local token: string = parseToken(machineState)
|
|
if isNumber(machineState, token) then
|
|
machineState.activeDataStack:push(tonumber(token))
|
|
else
|
|
for i, dictionary in ipairs(machineState.dictionaries) do
|
|
local wordinfo = dictionary:lookup(token)
|
|
if wordinfo then
|
|
(wordinfo as WordInfo).func(machineState)
|
|
goto continue
|
|
end
|
|
end
|
|
print(string.format('%q ?', token))
|
|
::continue::
|
|
end
|
|
end
|
|
|
|
|