diff options
author | Tim-Philipp Müller <tim@centricular.com> | 2016-03-19 17:57:11 +0000 |
---|---|---|
committer | Tim-Philipp Müller <tim@centricular.com> | 2016-03-19 17:57:11 +0000 |
commit | 3eea1703ffb7559cc7d7454ced119aa45c167c9f (patch) | |
tree | c122e871e6db75250f0d4ad776cdfe3989e8ed27 /mesonbuild/interpreter.py | |
parent | dc049660e7d5e7c4b4a9ded4acb02fe90860adfb (diff) | |
download | meson-3eea1703ffb7559cc7d7454ced119aa45c167c9f.zip meson-3eea1703ffb7559cc7d7454ced119aa45c167c9f.tar.gz meson-3eea1703ffb7559cc7d7454ced119aa45c167c9f.tar.bz2 |
Add bool to_string() and to_int() methods
bool to_int() will return 0 or 1, useful if one wants to set
a define to 0 or 1 based on a boolean result instead of having
it just defined or undefined.
bool to_string() will return 'true' or 'false' by default same
as when using it to format a string, but with the additional
possibility to specify two extra string arguments to be returned
as true/false values, e.g. to_string('yes', 'no'). This can be
useful when outputting messages to be shown to the user.
Diffstat (limited to 'mesonbuild/interpreter.py')
-rw-r--r-- | mesonbuild/interpreter.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 35cbc3e..d7cf837 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -2017,6 +2017,30 @@ class Interpreter(): reduced_pos = [reduced_pos] return (reduced_pos, reduced_kw) + def bool_method_call(self, obj, method_name, args): + obj = self.to_native(obj) + (posargs, _) = self.reduce_arguments(args) + if method_name == 'to_string': + if len(posargs) == 0: + if obj == True: + return 'true' + else: + return 'false' + elif len(posargs) == 2 and isinstance(posargs[0], str) and isinstance(posargs[1], str): + if obj == True: + return posargs[0] + else: + return posargs[1] + else: + raise InterpreterException('bool.to_string() must have either no arguments or exactly two string arguments.') + elif method_name == 'to_int': + if obj == True: + return 1 + else: + return 0 + else: + raise InterpreterException('Unknown method "%s" for a boolean.' % method_name) + def string_method_call(self, obj, method_name, args): obj = self.to_native(obj) (posargs, _) = self.reduce_arguments(args) @@ -2089,6 +2113,8 @@ class Interpreter(): obj = obj.get_value() if isinstance(obj, str): 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, list): return self.array_method_call(obj, method_name, self.reduce_arguments(args)[0]) if not isinstance(obj, InterpreterObject): |