From 3f8d8e2bfc651d681169c0bd104f6b0a0e5529c7 Mon Sep 17 00:00:00 2001 From: Starfflame Date: Mon, 10 May 2021 01:23:32 -0500 Subject: [PATCH] Adding some base data structures --- ForthDictionary.tl | 7 +++++++ ForthStack.lua | 51 ++++++++++++++++++++++++++++++++++++++++++++++ ForthStack.tl | 51 ++++++++++++++++++++++++++++++++++++++++++++++ ForthState.lua | 3 +++ ForthState.tl | 43 +++++++++++++++++++++++++++++++++++--- tlconfig.lua | 5 +++++ 6 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 ForthDictionary.tl create mode 100644 ForthStack.lua create mode 100644 ForthStack.tl create mode 100644 ForthState.lua create mode 100644 tlconfig.lua diff --git a/ForthDictionary.tl b/ForthDictionary.tl new file mode 100644 index 0000000..f6d0c09 --- /dev/null +++ b/ForthDictionary.tl @@ -0,0 +1,7 @@ +local state = require("ForthState") + + +global type ForthDictionary = record + contents: {string: function(State)} +end + diff --git a/ForthStack.lua b/ForthStack.lua new file mode 100644 index 0000000..e87b791 --- /dev/null +++ b/ForthStack.lua @@ -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 diff --git a/ForthStack.tl b/ForthStack.tl new file mode 100644 index 0000000..d9c2a95 --- /dev/null +++ b/ForthStack.tl @@ -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 diff --git a/ForthState.lua b/ForthState.lua new file mode 100644 index 0000000..634bc2e --- /dev/null +++ b/ForthState.lua @@ -0,0 +1,3 @@ +local ForthStack = require("ForthStack") + +local GlobalState = {} diff --git a/ForthState.tl b/ForthState.tl index 67d236a..2534d33 100644 --- a/ForthState.tl +++ b/ForthState.tl @@ -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 diff --git a/tlconfig.lua b/tlconfig.lua new file mode 100644 index 0000000..ca8cfa8 --- /dev/null +++ b/tlconfig.lua @@ -0,0 +1,5 @@ +return { + include_dir = { + "./" + } +}