More comparison operators. Closes #363.

pull/373/head
Jussi Pakkanen 9 years ago
parent 755d22afbd
commit 4cd671e69e
  1. 12
      mesonbuild/interpreter.py
  2. 21
      mesonbuild/mparser.py
  3. 12
      test cases/common/68 number arithmetic/meson.build

@ -1,4 +1,4 @@
# Copyright 2012-2015 The Meson development team
# Copyright 2012-2016 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -2181,8 +2181,16 @@ class Interpreter():
return val1 == val2
elif node.ctype == '!=':
return val1 != val2
elif node.ctype == '<':
return val1 < val2
elif node.ctype == '<=':
return val1 <= val2
elif node.ctype == '>':
return val1 > val2
elif node.ctype == '>=':
return val1 >= val2
else:
raise InvalidCode('You broke me.')
raise InvalidCode('You broke my compare eval.')
def evaluate_andstatement(self, cur):
l = self.evaluate_statement(cur.left)

@ -1,4 +1,4 @@
# Copyright 2014-2015 The Meson development team
# Copyright 2014-2016 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -63,6 +63,10 @@ class Lexer:
('equal', re.compile(r'==')),
('nequal', re.compile(r'\!=')),
('assign', re.compile(r'=')),
('le', re.compile(r'<=')),
('lt', re.compile(r'<')),
('ge', re.compile(r'>=')),
('gt', re.compile(r'>')),
]
def lex(self, code):
@ -313,6 +317,14 @@ class ArgumentNode():
def __len__(self):
return self.num_args() # Fixme
comparison_map = {'equal': '==',
'nequal': '!=',
'lt': '<',
'le': '<=',
'gt': '>',
'ge': '>='
}
# Recursive descent parser for Meson's definition language.
# Very basic apart from the fact that we have many precedence
# levels so there are not enough words to describe them all.
@ -387,10 +399,9 @@ class Parser:
def e4(self):
left = self.e5()
if self.accept('equal'):
return ComparisonNode(left.lineno, left.colno, '==', left, self.e5())
if self.accept('nequal'):
return ComparisonNode(left.lineno, left.colno, '!=', left, self.e5())
for nodename, operator_type in comparison_map.items():
if self.accept(nodename):
return ComparisonNode(left.lineno, left.colno, operator_type, left, self.e5())
return left
def e5(self):

@ -20,3 +20,15 @@ endif
if (5 / 3) * 3 != 3
error('Integer division is broken')
endif
assert(3 < 4, 'Lt broken')
assert(not(4 < 3), 'Lt broken')
assert(3 <= 4, 'Lte broken')
assert(not(4 <= 3), 'Lte broken')
assert(3 <= 3, 'Lte broken')
assert(4 > 3, 'Gt broken')
assert(not(3 > 4), 'Gt broken')
assert(4 >= 3, 'Gte broken')
assert(not(3 >= 4), 'Gte broken')
assert(3 >= 3, 'Gte broken')

Loading…
Cancel
Save