Skip to content

operators

ASTx operators.

BinaryOp

BinaryOp(op_code: str, lhs: DataType, rhs: DataType, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: DataTypeOps

AST class for the binary operator.

Source code in src/astx/datatypes.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def __init__(
    self,
    op_code: str,
    lhs: DataType,
    rhs: DataType,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the BinaryOp instance."""
    super().__init__()

    self.loc = loc
    self.op_code = op_code
    self.lhs = lhs
    self.rhs = rhs
    self.kind = ASTKind.BinaryOpKind

    if not (
        issubclass(lhs.type_, (Number, BinaryOp, DataType))
        and issubclass(rhs.type_, (Number, BinaryOp, DataType))
    ):
        raise Exception(
            "For now, binary operators are just allowed for numbers."
            f"LHS: {lhs.type_}, RHS: {rhs.type_}"
        )

    if lhs.type_ == rhs.type_:
        self.type_ = lhs.type_
    else:
        # type inference
        self.type_ = max([lhs.type_, rhs.type_], key=lambda v: v.nbytes)

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure that represents the object.

Source code in src/astx/datatypes.py
158
159
160
161
162
163
164
165
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure that represents the object."""
    key = f"BINARY[{self.op_code}]"
    lhs = {"lhs": self.lhs.get_struct(simplified)}
    rhs = {"rhs": self.rhs.get_struct(simplified)}

    content: ReprStruct = {**lhs, **rhs}
    return self._prepare_struct(key, content, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
217
218
219
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
211
212
213
214
215
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

DataTypeOps

DataTypeOps(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: DataType

Overload some magic functions used for the main operations.

Source code in src/astx/base.py
301
302
303
304
305
306
307
308
309
310
311
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc)
    self.name = f"temp_{DataType._tmp_id}"
    DataType._tmp_id += 1
    # set it as a generic data type
    self.type_: ExprType = DataType
    self.parent = parent

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a simple structure that represents the object.

Source code in src/astx/base.py
317
318
319
320
321
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a simple structure that represents the object."""
    key = "DATA-TYPE"
    value = self.name
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
217
218
219
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
211
212
213
214
215
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

UnaryOp

UnaryOp(op_code: str, operand: DataType, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: DataTypeOps

AST class for the unary operator.

Source code in src/astx/datatypes.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def __init__(
    self,
    op_code: str,
    operand: DataType,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the UnaryOp instance."""
    super().__init__()
    self.loc = loc
    self.op_code = op_code
    self.operand = operand
    self.kind = ASTKind.UnaryOpKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/datatypes.py
110
111
112
113
114
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = f"UNARY[{self.op_code}]"
    value = self.operand.get_struct(simplified)
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
217
218
219
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
211
212
213
214
215
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )