Skip to content

flows

Module for controle flow AST.

Classes:

ForCountLoopExpr

ForCountLoopExpr(initializer: InlineVariableDeclaration, condition: Expr, update: Expr, body: Block, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for a simple Count-Controlled For Loop expression.

This is a very basic for loop, used by languages like C or C++.

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/flows.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
def __init__(
    self,
    initializer: InlineVariableDeclaration,
    condition: Expr,
    update: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the ForLoopCountExpr instance."""
    super().__init__(loc=loc, parent=parent)
    self.initializer = initializer
    self.condition = condition
    self.update = update
    self.body = body
    self.kind = ASTKind.ForCountLoopExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    for_init = {"initialization": self.initializer.get_struct(simplified)}
    for_cond = {"condition": self.condition.get_struct(simplified)}
    for_update = {"update": self.update.get_struct(simplified)}
    for_body = self.body.get_struct(simplified)

    key = "FOR-COUNT-EXPR"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, for_init),
        **cast(DictDataTypesStruct, for_cond),
        **cast(DictDataTypesStruct, for_update),
        **cast(DictDataTypesStruct, for_body),
    }
    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)
    )

ForCountLoopStmt

ForCountLoopStmt(initializer: InlineVariableDeclaration, condition: Expr, update: Expr, body: Block, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for a simple Count-Controlled For Loop statement.

This is a very basic for loop, used by languages like C or C++.

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/flows.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def __init__(
    self,
    initializer: InlineVariableDeclaration,
    condition: Expr,
    update: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the ForCountLoopStmt instance."""
    super().__init__(loc=loc, parent=parent)
    self.initializer = initializer
    self.condition = condition
    self.update = update
    self.body = body
    self.kind = ASTKind.ForCountLoopStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    for_init = {"initialization": self.initializer.get_struct(simplified)}
    for_cond = {"condition": self.condition.get_struct(simplified)}
    for_update = {"update": self.update.get_struct(simplified)}
    for_body = self.body.get_struct(simplified)

    key = "FOR-COUNT-STMT"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, for_init),
        **cast(DictDataTypesStruct, for_cond),
        **cast(DictDataTypesStruct, for_update),
        **cast(DictDataTypesStruct, for_body),
    }

    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)
    )

ForRangeLoopExpr

ForRangeLoopExpr(variable: InlineVariableDeclaration, start: Expr, end: Expr, step: Expr, body: Block, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for For Range Expression.

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/flows.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def __init__(
    self,
    variable: InlineVariableDeclaration,
    start: Expr,
    end: Expr,
    step: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the ForRangeLoopExpr instance."""
    super().__init__(loc=loc, parent=parent)
    self.variable = variable
    self.start = start
    self.end = end
    self.step = step
    self.body = body
    self.kind = ASTKind.ForRangeLoopExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    for_var = {"var": self.variable.get_struct(simplified)}
    for_start = {"start": self.start.get_struct(simplified)}
    for_end = {"end": self.end.get_struct(simplified)}
    for_step = {"step": self.step.get_struct(simplified)}
    for_body = self.body.get_struct(simplified)

    key = "FOR-RANGE-LOOP-EXPR"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, for_var),
        **cast(DictDataTypesStruct, for_start),
        **cast(DictDataTypesStruct, for_end),
        **cast(DictDataTypesStruct, for_step),
        **cast(DictDataTypesStruct, for_body),
    }
    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)
    )

ForRangeLoopStmt

ForRangeLoopStmt(variable: InlineVariableDeclaration, start: Expr, end: Expr, step: Expr, body: Block, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for For Range 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/flows.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def __init__(
    self,
    variable: InlineVariableDeclaration,
    start: Expr,
    end: Expr,
    step: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the ForRangeLoopStmt instance."""
    super().__init__(loc=loc, parent=parent)
    self.variable = variable
    self.start = start
    self.end = end
    self.step = step
    self.body = body
    self.kind = ASTKind.ForRangeLoopStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    for_start = {"start": self.start.get_struct(simplified)}
    for_end = {"end": self.end.get_struct(simplified)}
    for_step = {"step": self.step.get_struct(simplified)}
    for_body = self.body.get_struct(simplified)

    key = "FOR-RANGE-LOOP-STMT"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, for_start),
        **cast(DictDataTypesStruct, for_end),
        **cast(DictDataTypesStruct, for_step),
        **cast(DictDataTypesStruct, for_body),
    }
    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)
    )

IfExpr

IfExpr(condition: Expr, then: Block, else_: Optional[Block] = None, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for if expression.

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/flows.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def __init__(
    self,
    condition: Expr,
    then: Block,
    else_: Optional[Block] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the IfExpr instance."""
    super().__init__(loc=loc, parent=parent)
    self.loc = loc
    self.condition = condition
    self.then = then
    self.else_ = else_
    self.kind = ASTKind.IfExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    if_condition = {"condition": self.condition.get_struct(simplified)}
    if_then = {"then-block": self.then.get_struct(simplified)}
    if_else: ReprStruct = {}

    if self.else_ is not None:
        if_else = {"else-block": self.else_.get_struct(simplified)}

    key = "IF-EXPR"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, if_condition),
        **cast(DictDataTypesStruct, if_then),
        **cast(DictDataTypesStruct, if_else),
    }

    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)
    )

IfStmt

IfStmt(condition: Expr, then: Block, else_: Optional[Block] = None, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for if 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/flows.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def __init__(
    self,
    condition: Expr,
    then: Block,
    else_: Optional[Block] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the IfStmt instance."""
    super().__init__(loc=loc, parent=parent)
    self.loc = loc
    self.condition = condition
    self.then = then
    self.else_ = else_
    self.kind = ASTKind.IfStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    if_condition = {"condition": self.condition.get_struct(simplified)}
    if_then = {"then-block": self.then.get_struct(simplified)}
    if_else: ReprStruct = {}

    if self.else_ is not None:
        if_else = {"else-block": self.else_.get_struct(simplified)}

    key = "IF-STMT"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, if_condition),
        **cast(DictDataTypesStruct, if_then),
        **cast(DictDataTypesStruct, if_else),
    }

    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)
    )

WhileExpr

WhileExpr(condition: Expr, body: Block, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for while expression.

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/flows.py
392
393
394
395
396
397
398
399
400
401
402
403
def __init__(
    self,
    condition: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the WhileExpr instance."""
    super().__init__(loc=loc, parent=parent)
    self.condition = condition
    self.body = body
    self.kind = ASTKind.WhileExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
409
410
411
412
413
414
415
416
417
418
419
420
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    while_condition = self.condition.get_struct(simplified)
    while_body = self.body.get_struct(simplified)

    key = "WHILE-EXPR"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, while_condition),
        **cast(DictDataTypesStruct, while_body),
    }

    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)
    )

WhileStmt

WhileStmt(condition: Expr, body: Block, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for while 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/flows.py
353
354
355
356
357
358
359
360
361
362
363
364
def __init__(
    self,
    condition: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the WhileStmt instance."""
    super().__init__(loc=loc, parent=parent)
    self.condition = condition
    self.body = body
    self.kind = ASTKind.WhileStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
370
371
372
373
374
375
376
377
378
379
380
381
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    while_condition = self.condition.get_struct(simplified)
    while_body = self.body.get_struct(simplified)

    key = "WHILE-STMT"
    value: ReprStruct = {
        **cast(DictDataTypesStruct, while_condition),
        **cast(DictDataTypesStruct, while_body),
    }

    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)
    )