forth-stuff/InputStream.tl

49 lines
799 B
Plaintext
Raw Normal View History

2021-05-12 03:58:33 +00:00
local type InputStream = record
str: string
offset: number
refill: function(): string
end
local istream_mt = {__index = InputStream}
function InputStream:_manageBuffer()
if self.offset > #self.str then
self.str = self.refill()
self.offset = 1
end
end
-- constructors
function InputStream:new(): InputStream
return setmetatable(
{
string = "",
offset = 1
} as InputStream,
istream_mt)
end
-- setters/getters
function InputStream:setRefill(func: function(): string)
self.refill = func
end
--
function InputStream:readCurrentCharacter(): string
self:_manageBuffer()
return self.str:sub(self.offset, 1)
end
function InputStream:advanceOffset(): string
self.offset = self.offset + 1
self:_manageBuffer()
return self.str:sub(self.offset, 1)
end
return InputStream