forth-stuff/CoreHelpers.tl

68 lines
1.7 KiB
Plaintext
Raw Normal View History

local ds = require("DataStructures")
2021-05-14 08:29:54 +00:00
local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment
local CoreHelpers = {}
2021-05-14 08:29:54 +00:00
function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean)
2021-05-13 10:54:06 +00:00
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
2021-05-14 08:29:54 +00:00
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
2021-05-14 08:29:54 +00:00
function CoreHelpers.skipWhitespace(state: Environment)
local chr = state.activeInputStream:readCurrentCharacter()
while (CoreHelpers.isWhitespace(chr)) do
chr = state.activeInputStream:advanceOffset()
end
end
2021-05-14 08:29:54 +00:00
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
2021-05-14 08:29:54 +00:00
function CoreHelpers.parseToken(state: Environment): 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
2021-05-18 01:59:46 +00:00
function CoreHelpers.reset(env: Environment)
env.instructionPointer.index = 0 -- will get incremented after this instruction
end
return CoreHelpers