Compare commits
No commits in common. "fe6bc4f8cb271731c3d88f40f6518aee81b8462a" and "5dc66425f527d28a9e1d90a447c6d8e7df4c0061" have entirely different histories.
fe6bc4f8cb
...
5dc66425f5
103
CoreWords.tl
103
CoreWords.tl
|
@ -1,34 +1,18 @@
|
|||
local ds = require("DataStructures")
|
||||
local Dictionary, Stack, WordInfo, State = ds.Dictionary, ds.Stack, ds.WordInfo, ds.State
|
||||
local Dictionary = require("Dictionary")
|
||||
local Stack = require("Stack")
|
||||
local WordInfo = require("WordInfo")
|
||||
local State = require("State")
|
||||
|
||||
|
||||
|
||||
-- helper functions
|
||||
local function areNumbers(state: State, a: any, b: any): boolean
|
||||
function areNumbers(a: any, b: any): boolean
|
||||
return a is number and b is number
|
||||
end
|
||||
|
||||
local function getActiveDataStack(state: State): Stack
|
||||
function getActiveDataStack(state: State): Stack
|
||||
return state.activeDataStack
|
||||
end
|
||||
|
||||
local function isNumber(state: State, token: string): boolean
|
||||
if tonumber(token) ~= nil then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
local function isWhitespace(state: State, chr: string): boolean
|
||||
return (chr == " " or
|
||||
chr == "\t" or
|
||||
chr == "\r" or
|
||||
chr == "\n" or
|
||||
chr == "\v" or
|
||||
chr == "\f")
|
||||
end
|
||||
|
||||
local function popTwoOperands(state: State): any, any
|
||||
function popTwoOperands(state: State): any, any
|
||||
local stack = getActiveDataStack(state)
|
||||
local b: any = stack:pop()
|
||||
local a: any = stack:pop()
|
||||
|
@ -37,36 +21,36 @@ end
|
|||
|
||||
|
||||
-- Mathematical operations
|
||||
local function add(state: State)
|
||||
function add(state: State)
|
||||
local a, b = popTwoOperands(state)
|
||||
if areNumbers(state, a,b) then
|
||||
if areNumbers(a,b) then
|
||||
local c = (a as number) + (b as number)
|
||||
getActiveDataStack(state):push(c)
|
||||
else
|
||||
error("invalid operands for add operation!")
|
||||
end
|
||||
end
|
||||
local function sub(state: State)
|
||||
function sub(state: State)
|
||||
local a, b = popTwoOperands(state)
|
||||
if areNumbers(state, a, b) then
|
||||
if areNumbers(a, b) then
|
||||
local c = (a as number) - (b as number)
|
||||
getActiveDataStack(state):push(c)
|
||||
else
|
||||
error("invalid operands for sub operation!")
|
||||
end
|
||||
end
|
||||
local function mul(state: State)
|
||||
function mul(state: State)
|
||||
local a, b = popTwoOperands(state)
|
||||
if areNumbers(state, a, b) then
|
||||
if areNumbers(a, b) then
|
||||
local c = (a as number) * (b as number)
|
||||
getActiveDataStack(state):push(c)
|
||||
else
|
||||
error("invalid operands for mul operation!")
|
||||
end
|
||||
end
|
||||
local function div(state: State)
|
||||
function div(state: State)
|
||||
local a, b = popTwoOperands(state)
|
||||
if areNumbers(state, a, b) then
|
||||
if areNumbers(a, b) then
|
||||
local c = (a as number) / (b as number)
|
||||
getActiveDataStack(state):push(c)
|
||||
else
|
||||
|
@ -74,56 +58,23 @@ local function div(state: State)
|
|||
end
|
||||
end
|
||||
-- I/O operations
|
||||
local function dot(state: State)
|
||||
function dot(state: State)
|
||||
local out = state.activeDataStack:pop()
|
||||
print(out)
|
||||
|
||||
end
|
||||
|
||||
local function skipWhitespace(state: State)
|
||||
local chr = state.activeInputStream:readCurrentCharacter()
|
||||
while (isWhitespace(state, chr)) do
|
||||
chr = state.activeInputStream:advanceOffset()
|
||||
end
|
||||
end
|
||||
|
||||
local function parseToken(state: State): string
|
||||
local chr = state.activeInputStream:readCurrentCharacter()
|
||||
local token = ""
|
||||
while(not isWhitespace(state, chr)) do
|
||||
token = token..chr
|
||||
chr = state.activeInputStream:advanceOffset()
|
||||
end
|
||||
return token
|
||||
end
|
||||
|
||||
|
||||
|
||||
local CoreWords = Dictionary:new()
|
||||
local addInfo = WordInfo:new(add, false)
|
||||
local subInfo = WordInfo:new(sub, false)
|
||||
local mulInfo = WordInfo:new(mul, false)
|
||||
local divInfo = WordInfo:new(div, false)
|
||||
local dotInfo = WordInfo:new(dot, false)
|
||||
local getActiveDataStackInfo = WordInfo:new(getActiveDataStack, true)
|
||||
local areNumbersInfo = WordInfo:new(areNumbers, false)
|
||||
local isNumberInfo = WordInfo:new(isNumber, false)
|
||||
local isWhitespaceInfo = WordInfo:new(isWhitespace, false)
|
||||
local popTwoOperandsInfo = WordInfo:new(popTwoOperands, false)
|
||||
local parseTokenInfo = WordInfo:new(parseToken, true)
|
||||
local skipWhitespaceInfo = WordInfo:new(skipWhitespace, true)
|
||||
CoreWords:define("+", addInfo)
|
||||
CoreWords:define("-", subInfo)
|
||||
CoreWords:define("*", mulInfo)
|
||||
CoreWords:define("/", divInfo)
|
||||
CoreWords:define(".", dotInfo)
|
||||
CoreWords:define("GetActiveData", getActiveDataStackInfo)
|
||||
CoreWords:define("areNumbers", areNumbersInfo)
|
||||
CoreWords:define("isNumber", isNumberInfo)
|
||||
CoreWords:define("isWhitespace", isWhitespaceInfo)
|
||||
CoreWords:define("popTwo", popTwoOperandsInfo)
|
||||
CoreWords:define("parseToken", parseTokenInfo)
|
||||
CoreWords:define("skipWhitespace", skipWhitespaceInfo)
|
||||
local AddInfo = WordInfo:new(add, false)
|
||||
local SubInfo = WordInfo:new(sub, false)
|
||||
local MulInfo = WordInfo:new(mul, false)
|
||||
local DivInfo = WordInfo:new(div, false)
|
||||
|
||||
CoreWords:define("+", AddInfo)
|
||||
CoreWords:define("-", SubInfo)
|
||||
CoreWords:define("*", MulInfo)
|
||||
CoreWords:define("/", DivInfo)
|
||||
|
||||
return CoreWords
|
||||
|
||||
|
||||
|
|
|
@ -1,114 +0,0 @@
|
|||
local InputStream = require("InputStream")
|
||||
|
||||
|
||||
local type DataStructures = record
|
||||
record Stack
|
||||
contents: {any}
|
||||
top: number
|
||||
push: function(Stack, any)
|
||||
pop: function(Stack)
|
||||
|
||||
new: function(Stack): Stack
|
||||
end
|
||||
|
||||
record Dictionary
|
||||
contents: {string: WordInfo}
|
||||
|
||||
define: function(Dictionary, string, WordInfo)
|
||||
end
|
||||
record WordInfo
|
||||
func: function(State, ...: any)
|
||||
immediate: boolean
|
||||
new: function(WordInfo, function(State), boolean): WordInfo
|
||||
end
|
||||
|
||||
|
||||
|
||||
record State
|
||||
dataStacks: {Stack}
|
||||
compilerStack: Stack
|
||||
activeDataStack: Stack
|
||||
interrupts: {function(State)}
|
||||
activeInputStream: InputStream
|
||||
dictionaries: {Dictionary}
|
||||
|
||||
new: function(State): State
|
||||
addDataStack: function(State, Stack)
|
||||
changeCompilerStack: function(State, Stack)
|
||||
changeActiveDataStack: function(State, number)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
local Stack, State, Dictionary, WordInfo = DataStructures.Stack, DataStructures.State, DataStructures.Dictionary, DataStructures.WordInfo
|
||||
|
||||
local wordi_mt = {__index = WordInfo}
|
||||
function WordInfo:new(funct: function(State, ...: any), imm: boolean): WordInfo
|
||||
return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt)
|
||||
end
|
||||
|
||||
|
||||
local state_mt = {__index = State}
|
||||
function State:new(): State
|
||||
return setmetatable(
|
||||
{
|
||||
dataStacks = {},
|
||||
compilerStack = Stack:new(),
|
||||
dictionaries = {}
|
||||
} 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
|
||||
--function State:changeActiveDataStack(stackIndex: number)
|
||||
-- assert(stackIndex <= #self.dataStacks and stackIndex > 0)
|
||||
-- self.activeDataStack = self.dataStacks[stackIndex]
|
||||
--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 Dictionary:lookup(word: string): WordInfo | nil
|
||||
return self.contents[word]
|
||||
end
|
||||
|
||||
function Dictionary:define(word: string, info: WordInfo)
|
||||
self.contents[word] = info
|
||||
end
|
||||
local dict_mt = {__index = Dictionary}
|
||||
function Dictionary:new(): Dictionary
|
||||
return setmetatable({contents = {}} as Dictionary, dict_mt)
|
||||
end
|
||||
|
||||
return DataStructures
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
local State = require("State")
|
||||
local WordInfo = require("WordInfo")
|
||||
|
||||
|
||||
local type Dictionary = record
|
||||
contents: {string: WordInfo}
|
||||
end
|
||||
|
||||
function Dictionary:define(word: string, info: WordInfo)
|
||||
self.contents[word] = info
|
||||
end
|
||||
local dict_mt = {__index = Dictionary}
|
||||
function Dictionary:new(): Dictionary
|
||||
return setmetatable({contents = {}} as Dictionary, dict_mt)
|
||||
end
|
||||
|
||||
|
||||
|
||||
return Dictionary
|
|
@ -1,45 +0,0 @@
|
|||
local CoreWords = require("CoreWords")
|
||||
local ds = require("DataStructures")
|
||||
local istream = require("InputStream")
|
||||
local Stack, Dictionary, WordInfo, State = ds.Stack, ds.Dictionary, ds.WordInfo, ds.State
|
||||
|
||||
local machineState = State:new()
|
||||
|
||||
function standardInputRefill(): string
|
||||
local input = io.read().."\n"
|
||||
return input
|
||||
end
|
||||
|
||||
local standardInputStream = istream:new(standardInputRefill)
|
||||
|
||||
machineState.activeInputStream = standardInputStream
|
||||
machineState.activeDataStack = Stack:new()
|
||||
table.insert(machineState.dictionaries, CoreWords)
|
||||
|
||||
local getActiveDataStacks = (CoreWords:lookup("GetActiveData") as WordInfo).func
|
||||
local areNumbers = (CoreWords:lookup("areNumbers") as WordInfo).func
|
||||
local isNumber = (CoreWords:lookup("isNumber") as WordInfo).func
|
||||
local isWhitespace = (CoreWords:lookup("isWhitespace") as WordInfo).func
|
||||
local popTwoOperands = (CoreWords:lookup("popTwo") as WordInfo).func
|
||||
local skipWhitespace = (CoreWords:lookup("skipWhitespace") as WordInfo).func
|
||||
local parseToken = (CoreWords:lookup("parseToken") as WordInfo).func
|
||||
|
||||
while(true) do
|
||||
skipWhitespace(machineState)
|
||||
local token: string = parseToken(machineState)
|
||||
if isNumber(machineState, token) then
|
||||
machineState.activeDataStack:push(tonumber(token))
|
||||
else
|
||||
for i, dictionary in ipairs(machineState.dictionaries) do
|
||||
local wordinfo = dictionary:lookup(token)
|
||||
if wordinfo then
|
||||
(wordinfo as WordInfo).func(machineState)
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
print(string.format('%q ?', token))
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -2,24 +2,12 @@ local type InputStream = record
|
|||
str: string
|
||||
offset: number
|
||||
refill: function(): string
|
||||
|
||||
new: function(InputStream): InputStream
|
||||
new: function(InputStream, function(): string): InputStream
|
||||
|
||||
setRefill: function(InputStream, function(): string)
|
||||
|
||||
readCurrentCharacter: function(): string
|
||||
advanceOffset: function(): string
|
||||
end
|
||||
|
||||
local istream_mt = {__index = InputStream}
|
||||
|
||||
function InputStream:_manageBuffer()
|
||||
if not self.str then
|
||||
self.str = self.refill()
|
||||
self.offset = 1
|
||||
end
|
||||
if self.offset > #self.str + 1 then
|
||||
if self.offset > #self.str then
|
||||
self.str = self.refill()
|
||||
self.offset = 1
|
||||
end
|
||||
|
@ -36,19 +24,6 @@ function InputStream:new(): InputStream
|
|||
} as InputStream,
|
||||
istream_mt)
|
||||
end
|
||||
function InputStream:new(refill: function(): string): InputStream
|
||||
local mt = setmetatable(
|
||||
{
|
||||
string = "",
|
||||
offset = 1
|
||||
} as InputStream,
|
||||
istream_mt)
|
||||
mt:setRefill(refill)
|
||||
return mt
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- setters/getters
|
||||
function InputStream:setRefill(func: function(): string)
|
||||
|
@ -58,13 +33,13 @@ end
|
|||
--
|
||||
function InputStream:readCurrentCharacter(): string
|
||||
self:_manageBuffer()
|
||||
return self.str:sub(self.offset, self.offset)
|
||||
return self.str:sub(self.offset, 1)
|
||||
end
|
||||
|
||||
function InputStream:advanceOffset(): string
|
||||
self.offset = self.offset + 1
|
||||
self:_manageBuffer()
|
||||
return self.str:sub(self.offset, self.offset)
|
||||
return self.str:sub(self.offset, 1)
|
||||
end
|
||||
|
||||
return InputStream
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
|
||||
local 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
|
||||
|
||||
return Stack
|
|
@ -0,0 +1,40 @@
|
|||
local Stack = require("Stack")
|
||||
|
||||
|
||||
|
||||
|
||||
local type State = record
|
||||
dataStacks: {Stack}
|
||||
compilerStack: Stack
|
||||
activeDataStack: Stack
|
||||
interrupts: {function(State)}
|
||||
-- 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
|
||||
function State:changeActiveDataStack(stackIndex: number)
|
||||
assert(stackIndex <= #self.dataStacks and stackIndex > 0)
|
||||
self.activeDataStack = self.dataStacks[stackIndex]
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return State
|
|
@ -0,0 +1,14 @@
|
|||
local State = require("State")
|
||||
|
||||
local type WordInfo = record
|
||||
func: function(State)
|
||||
immediate: boolean
|
||||
end
|
||||
local wordi_mt = {__index = WordInfo}
|
||||
function WordInfo:new(funct: function(State), imm: boolean): WordInfo
|
||||
return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt)
|
||||
end
|
||||
|
||||
return WordInfo
|
||||
|
||||
|
Loading…
Reference in New Issue