| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2021 The Meson development team |
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import typing as T |
| 6 |
|
| 7 |
from ...interpreterbase import ( |
| 8 |
InterpreterObject, |
| 9 |
IterableObject, |
| 10 |
KwargInfo, |
| 11 |
MesonOperator, |
| 12 |
ObjectHolder, |
| 13 |
typed_operator, |
| 14 |
noKwargs, |
| 15 |
noPosargs, |
| 16 |
noArgsFlattening, |
| 17 |
typed_kwargs, |
| 18 |
typed_pos_args, |
| 19 |
FeatureNew, |
| 20 |
|
| 21 |
TYPE_var, |
| 22 |
|
| 23 |
InvalidArguments, |
| 24 |
) |
| 25 |
from ...mparser import PlusAssignmentNode |
| 26 |
|
| 27 |
if T.TYPE_CHECKING: |
| 28 |
from ...interpreterbase import TYPE_kwargs |
| 29 |
|
| 30 |
class ArrayHolder(ObjectHolder[T.List[TYPE_var]], IterableObject): |
| 31 |
# Operators that only require type checks |
| 32 |
TRIVIAL_OPERATORS = { |
| 33 |
MesonOperator.EQUALS: (list, lambda obj, x: obj.held_object == x), |
| 34 |
MesonOperator.NOT_EQUALS: (list, lambda obj, x: obj.held_object != x), |
| 35 |
MesonOperator.IN: (object, lambda obj, x: x in obj.held_object), |
| 36 |
MesonOperator.NOT_IN: (object, lambda obj, x: x not in obj.held_object), |
| 37 |
} |
| 38 |
|
| 39 |
def display_name(self) -> str: |
| 40 |
return 'array' |
| 41 |
|
| 42 |
def iter_tuple_size(self) -> None: |
| 43 |
return None |
| 44 |
|
| 45 |
def iter_self(self) -> T.Iterator[TYPE_var]: |
| 46 |
return iter(self.held_object) |
| 47 |
|
| 48 |
def size(self) -> int: |
| 49 |
return len(self.held_object) |
| 50 |
|
| 51 |
@noArgsFlattening |
| 52 |
@noKwargs |
| 53 |
@typed_pos_args('array.contains', object) |
| 54 |
@InterpreterObject.method('contains') |
| 55 |
def contains_method(self, args: T.Tuple[object], kwargs: TYPE_kwargs) -> bool: |
| 56 |
def check_contains(el: T.List[TYPE_var]) -> bool: |
| 57 |
for element in el: |
| 58 |
if isinstance(element, list): |
| 59 |
found = check_contains(element) |
| 60 |
if found: |
| 61 |
return True |
| 62 |
if element == args[0]: |
| 63 |
return True |
| 64 |
return False |
| 65 |
return check_contains(self.held_object) |
| 66 |
|
| 67 |
@noKwargs |
| 68 |
@noPosargs |
| 69 |
@InterpreterObject.method('length') |
| 70 |
def length_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> int: |
| 71 |
return len(self.held_object) |
| 72 |
|
| 73 |
@noArgsFlattening |
| 74 |
@noKwargs |
| 75 |
@typed_pos_args('array.get', int, optargs=[object]) |
| 76 |
@InterpreterObject.method('get') |
| 77 |
def get_method(self, args: T.Tuple[int, T.Optional[TYPE_var]], kwargs: TYPE_kwargs) -> TYPE_var: |
| 78 |
index = args[0] |
| 79 |
if index < -len(self.held_object) or index >= len(self.held_object): |
| 80 |
if args[1] is None: |
| 81 |
raise InvalidArguments(f'Array index {index} is out of bounds for array of size {len(self.held_object)}.') |
| 82 |
return args[1] |
| 83 |
return self.held_object[index] |
| 84 |
|
| 85 |
@FeatureNew('array.slice', '1.10.0') |
| 86 |
@typed_kwargs('array.slice', KwargInfo('step', int, default=1)) |
| 87 |
@typed_pos_args('array.slice', optargs=[int, int]) |
| 88 |
@InterpreterObject.method('slice') |
| 89 |
def slice_method(self, args: T.Tuple[T.Optional[int], T.Optional[int]], kwargs: T.Dict[str, int]) -> TYPE_var: |
| 90 |
start, stop = args |
| 91 |
if start is not None and stop is None: |
| 92 |
raise InvalidArguments('Providing only one positional slice argument is ambiguous.') |
| 93 |
if kwargs['step'] == 0: |
| 94 |
raise InvalidArguments('Slice step cannot be zero.') |
| 95 |
return self.held_object[start:stop:kwargs['step']] |
| 96 |
|
| 97 |
@typed_operator(MesonOperator.PLUS, object) |
| 98 |
@InterpreterObject.operator(MesonOperator.PLUS) |
| 99 |
def op_plus(self, other: TYPE_var) -> T.List[TYPE_var]: |
| 100 |
if not isinstance(other, list): |
| 101 |
if not isinstance(self.current_node, PlusAssignmentNode): |
| 102 |
FeatureNew.single_use('list.<plus>', '0.60.0', self.subproject, 'The right hand operand was not a list.', |
| 103 |
location=self.current_node) |
| 104 |
other = [other] |
| 105 |
return self.held_object + other |
| 106 |
|
| 107 |
@typed_operator(MesonOperator.INDEX, int) |
| 108 |
@InterpreterObject.operator(MesonOperator.INDEX) |
| 109 |
def op_index(self, other: int) -> TYPE_var: |
| 110 |
try: |
| 111 |
return self.held_object[other] |
| 112 |
except IndexError: |
| 113 |
raise InvalidArguments(f'Index {other} out of bounds of array of size {len(self.held_object)}.') |
| 114 |
|
| 115 |
@noPosargs |
| 116 |
@noKwargs |
| 117 |
@FeatureNew('array.flatten', '1.9.0') |
| 118 |
@InterpreterObject.method('flatten') |
| 119 |
def flatten_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> TYPE_var: |
| 120 |
def flatten(obj: TYPE_var) -> T.Iterable[TYPE_var]: |
| 121 |
if isinstance(obj, list): |
| 122 |
for o in obj: |
| 123 |
yield from flatten(o) |
| 124 |
else: |
| 125 |
yield obj |
| 126 |
|
| 127 |
return list(flatten(self.held_object)) |