forth-stuff/CoreHelpers.tl

87 lines
2.1 KiB
Plaintext

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)
local info = WordInfo:new(func, imm)
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
end
function CoreHelpers.getActiveDataStack(state: Environment): Stack
return state.activeDataStack
end
function CoreHelpers.isNumber(token: string): boolean
if tonumber(token) ~= nil then
return true
else
return false
end
end
function CoreHelpers.isWhitespace(chr: string): boolean
return (chr == " " or
chr == "\t" or
chr == "\r" or
chr == "\n" or
chr == "\v" or
chr == "\f")
end
function CoreHelpers.skipWhitespace(state: Environment)
local chr = state.activeInputStream:readCurrentCharacter()
while (CoreHelpers.isWhitespace(chr)) do
chr = state.activeInputStream:advanceOffset()
end
end
function CoreHelpers.popTwoOperands(state: Environment): any, any
local stack = CoreHelpers.getActiveDataStack(state)
local b: any = stack:pop()
local a: any = stack:pop()
return a, b
end
function CoreHelpers.parseToken(state: Environment): string
local chr = ""
local token = ""
while(not CoreHelpers.isWhitespace(chr)) do
token = token..chr
chr = state.activeInputStream:advanceOffset()
end
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