@@ -49,25 +49,18 @@ local unary_minus = P('-')
49
49
local unary_plus = P (' +' )
50
50
51
51
-- Possible binary operator patterns:
52
- -- 1) Logical and.
53
52
local logic_and = P (' &&' )
54
- -- 2) logical or.
55
53
local logic_or = P (' ||' )
56
- -- 3) +
57
54
local addition = P (' +' )
58
- -- 4) -
59
55
local subtraction = P (' -' )
60
- -- 5) ==
56
+ local multiplication = P (' *' )
57
+ local division = P (' /' )
58
+ local modulo = P (' %' )
61
59
local eq = P (' ==' )
62
- -- 6) !=
63
60
local not_eq = P (' !=' )
64
- -- 7) >
65
61
local gt = P (' >' )
66
- -- 8) >=
67
62
local ge = P (' >=' )
68
- -- 9) <
69
63
local lt = P (' <' )
70
- -- 10) <=
71
64
local le = P (' <=' )
72
65
73
66
-- AST nodes generating functions.
@@ -161,7 +154,8 @@ local _literal = _bool + _number + _string
161
154
local _logic_or = logic_or / op_name
162
155
local _logic_and = logic_and / op_name
163
156
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
165
159
local _unary_op = (negation + unary_minus + unary_plus ) / op_name
166
160
local _functions = (is_null + is_not_null + regexp ) / identical
167
161
@@ -176,10 +170,13 @@ local expression_grammar = P {
176
170
spaces * V (' log_expr_and' )) ^ 0 / bin_op_node ,
177
171
log_expr_and = V (' comparison' ) * (spaces * _logic_and * spaces *
178
172
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 ,
183
180
184
181
unary_expr = (_unary_op * V (' first_prio' ) / unary_op_node ) +
185
182
(V (' first_prio' ) / identical_node ),
@@ -262,27 +259,21 @@ local function execute_node(node, context)
262
259
end
263
260
return field
264
261
elseif node .kind == ' func' then
265
- -- regexp() implementation.
266
262
if node .name == ' regexp' then
267
263
return utils .regexp (execute_node (node .args [1 ], context ),
268
264
execute_node (node .args [2 ], context ))
269
- -- is_null() implementation.
270
265
elseif node .name == ' is_null' then
271
266
return execute_node (node .args [1 ], context ) == nil
272
- -- is_not_null() implementation.
273
267
elseif node .name == ' is_not_null' then
274
268
return execute_node (node .args [1 ], context ) ~= nil
275
269
else
276
270
error (' Unknown func name: ' .. tostring (node .name ))
277
271
end
278
272
elseif node .kind == ' unary_operation' then
279
- -- Negation.
280
273
if node .op == ' !' then
281
274
return not execute_node (node .node , context )
282
- -- Unary '+'.
283
275
elseif node .op == ' +' then
284
276
return execute_node (node .node , context )
285
- -- Unary '-'.
286
277
elseif node .op == ' -' then
287
278
return - execute_node (node .node , context )
288
279
else
@@ -293,34 +284,30 @@ local function execute_node(node, context)
293
284
local left = execute_node (node .left , context )
294
285
local right = execute_node (node .right , context )
295
286
296
- -- Sum.
297
287
if op == ' +' then
298
288
return sum (left , right )
299
- -- Subtraction.
300
289
elseif op == ' -' then
301
290
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
303
297
elseif op == ' &&' then
304
298
return left and right
305
- -- Logical or.
306
299
elseif op == ' ||' then
307
300
return left or right
308
- -- Equal.
309
301
elseif op == ' ==' then
310
302
return left == right
311
- -- Not equal.
312
303
elseif op == ' !=' then
313
304
return left ~= right
314
- -- Greater than.
315
305
elseif op == ' >' then
316
306
return left > right
317
- -- Greater or equal.
318
307
elseif op == ' >=' then
319
308
return left >= right
320
- -- Lower than.
321
309
elseif op == ' <' then
322
310
return left < right
323
- -- Lower or equal.
324
311
elseif op == ' <=' then
325
312
return left <= right
326
313
else
0 commit comments