Skip to content

flows

Module for controle flow AST.

ForCountLoop

ForCountLoop(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++.

Source code in src/astx/flows.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def __init__(
    self,
    initializer: InlineVariableDeclaration,
    condition: Expr,
    update: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the ForStmt instance."""
    super().__init__(loc=loc, parent=parent)
    self.initializer = initializer
    self.condition = condition
    self.update = update
    self.body = body
    self.kind = ASTKind.ForCountKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
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)
    )

ForRangeLoop

ForRangeLoop(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 Loop Range statement.

Source code in src/astx/flows.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 ForStmt 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.ForRangeKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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-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
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)
    )

If

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

Bases: StatementType

AST class for if statement.

Source code in src/astx/flows.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def __init__(
    self,
    condition: Expr,
    then: Block,
    else_: Optional[Block] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the If instance."""
    super().__init__(loc=loc, parent=parent)
    self.loc = loc
    self.condition = condition
    self.then = then
    self.else_ = else_
    self.kind = ASTKind.IfKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
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)
    )

While

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

Bases: StatementType

AST class for while statement.

Source code in src/astx/flows.py
186
187
188
189
190
191
192
193
194
195
196
197
def __init__(
    self,
    condition: Expr,
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the While instance."""
    super().__init__(loc=loc, parent=parent)
    self.condition = condition
    self.body = body
    self.kind = ASTKind.WhileKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/flows.py
203
204
205
206
207
208
209
210
211
212
213
214
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
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)
    )