Skip to content

Commit

Permalink
Make the compiler support the latest "IfStatement" node structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bytexenon committed Jun 22, 2024
1 parent e739c95 commit 803f894
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions the-tiny-lua-compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1618,29 +1618,27 @@ function InstructionGenerator.generate(ast)
elseif nodeType == "DoBlock" then
processCodeBlock(node.Codeblock)
elseif nodeType == "IfStatement" then
local conditionCodeblockStatements = { { Condition = node.Condition, Codeblock = node.Codeblock } }
for _, elseifNode in ipairs(node.ElseIfs) do
table.insert(conditionCodeblockStatements, elseifNode)
end
local branches = node.Branches
local elseCodeBlock = node.ElseCodeBlock
local jumpToEndInstructions = {}
for index, conditionCodeblockStatement in ipairs(conditionCodeblockStatements) do
local condition = conditionCodeblockStatement.Condition
local codeBlock = conditionCodeblockStatement.Codeblock
for index, branch in ipairs(branches) do
local condition = branch.Condition
local codeBlock = branch.CodeBlock
local conditionRegister = processExpressionNode(condition)
-- OP_TEST [A, C] if not (R(A) <=> C) then pc++
addInstruction("TEST", conditionRegister, 0, 0)
-- OP_JMP [A, sBx] pc+=sBx
local conditionJumpInstruction, conditionJumpInstructionIndex = addInstruction("JMP", 0, 0) -- Placeholder
deallocateRegister(conditionRegister)
processCodeBlock(codeBlock)
if index < #conditionCodeblockStatements or node.ElseCodeblock then
if index < #branches or elseCodeBlock then
-- OP_JMP [A, sBx] pc+=sBx
table.insert(jumpToEndInstructions, { addInstruction("JMP", 0, 0) })
end
conditionJumpInstruction[3] = #code - conditionJumpInstructionIndex
end
if node.ElseCodeblock then
processCodeBlock(node.ElseCodeblock)
if elseCodeBlock then
processCodeBlock(elseCodeBlock)
end

for _, jumpToEndInstruction in ipairs(jumpToEndInstructions) do
Expand Down

0 comments on commit 803f894

Please sign in to comment.