Compare commits

..

No commits in common. "444a5590cd359f7012ee03330301952129fca798" and "6959c473f80a0a318ff119adda93db7414f5eb93" have entirely different histories.

6 changed files with 28 additions and 128 deletions

View File

@ -1,4 +0,0 @@
package.path = "?.lua;"..package.path
return {
}

View File

@ -1,6 +1,6 @@
local ds = require("DataStructures") local ds = require("DataStructures")
local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment
local Pointer = ds.Pointer
local CoreHelpers = {} local CoreHelpers = {}
function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean) function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean)
@ -8,10 +8,7 @@ function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(En
dict:define(str, info) dict:define(str, info)
end end
function CoreHelpers.standardInputRefill(): string
local input = io.read().."\n"
return input
end
function CoreHelpers.areNumbers(a: any, b: any): boolean function CoreHelpers.areNumbers(a: any, b: any): boolean
return a is number and b is number return a is number and b is number
@ -62,25 +59,5 @@ function CoreHelpers.parseToken(state: Environment): string
return token return token
end end
function CoreHelpers.reset(env: Environment)
env.instructionPointer.index = 0 -- will get incremented after this instruction
end
function CoreHelpers.ret(env: Environment)
local retAddr = env.returnStack:pop() as Pointer
env.instructionPointer = retAddr
end
function CoreHelpers.makeCall(body: {function(Environment)}): function(Environment)
return function(e: Environment)
e.returnStack:push(e.instructionPointer)
e.instructionPointer = Pointer:new(body, 0)
end
end
return CoreHelpers return CoreHelpers

View File

@ -5,8 +5,6 @@ local areNumbers, getActiveDataStack, isNumber, isWhitespace= helpers.areNumbers
local skipWhitespace, parseToken = helpers.skipWhitespace, helpers.parseToken local skipWhitespace, parseToken = helpers.skipWhitespace, helpers.parseToken
local popTwoOperands = helpers.popTwoOperands local popTwoOperands = helpers.popTwoOperands
local defineWord = helpers.defineWord local defineWord = helpers.defineWord
local ret = helpers.ret
local makeCall = helpers.makeCall
-- Mathematical operations -- Mathematical operations
local function add(state: Environment) local function add(state: Environment)
local a, b = popTwoOperands(state) local a, b = popTwoOperands(state)
@ -191,21 +189,6 @@ CoreWords:define("2DUP", twoDupInfo)
CoreWords:define("2SWAP", twoSwapInfo) CoreWords:define("2SWAP", twoSwapInfo)
CoreWords:define("2OVER", twoOverInfo) CoreWords:define("2OVER", twoOverInfo)
CoreWords:define("NIP", nipInfo) CoreWords:define("NIP", nipInfo)
local sqr = {dup, mul, ret}
local sqrf = makeCall(sqr)
defineWord(CoreWords, "SQR", sqrf, false)
return CoreWords return CoreWords

View File

@ -22,18 +22,11 @@ local type DataStructures = record
new: function(WordInfo, function(Environment), boolean): WordInfo new: function(WordInfo, function(Environment), boolean): WordInfo
end end
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
end
record Environment record Environment
dataStacks: {Stack} dataStacks: {Stack}
compilerStack: Stack compilerStack: Stack
returnStack: Stack
activeDataStack: Stack activeDataStack: Stack
interrupts: {function(Environment)} interrupts: {function(Environment)}
activeInputStream: InputStream activeInputStream: InputStream
@ -43,14 +36,13 @@ local type DataStructures = record
addDataStack: function(Environment, Stack) addDataStack: function(Environment, Stack)
changeCompilerStack: function(Environment, Stack) changeCompilerStack: function(Environment, Stack)
changeActiveDataStack: function(Environment, number) changeActiveDataStack: function(Environment, number)
instructionPointer: Pointer
end end
end end
local Stack, Environment, Dictionary, WordInfo = DataStructures.Stack, DataStructures.Environment, DataStructures.Dictionary, DataStructures.WordInfo local Stack, Environment, Dictionary, WordInfo = DataStructures.Stack, DataStructures.Environment, DataStructures.Dictionary, DataStructures.WordInfo
local Pointer = DataStructures.Pointer
local wordi_mt = {__index = WordInfo} local wordi_mt = {__index = WordInfo}
function WordInfo:new(funct: function(Environment, ...: any), imm: boolean): WordInfo function WordInfo:new(funct: function(Environment, ...: any), imm: boolean): WordInfo
return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt) return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt)
@ -117,21 +109,6 @@ function Dictionary:new(): Dictionary
return setmetatable({contents = {}} as Dictionary, dict_mt) return setmetatable({contents = {}} as Dictionary, dict_mt)
end end
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 return DataStructures

View File

@ -3,32 +3,37 @@ local ds = require("DataStructures")
local istream = require("InputStream") local istream = require("InputStream")
local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment
local helpers = require("CoreHelpers") local helpers = require("CoreHelpers")
local interpreters = require("Interpreters")
local isNumber, skipWhitespace, parseToken = helpers.isNumber, helpers.skipWhitespace, helpers.parseToken local isNumber, skipWhitespace, parseToken = helpers.isNumber, helpers.skipWhitespace, helpers.parseToken
local reset = helpers.reset
local Pointer = ds.Pointer
local standardInputRefill = helpers.standardInputRefill
local innerInterpreter, outerInterpreter = interpreters.inner, interpreters.outer
local machineEnvironment = Environment:new() local machineEnvironment = Environment:new()
function standardInputRefill(): string
local input = io.read().."\n"
return input
end
local standardInputStream = istream:new(standardInputRefill) local standardInputStream = istream:new(standardInputRefill)
machineEnvironment.activeInputStream = standardInputStream machineEnvironment.activeInputStream = standardInputStream
machineEnvironment.activeDataStack = Stack:new() machineEnvironment.activeDataStack = Stack:new()
machineEnvironment.returnStack = Stack:new()
table.insert(machineEnvironment.dictionaries, CoreWords) table.insert(machineEnvironment.dictionaries, CoreWords)
local interpreterInstructions = {} while(true) do
table.insert(interpreterInstructions, outerInterpreter) skipWhitespace(machineEnvironment)
table.insert(interpreterInstructions, reset) local token: string = parseToken(machineEnvironment)
local startPointer = Pointer:new() if isNumber(token) then
startPointer.referant = interpreterInstructions machineEnvironment.activeDataStack:push(tonumber(token))
else
for i, dictionary in ipairs(machineEnvironment.dictionaries) do
local wordinfo = dictionary:lookup(token)
if wordinfo then
(wordinfo as WordInfo).func(machineEnvironment)
goto continue
end
end
print(string.format('%q ?', token))
::continue::
end
end
machineEnvironment.instructionPointer = startPointer
innerInterpreter(machineEnvironment)

View File

@ -1,38 +0,0 @@
local ds = require("DataStructures")
local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment
local helpers = require("CoreHelpers")
local isNumber, skipWhitespace, parseToken = helpers.isNumber, helpers.skipWhitespace, helpers.parseToken
local type Interpreters = record
inner: function(Environment)
outer: function(Environment)
end
function Interpreters.outer(env: Environment)
skipWhitespace(env)
local token: string = parseToken(env)
if isNumber(token) then
env.activeDataStack:push(tonumber(token))
else
for _, dictionary in ipairs(env.dictionaries) do
local wordinfo = dictionary:lookup(token)
if wordinfo then
(wordinfo as WordInfo).func(env)
goto continue
end
end
print(string.format('%q ?', token))
::continue::
end
end
function Interpreters.inner(env: Environment)
while(true) do
env.instructionPointer:deref() as function(Environment)(env)
env.instructionPointer:inc()
end
end
return Interpreters