forth-stuff/CoreHelpers.tl

64 lines
1.5 KiB
Plaintext

local ds = require("DataStructures")
local Dictionary, Stack, WordInfo, State = ds.Dictionary, ds.Stack, ds.WordInfo, ds.State
local CoreHelpers = {}
function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(State, ...: any): any..., imm: boolean)
local info = WordInfo:new(func, imm)
dict:define(str, info)
end
function CoreHelpers.areNumbers(a: any, b: any): boolean
return a is number and b is number
end
function CoreHelpers.getActiveDataStack(state: State): 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: State)
local chr = state.activeInputStream:readCurrentCharacter()
while (CoreHelpers.isWhitespace(chr)) do
chr = state.activeInputStream:advanceOffset()
end
end
function CoreHelpers.popTwoOperands(state: State): 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: State): string
local chr = state.activeInputStream:readCurrentCharacter()
local token = ""
while(not CoreHelpers.isWhitespace(chr)) do
token = token..chr
chr = state.activeInputStream:advanceOffset()
end
return token
end
return CoreHelpers