forth-stuff/DataStructures.tl

159 lines
4.2 KiB
Plaintext
Raw Permalink Normal View History

local InputStream = require("InputStream")
2021-05-12 08:32:25 +00:00
local type DataStructures = record
record Stack
contents: {integer:any}
top: integer
2021-05-12 08:32:25 +00:00
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}
wordCount: integer
define: function(Dictionary, string, WordInfo)
2021-05-12 08:32:25 +00:00
end
record WordInfo
toFunc: function(Environment)
2021-05-25 08:41:39 +00:00
func: function(Environment)
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-25 07:27:57 +00:00
compileState: 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)
recognizers: {function(Environment)}
2021-05-18 01:59:46 +00:00
instructionPointer: Pointer
running: boolean
2021-05-26 04:27:18 +00:00
currentDefinitionName: string | nil
2021-05-25 08:07:03 +00:00
currentDefinition: {function(Environment)}
2021-05-29 07:12:29 +00:00
mostRecentDefinition: string
toMode: boolean
locals: Dictionary
localBuffer: {function(integer, Environment)}
2021-07-20 03:15:55 +00:00
values: {any}
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}
function WordInfo:new(funct: function(Environment, ...: any), imm: boolean, toFunct: function(Environment) | nil): WordInfo
return setmetatable({func = funct, immediate = imm, toFunc = toFunct} as WordInfo, wordi_mt)
2021-05-12 08:32:25 +00:00
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)
if not self.contents[word] then self.wordCount = self.wordCount + 1 end
2021-05-12 08:32:25 +00:00
self.contents[word] = info
end
local dict_mt = {__index = Dictionary}
function Dictionary:new(): Dictionary
return setmetatable({contents = {}, wordCount = 0} as Dictionary, dict_mt)
2021-05-12 08:32:25 +00:00
end
local state_mt = {__index = Environment}
function Environment:new(input: InputStream, dict: {Dictionary} ): Environment
local dicts = dict or {}
return setmetatable(
{
activeDataStack= Stack:new(),
compilerStack = Stack:new(),
returnStack = Stack:new(),
dictionaries = dicts,
activeInputStream = input,
running = true,
recognizers = {},
compileState = false,
currentDefinition = {},
locals = Dictionary:new(),
2021-07-20 03:15:55 +00:00
localBuffer = {},
values = {}
} 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
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