| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2021 The Meson development team |
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from ...interpreterbase import ( |
| 6 |
InterpreterObject, MesonOperator, ObjectHolder, |
| 7 |
FeatureBroken, InvalidArguments, KwargInfo, |
| 8 |
noKwargs, noPosargs, typed_operator, typed_kwargs |
| 9 |
) |
| 10 |
from ..type_checking import in_set_validator |
| 11 |
|
| 12 |
import typing as T |
| 13 |
|
| 14 |
if T.TYPE_CHECKING: |
| 15 |
from typing_extensions import Literal, TypedDict |
| 16 |
|
| 17 |
from ...interpreterbase import TYPE_var, TYPE_kwargs |
| 18 |
|
| 19 |
class ToStringKw(TypedDict): |
| 20 |
fill: int |
| 21 |
format: Literal['dec', 'hex', 'oct', 'bin'] |
| 22 |
|
| 23 |
class IntegerHolder(ObjectHolder[int]): |
| 24 |
# Operators that only require type checks |
| 25 |
TRIVIAL_OPERATORS = { |
| 26 |
# Arithmetic |
| 27 |
MesonOperator.UMINUS: (None, lambda obj, x: -obj.held_object), |
| 28 |
MesonOperator.PLUS: (int, lambda obj, x: obj.held_object + x), |
| 29 |
MesonOperator.MINUS: (int, lambda obj, x: obj.held_object - x), |
| 30 |
MesonOperator.TIMES: (int, lambda obj, x: obj.held_object * x), |
| 31 |
|
| 32 |
# Comparison |
| 33 |
MesonOperator.EQUALS: (int, lambda obj, x: obj.held_object == x), |
| 34 |
MesonOperator.NOT_EQUALS: (int, lambda obj, x: obj.held_object != x), |
| 35 |
MesonOperator.GREATER: (int, lambda obj, x: obj.held_object > x), |
| 36 |
MesonOperator.LESS: (int, lambda obj, x: obj.held_object < x), |
| 37 |
MesonOperator.GREATER_EQUALS: (int, lambda obj, x: obj.held_object >= x), |
| 38 |
MesonOperator.LESS_EQUALS: (int, lambda obj, x: obj.held_object <= x), |
| 39 |
} |
| 40 |
|
| 41 |
def display_name(self) -> str: |
| 42 |
return 'int' |
| 43 |
|
| 44 |
def operator_call(self, operator: MesonOperator, other: TYPE_var) -> TYPE_var: |
| 45 |
if isinstance(other, bool): |
| 46 |
FeatureBroken.single_use('int operations with non-int', '1.2.0', self.subproject, |
| 47 |
'It is not commutative and only worked because of leaky Python abstractions.', |
| 48 |
location=self.current_node) |
| 49 |
return super().operator_call(operator, other) |
| 50 |
|
| 51 |
@noKwargs |
| 52 |
@noPosargs |
| 53 |
@InterpreterObject.method('is_even') |
| 54 |
def is_even_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> bool: |
| 55 |
return self.held_object % 2 == 0 |
| 56 |
|
| 57 |
@noKwargs |
| 58 |
@noPosargs |
| 59 |
@InterpreterObject.method('is_odd') |
| 60 |
def is_odd_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> bool: |
| 61 |
return self.held_object % 2 != 0 |
| 62 |
|
| 63 |
@typed_kwargs( |
| 64 |
'to_string', |
| 65 |
KwargInfo('fill', int, default=0, since='1.3.0'), |
| 66 |
KwargInfo('format', str, default='dec', since='1.12.0', |
| 67 |
validator=in_set_validator({'dec', 'hex', 'oct', 'bin'})), |
| 68 |
) |
| 69 |
@noPosargs |
| 70 |
@InterpreterObject.method('to_string') |
| 71 |
def to_string_method(self, args: T.List[TYPE_var], kwargs: ToStringKw) -> str: |
| 72 |
format_codes = {'hex': 'x', 'oct': 'o', 'bin': 'b', 'dec': 'd'} |
| 73 |
return '{:#0{fill}{format}}'.format(self.held_object, |
| 74 |
fill=max(0, kwargs['fill']), |
| 75 |
format=format_codes[kwargs['format']]) |
| 76 |
|
| 77 |
@typed_operator(MesonOperator.DIV, int) |
| 78 |
@InterpreterObject.operator(MesonOperator.DIV) |
| 79 |
def op_div(self, other: int) -> int: |
| 80 |
if other == 0: |
| 81 |
raise InvalidArguments('Tried to divide by 0') |
| 82 |
return self.held_object // other |
| 83 |
|
| 84 |
@typed_operator(MesonOperator.MOD, int) |
| 85 |
@InterpreterObject.operator(MesonOperator.MOD) |
| 86 |
def op_mod(self, other: int) -> int: |
| 87 |
if other == 0: |
| 88 |
raise InvalidArguments('Tried to divide by 0') |
| 89 |
return self.held_object % other |