Debian Codegraph @

mesonbuild/interpreter/primitives/boolean.py

raw
1 # Copyright 2021 The Meson development team
2 # SPDX-License-Identifier: Apache-2.0
3 from __future__ import annotations
4
5 from ...interpreterbase import (
6 InterpreterObject,
7 MesonOperator,
8 ObjectHolder,
9 typed_pos_args,
10 noKwargs,
11 noPosargs,
12
13 InvalidArguments
14 )
15
16 import typing as T
17
18 if T.TYPE_CHECKING:
19 from ...interpreterbase import TYPE_var, TYPE_kwargs
20
21 class BooleanHolder(ObjectHolder[bool]):
22 TRIVIAL_OPERATORS = {
23 MesonOperator.BOOL: (None, lambda obj, x: obj.held_object),
24 MesonOperator.NOT: (None, lambda obj, x: not obj.held_object),
25 MesonOperator.EQUALS: (bool, lambda obj, x: obj.held_object == x),
26 MesonOperator.NOT_EQUALS: (bool, lambda obj, x: obj.held_object != x),
27 }
28
29 def display_name(self) -> str:
30 return 'bool'
31
32 @noKwargs
33 @noPosargs
34 @InterpreterObject.method('to_int')
35 def to_int_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> int:
36 return 1 if self.held_object else 0
37
38 @noKwargs
39 @typed_pos_args('bool.to_string', optargs=[str, str])
40 @InterpreterObject.method('to_string')
41 def to_string_method(self, args: T.Tuple[T.Optional[str], T.Optional[str]], kwargs: TYPE_kwargs) -> str:
42 true_str = args[0] or 'true'
43 false_str = args[1] or 'false'
44 if any(x is not None for x in args) and not all(x is not None for x in args):
45 raise InvalidArguments('bool.to_string() must have either no arguments or exactly two string arguments that signify what values to return for true and false.')
46 return true_str if self.held_object else false_str