diff --git a/CoreHelpers.tl b/CoreHelpers.tl index ecea583..e76050c 100644 --- a/CoreHelpers.tl +++ b/CoreHelpers.tl @@ -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 diff --git a/InputStream.tl b/InputStream.tl index 4d8db6a..03c9a48 100644 --- a/InputStream.tl +++ b/InputStream.tl @@ -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 diff --git a/tests.lua b/tests.lua index 8368156..04deecb 100644 --- a/tests.lua +++ b/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)