2020-04-20 02:05:58 +02:00
|
|
|
import abc
|
2019-04-22 21:56:30 +02:00
|
|
|
import inspect
|
2019-03-18 15:07:20 +01:00
|
|
|
import math
|
2021-01-24 00:17:15 +01:00
|
|
|
import re
|
2021-02-06 16:09:15 +01:00
|
|
|
from esphome.yaml_util import ESPHomeDataBase
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
# pylint: disable=unused-import, wrong-import-order
|
2020-04-20 02:05:58 +02:00
|
|
|
from typing import Any, Generator, List, Optional, Tuple, Type, Union, Sequence
|
2019-04-08 18:08:58 +02:00
|
|
|
|
|
|
|
from esphome.core import ( # noqa
|
2021-03-07 20:03:16 +01:00
|
|
|
CORE,
|
|
|
|
HexInt,
|
|
|
|
ID,
|
|
|
|
Lambda,
|
|
|
|
TimePeriod,
|
|
|
|
TimePeriodMicroseconds,
|
|
|
|
TimePeriodMilliseconds,
|
|
|
|
TimePeriodMinutes,
|
|
|
|
TimePeriodSeconds,
|
|
|
|
coroutine,
|
|
|
|
Library,
|
|
|
|
Define,
|
|
|
|
EnumValue,
|
|
|
|
)
|
2019-04-08 18:08:58 +02:00
|
|
|
from esphome.helpers import cpp_string_escape, indent_all_but_first_and_last
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome.util import OrderedDict
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
class Expression(abc.ABC):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2018-12-05 21:22:06 +01:00
|
|
|
def __str__(self):
|
2020-04-20 02:05:58 +02:00
|
|
|
"""
|
|
|
|
Convert expression into C++ code
|
|
|
|
"""
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
SafeExpType = Union[
|
|
|
|
Expression,
|
|
|
|
bool,
|
|
|
|
str,
|
|
|
|
str,
|
|
|
|
int,
|
|
|
|
float,
|
|
|
|
TimePeriod,
|
|
|
|
Type[bool],
|
|
|
|
Type[int],
|
|
|
|
Type[float],
|
|
|
|
Sequence[Any],
|
|
|
|
]
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class RawExpression(Expression):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("text",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, text: str):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.text = text
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-04-20 02:05:58 +02:00
|
|
|
return self.text
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AssignmentExpression(Expression):
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ("type", "modifier", "name", "rhs", "obj")
|
|
|
|
|
|
|
|
def __init__(self, type_, modifier, name, rhs, obj):
|
|
|
|
self.type = type_
|
2018-12-05 21:22:06 +01:00
|
|
|
self.modifier = modifier
|
|
|
|
self.name = name
|
|
|
|
self.rhs = safe_exp(rhs)
|
|
|
|
self.obj = obj
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-04-17 12:06:00 +02:00
|
|
|
if self.type is None:
|
2019-12-07 18:28:55 +01:00
|
|
|
return f"{self.name} = {self.rhs}"
|
|
|
|
return f"{self.type} {self.modifier}{self.name} = {self.rhs}"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
class VariableDeclarationExpression(Expression):
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ("type", "modifier", "name")
|
|
|
|
|
|
|
|
def __init__(self, type_, modifier, name):
|
|
|
|
self.type = type_
|
2019-04-17 12:06:00 +02:00
|
|
|
self.modifier = modifier
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-12-07 18:28:55 +01:00
|
|
|
return f"{self.type} {self.modifier}{self.name}"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ExpressionList(Expression):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("args",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, *args: Optional[SafeExpType]):
|
2018-12-05 21:22:06 +01:00
|
|
|
# Remove every None on end
|
|
|
|
args = list(args)
|
|
|
|
while args and args[-1] is None:
|
|
|
|
args.pop()
|
2019-04-17 12:06:00 +02:00
|
|
|
self.args = [safe_exp(arg) for arg in args]
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-12-07 18:28:55 +01:00
|
|
|
text = ", ".join(str(x) for x in self.args)
|
2018-12-05 21:22:06 +01:00
|
|
|
return indent_all_but_first_and_last(text)
|
|
|
|
|
2019-04-24 23:49:02 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.args)
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
class TemplateArguments(Expression):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("args",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, *args: SafeExpType):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.args = ExpressionList(*args)
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"<{self.args}>"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2019-04-24 23:49:02 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.args)
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
class CallExpression(Expression):
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ("base", "template_args", "args")
|
|
|
|
|
|
|
|
def __init__(self, base: Expression, *args: SafeExpType):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.base = base
|
|
|
|
if args and isinstance(args[0], TemplateArguments):
|
|
|
|
self.template_args = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
else:
|
|
|
|
self.template_args = None
|
|
|
|
self.args = ExpressionList(*args)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.template_args is not None:
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"{self.base}{self.template_args}({self.args})"
|
|
|
|
return f"{self.base}({self.args})"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class StructInitializer(Expression):
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ("base", "args")
|
|
|
|
|
|
|
|
def __init__(self, base: Expression, *args: Tuple[str, Optional[SafeExpType]]):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.base = base
|
2020-04-20 02:05:58 +02:00
|
|
|
# TODO: args is always a Tuple, is this check required?
|
2018-12-05 21:22:06 +01:00
|
|
|
if not isinstance(args, OrderedDict):
|
|
|
|
args = OrderedDict(args)
|
|
|
|
self.args = OrderedDict()
|
2019-01-02 14:11:11 +01:00
|
|
|
for key, value in args.items():
|
2018-12-05 21:22:06 +01:00
|
|
|
if value is None:
|
|
|
|
continue
|
|
|
|
exp = safe_exp(value)
|
|
|
|
self.args[key] = exp
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-03-07 20:03:16 +01:00
|
|
|
cpp = f"{self.base}{{\n"
|
2019-01-02 14:11:11 +01:00
|
|
|
for key, value in self.args.items():
|
2021-03-07 20:03:16 +01:00
|
|
|
cpp += f" .{key} = {value},\n"
|
|
|
|
cpp += "}"
|
2018-12-05 21:22:06 +01:00
|
|
|
return cpp
|
|
|
|
|
|
|
|
|
|
|
|
class ArrayInitializer(Expression):
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ("multiline", "args")
|
|
|
|
|
|
|
|
def __init__(self, *args: Any, multiline: bool = False):
|
|
|
|
self.multiline = multiline
|
2018-12-05 21:22:06 +01:00
|
|
|
self.args = []
|
|
|
|
for arg in args:
|
|
|
|
if arg is None:
|
|
|
|
continue
|
|
|
|
exp = safe_exp(arg)
|
|
|
|
self.args.append(exp)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if not self.args:
|
2021-03-07 20:03:16 +01:00
|
|
|
return "{}"
|
2018-12-05 21:22:06 +01:00
|
|
|
if self.multiline:
|
2021-03-07 20:03:16 +01:00
|
|
|
cpp = "{\n"
|
2018-12-05 21:22:06 +01:00
|
|
|
for arg in self.args:
|
2021-03-07 20:03:16 +01:00
|
|
|
cpp += f" {arg},\n"
|
|
|
|
cpp += "}"
|
2018-12-05 21:22:06 +01:00
|
|
|
else:
|
2021-09-19 19:22:28 +02:00
|
|
|
cpp = f"{{{', '.join(str(arg) for arg in self.args)}}}"
|
2018-12-05 21:22:06 +01:00
|
|
|
return cpp
|
|
|
|
|
|
|
|
|
|
|
|
class ParameterExpression(Expression):
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ("type", "id")
|
|
|
|
|
|
|
|
def __init__(self, type_, id_):
|
|
|
|
self.type = safe_exp(type_)
|
|
|
|
self.id = id_
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-12-07 18:28:55 +01:00
|
|
|
return f"{self.type} {self.id}"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ParameterListExpression(Expression):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("parameters",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
def __init__(
|
|
|
|
self, *parameters: Union[ParameterExpression, Tuple[SafeExpType, str]]
|
|
|
|
):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.parameters = []
|
|
|
|
for parameter in parameters:
|
|
|
|
if not isinstance(parameter, ParameterExpression):
|
|
|
|
parameter = ParameterExpression(*parameter)
|
|
|
|
self.parameters.append(parameter)
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-12-07 18:28:55 +01:00
|
|
|
return ", ".join(str(x) for x in self.parameters)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LambdaExpression(Expression):
|
2021-01-24 00:17:15 +01:00
|
|
|
__slots__ = ("parts", "parameters", "capture", "return_type", "source")
|
2020-04-20 02:05:58 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
def __init__(
|
|
|
|
self, parts, parameters, capture: str = "=", return_type=None, source=None
|
|
|
|
):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.parts = parts
|
|
|
|
if not isinstance(parameters, ParameterListExpression):
|
|
|
|
parameters = ParameterListExpression(*parameters)
|
|
|
|
self.parameters = parameters
|
2021-01-24 00:17:15 +01:00
|
|
|
self.source = source
|
2018-12-05 21:22:06 +01:00
|
|
|
self.capture = capture
|
2019-04-08 18:08:58 +02:00
|
|
|
self.return_type = safe_exp(return_type) if return_type is not None else None
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-03-07 20:03:16 +01:00
|
|
|
cpp = f"[{self.capture}]({self.parameters})"
|
2018-12-05 21:22:06 +01:00
|
|
|
if self.return_type is not None:
|
2021-03-07 20:03:16 +01:00
|
|
|
cpp += f" -> {self.return_type}"
|
|
|
|
cpp += " {\n"
|
2021-01-24 00:17:15 +01:00
|
|
|
if self.source is not None:
|
2021-03-07 20:03:16 +01:00
|
|
|
cpp += f"{self.source.as_line_directive}\n"
|
|
|
|
cpp += f"{self.content}\n}}"
|
2018-12-05 21:22:06 +01:00
|
|
|
return indent_all_but_first_and_last(cpp)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def content(self):
|
2021-03-07 20:03:16 +01:00
|
|
|
return "".join(str(part) for part in self.parts)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
# pylint: disable=abstract-method
|
|
|
|
class Literal(Expression, metaclass=abc.ABCMeta):
|
|
|
|
__slots__ = ()
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class StringLiteral(Literal):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("string",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, string: str):
|
2020-05-24 01:33:58 +02:00
|
|
|
super().__init__()
|
2018-12-05 21:22:06 +01:00
|
|
|
self.string = string
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-04-20 02:05:58 +02:00
|
|
|
return cpp_string_escape(self.string)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class IntLiteral(Literal):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("i",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, i: int):
|
2020-05-24 01:33:58 +02:00
|
|
|
super().__init__()
|
2018-12-05 21:22:06 +01:00
|
|
|
self.i = i
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.i > 4294967295:
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"{self.i}ULL"
|
2018-12-05 21:22:06 +01:00
|
|
|
if self.i > 2147483647:
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"{self.i}UL"
|
2018-12-05 21:22:06 +01:00
|
|
|
if self.i < -2147483648:
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"{self.i}LL"
|
2019-12-07 18:28:55 +01:00
|
|
|
return str(self.i)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BoolLiteral(Literal):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("binary",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, binary: bool):
|
2019-12-07 18:28:55 +01:00
|
|
|
super().__init__()
|
2018-12-05 21:22:06 +01:00
|
|
|
self.binary = binary
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-12-07 18:28:55 +01:00
|
|
|
return "true" if self.binary else "false"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HexIntLiteral(Literal):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("i",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, i: int):
|
2020-05-24 01:33:58 +02:00
|
|
|
super().__init__()
|
2018-12-05 21:22:06 +01:00
|
|
|
self.i = HexInt(i)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.i)
|
|
|
|
|
|
|
|
|
|
|
|
class FloatLiteral(Literal):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("f",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, value: float):
|
2020-05-24 01:33:58 +02:00
|
|
|
super().__init__()
|
2020-04-20 02:05:58 +02:00
|
|
|
self.f = value
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-04-20 02:05:58 +02:00
|
|
|
if math.isnan(self.f):
|
2019-12-07 18:28:55 +01:00
|
|
|
return "NAN"
|
2020-04-20 02:05:58 +02:00
|
|
|
return f"{self.f}f"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-09-23 11:19:17 +02:00
|
|
|
class BinOpExpression(Expression):
|
|
|
|
__slots__ = ("op", "lhs", "rhs")
|
|
|
|
|
2021-09-23 11:53:10 +02:00
|
|
|
def __init__(self, lhs: SafeExpType, op: str, rhs: SafeExpType):
|
2021-09-23 11:19:17 +02:00
|
|
|
self.lhs = safe_exp(lhs)
|
2021-09-23 11:53:10 +02:00
|
|
|
self.op = op
|
2021-09-23 11:19:17 +02:00
|
|
|
self.rhs = safe_exp(rhs)
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-09-23 11:53:10 +02:00
|
|
|
# Surround with parentheses to ensure generated code has same
|
|
|
|
# order as python one
|
|
|
|
return f"({self.lhs} {self.op} {self.rhs})"
|
|
|
|
|
|
|
|
|
|
|
|
class UnaryOpExpression(Expression):
|
|
|
|
__slots__ = ("op", "exp")
|
|
|
|
|
|
|
|
def __init__(self, op: str, exp: SafeExpType):
|
|
|
|
self.op = op
|
|
|
|
self.exp = safe_exp(exp)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"({self.op}{self.exp})"
|
2021-09-23 11:19:17 +02:00
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def safe_exp(obj: SafeExpType) -> Expression:
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Try to convert obj to an expression by automatically converting native python types to
|
|
|
|
expressions/literals.
|
|
|
|
"""
|
2019-04-08 18:08:58 +02:00
|
|
|
from esphome.cpp_types import bool_, float_, int32
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
if isinstance(obj, Expression):
|
|
|
|
return obj
|
2019-04-22 21:56:30 +02:00
|
|
|
if isinstance(obj, EnumValue):
|
|
|
|
return safe_exp(obj.enum_value)
|
2019-01-02 14:11:11 +01:00
|
|
|
if isinstance(obj, bool):
|
2018-12-05 21:22:06 +01:00
|
|
|
return BoolLiteral(obj)
|
2019-12-07 18:28:55 +01:00
|
|
|
if isinstance(obj, str):
|
2018-12-05 21:22:06 +01:00
|
|
|
return StringLiteral(obj)
|
2019-01-02 14:11:11 +01:00
|
|
|
if isinstance(obj, HexInt):
|
2018-12-05 21:22:06 +01:00
|
|
|
return HexIntLiteral(obj)
|
2019-12-07 18:28:55 +01:00
|
|
|
if isinstance(obj, int):
|
2018-12-05 21:22:06 +01:00
|
|
|
return IntLiteral(obj)
|
2019-01-02 14:11:11 +01:00
|
|
|
if isinstance(obj, float):
|
2018-12-05 21:22:06 +01:00
|
|
|
return FloatLiteral(obj)
|
2019-01-02 14:11:11 +01:00
|
|
|
if isinstance(obj, TimePeriodMicroseconds):
|
2018-12-05 21:22:06 +01:00
|
|
|
return IntLiteral(int(obj.total_microseconds))
|
2019-01-02 14:11:11 +01:00
|
|
|
if isinstance(obj, TimePeriodMilliseconds):
|
2018-12-05 21:22:06 +01:00
|
|
|
return IntLiteral(int(obj.total_milliseconds))
|
2019-01-02 14:11:11 +01:00
|
|
|
if isinstance(obj, TimePeriodSeconds):
|
2018-12-05 21:22:06 +01:00
|
|
|
return IntLiteral(int(obj.total_seconds))
|
2019-03-06 12:39:52 +01:00
|
|
|
if isinstance(obj, TimePeriodMinutes):
|
|
|
|
return IntLiteral(int(obj.total_minutes))
|
2019-02-03 20:46:18 +01:00
|
|
|
if isinstance(obj, (tuple, list)):
|
|
|
|
return ArrayInitializer(*[safe_exp(o) for o in obj])
|
2019-04-08 18:08:58 +02:00
|
|
|
if obj is bool:
|
|
|
|
return bool_
|
|
|
|
if obj is int:
|
|
|
|
return int32
|
|
|
|
if obj is float:
|
|
|
|
return float_
|
2019-04-22 21:56:30 +02:00
|
|
|
if isinstance(obj, ID):
|
2021-03-07 20:03:16 +01:00
|
|
|
raise ValueError(
|
2021-09-19 19:22:28 +02:00
|
|
|
f"Object {obj} is an ID. Did you forget to register the variable?"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2019-04-22 21:56:30 +02:00
|
|
|
if inspect.isgenerator(obj):
|
2021-03-07 20:03:16 +01:00
|
|
|
raise ValueError(
|
2021-09-19 19:22:28 +02:00
|
|
|
f"Object {obj} is a coroutine. Did you forget to await the expression with 'await'?"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2019-12-07 18:28:55 +01:00
|
|
|
raise ValueError("Object is not an expression", obj)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
class Statement(abc.ABC):
|
|
|
|
__slots__ = ()
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
@abc.abstractmethod
|
2018-12-05 21:22:06 +01:00
|
|
|
def __str__(self):
|
2020-04-20 02:05:58 +02:00
|
|
|
"""
|
|
|
|
Convert statement into C++ code
|
|
|
|
"""
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class RawStatement(Statement):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("text",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, text: str):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.text = text
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.text
|
|
|
|
|
|
|
|
|
|
|
|
class ExpressionStatement(Statement):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("expression",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def __init__(self, expression):
|
|
|
|
self.expression = safe_exp(expression)
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-12-07 18:28:55 +01:00
|
|
|
return f"{self.expression};"
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
class LineComment(Statement):
|
2021-03-07 20:03:16 +01:00
|
|
|
__slots__ = ("value",)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
def __init__(self, value: str):
|
|
|
|
self.value = value
|
2019-05-12 23:04:36 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-03-07 20:03:16 +01:00
|
|
|
parts = re.sub(r"\\\s*\n", r"<cont>\n", self.value, re.MULTILINE).split("\n")
|
|
|
|
parts = [f"// {x}" for x in parts]
|
|
|
|
return "\n".join(parts)
|
2019-05-12 23:04:36 +02:00
|
|
|
|
|
|
|
|
2019-02-10 16:41:12 +01:00
|
|
|
class ProgmemAssignmentExpression(AssignmentExpression):
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __init__(self, type_, name, rhs, obj):
|
2021-03-07 20:03:16 +01:00
|
|
|
super().__init__(type_, "", name, rhs, obj)
|
2019-02-10 16:41:12 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-04-20 02:05:58 +02:00
|
|
|
return f"static const {self.type} {self.name}[] PROGMEM = {self.rhs}"
|
2019-02-10 16:41:12 +01:00
|
|
|
|
|
|
|
|
2021-05-23 22:24:54 +02:00
|
|
|
class StaticConstAssignmentExpression(AssignmentExpression):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __init__(self, type_, name, rhs, obj):
|
|
|
|
super().__init__(type_, "", name, rhs, obj)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"static const {self.type} {self.name}[] = {self.rhs}"
|
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def progmem_array(id_, rhs) -> "MockObj":
|
2019-02-10 16:41:12 +01:00
|
|
|
rhs = safe_exp(rhs)
|
2021-03-07 20:03:16 +01:00
|
|
|
obj = MockObj(id_, ".")
|
2020-04-20 02:05:58 +02:00
|
|
|
assignment = ProgmemAssignmentExpression(id_.type, id_, rhs, obj)
|
2019-02-10 16:41:12 +01:00
|
|
|
CORE.add(assignment)
|
2020-04-20 02:05:58 +02:00
|
|
|
CORE.register_variable(id_, obj)
|
2019-02-10 16:41:12 +01:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2021-05-23 22:24:54 +02:00
|
|
|
def static_const_array(id_, rhs) -> "MockObj":
|
|
|
|
rhs = safe_exp(rhs)
|
|
|
|
obj = MockObj(id_, ".")
|
|
|
|
assignment = StaticConstAssignmentExpression(id_.type, id_, rhs, obj)
|
|
|
|
CORE.add(assignment)
|
|
|
|
CORE.register_variable(id_, obj)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def statement(expression: Union[Expression, Statement]) -> Statement:
|
2021-03-07 20:03:16 +01:00
|
|
|
"""Convert expression into a statement unless is already a statement."""
|
2018-12-05 21:22:06 +01:00
|
|
|
if isinstance(expression, Statement):
|
|
|
|
return expression
|
|
|
|
return ExpressionStatement(expression)
|
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def variable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj":
|
2021-03-07 20:03:16 +01:00
|
|
|
"""Declare a new variable, not pointer type, in the code generation.
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
:param id_: The ID used to declare the variable.
|
2019-04-22 21:56:30 +02:00
|
|
|
:param rhs: The expression to place on the right hand side of the assignment.
|
2020-04-20 02:05:58 +02:00
|
|
|
:param type_: Manually define a type for the variable, only use this when it's not possible
|
2019-04-22 21:56:30 +02:00
|
|
|
to do so during config validation phase (for example because of template arguments).
|
|
|
|
|
|
|
|
:returns The new variable as a MockObj.
|
|
|
|
"""
|
2020-04-20 02:05:58 +02:00
|
|
|
assert isinstance(id_, ID)
|
2018-12-05 21:22:06 +01:00
|
|
|
rhs = safe_exp(rhs)
|
2021-03-07 20:03:16 +01:00
|
|
|
obj = MockObj(id_, ".")
|
2020-04-20 02:05:58 +02:00
|
|
|
if type_ is not None:
|
|
|
|
id_.type = type_
|
2021-03-07 20:03:16 +01:00
|
|
|
assignment = AssignmentExpression(id_.type, "", id_, rhs, obj)
|
2018-12-05 21:22:06 +01:00
|
|
|
CORE.add(assignment)
|
2020-04-20 02:05:58 +02:00
|
|
|
CORE.register_variable(id_, obj)
|
2018-12-05 21:22:06 +01:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2021-03-19 11:40:05 +01:00
|
|
|
def new_variable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj":
|
|
|
|
"""Declare and define a new variable, not pointer type, in the code generation.
|
|
|
|
|
|
|
|
:param id_: The ID used to declare the variable.
|
|
|
|
:param rhs: The expression to place on the right hand side of the assignment.
|
|
|
|
:param type_: Manually define a type for the variable, only use this when it's not possible
|
|
|
|
to do so during config validation phase (for example because of template arguments).
|
|
|
|
|
|
|
|
:returns The new variable as a MockObj.
|
|
|
|
"""
|
|
|
|
assert isinstance(id_, ID)
|
|
|
|
rhs = safe_exp(rhs)
|
|
|
|
obj = MockObj(id_, ".")
|
|
|
|
if type_ is not None:
|
|
|
|
id_.type = type_
|
|
|
|
decl = VariableDeclarationExpression(id_.type, "", id_)
|
|
|
|
CORE.add_global(decl)
|
|
|
|
assignment = AssignmentExpression(None, "", id_, rhs, obj)
|
|
|
|
CORE.add(assignment)
|
|
|
|
CORE.register_variable(id_, obj)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def Pvariable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj":
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Declare a new pointer variable in the code generation.
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
:param id_: The ID used to declare the variable.
|
2019-04-22 21:56:30 +02:00
|
|
|
:param rhs: The expression to place on the right hand side of the assignment.
|
2020-04-20 02:05:58 +02:00
|
|
|
:param type_: Manually define a type for the variable, only use this when it's not possible
|
2019-04-22 21:56:30 +02:00
|
|
|
to do so during config validation phase (for example because of template arguments).
|
|
|
|
|
|
|
|
:returns The new variable as a MockObj.
|
|
|
|
"""
|
2018-12-05 21:22:06 +01:00
|
|
|
rhs = safe_exp(rhs)
|
2021-03-07 20:03:16 +01:00
|
|
|
obj = MockObj(id_, "->")
|
2020-04-20 02:05:58 +02:00
|
|
|
if type_ is not None:
|
|
|
|
id_.type = type_
|
2021-03-07 20:03:16 +01:00
|
|
|
decl = VariableDeclarationExpression(id_.type, "*", id_)
|
2019-04-17 12:06:00 +02:00
|
|
|
CORE.add_global(decl)
|
2020-04-20 02:05:58 +02:00
|
|
|
assignment = AssignmentExpression(None, None, id_, rhs, obj)
|
2018-12-05 21:22:06 +01:00
|
|
|
CORE.add(assignment)
|
2020-04-20 02:05:58 +02:00
|
|
|
CORE.register_variable(id_, obj)
|
2018-12-05 21:22:06 +01:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def new_Pvariable(id_: ID, *args: SafeExpType) -> Pvariable:
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Declare a new pointer variable in the code generation by calling it's constructor
|
|
|
|
with the given arguments.
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
:param id_: The ID used to declare the variable (also specifies the type).
|
2019-04-22 21:56:30 +02:00
|
|
|
:param args: The values to pass to the constructor.
|
|
|
|
|
|
|
|
:returns The new variable as a MockObj.
|
|
|
|
"""
|
|
|
|
if args and isinstance(args[0], TemplateArguments):
|
2020-04-20 02:05:58 +02:00
|
|
|
id_ = id_.copy()
|
|
|
|
id_.type = id_.type.template(args[0])
|
2019-04-22 21:56:30 +02:00
|
|
|
args = args[1:]
|
2020-04-20 02:05:58 +02:00
|
|
|
rhs = id_.type.new(*args)
|
|
|
|
return Pvariable(id_, rhs)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def add(expression: Union[Expression, Statement]):
|
2019-05-11 12:31:00 +02:00
|
|
|
"""Add an expression to the codegen section.
|
|
|
|
|
|
|
|
After this is called, the given given expression will
|
|
|
|
show up in the setup() function after this has been called.
|
|
|
|
"""
|
2019-04-17 12:06:00 +02:00
|
|
|
CORE.add(expression)
|
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def add_global(expression: Union[SafeExpType, Statement]):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Add an expression to the codegen global storage (above setup())."""
|
2019-04-17 12:06:00 +02:00
|
|
|
CORE.add_global(expression)
|
|
|
|
|
|
|
|
|
2021-07-26 10:50:45 +02:00
|
|
|
def add_library(name: str, version: Optional[str], repository: Optional[str] = None):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Add a library to the codegen library storage.
|
|
|
|
|
|
|
|
:param name: The name of the library (for example 'AsyncTCP')
|
|
|
|
:param version: The version of the library, may be None.
|
|
|
|
"""
|
2021-07-26 10:50:45 +02:00
|
|
|
CORE.add_library(Library(name, version, repository))
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def add_build_flag(build_flag: str):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Add a global build flag to the compiler flags."""
|
2019-04-17 12:06:00 +02:00
|
|
|
CORE.add_build_flag(build_flag)
|
|
|
|
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def add_define(name: str, value: SafeExpType = None):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Add a global define to the auto-generated defines.h file.
|
|
|
|
|
|
|
|
Optionally define a value to set this define to.
|
|
|
|
"""
|
2019-04-17 12:06:00 +02:00
|
|
|
if value is None:
|
|
|
|
CORE.add_define(Define(name))
|
|
|
|
else:
|
|
|
|
CORE.add_define(Define(name, safe_exp(value)))
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
def add_platformio_option(key: str, value: Union[str, List[str]]):
|
|
|
|
CORE.add_platformio_option(key, value)
|
|
|
|
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
async def get_variable(id_: ID) -> "MockObj":
|
2019-04-22 21:56:30 +02:00
|
|
|
"""
|
|
|
|
Wait for the given ID to be defined in the code generation and
|
|
|
|
return it as a MockObj.
|
|
|
|
|
2021-05-24 21:45:31 +02:00
|
|
|
This is a coroutine, you need to await it with a 'await' expression!
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
:param id_: The ID to retrieve
|
2019-04-22 21:56:30 +02:00
|
|
|
:return: The variable as a MockObj.
|
|
|
|
"""
|
2021-05-17 07:14:15 +02:00
|
|
|
return await CORE.get_variable(id_)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
async def get_variable_with_full_id(id_: ID) -> Tuple[ID, "MockObj"]:
|
2019-04-24 23:49:02 +02:00
|
|
|
"""
|
|
|
|
Wait for the given ID to be defined in the code generation and
|
|
|
|
return it as a MockObj.
|
|
|
|
|
2021-05-24 21:45:31 +02:00
|
|
|
This is a coroutine, you need to await it with a 'await' expression!
|
2019-04-24 23:49:02 +02:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
:param id_: The ID to retrieve
|
2019-04-24 23:49:02 +02:00
|
|
|
:return: The variable as a MockObj.
|
|
|
|
"""
|
2021-05-17 07:14:15 +02:00
|
|
|
return await CORE.get_variable_with_full_id(id_)
|
2019-04-24 23:49:02 +02:00
|
|
|
|
|
|
|
|
2021-05-23 22:10:30 +02:00
|
|
|
async def process_lambda(
|
2021-03-07 20:03:16 +01:00
|
|
|
value: Lambda,
|
|
|
|
parameters: List[Tuple[SafeExpType, str]],
|
|
|
|
capture: str = "=",
|
|
|
|
return_type: SafeExpType = None,
|
2020-04-20 02:05:58 +02:00
|
|
|
) -> Generator[LambdaExpression, None, None]:
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Process the given lambda value into a LambdaExpression.
|
|
|
|
|
|
|
|
This is a coroutine because lambdas can depend on other IDs,
|
2021-05-24 21:45:31 +02:00
|
|
|
you need to await it with 'await'!
|
2019-04-22 21:56:30 +02:00
|
|
|
|
|
|
|
:param value: The lambda to process.
|
|
|
|
:param parameters: The parameters to pass to the Lambda, list of tuples
|
|
|
|
:param capture: The capture expression for the lambda, usually ''.
|
|
|
|
:param return_type: The return type of the lambda.
|
|
|
|
:return: The generated lambda expression.
|
|
|
|
"""
|
2021-10-04 16:15:25 +02:00
|
|
|
from esphome.components.globals import GlobalsComponent, RestoringGlobalsComponent
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
if value is None:
|
|
|
|
return
|
|
|
|
parts = value.parts[:]
|
|
|
|
for i, id in enumerate(value.requires_ids):
|
2021-05-23 22:10:30 +02:00
|
|
|
full_id, var = await get_variable_with_full_id(id)
|
2021-03-07 20:03:16 +01:00
|
|
|
if (
|
|
|
|
full_id is not None
|
|
|
|
and isinstance(full_id.type, MockObjClass)
|
2021-10-04 16:15:25 +02:00
|
|
|
and (
|
|
|
|
full_id.type.inherits_from(GlobalsComponent)
|
|
|
|
or full_id.type.inherits_from(RestoringGlobalsComponent)
|
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
):
|
2018-12-05 21:22:06 +01:00
|
|
|
parts[i * 3 + 1] = var.value()
|
|
|
|
continue
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
if parts[i * 3 + 2] == ".":
|
2018-12-05 21:22:06 +01:00
|
|
|
parts[i * 3 + 1] = var._
|
|
|
|
else:
|
|
|
|
parts[i * 3 + 1] = var
|
2021-03-07 20:03:16 +01:00
|
|
|
parts[i * 3 + 2] = ""
|
2021-02-06 16:09:15 +01:00
|
|
|
|
|
|
|
if isinstance(value, ESPHomeDataBase) and value.esp_range is not None:
|
|
|
|
location = value.esp_range.start_mark
|
|
|
|
location.line += value.content_offset
|
|
|
|
else:
|
|
|
|
location = None
|
2021-05-23 22:10:30 +02:00
|
|
|
return LambdaExpression(parts, parameters, capture, return_type, location)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
def is_template(value):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Return if value is a lambda expression."""
|
2019-04-17 12:06:00 +02:00
|
|
|
return isinstance(value, Lambda)
|
|
|
|
|
|
|
|
|
2021-05-23 22:10:30 +02:00
|
|
|
async def templatable(
|
2021-03-07 20:03:16 +01:00
|
|
|
value: Any,
|
|
|
|
args: List[Tuple[SafeExpType, str]],
|
|
|
|
output_type: Optional[SafeExpType],
|
|
|
|
to_exp: Any = None,
|
|
|
|
):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""Generate code for a templatable config option.
|
|
|
|
|
|
|
|
If `value` is a templated value, the lambda expression is returned.
|
|
|
|
Otherwise the value is returned as-is (optionally process with to_exp).
|
|
|
|
|
|
|
|
:param value: The value to process.
|
|
|
|
:param args: The arguments for the lambda expression.
|
|
|
|
:param output_type: The output type of the lambda expression.
|
|
|
|
:param to_exp: An optional callable to use for converting non-templated values.
|
|
|
|
:return: The potentially templated value.
|
|
|
|
"""
|
2019-04-17 12:06:00 +02:00
|
|
|
if is_template(value):
|
2021-05-23 22:10:30 +02:00
|
|
|
return await process_lambda(value, args, return_type=output_type)
|
|
|
|
if to_exp is None:
|
|
|
|
return value
|
|
|
|
if isinstance(to_exp, dict):
|
|
|
|
return to_exp[value]
|
|
|
|
return to_exp(value)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MockObj(Expression):
|
2019-04-22 21:56:30 +02:00
|
|
|
"""A general expression that can be used to represent any value.
|
|
|
|
|
|
|
|
Mostly consists of magic methods that allow ESPHome's codegen syntax.
|
|
|
|
"""
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
__slots__ = ("base", "op")
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
def __init__(self, base, op="."):
|
2018-12-05 21:22:06 +01:00
|
|
|
self.base = base
|
|
|
|
self.op = op
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def __getattr__(self, attr: str) -> "MockObj":
|
2021-05-17 07:14:15 +02:00
|
|
|
# prevent python dunder methods being replaced by mock objects
|
|
|
|
if attr.startswith("__"):
|
|
|
|
raise AttributeError()
|
2021-03-07 20:03:16 +01:00
|
|
|
next_op = "."
|
|
|
|
if attr.startswith("P") and self.op not in ["::", ""]:
|
2018-12-05 21:22:06 +01:00
|
|
|
attr = attr[1:]
|
2021-03-07 20:03:16 +01:00
|
|
|
next_op = "->"
|
|
|
|
if attr.startswith("_"):
|
2018-12-05 21:22:06 +01:00
|
|
|
attr = attr[1:]
|
2021-03-07 20:03:16 +01:00
|
|
|
return MockObj(f"{self.base}{self.op}{attr}", next_op)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2019-04-24 23:49:02 +02:00
|
|
|
def __call__(self, *args): # type: (SafeExpType) -> MockObj
|
2018-12-05 21:22:06 +01:00
|
|
|
call = CallExpression(self.base, *args)
|
2019-04-17 12:06:00 +02:00
|
|
|
return MockObj(call, self.op)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def __str__(self):
|
2019-12-07 18:28:55 +01:00
|
|
|
return str(self.base)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
def __repr__(self):
|
2021-09-19 19:22:28 +02:00
|
|
|
return f"MockObj<{str(self.base)}>"
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
@property
|
2020-04-20 02:05:58 +02:00
|
|
|
def _(self) -> "MockObj":
|
2021-03-07 20:03:16 +01:00
|
|
|
return MockObj(f"{self.base}{self.op}")
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
@property
|
2020-04-20 02:05:58 +02:00
|
|
|
def new(self) -> "MockObj":
|
2021-03-07 20:03:16 +01:00
|
|
|
return MockObj(f"new {self.base}", "->")
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def template(self, *args: SafeExpType) -> "MockObj":
|
2021-09-23 11:53:10 +02:00
|
|
|
"""Apply template parameters to this object."""
|
2019-02-26 19:28:11 +01:00
|
|
|
if len(args) != 1 or not isinstance(args[0], TemplateArguments):
|
|
|
|
args = TemplateArguments(*args)
|
|
|
|
else:
|
|
|
|
args = args[0]
|
2021-03-07 20:03:16 +01:00
|
|
|
return MockObj(f"{self.base}{args}")
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def namespace(self, name: str) -> "MockObj":
|
2021-03-07 20:03:16 +01:00
|
|
|
return MockObj(f"{self._}{name}", "::")
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def class_(self, name: str, *parents: "MockObjClass") -> "MockObjClass":
|
2021-03-07 20:03:16 +01:00
|
|
|
op = "" if self.op == "" else "::"
|
|
|
|
return MockObjClass(f"{self.base}{op}{name}", ".", parents=parents)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def struct(self, name: str) -> "MockObjClass":
|
2018-12-05 21:22:06 +01:00
|
|
|
return self.class_(name)
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def enum(self, name: str, is_class: bool = False) -> "MockObj":
|
2019-04-24 23:49:02 +02:00
|
|
|
return MockObjEnum(enum=name, is_class=is_class, base=self.base, op=self.op)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def operator(self, name: str) -> "MockObj":
|
2021-09-23 11:53:10 +02:00
|
|
|
"""Various other operations.
|
|
|
|
|
|
|
|
Named operator because it's a C++ keyword and can't occur in valid code.
|
|
|
|
"""
|
2021-03-07 20:03:16 +01:00
|
|
|
if name == "ref":
|
|
|
|
return MockObj(f"{self.base} &", "")
|
|
|
|
if name == "ptr":
|
|
|
|
return MockObj(f"{self.base} *", "")
|
2022-02-08 10:27:22 +01:00
|
|
|
if name == "const_ptr":
|
|
|
|
return MockObj(f"{self.base} *const", "")
|
2018-12-05 21:22:06 +01:00
|
|
|
if name == "const":
|
2021-03-07 20:03:16 +01:00
|
|
|
return MockObj(f"const {self.base}", "")
|
2022-02-08 10:27:22 +01:00
|
|
|
raise ValueError("Expected one of ref, ptr, const_ptr, const.")
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
@property
|
2020-04-20 02:05:58 +02:00
|
|
|
def using(self) -> "MockObj":
|
2021-03-07 20:03:16 +01:00
|
|
|
assert self.op == "::"
|
|
|
|
return MockObj(f"using namespace {self.base}")
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def __getitem__(self, item: Union[str, Expression]) -> "MockObj":
|
2021-03-07 20:03:16 +01:00
|
|
|
next_op = "."
|
|
|
|
if isinstance(item, str) and item.startswith("P"):
|
2018-12-05 21:22:06 +01:00
|
|
|
item = item[1:]
|
2021-03-07 20:03:16 +01:00
|
|
|
next_op = "->"
|
|
|
|
return MockObj(f"{self.base}[{item}]", next_op)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2021-09-23 11:53:10 +02:00
|
|
|
def __lt__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "<", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __le__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "<=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __eq__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "==", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __ne__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "!=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __gt__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, ">", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __ge__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, ">=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __add__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "+", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __sub__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "-", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __mul__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "*", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __truediv__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "/", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __mod__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "%", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __lshift__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "<<", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rshift__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, ">>", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __and__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "&", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __xor__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "^", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
2021-09-23 11:19:17 +02:00
|
|
|
def __or__(self, other: SafeExpType) -> "MockObj":
|
2021-09-23 11:53:10 +02:00
|
|
|
op = BinOpExpression(self, "|", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __radd__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "+", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rsub__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "-", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rmul__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "*", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rtruediv__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "/", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rmod__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "%", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rlshift__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "<<", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rrshift__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, ">>", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rand__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "&", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __rxor__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "^", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __ror__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(other, "|", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __iadd__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "+=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __isub__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "-=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __imul__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "*=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __itruediv__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "/=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __imod__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "%=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __ilshift__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "<<=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __irshift__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, ">>=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __iand__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "&=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __ixor__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "^=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __ior__(self, other: SafeExpType) -> "MockObj":
|
|
|
|
op = BinOpExpression(self, "|=", other)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __neg__(self) -> "MockObj":
|
|
|
|
op = UnaryOpExpression("-", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __pos__(self) -> "MockObj":
|
|
|
|
op = UnaryOpExpression("+", self)
|
|
|
|
return MockObj(op)
|
|
|
|
|
|
|
|
def __invert__(self) -> "MockObj":
|
|
|
|
op = UnaryOpExpression("~", self)
|
2021-09-23 11:19:17 +02:00
|
|
|
return MockObj(op)
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2019-04-24 23:49:02 +02:00
|
|
|
class MockObjEnum(MockObj):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-03-07 20:03:16 +01:00
|
|
|
self._enum = kwargs.pop("enum")
|
|
|
|
self._is_class = kwargs.pop("is_class")
|
|
|
|
base = kwargs.pop("base")
|
2019-04-24 23:49:02 +02:00
|
|
|
if self._is_class:
|
2021-09-19 19:22:28 +02:00
|
|
|
base = f"{base}::{self._enum}"
|
2021-03-07 20:03:16 +01:00
|
|
|
kwargs["op"] = "::"
|
|
|
|
kwargs["base"] = base
|
2019-04-24 23:49:02 +02:00
|
|
|
MockObj.__init__(self, *args, **kwargs)
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def __str__(self):
|
2019-04-24 23:49:02 +02:00
|
|
|
if self._is_class:
|
2019-12-07 18:28:55 +01:00
|
|
|
return super().__str__()
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"{self.base}{self.op}{self._enum}"
|
2019-04-24 23:49:02 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"MockObj<{str(self.base)}>"
|
2019-04-24 23:49:02 +02:00
|
|
|
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
class MockObjClass(MockObj):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-03-07 20:03:16 +01:00
|
|
|
parens = kwargs.pop("parents")
|
2018-12-05 21:22:06 +01:00
|
|
|
MockObj.__init__(self, *args, **kwargs)
|
|
|
|
self._parents = []
|
|
|
|
for paren in parens:
|
|
|
|
if not isinstance(paren, MockObjClass):
|
|
|
|
raise ValueError
|
|
|
|
self._parents.append(paren)
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
self._parents += paren._parents
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def inherits_from(self, other: "MockObjClass") -> bool:
|
2021-09-23 11:53:10 +02:00
|
|
|
if str(self) == str(other):
|
2018-12-05 21:22:06 +01:00
|
|
|
return True
|
|
|
|
for parent in self._parents:
|
2021-09-23 11:53:10 +02:00
|
|
|
if str(parent) == str(other):
|
2018-12-05 21:22:06 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-04-20 02:05:58 +02:00
|
|
|
def template(self, *args: SafeExpType) -> "MockObjClass":
|
2019-02-26 19:28:11 +01:00
|
|
|
if len(args) != 1 or not isinstance(args[0], TemplateArguments):
|
|
|
|
args = TemplateArguments(*args)
|
|
|
|
else:
|
|
|
|
args = args[0]
|
2018-12-05 21:22:06 +01:00
|
|
|
new_parents = self._parents[:]
|
|
|
|
new_parents.append(self)
|
2021-03-07 20:03:16 +01:00
|
|
|
return MockObjClass(f"{self.base}{args}", parents=new_parents)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2021-03-07 20:03:16 +01:00
|
|
|
return f"MockObjClass<{str(self.base)}, parents={self._parents}>"
|