Compare commits

...

2 Commits

Author SHA1 Message Date
Starfflame 3ddc671cb3 Add colon and semicolon operators 2021-05-25 23:27:18 -05:00
Starfflame 5efd5dbb60 Add recognizers for words and numbers 2021-05-25 03:41:39 -05:00
5 changed files with 134 additions and 33 deletions

View File

@ -115,16 +115,83 @@ function CoreHelpers.makeCall(body: {function(Environment)}): function(Environme
end 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) function CoreHelpers.recNumber(env: Environment)
local dataStack = CoreHelpers.getActiveDataStack(env) local dataStack = CoreHelpers.getActiveDataStack(env)
local token = dataStack:pop() as string local token = dataStack:pop() as string
if CoreHelpers.isNumber(token) then 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) dataStack:push(true)
return
end 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
if (wordinfo as WordInfo).immediate then
dataStack:push(CoreHelpers.execute)
else
dataStack:push(CoreHelpers.compile)
end
-- immediate
dataStack:push(CoreHelpers.postpone)
-- return success
dataStack:push(true)
return
end
end
dataStack:push(token)
dataStack:push(false)
end end
function CoreHelpers.ack(e: Environment)
io.write("ok\n")
end
return CoreHelpers return CoreHelpers

View File

@ -7,6 +7,9 @@ local popTwoOperands = helpers.popTwoOperands
local defineWord = helpers.defineWord local defineWord = helpers.defineWord
local ret = helpers.ret local ret = helpers.ret
local makeCall = helpers.makeCall local makeCall = helpers.makeCall
local literal = helpers.literal
local compile = helpers.compile
-- Mathematical operations -- Mathematical operations
local function add(env: Environment) local function add(env: Environment)
local a, b = popTwoOperands(env) local a, b = popTwoOperands(env)
@ -161,26 +164,29 @@ local function exitCompileMode(e: Environment)
e.compileState = false e.compileState = false
end end
local function compile(env: Environment) local function colon(e: Environment)
local stack = env.activeDataStack skipWhitespace(e)
local f = stack:pop() as function(Environment) local name: string | nil = parseToken(e)
table.insert(env.currentDefinition, f) if not name then
end e.running = false
local function literal(env: Environment) return
local stack = env.activeDataStack
local val = stack:pop()
local fun = function(e: Environment)
local s = e.activeDataStack
s:push(val)
end end
table.insert(env.currentDefinition, fun) e.currentDefinitionName = name as string
enterCompileMode(e)
end end
local function postpone(env: Environment) local function semicolon(e: Environment)
table.insert(e.currentDefinition, helpers.ret)
local instrs = e.currentDefinition
local call = helpers.makeCall(instrs)
local dict = e.dictionaries[1]
defineWord(dict, e.currentDefinitionName, call, false)
e.currentDefinitionName = nil
e.currentDefinition = {}
exitCompileMode(e)
end end
local CoreWords = Dictionary:new() local CoreWords = Dictionary:new()
local addInfo = WordInfo:new(add, false) local addInfo = WordInfo:new(add, false)
local subInfo = WordInfo:new(sub, false) local subInfo = WordInfo:new(sub, false)
@ -205,7 +211,8 @@ defineWord(CoreWords, "[", exitCompileMode, true)
defineWord(CoreWords, "]", enterCompileMode, false) defineWord(CoreWords, "]", enterCompileMode, false)
defineWord(CoreWords, "COMPILE,", compile, true) defineWord(CoreWords, "COMPILE,", compile, true)
defineWord(CoreWords, "LITERAL", literal, true) defineWord(CoreWords, "LITERAL", literal, true)
defineWord(CoreWords, ":", colon, true)
defineWord(CoreWords, ";", semicolon, true)
CoreWords:define("+", addInfo) CoreWords:define("+", addInfo)
CoreWords:define("-", subInfo) CoreWords:define("-", subInfo)

View File

@ -17,7 +17,7 @@ local type DataStructures = record
define: function(Dictionary, string, WordInfo) define: function(Dictionary, string, WordInfo)
end end
record WordInfo record WordInfo
func: function(Environment, ...: any) func: function(Environment)
immediate: boolean immediate: boolean
new: function(WordInfo, function(Environment), boolean): WordInfo new: function(WordInfo, function(Environment), boolean): WordInfo
end end
@ -46,7 +46,7 @@ local type DataStructures = record
recognizers: {function(Environment)} recognizers: {function(Environment)}
instructionPointer: Pointer instructionPointer: Pointer
running: boolean running: boolean
currentDefinitionName: string | nil
currentDefinition: {function(Environment)} currentDefinition: {function(Environment)}
end end
@ -71,7 +71,10 @@ function Environment:new(input: InputStream, dict: {Dictionary} ): Environment
returnStack = Stack:new(), returnStack = Stack:new(),
dictionaries = dicts, dictionaries = dicts,
activeInputStream = input, activeInputStream = input,
running = true running = true,
recognizers = {},
compileState = false,
currentDefinition = {}
} as Environment, } as Environment,
state_mt) state_mt)
end end

View File

@ -15,6 +15,8 @@ function Interpreters.start(env: Environment)
local interpreterInstructions = {} local interpreterInstructions = {}
table.insert(interpreterInstructions, Interpreters.outer) table.insert(interpreterInstructions, Interpreters.outer)
table.insert(interpreterInstructions, helpers.reset) table.insert(interpreterInstructions, helpers.reset)
table.insert(env.recognizers, helpers.recNumber)
table.insert(env.recognizers, helpers.recWord)
local startPointer = Pointer:new() local startPointer = Pointer:new()
startPointer.referant = interpreterInstructions startPointer.referant = interpreterInstructions
env.instructionPointer = startPointer env.instructionPointer = startPointer
@ -22,27 +24,44 @@ function Interpreters.start(env: Environment)
end 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) function Interpreters.outer(env: Environment)
local dataStack = env.activeDataStack
skipWhitespace(env) skipWhitespace(env)
local token: string | nil = parseToken(env) local token: string | nil = parseToken(env)
if not token then if not token then
env.running = false env.running = false
return return
end end
if isNumber(token) then for _, rec in ipairs(env.recognizers) do
env.activeDataStack:push(tonumber(token)) dataStack:push(token)
else rec(env)
for _, dictionary in ipairs(env.dictionaries) do local result = dataStack:pop() as boolean
local wordinfo = dictionary:lookup(token) if not result then
if wordinfo then token = dataStack:pop() as string
(wordinfo as WordInfo).func(env) end
goto continue if result then
end (selectRecXT(env))(env)
return
end end
print(string.format('%q ?', token))
::continue::
end end
-- We did not recognize the token.
io.write("\""..token.."\"?\n")
end end
function Interpreters.inner(env: Environment) function Interpreters.inner(env: Environment)

View File

@ -231,6 +231,11 @@ describe("Interpreter tests", function()
assert.are.same(expectedResult,testEnv.activeDataStack.contents[1]) assert.are.same(expectedResult,testEnv.activeDataStack.contents[1])
end end
end) end)
it("does not push anything when a garbage input is given", function()
local testEnv = buildEnvironment("BOGUS")
interpreters.start(testEnv)
assert.are.same({}, testEnv.activeDataStack.contents)
end)
end) end)
describe("Core word tests", function() describe("Core word tests", function()
describe("DUP tests", function() describe("DUP tests", function()