fixed bad code

This commit is contained in:
Starfflame 2021-05-19 01:26:57 -05:00
parent ce6b85a5e7
commit ca93ea00d4
3 changed files with 20 additions and 27 deletions

View File

@ -38,9 +38,9 @@ function CoreHelpers.isWhitespace(chr: string): boolean
chr == "\f") chr == "\f")
end end
function CoreHelpers.skipWhitespace(state: Environment) function CoreHelpers.skipWhitespace(state: Environment)
local chr = state.activeInputStream:readCurrentCharacter() local chr = state.activeInputStream:peek()
while (CoreHelpers.isWhitespace(chr)) do while (CoreHelpers.isWhitespace(chr)) do
chr = state.activeInputStream:advanceOffset() chr = state.activeInputStream:advance()
end end
end end
@ -57,7 +57,7 @@ function CoreHelpers.parseToken(state: Environment): string
local token = "" local token = ""
while(not CoreHelpers.isWhitespace(chr)) do while(not CoreHelpers.isWhitespace(chr)) do
token = token..chr token = token..chr
chr = state.activeInputStream:advanceOffset() chr = state.activeInputStream:next()
end end
return token return token
end end

View File

@ -5,7 +5,7 @@ local type InputStream = record
new: function(InputStream, function(): string): InputStream new: function(InputStream, function(): string): InputStream
setRefill: function(InputStream, function(): string) __setRefill: function(InputStream, function(): string)
readCurrentCharacter: function(): string readCurrentCharacter: function(): string
advanceOffset: function(): string advanceOffset: function(): string
@ -30,46 +30,33 @@ end
-- constructors -- 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 function InputStream:new(refill: function(): string): InputStream
refill = refill or initial_stream refill = refill or initial_stream
local mt = setmetatable( local istream = setmetatable(
{ {
string = "", string = "",
offset = 1, offset = 1,
refill = function(): string return "DUMMY" end
} as InputStream, } as InputStream,
istream_mt) istream_mt)
mt:setRefill(refill) istream:__setRefill(refill)
return mt return istream
end end
-- setters/getters -- setters/getters
function InputStream:setRefill(func: function(): string) function InputStream:__setRefill(func: function(): string)
self.refill = func self.refill = func
self:_manageBuffer() self:_manageBuffer()
end end
-- --
function InputStream:readCurrentCharacter(): string function InputStream:curr(): string
self:_manageBuffer()
return self.str:sub(self.offset, self.offset) return self.str:sub(self.offset, self.offset)
end end
function InputStream:advanceOffset(): string function InputStream:next(): string
self:_manageBuffer() self:_manageBuffer()
local current_char = self.str:sub(self.offset, self.offset) local current_char = self.str:sub(self.offset, self.offset)
self.offset = self.offset + 1 self.offset = self.offset + 1

View File

@ -53,17 +53,15 @@ end
describe("InputStream tests", function() describe("InputStream tests", function()
local input = istream:new(dummyInput) local input = istream:new(dummyInput)
it("gets a new refill accurately", function() it("gets a new refill accurately", function()
input = istream:new() input = istream:new(dummyInput)
input:setRefill(dummyInput)
assert.are.equal(input.refill, dummyInput) assert.are.equal(input.refill, dummyInput)
end) end)
it("reads text properly", function() it("reads text properly", function()
input = istream:new(dummyInput) input = istream:new(dummyInput)
input:setRefill(dummyInput)
local inputString = dummyInput() local inputString = dummyInput()
for i = 0, 100 do for i = 0, 100 do
local j = i % #inputString + 1 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) end)
end) end)
@ -88,6 +86,14 @@ describe("Dictionary tests", function()
dict:define("TEST",wi) dict:define("TEST",wi)
end) 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) end)