Compare commits

..

2 Commits

6 changed files with 128 additions and 28 deletions

4
.busted Normal file
View File

@ -0,0 +1,4 @@
package.path = "?.lua;"..package.path
return {
}

View File

@ -1,6 +1,6 @@
local ds = require("DataStructures") local ds = require("DataStructures")
local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment
local Pointer = ds.Pointer
local CoreHelpers = {} local CoreHelpers = {}
function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean) function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean)
@ -8,7 +8,10 @@ function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(En
dict:define(str, info) dict:define(str, info)
end end
function CoreHelpers.standardInputRefill(): string
local input = io.read().."\n"
return input
end
function CoreHelpers.areNumbers(a: any, b: any): boolean function CoreHelpers.areNumbers(a: any, b: any): boolean
return a is number and b is number return a is number and b is number
@ -59,5 +62,25 @@ function CoreHelpers.parseToken(state: Environment): string
return token return token
end end
function CoreHelpers.reset(env: Environment)
env.instructionPointer.index = 0 -- will get incremented after this instruction
end
function CoreHelpers.ret(env: Environment)
local retAddr = env.returnStack:pop() as Pointer
env.instructionPointer = retAddr
end
function CoreHelpers.makeCall(body: {function(Environment)}): function(Environment)
return function(e: Environment)
e.returnStack:push(e.instructionPointer)
e.instructionPointer = Pointer:new(body, 0)
end
end
return CoreHelpers return CoreHelpers

View File

@ -5,6 +5,8 @@ local areNumbers, getActiveDataStack, isNumber, isWhitespace= helpers.areNumbers
local skipWhitespace, parseToken = helpers.skipWhitespace, helpers.parseToken local skipWhitespace, parseToken = helpers.skipWhitespace, helpers.parseToken
local popTwoOperands = helpers.popTwoOperands local popTwoOperands = helpers.popTwoOperands
local defineWord = helpers.defineWord local defineWord = helpers.defineWord
local ret = helpers.ret
local makeCall = helpers.makeCall
-- Mathematical operations -- Mathematical operations
local function add(state: Environment) local function add(state: Environment)
local a, b = popTwoOperands(state) local a, b = popTwoOperands(state)
@ -189,6 +191,21 @@ CoreWords:define("2DUP", twoDupInfo)
CoreWords:define("2SWAP", twoSwapInfo) CoreWords:define("2SWAP", twoSwapInfo)
CoreWords:define("2OVER", twoOverInfo) CoreWords:define("2OVER", twoOverInfo)
CoreWords:define("NIP", nipInfo) CoreWords:define("NIP", nipInfo)
local sqr = {dup, mul, ret}
local sqrf = makeCall(sqr)
defineWord(CoreWords, "SQR", sqrf, false)
return CoreWords return CoreWords

View File

@ -22,11 +22,18 @@ local type DataStructures = record
new: function(WordInfo, function(Environment), boolean): WordInfo new: function(WordInfo, function(Environment), boolean): WordInfo
end end
record Pointer
index: number
referant: {number: any}
deref: function(self: Pointer): any
inc: function(self: Pointer)
new: function(self: Pointer, body: {function(Environment)}, idx: number): Pointer
end
record Environment record Environment
dataStacks: {Stack} dataStacks: {Stack}
compilerStack: Stack compilerStack: Stack
returnStack: Stack
activeDataStack: Stack activeDataStack: Stack
interrupts: {function(Environment)} interrupts: {function(Environment)}
activeInputStream: InputStream activeInputStream: InputStream
@ -37,12 +44,13 @@ local type DataStructures = record
changeCompilerStack: function(Environment, Stack) changeCompilerStack: function(Environment, Stack)
changeActiveDataStack: function(Environment, number) changeActiveDataStack: function(Environment, number)
instructionPointer: Pointer
end end
end end
local Stack, Environment, Dictionary, WordInfo = DataStructures.Stack, DataStructures.Environment, DataStructures.Dictionary, DataStructures.WordInfo local Stack, Environment, Dictionary, WordInfo = DataStructures.Stack, DataStructures.Environment, DataStructures.Dictionary, DataStructures.WordInfo
local Pointer = DataStructures.Pointer
local wordi_mt = {__index = WordInfo} local wordi_mt = {__index = WordInfo}
function WordInfo:new(funct: function(Environment, ...: any), imm: boolean): WordInfo function WordInfo:new(funct: function(Environment, ...: any), imm: boolean): WordInfo
return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt) return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt)
@ -109,6 +117,21 @@ function Dictionary:new(): Dictionary
return setmetatable({contents = {}} as Dictionary, dict_mt) return setmetatable({contents = {}} as Dictionary, dict_mt)
end end
function Pointer:deref(): any
return self.referant[self.index]
end
function Pointer:inc()
self.index = self.index + 1
end
local pointer_mt = {__index = Pointer}
function Pointer:new(): Pointer
return setmetatable({referant = {}, index = 1} as Pointer, pointer_mt)
end
function Pointer:new(body: {function(Environment)}, idx: number): Pointer
local tbl = body or {}
local jdx = idx or 1
return setmetatable({referant = tbl, index = jdx} as Pointer, pointer_mt)
end
return DataStructures return DataStructures

View File

@ -3,37 +3,32 @@ local ds = require("DataStructures")
local istream = require("InputStream") local istream = require("InputStream")
local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment
local helpers = require("CoreHelpers") local helpers = require("CoreHelpers")
local interpreters = require("Interpreters")
local isNumber, skipWhitespace, parseToken = helpers.isNumber, helpers.skipWhitespace, helpers.parseToken local isNumber, skipWhitespace, parseToken = helpers.isNumber, helpers.skipWhitespace, helpers.parseToken
local reset = helpers.reset
local Pointer = ds.Pointer
local standardInputRefill = helpers.standardInputRefill
local innerInterpreter, outerInterpreter = interpreters.inner, interpreters.outer
local machineEnvironment = Environment:new() local machineEnvironment = Environment:new()
function standardInputRefill(): string
local input = io.read().."\n"
return input
end
local standardInputStream = istream:new(standardInputRefill) local standardInputStream = istream:new(standardInputRefill)
machineEnvironment.activeInputStream = standardInputStream machineEnvironment.activeInputStream = standardInputStream
machineEnvironment.activeDataStack = Stack:new() machineEnvironment.activeDataStack = Stack:new()
machineEnvironment.returnStack = Stack:new()
table.insert(machineEnvironment.dictionaries, CoreWords) table.insert(machineEnvironment.dictionaries, CoreWords)
while(true) do local interpreterInstructions = {}
skipWhitespace(machineEnvironment) table.insert(interpreterInstructions, outerInterpreter)
local token: string = parseToken(machineEnvironment) table.insert(interpreterInstructions, reset)
if isNumber(token) then local startPointer = Pointer:new()
machineEnvironment.activeDataStack:push(tonumber(token)) startPointer.referant = interpreterInstructions
else
for i, dictionary in ipairs(machineEnvironment.dictionaries) do
local wordinfo = dictionary:lookup(token)
if wordinfo then
(wordinfo as WordInfo).func(machineEnvironment)
goto continue
end
end
print(string.format('%q ?', token))
::continue::
end
end
machineEnvironment.instructionPointer = startPointer
innerInterpreter(machineEnvironment)

38
Interpreters.tl Normal file
View File

@ -0,0 +1,38 @@
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 type Interpreters = record
inner: function(Environment)
outer: function(Environment)
end
function Interpreters.outer(env: Environment)
skipWhitespace(env)
local token: string = parseToken(env)
if isNumber(token) then
env.activeDataStack:push(tonumber(token))
else
for _, dictionary in ipairs(env.dictionaries) do
local wordinfo = dictionary:lookup(token)
if wordinfo then
(wordinfo as WordInfo).func(env)
goto continue
end
end
print(string.format('%q ?', token))
::continue::
end
end
function Interpreters.inner(env: Environment)
while(true) do
env.instructionPointer:deref() as function(Environment)(env)
env.instructionPointer:inc()
end
end
return Interpreters