aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2016-09-02 18:02:37 +0100
committerEmmanuele Bassi <ebassi@gnome.org>2016-09-02 18:52:45 +0100
commit2dd1ec6f8c82dc9a2d97c246664e92246a5af95b (patch)
tree9c27384bbdcac3ff5bf2be093c7e3759a14fa2e3
parent00e5962aaada57215eaf32879041f92004e011d8 (diff)
downloadmeson-2dd1ec6f8c82dc9a2d97c246664e92246a5af95b.zip
meson-2dd1ec6f8c82dc9a2d97c246664e92246a5af95b.tar.gz
meson-2dd1ec6f8c82dc9a2d97c246664e92246a5af95b.tar.bz2
Add is_even() and is_odd() integer methods
Convenience methods for modulo operations involving even and odd numbers.
-rw-r--r--mesonbuild/interpreter.py18
-rw-r--r--test cases/common/68 number arithmetic/meson.build2
2 files changed, 20 insertions, 0 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index 535a3f0..abbeaf7 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -2293,6 +2293,22 @@ class Interpreter():
else:
raise InterpreterException('Unknown method "%s" for a boolean.' % method_name)
+ def int_method_call(self, obj, method_name, args):
+ obj = self.to_native(obj)
+ (posargs, _) = self.reduce_arguments(args)
+ if method_name == 'is_even':
+ if len(posargs) == 0:
+ return obj % 2 == 0
+ else:
+ raise InterpreterException('int.is_even() must have no arguments.')
+ elif method_name == 'is_odd':
+ if len(posargs) == 0:
+ return obj % 2 != 0
+ else:
+ raise InterpreterException('int.is_odd() must have no arguments.')
+ else:
+ raise InterpreterException('Unknown method "%s" for an integer.' % method_name)
+
def string_method_call(self, obj, method_name, args):
obj = self.to_native(obj)
(posargs, _) = self.reduce_arguments(args)
@@ -2379,6 +2395,8 @@ class Interpreter():
return self.string_method_call(obj, method_name, args)
if isinstance(obj, bool):
return self.bool_method_call(obj, method_name, args)
+ if isinstance(obj, int):
+ return self.int_method_call(obj, method_name, args)
if isinstance(obj, list):
return self.array_method_call(obj, method_name, self.reduce_arguments(args)[0])
if not isinstance(obj, InterpreterObject):
diff --git a/test cases/common/68 number arithmetic/meson.build b/test cases/common/68 number arithmetic/meson.build
index 1bc8155..9ecfc19 100644
--- a/test cases/common/68 number arithmetic/meson.build
+++ b/test cases/common/68 number arithmetic/meson.build
@@ -23,6 +23,8 @@ endif
assert((5 % 2) == 1, 'Integer modulo (odd) is broken')
assert((4 % 2) == 0, 'Integer modulo (even) is broken')
+assert(2.is_even() == 1, 'int is_even() broken')
+assert(2.is_odd() == 0, 'int is_odd() broken')
assert(3 < 4, 'Lt broken')
assert(not(4 < 3), 'Lt broken')