fixed bad code
This commit is contained in:
parent
ce6b85a5e7
commit
ca93ea00d4
|
@ -38,9 +38,9 @@ function CoreHelpers.isWhitespace(chr: string): boolean
|
|||
chr == "\f")
|
||||
end
|
||||
function CoreHelpers.skipWhitespace(state: Environment)
|
||||
local chr = state.activeInputStream:readCurrentCharacter()
|
||||
local chr = state.activeInputStream:peek()
|
||||
while (CoreHelpers.isWhitespace(chr)) do
|
||||
chr = state.activeInputStream:advanceOffset()
|
||||
chr = state.activeInputStream:advance()
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -57,7 +57,7 @@ function CoreHelpers.parseToken(state: Environment): string
|
|||
local token = ""
|
||||
while(not CoreHelpers.isWhitespace(chr)) do
|
||||
token = token..chr
|
||||
chr = state.activeInputStream:advanceOffset()
|
||||
chr = state.activeInputStream:next()
|
||||
end
|
||||
return token
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ local type InputStream = record
|
|||
|
||||
new: function(InputStream, function(): string): InputStream
|
||||
|
||||
setRefill: function(InputStream, function(): string)
|
||||
__setRefill: function(InputStream, function(): string)
|
||||
|
||||
readCurrentCharacter: function(): string
|
||||
advanceOffset: function(): string
|
||||
|
@ -30,46 +30,33 @@ end
|
|||
|
||||
|
||||
-- constructors
|
||||
function InputStream:new(): InputStream
|
||||
local mt = setmetatable(
|
||||
{
|
||||
string = "",
|
||||
offset = 1,
|
||||
refill = initial_stream
|
||||
} as InputStream,
|
||||
istream_mt)
|
||||
mt:setRefill(initial_stream)
|
||||
return mt
|
||||
end
|
||||
function InputStream:new(refill: function(): string): InputStream
|
||||
refill = refill or initial_stream
|
||||
local mt = setmetatable(
|
||||
local istream = setmetatable(
|
||||
{
|
||||
string = "",
|
||||
offset = 1,
|
||||
refill = function(): string return "DUMMY" end
|
||||
} as InputStream,
|
||||
istream_mt)
|
||||
mt:setRefill(refill)
|
||||
return mt
|
||||
istream:__setRefill(refill)
|
||||
return istream
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- setters/getters
|
||||
function InputStream:setRefill(func: function(): string)
|
||||
function InputStream:__setRefill(func: function(): string)
|
||||
self.refill = func
|
||||
self:_manageBuffer()
|
||||
end
|
||||
|
||||
--
|
||||
function InputStream:readCurrentCharacter(): string
|
||||
self:_manageBuffer()
|
||||
function InputStream:curr(): string
|
||||
return self.str:sub(self.offset, self.offset)
|
||||
end
|
||||
|
||||
function InputStream:advanceOffset(): string
|
||||
function InputStream:next(): string
|
||||
self:_manageBuffer()
|
||||
local current_char = self.str:sub(self.offset, self.offset)
|
||||
self.offset = self.offset + 1
|
||||
|
|
14
tests.lua
14
tests.lua
|
@ -53,17 +53,15 @@ end
|
|||
describe("InputStream tests", function()
|
||||
local input = istream:new(dummyInput)
|
||||
it("gets a new refill accurately", function()
|
||||
input = istream:new()
|
||||
input:setRefill(dummyInput)
|
||||
input = istream:new(dummyInput)
|
||||
assert.are.equal(input.refill, dummyInput)
|
||||
end)
|
||||
it("reads text properly", function()
|
||||
input = istream:new(dummyInput)
|
||||
input:setRefill(dummyInput)
|
||||
local inputString = dummyInput()
|
||||
for i = 0, 100 do
|
||||
local j = i % #inputString + 1
|
||||
assert.are.same(inputString:sub(j, j), input:advanceOffset())
|
||||
assert.are.same(inputString:sub(j, j), input:advance())
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
@ -88,6 +86,14 @@ describe("Dictionary tests", function()
|
|||
|
||||
dict:define("TEST",wi)
|
||||
end)
|
||||
it("can get WordInfo back after it being defined", function()
|
||||
local env = Environment:new()
|
||||
local dict = Dictionary:new()
|
||||
local wi = WordInfo:new(function(env) end, false)
|
||||
|
||||
dict:define("TEST",wi)
|
||||
assert.are.same(dict:lookup("TEST"),wi)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue