Skip to content

Commit 93449ac

Browse files
committed
Add support for compund statements
1 parent fe43693 commit 93449ac

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

src/prometheus/ast.lua

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ local AstKind = {
2626
PassSelfFunctionCallStatement = "PassSelfFunctionCallStatement";
2727
AssignmentStatement = "AssignmentStatement";
2828

29+
-- LuaU Compound Statements
30+
CompoundAddStatement = "CompoundAddStatement";
31+
CompoundSubStatement = "CompoundSubStatement";
32+
CompoundMulStatement = "CompoundMulStatement";
33+
CompoundDivStatement = "CompoundDivStatement";
34+
CompoundModStatement = "CompoundModStatement";
35+
CompoundPowStatement = "CompoundPowStatement";
36+
CompoundConcatStatement = "CompoundConcatStatement";
37+
2938
-- Assignment Index
3039
AssignmentIndexing = "AssignmentIndexing";
3140
AssignmentVariable = "AssignmentVariable";
@@ -217,6 +226,62 @@ function Ast.AssignmentStatement(lhs, rhs)
217226
}
218227
end
219228

229+
function Ast.CompoundAddStatement(lhs, rhs)
230+
return {
231+
kind = AstKind.CompoundAddStatement,
232+
lhs = lhs,
233+
rhs = rhs,
234+
}
235+
end
236+
237+
function Ast.CompoundSubStatement(lhs, rhs)
238+
return {
239+
kind = AstKind.CompoundSubStatement,
240+
lhs = lhs,
241+
rhs = rhs,
242+
}
243+
end
244+
245+
function Ast.CompoundMulStatement(lhs, rhs)
246+
return {
247+
kind = AstKind.CompoundMulStatement,
248+
lhs = lhs,
249+
rhs = rhs,
250+
}
251+
end
252+
253+
function Ast.CompoundDivStatement(lhs, rhs)
254+
return {
255+
kind = AstKind.CompoundDivStatement,
256+
lhs = lhs,
257+
rhs = rhs,
258+
}
259+
end
260+
261+
function Ast.CompoundPowStatement(lhs, rhs)
262+
return {
263+
kind = AstKind.CompoundPowStatement,
264+
lhs = lhs,
265+
rhs = rhs,
266+
}
267+
end
268+
269+
function Ast.CompoundModStatement(lhs, rhs)
270+
return {
271+
kind = AstKind.CompoundModStatement,
272+
lhs = lhs,
273+
rhs = rhs,
274+
}
275+
end
276+
277+
function Ast.CompoundConcatStatement(lhs, rhs)
278+
return {
279+
kind = AstKind.CompoundConcatStatement,
280+
lhs = lhs,
281+
rhs = rhs,
282+
}
283+
end
284+
220285
function Ast.FunctionCallStatement(base, args)
221286
return {
222287
kind = AstKind.FunctionCallStatement,

src/prometheus/enums.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Enums.Conventions = {
6969
Symbols = {
7070
"+", "-", "*", "/", "%", "^", "#",
7171
"==", "~=", "<=", ">=", "<", ">", "=",
72-
"+=", "-=", "/=", "%=", "^=", "..=",
72+
"+=", "-=", "/=", "%=", "^=", "..=", "*=",
7373
"(", ")", "{", "}", "[", "]",
7474
";", ":", ",", ".", "..", "...",
7575
"::", "->", "?", "|", "&",

src/prometheus/parser.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,45 @@ function Parser:statement(scope, currentLoop)
404404
if(expr.kind == AstKind.VariableExpression) then
405405
expr.kind = AstKind.AssignmentVariable
406406
end
407+
408+
if(self.luaVersion == LuaVersion.LuaU) then
409+
-- LuaU Compound Assignment
410+
if(consume(self, TokenKind.Symbol, "+=")) then
411+
local rhs = self:expression(scope);
412+
return Ast.CompoundAddStatement(expr, rhs);
413+
end
414+
415+
if(consume(self, TokenKind.Symbol, "-=")) then
416+
local rhs = self:expression(scope);
417+
return Ast.CompoundSubStatement(expr, rhs);
418+
end
419+
420+
if(consume(self, TokenKind.Symbol, "*=")) then
421+
local rhs = self:expression(scope);
422+
return Ast.CompoundMulStatement(expr, rhs);
423+
end
424+
425+
if(consume(self, TokenKind.Symbol, "/=")) then
426+
local rhs = self:expression(scope);
427+
return Ast.CompoundDivStatement(expr, rhs);
428+
end
429+
430+
if(consume(self, TokenKind.Symbol, "%=")) then
431+
local rhs = self:expression(scope);
432+
return Ast.CompoundModStatement(expr, rhs);
433+
end
434+
435+
if(consume(self, TokenKind.Symbol, "^=")) then
436+
local rhs = self:expression(scope);
437+
return Ast.CompoundPowStatement(expr, rhs);
438+
end
439+
440+
if(consume(self, TokenKind.Symbol, "..=")) then
441+
local rhs = self:expression(scope);
442+
return Ast.CompoundConcatStatement(expr, rhs);
443+
end
444+
end
445+
407446
local lhs = {
408447
expr
409448
}

src/prometheus/unparser.lua

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local config = require("config");
1515
local Ast = require("prometheus.ast");
1616
local Enums = require("prometheus.enums");
1717
local util = require("prometheus.util");
18+
local logger = require("logger");
1819

1920
local lookupify = util.lookupify;
2021
local LuaVersion = Enums.LuaVersion;
@@ -104,7 +105,7 @@ end
104105

105106
function Unparser:unparse(ast)
106107
if(ast.kind ~= AstKind.TopNode) then
107-
error("Unparser:unparse expects a TopNode as first argument")
108+
logger:error("Unparser:unparse expects a TopNode as first argument")
108109
end
109110

110111
return self:unparseBlock(ast.body);
@@ -381,9 +382,26 @@ function Unparser:unparseStatement(statement, tabbing)
381382
code = code .. "," .. self:optionalWhitespace() .. exprcode;
382383
end
383384
end
384-
-- While Statement
385+
elseif self.luaVersion == LuaVersion.LuaU then
386+
if statement.kind == AstKind.CompoundAddStatement then
387+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "+=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
388+
elseif statement.kind == AstKind.CompoundSubStatement then
389+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "-=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
390+
elseif statement.kind == AstKind.CompoundMulStatement then
391+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "*=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
392+
elseif statement.kind == AstKind.CompoundDivStatement then
393+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "/=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
394+
elseif statement.kind == AstKind.CompoundModStatement then
395+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "%=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
396+
elseif statement.kind == AstKind.CompoundPowStatement then
397+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "^=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
398+
elseif statement.kind == AstKind.CompoundConcatStatement then
399+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "..=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
400+
else
401+
logger:error(string.format("\"%s\" is not a valid unparseable statement in %s!", statement.kind, self.luaVersion));
402+
end
385403
else
386-
error(string.format("\"%s\" is not a valid unparseable statement", statement.kind));
404+
logger:error(string.format("\"%s\" is not a valid unparseable statement in %s!", statement.kind, self.luaVersion));
387405
end
388406

389407
return self:tabs(tabbing, false) .. code;
@@ -839,7 +857,7 @@ function Unparser:unparseExpression(expression, tabbing)
839857
return code .. self:optionalWhitespace((p and "," or "") .. self:newline() .. self:tabs(tabbing)) .. "}";
840858
end
841859

842-
error(string.format("\"%s\" is not a valid unparseable expression", expression.kind));
860+
logger:error(string.format("\"%s\" is not a valid unparseable expression", expression.kind));
843861
end
844862

845863
return Unparser

0 commit comments

Comments
 (0)