Add recognizers for words and numbers

This commit is contained in:
Starfflame 2021-05-25 03:41:39 -05:00
parent 70e8c7e959
commit 5efd5dbb60
3 changed files with 63 additions and 23 deletions

View File

@ -115,16 +115,73 @@ function CoreHelpers.makeCall(body: {function(Environment)}): function(Environme
end
function CoreHelpers.compile(env: Environment)
local stack = env.activeDataStack
local f = stack:pop() as function(Environment)
table.insert(env.currentDefinition, f)
end
function CoreHelpers.literal(env: Environment)
local stack = env.activeDataStack
local val = stack:pop()
local f = function(e: Environment)
local s = e.activeDataStack
s:push(val)
end
table.insert(env.currentDefinition, f)
end
function CoreHelpers.noop(_: Environment)
return
end
function CoreHelpers.postpone(e: Environment)
CoreHelpers.noop(e)
end
function CoreHelpers.execute(env: Environment)
local stack = CoreHelpers.getActiveDataStack(env)
local func: function(Environment) = stack:pop() as function(Environment)
func(env)
end
function CoreHelpers.recNumber(env: Environment)
local dataStack = CoreHelpers.getActiveDataStack(env)
local token = dataStack:pop() as string
if CoreHelpers.isNumber(token) then
-- push the XTs onto the stack
-- number
dataStack:push(tonumber(token))
-- interpret
dataStack:push(CoreHelpers.noop)
-- compile
dataStack:push(CoreHelpers.literal)
-- immediate
dataStack:push(CoreHelpers.literal)
-- return success
dataStack:push(true)
end
dataStack:push(token)
dataStack:push(false)
end
function CoreHelpers.recWord(env: Environment)
local dataStack = CoreHelpers.getActiveDataStack(env)
local token = dataStack:pop() as string
for _, dictionary in ipairs(env.dictionaries) do
local wordinfo = dictionary:lookup(token)
if wordinfo then
-- XT
dataStack:push((wordinfo as WordInfo).func)
-- interpret
dataStack:push(CoreHelpers.execute)
-- compile
dataStack:push(CoreHelpers.compile)
-- immediate
dataStack:push(CoreHelpers.postpone)
-- return success
dataStack:push(true)
end
dataStack:push(token)
dataStack:push(false)
end
end
return CoreHelpers

View File

@ -7,6 +7,9 @@ local popTwoOperands = helpers.popTwoOperands
local defineWord = helpers.defineWord
local ret = helpers.ret
local makeCall = helpers.makeCall
local literal = helpers.literal
local compile = helpers.compile
-- Mathematical operations
local function add(env: Environment)
local a, b = popTwoOperands(env)
@ -161,26 +164,6 @@ local function exitCompileMode(e: Environment)
e.compileState = false
end
local function compile(env: Environment)
local stack = env.activeDataStack
local f = stack:pop() as function(Environment)
table.insert(env.currentDefinition, f)
end
local function literal(env: Environment)
local stack = env.activeDataStack
local val = stack:pop()
local fun = function(e: Environment)
local s = e.activeDataStack
s:push(val)
end
table.insert(env.currentDefinition, fun)
end
local function postpone(env: Environment)
end
local CoreWords = Dictionary:new()
local addInfo = WordInfo:new(add, false)
local subInfo = WordInfo:new(sub, false)

View File

@ -17,7 +17,7 @@ local type DataStructures = record
define: function(Dictionary, string, WordInfo)
end
record WordInfo
func: function(Environment, ...: any)
func: function(Environment)
immediate: boolean
new: function(WordInfo, function(Environment), boolean): WordInfo
end