Skip to content

variables

Module for Variables.

InlineVariableDeclaration

InlineVariableDeclaration(name: str, type_: ExprType, mutability: MutabilityKind = MutabilityKind.constant, visibility: VisibilityKind = VisibilityKind.public, scope: ScopeKind = ScopeKind.local, value: Expr = UNDEFINED, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for inline variable declaration expression.

Can be used in expressions like for loops.

Source code in src/astx/variables.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def __init__(
    self,
    name: str,
    type_: ExprType,
    mutability: MutabilityKind = MutabilityKind.constant,
    visibility: VisibilityKind = VisibilityKind.public,
    scope: ScopeKind = ScopeKind.local,
    value: Expr = UNDEFINED,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the VarExprAST instance."""
    super().__init__(loc=loc, parent=parent)
    self.mutability = mutability
    self.scope = scope
    self.visibility = visibility
    self.name = name
    self.type_ = type_
    self.value = value
    self.kind = ASTKind.VarDeclKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a string that represents the object.

Source code in src/astx/variables.py
109
110
111
112
113
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a string that represents the object."""
    key = str(self)
    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
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)
    )

Variable

Variable(name: str, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: DataTypeOps

AST class for the variable usage.

Source code in src/astx/variables.py
154
155
156
157
158
159
160
161
162
def __init__(
    self,
    name: str,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the Variable instance."""
    super().__init__(loc=loc, parent=parent)
    self.name = name

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a string that represents the object.

Source code in src/astx/variables.py
168
169
170
171
172
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a string that represents the object."""
    key = f"Variable[{self.name}]"
    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)
    )

VariableAssignment

VariableAssignment(name: str, value: Expr, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for variable declaration.

Source code in src/astx/variables.py
123
124
125
126
127
128
129
130
131
132
133
134
135
def __init__(
    self,
    name: str,
    value: Expr,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the VarExprAST instance."""
    super().__init__(loc=loc, parent=parent)
    self.loc = loc
    self.name = name
    self.value = value
    self.kind = ASTKind.VariableAssignmentKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a string that represents the object.

Source code in src/astx/variables.py
141
142
143
144
145
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a string that represents the object."""
    key = str(self)
    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
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)
    )

VariableDeclaration

VariableDeclaration(name: str, type_: ExprType, mutability: MutabilityKind = MutabilityKind.constant, visibility: VisibilityKind = VisibilityKind.public, scope: ScopeKind = ScopeKind.local, value: Expr = UNDEFINED, parent: Optional[ASTNodes] = None, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: StatementType

AST class for variable declaration.

Source code in src/astx/variables.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(
    self,
    name: str,
    type_: ExprType,
    mutability: MutabilityKind = MutabilityKind.constant,
    visibility: VisibilityKind = VisibilityKind.public,
    scope: ScopeKind = ScopeKind.local,
    value: Expr = UNDEFINED,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the VarExprAST instance."""
    super().__init__(loc=loc, parent=parent)
    self.mutability = mutability
    self.scope = scope
    self.visibility = visibility
    self.name = name
    self.type_ = type_
    self.value = value
    self.kind = ASTKind.VarDeclKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a string that represents the object.

Source code in src/astx/variables.py
63
64
65
66
67
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a string that represents the object."""
    key = str(self)
    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
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)
    )