Skip to content

Commit c113e04

Browse files
committed
refactor(codegenerator): simplify block processing and introduce function body handling
1 parent 7e1dad9 commit c113e04

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

the-tiny-lua-compiler.lua

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,16 +3042,28 @@ function CodeGenerator:processStatementList(statementList)
30423042
end
30433043
end
30443044

3045-
function CodeGenerator:processBlockNode(blockNode, variables, isFunction)
3046-
self:enterScope(isFunction)
3047-
if variables then
3048-
-- Declare variables while in the scope.
3049-
self:declareLocalVariables(variables)
3050-
end
3045+
function CodeGenerator:processBlockNode(blockNode)
3046+
self:enterScope()
30513047
self:processStatementList(blockNode.Statements)
30523048
self:leaveScope()
30533049
end
30543050

3051+
function CodeGenerator:processFunctionBody(node)
3052+
self:enterScope(true)
3053+
for _, paramName in ipairs(node.Parameters) do
3054+
self:declareLocalVariable(paramName)
3055+
end
3056+
3057+
if node.IsVararg then
3058+
-- Declare implicit "arg" variable to hold vararg values.
3059+
self:declareLocalVariable("arg")
3060+
end
3061+
3062+
self:processStatementList(node.Body.Statements)
3063+
self:emitInstruction("RETURN", 0, 1, 0) -- Default return statement
3064+
self:leaveScope()
3065+
end
3066+
30553067
-- Processes and compiles a function node into a function prototype.
30563068
--
30573069
-- functionNode - The AST node representing the function.
@@ -3064,8 +3076,7 @@ function CodeGenerator:processFunction(functionNode, closureRegister)
30643076
numParams = #functionNode.Parameters,
30653077
})
30663078
self.proto = childProto
3067-
self:processBlockNode(functionNode.Body, functionNode.Parameters, true)
3068-
self:emitInstruction("RETURN", 0, 1, 0) -- Default return statement
3079+
self:processFunctionBody(functionNode)
30693080
self.proto = parentProto
30703081

30713082
if closureRegister then

0 commit comments

Comments
 (0)