Skip to content

callables

Module for callable ASTx.

Classes:

Argument

Argument(name: str, type_: DataType, mutability: MutabilityKind = constant, default: Expr = UNDEFINED, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Variable

AST class for argument definition.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/callables.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __init__(
    self,
    name: str,
    type_: DataType,
    mutability: MutabilityKind = MutabilityKind.constant,
    default: Expr = UNDEFINED,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the VarExprAST instance."""
    super().__init__(name=name, loc=loc, parent=parent)
    self.mutability = mutability
    self.type_ = type_
    self.default = default
    self.kind = ASTKind.ArgumentKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/callables.py
60
61
62
63
64
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = f"Argument[{self.name}, {self.type_}] = {self.default}"
    value = self.default.get_struct()
    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
265
266
267
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
259
260
261
262
263
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)
    )

Arguments

Arguments(*args: Argument, **kwargs: Any)

Bases: ASTNodes[Argument]

AST class for argument definition.

Methods:

  • append

    Append a new node to the stack.

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/callables.py
72
73
74
75
def __init__(self, *args: Argument, **kwargs: Any) -> None:
    super().__init__(**kwargs)
    for arg in args:
        self.append(arg)

append

append(value: ASTType) -> None

Append a new node to the stack.

Source code in src/astx/base.py
305
306
307
def append(self, value: ASTType) -> None:
    """Append a new node to the stack."""
    self.nodes.append(value)

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/callables.py
81
82
83
84
85
86
87
88
89
90
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    args_nodes = []

    for node in self.nodes:
        args_nodes.append(node.get_struct(simplified))

    key = str(self)
    value = cast(ReprStruct, args_nodes)
    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
265
266
267
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
259
260
261
262
263
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)
    )

Function

Function(prototype: FunctionPrototype, body: Block, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for function definition.

Methods:

  • get_struct

    Get the AST structure that represent the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Attributes:

  • name (str) –

    Return the function prototype name.

Source code in src/astx/callables.py
216
217
218
219
220
221
222
223
224
225
226
227
def __init__(
    self,
    prototype: FunctionPrototype,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the Function instance."""
    super().__init__(loc=loc, parent=parent)
    self.prototype = prototype
    self.body = body
    self.kind = ASTKind.FunctionKind

name property

name: str

Return the function prototype name.

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Get the AST structure that represent the object.

Source code in src/astx/callables.py
247
248
249
250
251
252
253
254
255
256
257
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Get the AST structure that represent the object."""
    fn_args = self.prototype.args.get_struct(simplified)
    fn_body = self.body.get_struct(simplified)

    key = f"FUNCTION[{self.prototype.name}]"
    args_struct = {"args": fn_args}
    body_struct = {"body": fn_body}

    value: ReprStruct = {**args_struct, **body_struct}
    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
265
266
267
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
259
260
261
262
263
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)
    )

FunctionCall

FunctionCall(fn: Function, args: Iterable[DataType], type_: DataType = AnyType(), loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: DataType

AST class for function call.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/callables.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def __init__(
    self,
    fn: Function,
    args: Iterable[DataType],
    type_: DataType = AnyType(),
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the Call instance."""
    super().__init__(loc=loc, parent=parent)
    self.fn = fn
    self.args = args
    self.kind = ASTKind.CallKind
    self.type_ = type_

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/callables.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    call_params = []

    for node in self.args:
        call_params.append(node.get_struct(simplified))

    key = f"FUNCTION-CALL[{self.fn.name}]"
    value = cast(
        ReprStruct,
        {
            f"Parameters ({len(call_params)})": {
                f"param({idx})": param
                for idx, param in enumerate(call_params)
            }
        },
    )

    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
265
266
267
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
259
260
261
262
263
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)
    )

FunctionPrototype

FunctionPrototype(name: str, args: Arguments, return_type: AnyType, scope: ScopeKind = global_, visibility: VisibilityKind = public, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for function prototype declaration.

Methods:

  • get_struct

    Get the AST structure that represent the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/callables.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def __init__(
    self,
    name: str,
    args: Arguments,
    return_type: AnyType,
    scope: ScopeKind = ScopeKind.global_,
    visibility: VisibilityKind = VisibilityKind.public,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the FunctionPrototype instance."""
    super().__init__(loc=loc, parent=parent)
    self.name = name
    self.args = args
    self.return_type = return_type
    self.loc = loc
    self.kind = ASTKind.PrototypeKind
    self.scope = scope
    self.visibility = visibility

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Get the AST structure that represent the object.

Source code in src/astx/callables.py
174
175
176
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Get the AST structure that represent the object."""
    raise Exception("Visitor method not necessary")

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
265
266
267
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
259
260
261
262
263
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)
    )

FunctionReturn

FunctionReturn(value: DataType, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for function return statement.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/callables.py
186
187
188
189
190
191
192
193
194
195
def __init__(
    self,
    value: DataType,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the Return instance."""
    super().__init__(loc=loc, parent=parent)
    self.value = value
    self.kind = ASTKind.ReturnKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/callables.py
201
202
203
204
205
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "RETURN"
    value = self.value.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
265
266
267
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
259
260
261
262
263
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)
    )

LambdaExpr

LambdaExpr(body: Expr, params: Arguments = Arguments(), loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for lambda expressions.

Methods:

  • get_struct

    Return the AST structure of the lambda expression.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/callables.py
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    body: Expr,
    params: Arguments = Arguments(),
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.params = params
    self.body = body
    self.kind = ASTKind.LambdaExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the lambda expression.

Source code in src/astx/callables.py
285
286
287
288
289
290
291
292
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the lambda expression."""
    key = "LambdaExpr"
    value: ReprStruct = {
        "params": self.params.get_struct(simplified),
        "body": self.body.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
265
266
267
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
259
260
261
262
263
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)
    )