75 lines
2.0 KiB
Plaintext
75 lines
2.0 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)
|
|
table.insert(env.recognizers, helpers.recNumber)
|
|
table.insert(env.recognizers, helpers.recWord)
|
|
local startPointer = Pointer:new()
|
|
startPointer.referant = interpreterInstructions
|
|
env.instructionPointer = startPointer
|
|
Interpreters.inner(env)
|
|
end
|
|
|
|
|
|
function selectRecXT(env: Environment): function(Environment)
|
|
local dataStack = env.activeDataStack
|
|
if env.compileState then
|
|
dataStack:pop()
|
|
local selected = dataStack:pop()
|
|
dataStack:pop()
|
|
return selected as function(Environment)
|
|
else
|
|
dataStack:pop()
|
|
dataStack:pop()
|
|
return dataStack:pop() as function(Environment)
|
|
end
|
|
end
|
|
|
|
|
|
function Interpreters.outer(env: Environment)
|
|
local dataStack = env.activeDataStack
|
|
skipWhitespace(env)
|
|
local token: string | nil = parseToken(env)
|
|
if not token then
|
|
env.running = false
|
|
return
|
|
end
|
|
for _, rec in ipairs(env.recognizers) do
|
|
dataStack:push(token)
|
|
rec(env)
|
|
local result = dataStack:pop() as boolean
|
|
if not result then
|
|
token = dataStack:pop() as string
|
|
end
|
|
if result then
|
|
(selectRecXT(env))(env)
|
|
return
|
|
end
|
|
end
|
|
|
|
-- We did not recognize the token.
|
|
io.write("\""..token.."\"?\n")
|
|
end
|
|
|
|
function Interpreters.inner(env: Environment)
|
|
while(env.running) do
|
|
env.instructionPointer:deref() as function(Environment)(env)
|
|
env.instructionPointer:inc()
|
|
end
|
|
end
|
|
|
|
return Interpreters
|