2021-05-13 09:19:54 +00:00
|
|
|
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
|
2021-05-13 09:19:54 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-13 09:19:54 +00:00
|
|
|
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
|
2021-05-13 09:19:54 +00:00
|
|
|
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)
|
2021-05-13 09:19:54 +00:00
|
|
|
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
|
2021-05-13 09:19:54 +00:00
|
|
|
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
|
2021-05-13 09:19:54 +00:00
|
|
|
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
|
|
|
|
|
2021-05-13 09:19:54 +00:00
|
|
|
return CoreHelpers
|
|
|
|
|