Add colon and semicolon operators

This commit is contained in:
Starfflame 2021-05-25 23:27:18 -05:00
parent 5efd5dbb60
commit 3ddc671cb3
5 changed files with 80 additions and 19 deletions

View File

@ -156,6 +156,7 @@ function CoreHelpers.recNumber(env: Environment)
dataStack:push(CoreHelpers.literal) dataStack:push(CoreHelpers.literal)
-- return success -- return success
dataStack:push(true) dataStack:push(true)
return
end end
dataStack:push(token) dataStack:push(token)
dataStack:push(false) dataStack:push(false)
@ -172,15 +173,24 @@ function CoreHelpers.recWord(env: Environment)
-- interpret -- interpret
dataStack:push(CoreHelpers.execute) dataStack:push(CoreHelpers.execute)
-- compile -- compile
dataStack:push(CoreHelpers.compile) if (wordinfo as WordInfo).immediate then
dataStack:push(CoreHelpers.execute)
else
dataStack:push(CoreHelpers.compile)
end
-- immediate -- immediate
dataStack:push(CoreHelpers.postpone) dataStack:push(CoreHelpers.postpone)
-- return success -- return success
dataStack:push(true) dataStack:push(true)
return
end end
dataStack:push(token)
dataStack:push(false)
end end
dataStack:push(token)
dataStack:push(false)
end
function CoreHelpers.ack(e: Environment)
io.write("ok\n")
end end
return CoreHelpers return CoreHelpers

View File

@ -164,6 +164,29 @@ local function exitCompileMode(e: Environment)
e.compileState = false e.compileState = false
end end
local function colon(e: Environment)
skipWhitespace(e)
local name: string | nil = parseToken(e)
if not name then
e.running = false
return
end
e.currentDefinitionName = name as string
enterCompileMode(e)
end
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
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)
@ -188,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

@ -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,8 +71,11 @@ 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,
} as Environment, recognizers = {},
compileState = false,
currentDefinition = {}
} 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,29 +24,46 @@ 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)
while(env.running) do while(env.running) do
env.instructionPointer:deref() as function(Environment)(env) env.instructionPointer:deref() as function(Environment)(env)

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()