Skip to content

Commit

Permalink
refactor: optimize whitespace consumption and register allocation
Browse files Browse the repository at this point in the history
- Removed unnecessary table creation and concatenation in `consumeWhitespace`.
- Simplified `allocateRegister` function to improve readability and efficiency.
  • Loading branch information
bytexenon committed Aug 22, 2024
1 parent 073723e commit 9f3c0f4
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions the-tiny-lua-compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,9 @@ function Tokenizer.tokenize(code)

--// CONSUMERS //--
local function consumeWhitespace()
local whitespace = { curChar }
while isWhitespace(lookAhead()) do
table.insert(whitespace, curChar)
consume()
end
return table.concat(whitespace)
end
local function consumeIdentifier()
local identifier = { curChar }
Expand Down Expand Up @@ -1295,13 +1292,12 @@ function InstructionGenerator.generate(ast)

--// REGISTER MANAGEMENT //--
local function allocateRegister()
for i = 0, 255 do
if not takenRegisters[i] then
takenRegisters[i] = true
return i
end
local newRegister = (takenRegisters[0] and #takenRegisters + 1) or 0
if newRegister > 255 then
error("Out of registers")
end
error("Out of registers")
takenRegisters[newRegister] = true
return newRegister
end
local function deallocateRegister(register)
takenRegisters[register] = nil
Expand Down

0 comments on commit 9f3c0f4

Please sign in to comment.