Compare commits
2 Commits
6959c473f8
...
444a5590cd
Author | SHA1 | Date |
---|---|---|
Starfflame | 444a5590cd | |
Starfflame | e8bcb186e3 |
|
@ -1,6 +1,6 @@
|
|||
local ds = require("DataStructures")
|
||||
local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment
|
||||
|
||||
local Pointer = ds.Pointer
|
||||
local CoreHelpers = {}
|
||||
|
||||
function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean)
|
||||
|
@ -8,7 +8,10 @@ function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(En
|
|||
dict:define(str, info)
|
||||
end
|
||||
|
||||
|
||||
function CoreHelpers.standardInputRefill(): string
|
||||
local input = io.read().."\n"
|
||||
return input
|
||||
end
|
||||
|
||||
function CoreHelpers.areNumbers(a: any, b: any): boolean
|
||||
return a is number and b is number
|
||||
|
@ -59,5 +62,25 @@ function CoreHelpers.parseToken(state: Environment): string
|
|||
return token
|
||||
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
|
||||
|
||||
|
|
17
CoreWords.tl
17
CoreWords.tl
|
@ -5,6 +5,8 @@ local areNumbers, getActiveDataStack, isNumber, isWhitespace= helpers.areNumbers
|
|||
local skipWhitespace, parseToken = helpers.skipWhitespace, helpers.parseToken
|
||||
local popTwoOperands = helpers.popTwoOperands
|
||||
local defineWord = helpers.defineWord
|
||||
local ret = helpers.ret
|
||||
local makeCall = helpers.makeCall
|
||||
-- Mathematical operations
|
||||
local function add(state: Environment)
|
||||
local a, b = popTwoOperands(state)
|
||||
|
@ -189,6 +191,21 @@ CoreWords:define("2DUP", twoDupInfo)
|
|||
CoreWords:define("2SWAP", twoSwapInfo)
|
||||
CoreWords:define("2OVER", twoOverInfo)
|
||||
CoreWords:define("NIP", nipInfo)
|
||||
|
||||
|
||||
|
||||
local sqr = {dup, mul, ret}
|
||||
local sqrf = makeCall(sqr)
|
||||
|
||||
defineWord(CoreWords, "SQR", sqrf, false)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return CoreWords
|
||||
|
||||
|
||||
|
|
|
@ -22,11 +22,18 @@ local type DataStructures = record
|
|||
new: function(WordInfo, function(Environment), boolean): WordInfo
|
||||
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
|
||||
dataStacks: {Stack}
|
||||
compilerStack: Stack
|
||||
returnStack: Stack
|
||||
activeDataStack: Stack
|
||||
interrupts: {function(Environment)}
|
||||
activeInputStream: InputStream
|
||||
|
@ -36,13 +43,14 @@ local type DataStructures = record
|
|||
addDataStack: function(Environment, Stack)
|
||||
changeCompilerStack: function(Environment, Stack)
|
||||
changeActiveDataStack: function(Environment, number)
|
||||
|
||||
|
||||
instructionPointer: Pointer
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
local Stack, Environment, Dictionary, WordInfo = DataStructures.Stack, DataStructures.Environment, DataStructures.Dictionary, DataStructures.WordInfo
|
||||
|
||||
local Pointer = DataStructures.Pointer
|
||||
local wordi_mt = {__index = WordInfo}
|
||||
function WordInfo:new(funct: function(Environment, ...: any), imm: boolean): WordInfo
|
||||
return setmetatable({func = funct, immediate = imm} as WordInfo, wordi_mt)
|
||||
|
@ -109,6 +117,21 @@ function Dictionary:new(): Dictionary
|
|||
return setmetatable({contents = {}} as Dictionary, dict_mt)
|
||||
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
|
||||
|
||||
|
||||
|
|
|
@ -3,37 +3,32 @@ local ds = require("DataStructures")
|
|||
local istream = require("InputStream")
|
||||
local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment
|
||||
local helpers = require("CoreHelpers")
|
||||
local interpreters = require("Interpreters")
|
||||
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()
|
||||
|
||||
function standardInputRefill(): string
|
||||
local input = io.read().."\n"
|
||||
return input
|
||||
end
|
||||
|
||||
local standardInputStream = istream:new(standardInputRefill)
|
||||
|
||||
machineEnvironment.activeInputStream = standardInputStream
|
||||
machineEnvironment.activeDataStack = Stack:new()
|
||||
machineEnvironment.returnStack = Stack:new()
|
||||
|
||||
table.insert(machineEnvironment.dictionaries, CoreWords)
|
||||
|
||||
|
||||
while(true) do
|
||||
skipWhitespace(machineEnvironment)
|
||||
local token: string = parseToken(machineEnvironment)
|
||||
if isNumber(token) then
|
||||
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
|
||||
local interpreterInstructions = {}
|
||||
table.insert(interpreterInstructions, outerInterpreter)
|
||||
table.insert(interpreterInstructions, reset)
|
||||
local startPointer = Pointer:new()
|
||||
startPointer.referant = interpreterInstructions
|
||||
|
||||
machineEnvironment.instructionPointer = startPointer
|
||||
|
||||
innerInterpreter(machineEnvironment)
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
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
|
Loading…
Reference in New Issue