forth-stuff/InputStream.tl

70 lines
1.2 KiB
Plaintext

local type InputStream = record
str: string
offset: number
refill: function(): string
new: function(InputStream, function(): string): InputStream
__setRefill: function(InputStream, function(): string)
readCurrentCharacter: function(): string
advanceOffset: function(): string
end
local istream_mt = {__index = InputStream}
function InputStream:_manageBuffer()
if not self.str then
self.str = self.refill()
self.offset = 1
end
if self.offset > #self.str then
self.str = self.refill()
self.offset = 1
end
end
local function initial_stream(): string
return "DUMMY"
end
-- constructors
function InputStream:new(refill: function(): string): InputStream
refill = refill or initial_stream
local istream = setmetatable(
{
string = "",
offset = 1,
} as InputStream,
istream_mt)
istream:__setRefill(refill)
return istream
end
-- setters/getters
function InputStream:__setRefill(func: function(): string)
self.refill = func
self:_manageBuffer()
end
--
function InputStream:curr(): string
return self.str:sub(self.offset, self.offset)
end
function InputStream:next(): string
self:_manageBuffer()
local current_char = self.str:sub(self.offset, self.offset)
self.offset = self.offset + 1
return current_char
end
return InputStream