forth-stuff/DataStructures.tl

139 lines
3.4 KiB
Plaintext
Raw Normal View History

local InputStream = require("InputStream")
2021-05-12 08:32:25 +00:00
local type DataStructures = record
record Stack
contents: {any}
top: number
push: function(Stack, any)
pop: function(Stack)
2021-05-12 08:32:25 +00:00
new: function(Stack): Stack
end
record Dictionary
contents: {string: WordInfo}
define: function(Dictionary, string, WordInfo)
2021-05-12 08:32:25 +00:00
end
record WordInfo
2021-05-14 08:29:54 +00:00
func: function(Environment, ...: any)
2021-05-12 08:32:25 +00:00
immediate: boolean
2021-05-14 08:29:54 +00:00
new: function(WordInfo, function(Environment), boolean): WordInfo
2021-05-12 08:32:25 +00:00
end
2021-05-18 01:59:46 +00:00
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
2021-05-18 01:59:46 +00:00
end
2021-05-14 08:29:54 +00:00
record Environment
2021-05-12 08:32:25 +00:00
dataStacks: {Stack}
compilerStack: Stack
2021-05-18 01:59:46 +00:00
returnStack: Stack
2021-05-12 08:32:25 +00:00
activeDataStack: Stack
2021-05-14 08:29:54 +00:00
interrupts: {function(Environment)}
2021-05-12 08:32:25 +00:00
activeInputStream: InputStream
dictionaries: {Dictionary}
2021-05-13 10:54:06 +00:00
state: boolean
2021-05-14 08:29:54 +00:00
new: function(Environment): Environment
addDataStack: function(Environment, Stack)
changeCompilerStack: function(Environment, Stack)
changeActiveDataStack: function(Environment, number)
2021-05-18 01:59:46 +00:00
instructionPointer: Pointer
2021-05-13 10:54:06 +00:00
end
end
2021-05-12 08:32:25 +00:00
2021-05-14 08:29:54 +00:00
local Stack, Environment, Dictionary, WordInfo = DataStructures.Stack, DataStructures.Environment, DataStructures.Dictionary, DataStructures.WordInfo
2021-05-18 01:59:46 +00:00
local Pointer = DataStructures.Pointer
local wordi_mt = {__index = WordInfo}
2021-05-14 08:29:54 +00:00
function WordInfo:new(funct: function(Environment, ...: any), imm: boolean): WordInfo
return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt)
2021-05-12 08:32:25 +00:00
end
2021-05-14 08:29:54 +00:00
local state_mt = {__index = Environment}
function Environment:new(): Environment
2021-05-12 08:32:25 +00:00
return setmetatable(
{
dataStacks = {},
compilerStack = Stack:new(),
dictionaries = {}
2021-05-14 08:29:54 +00:00
} as Environment,
2021-05-12 08:32:25 +00:00
state_mt)
end
2021-05-14 08:29:54 +00:00
function Environment:addDataStack(data: Stack)
2021-05-12 08:32:25 +00:00
table.insert(self.dataStacks, data)
end
2021-05-14 08:29:54 +00:00
function Environment:changeCompilerStack(compilerStack: Stack)
2021-05-12 08:32:25 +00:00
self.compilerStack = compilerStack
end
2021-05-14 08:29:54 +00:00
--function Environment:changeActiveDataStack(stackIndex: number)
-- assert(stackIndex <= #self.dataStacks and stackIndex > 0)
-- self.activeDataStack = self.dataStacks[stackIndex]
--end
2021-05-12 08:32:25 +00:00
function Stack:push(val: any)
2021-05-12 08:32:25 +00:00
self.top = self.top + 1
table.insert(self.contents,val)
end
function Stack:pop(): any
2021-05-12 08:32:25 +00:00
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)
2021-05-12 08:32:25 +00:00
end
-- operations
function Dictionary:lookup(word: string): WordInfo | nil
return self.contents[word]
end
2021-05-12 08:32:25 +00:00
function Dictionary:define(word: string, info: WordInfo)
2021-05-12 08:32:25 +00:00
self.contents[word] = info
end
local dict_mt = {__index = Dictionary}
function Dictionary:new(): Dictionary
2021-05-12 08:32:25 +00:00
return setmetatable({contents = {}} as Dictionary, dict_mt)
end
2021-05-18 01:59:46 +00:00
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