Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit dd9cbdd

Browse files
committed
expressions: add mul (*), div (/) and modulo (%)
Stripped non-informative comments.
1 parent d737ed0 commit dd9cbdd

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

graphql/expressions.lua

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,18 @@ local unary_minus = P('-')
4949
local unary_plus = P('+')
5050

5151
-- Possible binary operator patterns:
52-
-- 1) Logical and.
5352
local logic_and = P('&&')
54-
-- 2) logical or.
5553
local logic_or = P('||')
56-
-- 3) +
5754
local addition = P('+')
58-
-- 4) -
5955
local subtraction = P('-')
60-
-- 5) ==
56+
local multiplication = P('*')
57+
local division = P('/')
58+
local modulo = P('%')
6159
local eq = P('==')
62-
-- 6) !=
6360
local not_eq = P('!=')
64-
-- 7) >
6561
local gt = P('>')
66-
-- 8) >=
6762
local ge = P('>=')
68-
-- 9) <
6963
local lt = P('<')
70-
-- 10) <=
7164
local le = P('<=')
7265

7366
-- AST nodes generating functions.
@@ -161,7 +154,8 @@ local _literal = _bool + _number + _string
161154
local _logic_or = logic_or / op_name
162155
local _logic_and = logic_and / op_name
163156
local _comparison_op = (eq + not_eq + ge + gt + le + lt) / op_name
164-
local _arithmetic_op = (addition + subtraction) / op_name
157+
local _arithmetic_sum_op = (addition + subtraction) / op_name
158+
local _arithmetic_mul_op = (multiplication + division + modulo) / op_name
165159
local _unary_op = (negation + unary_minus + unary_plus) / op_name
166160
local _functions = (is_null + is_not_null + regexp) / identical
167161

@@ -176,10 +170,13 @@ local expression_grammar = P {
176170
spaces * V('log_expr_and')) ^ 0 / bin_op_node,
177171
log_expr_and = V('comparison') * (spaces * _logic_and * spaces *
178172
V('comparison')) ^ 0 / bin_op_node,
179-
comparison = V('arithmetic_expr') * (spaces * _comparison_op * spaces *
180-
V('arithmetic_expr')) ^ 0 / bin_op_node,
181-
arithmetic_expr = V('unary_expr') * (spaces * _arithmetic_op * spaces *
182-
V('unary_expr')) ^ 0 / bin_op_node,
173+
comparison = V('arithmetic_sum_expr') * (spaces * _comparison_op * spaces *
174+
V('arithmetic_sum_expr')) ^ 0 / bin_op_node,
175+
arithmetic_sum_expr = V('arithmetic_mul_expr') * (spaces *
176+
_arithmetic_sum_op * spaces *
177+
V('arithmetic_mul_expr')) ^ 0 / bin_op_node,
178+
arithmetic_mul_expr = V('unary_expr') * (spaces * _arithmetic_mul_op *
179+
spaces * V('unary_expr')) ^ 0 / bin_op_node,
183180

184181
unary_expr = (_unary_op * V('first_prio') / unary_op_node) +
185182
(V('first_prio') / identical_node),
@@ -262,27 +259,21 @@ local function execute_node(node, context)
262259
end
263260
return field
264261
elseif node.kind == 'func' then
265-
-- regexp() implementation.
266262
if node.name == 'regexp' then
267263
return utils.regexp(execute_node(node.args[1], context),
268264
execute_node(node.args[2], context))
269-
-- is_null() implementation.
270265
elseif node.name == 'is_null' then
271266
return execute_node(node.args[1], context) == nil
272-
-- is_not_null() implementation.
273267
elseif node.name == 'is_not_null' then
274268
return execute_node(node.args[1], context) ~= nil
275269
else
276270
error('Unknown func name: ' .. tostring(node.name))
277271
end
278272
elseif node.kind == 'unary_operation' then
279-
-- Negation.
280273
if node.op == '!' then
281274
return not execute_node(node.node, context)
282-
-- Unary '+'.
283275
elseif node.op == '+' then
284276
return execute_node(node.node, context)
285-
-- Unary '-'.
286277
elseif node.op == '-' then
287278
return -execute_node(node.node, context)
288279
else
@@ -293,34 +284,30 @@ local function execute_node(node, context)
293284
local left = execute_node(node.left, context)
294285
local right = execute_node(node.right, context)
295286

296-
-- Sum.
297287
if op == '+' then
298288
return sum(left, right)
299-
-- Subtraction.
300289
elseif op == '-' then
301290
return subtract(left, right)
302-
-- Logical and.
291+
elseif op == '*' then
292+
return left * right
293+
elseif op == '/' then
294+
return left / right
295+
elseif op == '%' then
296+
return left % right
303297
elseif op == '&&' then
304298
return left and right
305-
-- Logical or.
306299
elseif op == '||' then
307300
return left or right
308-
-- Equal.
309301
elseif op == '==' then
310302
return left == right
311-
-- Not equal.
312303
elseif op == '!=' then
313304
return left ~= right
314-
-- Greater than.
315305
elseif op == '>' then
316306
return left > right
317-
-- Greater or equal.
318307
elseif op == '>=' then
319308
return left >= right
320-
-- Lower than.
321309
elseif op == '<' then
322310
return left < right
323-
-- Lower or equal.
324311
elseif op == '<=' then
325312
return left <= right
326313
else

test/unit/expr_execute.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ local variables = {
2828

2929
-- case: complex
3030
local case_name = 'complex'
31-
local str = 'true && ($variable + 7 == field.path_1 && !(2399>23941)) && !false'
31+
local str = 'true && ($variable + 1 * 7 == field.path_1 && !(2399>23941)) && !false'
3232
local exp_result = true
3333
local expr = expressions.new(str)
3434
test:is(expr:execute(object, variables), exp_result, case_name)

test/unit/expr_tree.test.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ local binary_operators = {
2323
'&&',
2424
'+',
2525
'-',
26+
'*',
27+
'/',
28+
'%',
2629
'==',
2730
'!=',
2831
'>',
@@ -31,7 +34,7 @@ local binary_operators = {
3134
'<=',
3235
}
3336

34-
test:plan(1046)
37+
test:plan(1340)
3538

3639
-- Bunch of tests for all the sets of binary operator and its
3740
-- operands.

0 commit comments

Comments
 (0)