From 41276fde68008f0787430732d0b4b0c3a8f00cdc Mon Sep 17 00:00:00 2001 From: Starfflame Date: Mon, 10 May 2021 20:43:33 -0500 Subject: [PATCH] Add some things for Dictionaries; expand the definition of ForthState --- .gitignore | 2 ++ ForthDictionary.tl | 16 ++++++++++++++-- ForthState.tl | 7 ++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e985a99..422d884 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .envrc *.nix +*.lua +!*tlconfig.lua diff --git a/ForthDictionary.tl b/ForthDictionary.tl index f6d0c09..2352ebb 100644 --- a/ForthDictionary.tl +++ b/ForthDictionary.tl @@ -1,7 +1,19 @@ local state = require("ForthState") +local ForthDictionary = {} - -global type ForthDictionary = record +global type Dictionary = record contents: {string: function(State)} end + +function Dictionary:define(word: string, instructions: function(State)) + self.contents[word] = instructions +end +local dict_mt = {__index = Dictionary} +function Dictionary:new(): Dictionary + return setmetatable({contents = {}} as Dictionary, dict_mt) +end + + + +return ForthDictionary diff --git a/ForthState.tl b/ForthState.tl index 2534d33..5d06a1c 100644 --- a/ForthState.tl +++ b/ForthState.tl @@ -7,7 +7,8 @@ local ForthState = {} global type State = record dataStacks: {Stack} compilerStack: Stack - + activeDataStack: Stack + interrupts: {function(State)} -- dictionaries: {string:any} end @@ -28,6 +29,10 @@ end function State:changeCompilerStack(compilerStack: Stack) self.compilerStack = compilerStack end +function State:changeActiveDataStack(stackIndex: number) + assert(stackIndex <= #self.dataStacks and stackIndex > 0) + self.activeDataStack = self.dataStacks[stackIndex] +end local state = State:new()