Skip to content

Commit c5f103b

Browse files
committed
Add support for Compound Statements
1 parent 26191bc commit c5f103b

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/prometheus/compiler/compiler.lua

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,10 @@ function Compiler:registerAssignment(scope, id)
848848
end
849849

850850
-- Maybe convert ids to strings
851-
function Compiler:setRegister(scope, id, val)
851+
function Compiler:setRegister(scope, id, val, compundArg)
852+
if(compundArg) then
853+
return compundArg(self:registerAssignment(scope, id), val);
854+
end
852855
return Ast.AssignmentStatement({
853856
self:registerAssignment(scope, id)
854857
}, {
@@ -941,8 +944,11 @@ function Compiler:returnAssignment(scope)
941944
return Ast.AssignmentVariable(self.containerFuncScope, self.returnVar);
942945
end
943946

944-
function Compiler:setUpvalueMember(scope, idExpr, valExpr)
947+
function Compiler:setUpvalueMember(scope, idExpr, valExpr, compoundConstructor)
945948
scope:addReferenceToHigherScope(self.scope, self.upvaluesTable);
949+
if compoundConstructor then
950+
return compoundConstructor(Ast.AssignmentIndexing(Ast.VariableExpression(self.scope, self.upvaluesTable), idExpr), valExpr);
951+
end
946952
return Ast.AssignmentStatement({Ast.AssignmentIndexing(Ast.VariableExpression(self.scope, self.upvaluesTable), idExpr)}, {valExpr});
947953
end
948954

@@ -1758,6 +1764,55 @@ function Compiler:compileStatement(statement, funcDepth)
17581764
return;
17591765
end
17601766

1767+
-- Compund Statements
1768+
local compoundConstructors = {
1769+
[AstKind.CompoundAddStatement] = Ast.CompoundAddStatement,
1770+
[AstKind.CompoundSubStatement] = Ast.CompoundSubStatement,
1771+
[AstKind.CompoundMulStatement] = Ast.CompoundMulStatement,
1772+
[AstKind.CompoundDivStatement] = Ast.CompoundDivStatement,
1773+
[AstKind.CompoundModStatement] = Ast.CompoundModStatement,
1774+
[AstKind.CompoundPowStatement] = Ast.CompoundPowStatement,
1775+
[AstKind.CompoundConcatStatement] = Ast.CompoundConcatStatement,
1776+
}
1777+
if compoundConstructors[statement.kind] then
1778+
local compoundConstructor = compoundConstructors[statement.kind];
1779+
if statement.lhs.kind == AstKind.AssignmentIndexing then
1780+
local indexing = statement.lhs;
1781+
local baseReg = self:compileExpression(indexing.base, funcDepth, 1)[1];
1782+
local indexReg = self:compileExpression(indexing.index, funcDepth, 1)[1];
1783+
local valueReg = self:compileExpression(statement.rhs, funcDepth, 1)[1];
1784+
1785+
self:addStatement(compoundConstructor(Ast.AssignmentIndexing(self:register(scope, baseReg), self:register(scope, indexReg)), self:register(scope, valueReg)), {}, {baseReg, indexReg, valueReg}, true);
1786+
else
1787+
local valueReg = self:compileExpression(statement.rhs, funcDepth, 1)[1];
1788+
local primaryExpr = statement.lhs;
1789+
if primaryExpr.scope.isGlobal then
1790+
local tmpReg = self:allocRegister(false);
1791+
self:addStatement(self:setRegister(scope, tmpReg, Ast.StringExpression(primaryExpr.scope:getVariableName(primaryExpr.id))), {tmpReg}, {}, false);
1792+
self:addStatement(Ast.AssignmentStatement({Ast.AssignmentIndexing(self:env(scope), self:register(scope, tmpReg))},
1793+
{self:register(scope, valueReg)}), {}, {tmpReg, valueReg}, true);
1794+
self:freeRegister(tmpReg, false);
1795+
else
1796+
if self.scopeFunctionDepths[primaryExpr.scope] == funcDepth then
1797+
if self:isUpvalue(primaryExpr.scope, primaryExpr.id) then
1798+
local reg = self:getVarRegister(primaryExpr.scope, primaryExpr.id, funcDepth);
1799+
self:addStatement(self:setUpvalueMember(scope, self:register(scope, reg), self:register(scope, valueReg), compoundConstructor), {}, {reg, valueReg}, true);
1800+
else
1801+
local reg = self:getVarRegister(primaryExpr.scope, primaryExpr.id, funcDepth, valueReg);
1802+
if reg ~= valueReg then
1803+
self:addStatement(self:setRegister(scope, reg, self:register(scope, valueReg), compoundConstructor), {reg}, {valueReg}, false);
1804+
end
1805+
end
1806+
else
1807+
local upvalId = self:getUpvalueId(primaryExpr.scope, primaryExpr.id);
1808+
scope:addReferenceToHigherScope(self.containerFuncScope, self.currentUpvaluesVar);
1809+
self:addStatement(self:setUpvalueMember(scope, Ast.IndexExpression(Ast.VariableExpression(self.containerFuncScope, self.currentUpvaluesVar), Ast.NumberExpression(upvalId)), self:register(scope, valueReg), compoundConstructor), {}, {valueReg}, true);
1810+
end
1811+
end
1812+
end
1813+
return;
1814+
end
1815+
17611816
logger:error(string.format("%s is not a compileable statement!", statement.kind));
17621817
end
17631818

0 commit comments

Comments
 (0)