139 lines
3.4 KiB
Plaintext
139 lines
3.4 KiB
Plaintext
local InputStream = require("InputStream")
|
|
|
|
|
|
local type DataStructures = record
|
|
record Stack
|
|
contents: {any}
|
|
top: number
|
|
push: function(Stack, any)
|
|
pop: function(Stack)
|
|
|
|
new: function(Stack): Stack
|
|
end
|
|
|
|
record Dictionary
|
|
contents: {string: WordInfo}
|
|
|
|
define: function(Dictionary, string, WordInfo)
|
|
end
|
|
record WordInfo
|
|
func: function(Environment, ...: any)
|
|
immediate: boolean
|
|
new: function(WordInfo, function(Environment), boolean): WordInfo
|
|
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
|
|
dataStacks: {Stack}
|
|
compilerStack: Stack
|
|
returnStack: Stack
|
|
activeDataStack: Stack
|
|
interrupts: {function(Environment)}
|
|
activeInputStream: InputStream
|
|
dictionaries: {Dictionary}
|
|
state: boolean
|
|
new: function(Environment): Environment
|
|
addDataStack: function(Environment, Stack)
|
|
changeCompilerStack: function(Environment, Stack)
|
|
changeActiveDataStack: function(Environment, number)
|
|
|
|
instructionPointer: Pointer
|
|
end
|
|
|
|
end
|
|
|
|
local Stack, Environment, Dictionary, WordInfo = DataStructures.Stack, DataStructures.Environment, DataStructures.Dictionary, DataStructures.WordInfo
|
|
local Pointer = DataStructures.Pointer
|
|
local wordi_mt = {__index = WordInfo}
|
|
function WordInfo:new(funct: function(Environment, ...: any), imm: boolean): WordInfo
|
|
return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt)
|
|
end
|
|
|
|
|
|
local state_mt = {__index = Environment}
|
|
function Environment:new(): Environment
|
|
return setmetatable(
|
|
{
|
|
dataStacks = {},
|
|
compilerStack = Stack:new(),
|
|
dictionaries = {}
|
|
} as Environment,
|
|
state_mt)
|
|
end
|
|
|
|
function Environment:addDataStack(data: Stack)
|
|
table.insert(self.dataStacks, data)
|
|
end
|
|
|
|
function Environment:changeCompilerStack(compilerStack: Stack)
|
|
self.compilerStack = compilerStack
|
|
end
|
|
--function Environment:changeActiveDataStack(stackIndex: number)
|
|
-- assert(stackIndex <= #self.dataStacks and stackIndex > 0)
|
|
-- self.activeDataStack = self.dataStacks[stackIndex]
|
|
--end
|
|
|
|
|
|
|
|
|
|
function Stack:push(val: any)
|
|
self.top = self.top + 1
|
|
table.insert(self.contents,val)
|
|
end
|
|
function Stack:pop(): any
|
|
self.top = self.top -1
|
|
if self.top < 0 then
|
|
error("Stack underflow")
|
|
end
|
|
return table.remove(self.contents)
|
|
end
|
|
local stack_mt = {__index = Stack}
|
|
function Stack:new(): Stack
|
|
return setmetatable({contents = {}, top = 0} as Stack, stack_mt)
|
|
end
|
|
|
|
|
|
-- operations
|
|
|
|
|
|
|
|
|
|
function Dictionary:lookup(word: string): WordInfo | nil
|
|
return self.contents[word]
|
|
end
|
|
|
|
function Dictionary:define(word: string, info: WordInfo)
|
|
self.contents[word] = info
|
|
end
|
|
local dict_mt = {__index = Dictionary}
|
|
function Dictionary:new(): Dictionary
|
|
return setmetatable({contents = {}} as Dictionary, dict_mt)
|
|
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
|
|
|
|
|
|
|