Adding some base data structures

This commit is contained in:
Starfflame 2021-05-10 01:23:32 -05:00
parent 601cc43666
commit 3f8d8e2bfc
6 changed files with 157 additions and 3 deletions

7
ForthDictionary.tl Normal file
View File

@ -0,0 +1,7 @@
local state = require("ForthState")
global type ForthDictionary = record
contents: {string: function(State)}
end

51
ForthStack.lua Normal file
View File

@ -0,0 +1,51 @@
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local table = _tl_compat and _tl_compat.table or table; local ForthStack = {}
Stack = {}
function Stack:push(val)
self.top = self.top + 1
table.insert(self.contents, val)
end
function Stack:pop()
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()
return setmetatable({ contents = {}, top = 0 }, stack_mt)
end
function ForthStack.add(stack)
local a = stack:pop()
local b = stack:pop()
if type(a) == "number" and type(b) == "number" then
local c = a + b
stack:push(c)
else
error("invalid operands for add operation!")
end
end
function ForthStack.dot(s)
print(s:pop())
end
local s = Stack:new()
s:push(1)
s:push(4)
ForthStack.add(s)
print(ForthStack.dot(s))
return ForthStack

51
ForthStack.tl Normal file
View File

@ -0,0 +1,51 @@
local ForthStack = {}
global type Stack = record
contents: {any}
top: number
push: function(Stack, any)
pop: function(Stack)
new: function(Stack): Stack
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 ForthStack.add(stack: Stack)
local a: any =stack:pop()
local b: any=stack:pop()
if a is number and b is number then
local c=a+b
stack:push(c)
else
error("invalid operands for add operation!")
end
end
function ForthStack.dot(s: Stack)
print(s:pop())
end
local s = Stack:new()
s:push(1)
s:push(4)
ForthStack.add(s)
print(ForthStack.dot(s))
return ForthStack

3
ForthState.lua Normal file
View File

@ -0,0 +1,3 @@
local ForthStack = require("ForthStack")
local GlobalState = {}

View File

@ -1,8 +1,45 @@
local ForthStack = require("ForthStack")
local ForthState = {}
global type State = record
dataStacks: {Stack}
compilerStack: Stack
local record GlobalState
stacks: {Stack}
-- dictionaries: {string:any}
end
local state_mt = {__index = State}
function State:new(): State
return setmetatable(
{
dataStacks = {},
compilerStack = Stack:new()
} as State,
state_mt)
end
function State:addDataStack(data: Stack)
table.insert(self.dataStacks, data)
end
function State:changeCompilerStack(compilerStack: Stack)
self.compilerStack = compilerStack
end
local state = State:new()
local stck = Stack:new()
stck:push(1)
stck:push(2)
stck:push(8)
state:addDataStack(stck)
ForthStack.add(state.dataStacks[1])
print(ForthStack.dot(state.dataStacks[1]))
return ForthState

5
tlconfig.lua Normal file
View File

@ -0,0 +1,5 @@
return {
include_dir = {
"./"
}
}