Skip to content

packages

Define ASTx for more broader scope.

Module

Module(name: str = 'main', loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: Block

AST main expression class.

Source code in src/astx/packages.py
49
50
51
52
53
54
55
56
def __init__(
    self,
    name: str = "main",
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the AST instance."""
    super().__init__(name=name, loc=loc)
    self.kind = ASTKind.ModuleKind

block property

block: list[AST]

Define an alias for self.nodes.

append

append(value: AST) -> None

Append a new node to the stack.

Source code in src/astx/base.py
259
260
261
def append(self, value: AST) -> 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/packages.py
67
68
69
70
71
72
73
74
75
76
77
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    block_node = []

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

    key = f"MODULE[{self.name}]"
    value = cast(ReprStruct, block_node)

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

Package

Package(name: str = 'main', modules: list[Module] = [], packages: list[Package] = [], loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: ASTNodes

AST class for Package.

Source code in src/astx/packages.py
88
89
90
91
92
93
94
95
96
97
98
99
def __init__(
    self,
    name: str = "main",
    modules: list[Module] = [],
    packages: list[Package] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the AST instance."""
    super().__init__(loc=loc)
    self.name = name
    self.modules = copy.deepcopy(modules)
    self.packages = copy.deepcopy(packages)

append

append(value: AST) -> None

Append a new node to the stack.

Source code in src/astx/base.py
259
260
261
def append(self, value: AST) -> 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/packages.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    packages = []
    modules = []

    for package in self.packages:
        packages.append(package.get_struct(simplified))

    for module in self.modules:
        modules.append(module.get_struct(simplified))

    key = str(self)
    value = cast(
        ReprStruct,
        {
            "modules": modules,
            "packages": packages,
        },
    )

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

Program

Program(name: str = 'main', target: Target = Target('', ''), modules: list[Module] = [], packages: list[Package] = [], loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: Package

AST class for Program.

Source code in src/astx/packages.py
134
135
136
137
138
139
140
141
142
143
144
145
146
def __init__(
    self,
    name: str = "main",
    target: Target = Target("", ""),
    modules: list[Module] = [],
    packages: list[Package] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the AST instance."""
    super().__init__(
        name=name, modules=modules, packages=packages, loc=loc
    )
    self.target = copy.deepcopy(target)

append

append(value: AST) -> None

Append a new node to the stack.

Source code in src/astx/base.py
259
260
261
def append(self, value: AST) -> 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/packages.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    packages = []
    modules = []

    for package in self.packages:
        packages.append(package.get_struct(simplified))

    for module in self.modules:
        modules.append(module.get_struct(simplified))

    key = str(self)
    value = cast(
        ReprStruct,
        {
            "modules": modules,
            "packages": packages,
        },
    )

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

Target

Target(datalayout: str, triple: str)

Bases: Expr

Define the Architecture target for the program.

Source code in src/astx/packages.py
30
31
32
33
34
def __init__(self, datalayout: str, triple: str) -> None:
    """Initialize the AST instance."""
    super().__init__()
    self.datalayout = datalayout
    self.triple = triple

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/packages.py
36
37
38
39
40
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "TARGET"
    value = f"{self.datalayout}, {self.triple}"
    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)
    )