Skip to content

Emit AST

Set of classes and functions to emit the AST from a given source code.

ASTtoOutput

Bases: CodeGenBase

Show the AST for the given source code.

Source code in src/arx/codegen/ast_output.py
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
class ASTtoOutput(CodeGenBase):
    """Show the AST for the given source code."""

    result_stack: List[OutputValueAST]

    def __init__(self) -> None:
        self.result_stack: List[OutputValueAST] = []

    def visit_binary_expr(self, expr: ast.BinaryExprAST) -> None:
        """
        Visit a ast.BinaryExprAST node.

        Parameters
        ----------
            expr: The ast.BinaryExprAST node to visit.
        """
        self.visit(expr.lhs)
        lhs = self.result_stack.pop()

        self.visit(expr.rhs)
        rhs = self.result_stack.pop()

        node = {f"BINARY[{expr.op}]": {"lhs": lhs, "rhs": rhs}}
        self.result_stack.append(node)

    def visit_block(self, expr: ast.BlockAST) -> None:
        """
        Visit method for tree ast.

        Parameters
        ----------
            expr: The ast.BlockAST node to visit.
        """
        block_node = []

        for node in expr.nodes:
            self.visit(node)
            block_node.append(self.result_stack.pop())

        self.result_stack.append(block_node)

    def visit_call_expr(self, expr: ast.CallExprAST) -> None:
        """
        Visit a ast.CallExprAST node.

        Parameters
        ----------
            expr: The ast.CallExprAST node to visit.
        """
        call_args = []

        for node in expr.args:
            self.visit(node)
            call_args.append(self.result_stack.pop())

        call_node = {f"CALL[{expr.callee}]": {"args": call_args}}
        self.result_stack.append(call_node)

    def visit_float_expr(self, expr: ast.FloatExprAST) -> None:
        """
        Visit a ast.FloatExprAST node.

        Parameters
        ----------
            expr: The ast.FloatExprAST node to visit.
        """
        self.result_stack.append(f"FLOAT[{expr.value}]")

    def visit_if_stmt(self, expr: ast.IfStmtAST) -> None:
        """
        Visit an ast.IfStmtAST node.

        Parameters
        ----------
            expr: The ast.IfStmtAST node to visit.
        """
        self.visit(expr.cond)
        if_condition = self.result_stack.pop()

        self.visit(expr.then_)
        if_then = self.result_stack.pop()

        if expr.else_:
            self.visit(expr.else_)
            if_else = self.result_stack.pop()
        else:
            if_else = []

        node = {
            "IF-STMT": {
                "CONDITION": if_condition,
                "THEN": if_then,
            }
        }

        if if_else:
            node["IF-STMT"]["ELSE"] = if_else

        self.result_stack.append(node)

    def visit_for_stmt(self, expr: ast.ForStmtAST) -> None:
        """
        Visit a ast.ForStmtAST node.

        Parameters
        ----------
            expr: The ast.ForStmtAST node to visit.
        """
        self.visit(expr.start)
        for_start = self.result_stack.pop()

        self.visit(expr.end)
        for_end = self.result_stack.pop()

        self.visit(expr.step)
        for_step = self.result_stack.pop()

        self.visit(expr.body)
        for_body = self.result_stack.pop()

        node = {
            "FOR-STMT": {
                "start": for_start,
                "end": for_end,
                "step": for_step,
                "body": for_body,
            }
        }
        self.result_stack.append(node)

    def visit_function(self, expr: ast.FunctionAST) -> None:
        """
        Visit a ast.FunctionAST node.

        Parameters
        ----------
            expr: The ast.FunctionAST node to visit.
        """
        fn_args = []
        for node in expr.proto.args:
            self.visit(node)
            fn_args.append(self.result_stack.pop())

        self.visit(expr.body)
        fn_body = self.result_stack.pop()

        fn = {}
        fn[f"FUNCTION[{expr.proto.name}]"] = {
            "args": fn_args,
            "body": fn_body,
        }

        self.result_stack.append(fn)

    def visit_module(self, expr: ast.ModuleAST) -> None:
        """
        Visit method for tree ast.

        Parameters
        ----------
            expr: The ast.BlockAST node to visit.
        """
        block_node = []

        for node in expr.nodes:
            self.visit(node)
            block_node.append(self.result_stack.pop())

        module_node = {f"MODULE[{expr.name}]": block_node}

        self.result_stack.append(module_node)

    def visit_prototype(self, expr: ast.PrototypeAST) -> None:
        """
        Visit a ast.PrototypeAST node.

        Parameters
        ----------
            expr: The ast.PrototypeAST node to visit.
        """
        raise Exception("Visitor method not necessary")

    def visit_return_stmt(self, expr: ast.ReturnStmtAST) -> None:
        """
        Visit a ast.ReturnStmtAST node.

        Parameters
        ----------
            expr: The ast.ReturnStmtAST node to visit.
        """
        self.visit(expr.value)
        node = {"RETURN": self.result_stack.pop()}
        self.result_stack.append(node)

    def visit_unary_expr(self, expr: ast.UnaryExprAST) -> None:
        """
        Visit a ast.UnaryExprAST node.

        Parameters
        ----------
            expr: The ast.UnaryExprAST node to visit.
        """
        self.visit(expr.operand)
        node = {f"UNARY[{expr.op_code}]": self.result_stack.pop()}
        self.result_stack.append(node)

    def visit_var_expr(self, expr: ast.VarExprAST) -> None:
        """
        Visit a ast.VarExprAST node.

        Parameters
        ----------
            expr: The ast.VarExprAST node to visit.
        """
        raise Exception("Variable declaration will be changed soon.")

    def visit_variable_expr(self, expr: ast.VariableExprAST) -> None:
        """
        Visit a ast.VariableExprAST node.

        Parameters
        ----------
            expr: The ast.VariableExprAST node to visit.
        """
        self.result_stack.append(f"VARIABLE[{expr.name, expr.type_name}]")

    def emit_ast(self, tree_ast: ast.BlockAST) -> None:
        """Print the AST for the given source code."""
        self.visit_block(tree_ast)

        ast_output = {"ROOT": self.result_stack.pop()}
        print(yaml.dump(ast_output, sort_keys=False))

emit_ast(tree_ast)

Print the AST for the given source code.

Source code in src/arx/codegen/ast_output.py
239
240
241
242
243
244
def emit_ast(self, tree_ast: ast.BlockAST) -> None:
    """Print the AST for the given source code."""
    self.visit_block(tree_ast)

    ast_output = {"ROOT": self.result_stack.pop()}
    print(yaml.dump(ast_output, sort_keys=False))

visit_binary_expr(expr)

Visit a ast.BinaryExprAST node.

Parameters
expr: The ast.BinaryExprAST node to visit.
Source code in src/arx/codegen/ast_output.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def visit_binary_expr(self, expr: ast.BinaryExprAST) -> None:
    """
    Visit a ast.BinaryExprAST node.

    Parameters
    ----------
        expr: The ast.BinaryExprAST node to visit.
    """
    self.visit(expr.lhs)
    lhs = self.result_stack.pop()

    self.visit(expr.rhs)
    rhs = self.result_stack.pop()

    node = {f"BINARY[{expr.op}]": {"lhs": lhs, "rhs": rhs}}
    self.result_stack.append(node)

visit_block(expr)

Visit method for tree ast.

Parameters
expr: The ast.BlockAST node to visit.
Source code in src/arx/codegen/ast_output.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def visit_block(self, expr: ast.BlockAST) -> None:
    """
    Visit method for tree ast.

    Parameters
    ----------
        expr: The ast.BlockAST node to visit.
    """
    block_node = []

    for node in expr.nodes:
        self.visit(node)
        block_node.append(self.result_stack.pop())

    self.result_stack.append(block_node)

visit_call_expr(expr)

Visit a ast.CallExprAST node.

Parameters
expr: The ast.CallExprAST node to visit.
Source code in src/arx/codegen/ast_output.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def visit_call_expr(self, expr: ast.CallExprAST) -> None:
    """
    Visit a ast.CallExprAST node.

    Parameters
    ----------
        expr: The ast.CallExprAST node to visit.
    """
    call_args = []

    for node in expr.args:
        self.visit(node)
        call_args.append(self.result_stack.pop())

    call_node = {f"CALL[{expr.callee}]": {"args": call_args}}
    self.result_stack.append(call_node)

visit_float_expr(expr)

Visit a ast.FloatExprAST node.

Parameters
expr: The ast.FloatExprAST node to visit.
Source code in src/arx/codegen/ast_output.py
71
72
73
74
75
76
77
78
79
def visit_float_expr(self, expr: ast.FloatExprAST) -> None:
    """
    Visit a ast.FloatExprAST node.

    Parameters
    ----------
        expr: The ast.FloatExprAST node to visit.
    """
    self.result_stack.append(f"FLOAT[{expr.value}]")

visit_for_stmt(expr)

Visit a ast.ForStmtAST node.

Parameters
expr: The ast.ForStmtAST node to visit.
Source code in src/arx/codegen/ast_output.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def visit_for_stmt(self, expr: ast.ForStmtAST) -> None:
    """
    Visit a ast.ForStmtAST node.

    Parameters
    ----------
        expr: The ast.ForStmtAST node to visit.
    """
    self.visit(expr.start)
    for_start = self.result_stack.pop()

    self.visit(expr.end)
    for_end = self.result_stack.pop()

    self.visit(expr.step)
    for_step = self.result_stack.pop()

    self.visit(expr.body)
    for_body = self.result_stack.pop()

    node = {
        "FOR-STMT": {
            "start": for_start,
            "end": for_end,
            "step": for_step,
            "body": for_body,
        }
    }
    self.result_stack.append(node)

visit_function(expr)

Visit a ast.FunctionAST node.

Parameters
expr: The ast.FunctionAST node to visit.
Source code in src/arx/codegen/ast_output.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def visit_function(self, expr: ast.FunctionAST) -> None:
    """
    Visit a ast.FunctionAST node.

    Parameters
    ----------
        expr: The ast.FunctionAST node to visit.
    """
    fn_args = []
    for node in expr.proto.args:
        self.visit(node)
        fn_args.append(self.result_stack.pop())

    self.visit(expr.body)
    fn_body = self.result_stack.pop()

    fn = {}
    fn[f"FUNCTION[{expr.proto.name}]"] = {
        "args": fn_args,
        "body": fn_body,
    }

    self.result_stack.append(fn)

visit_if_stmt(expr)

Visit an ast.IfStmtAST node.

Parameters
expr: The ast.IfStmtAST node to visit.
Source code in src/arx/codegen/ast_output.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def visit_if_stmt(self, expr: ast.IfStmtAST) -> None:
    """
    Visit an ast.IfStmtAST node.

    Parameters
    ----------
        expr: The ast.IfStmtAST node to visit.
    """
    self.visit(expr.cond)
    if_condition = self.result_stack.pop()

    self.visit(expr.then_)
    if_then = self.result_stack.pop()

    if expr.else_:
        self.visit(expr.else_)
        if_else = self.result_stack.pop()
    else:
        if_else = []

    node = {
        "IF-STMT": {
            "CONDITION": if_condition,
            "THEN": if_then,
        }
    }

    if if_else:
        node["IF-STMT"]["ELSE"] = if_else

    self.result_stack.append(node)

visit_module(expr)

Visit method for tree ast.

Parameters
expr: The ast.BlockAST node to visit.
Source code in src/arx/codegen/ast_output.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def visit_module(self, expr: ast.ModuleAST) -> None:
    """
    Visit method for tree ast.

    Parameters
    ----------
        expr: The ast.BlockAST node to visit.
    """
    block_node = []

    for node in expr.nodes:
        self.visit(node)
        block_node.append(self.result_stack.pop())

    module_node = {f"MODULE[{expr.name}]": block_node}

    self.result_stack.append(module_node)

visit_prototype(expr)

Visit a ast.PrototypeAST node.

Parameters
expr: The ast.PrototypeAST node to visit.
Source code in src/arx/codegen/ast_output.py
185
186
187
188
189
190
191
192
193
def visit_prototype(self, expr: ast.PrototypeAST) -> None:
    """
    Visit a ast.PrototypeAST node.

    Parameters
    ----------
        expr: The ast.PrototypeAST node to visit.
    """
    raise Exception("Visitor method not necessary")

visit_return_stmt(expr)

Visit a ast.ReturnStmtAST node.

Parameters
expr: The ast.ReturnStmtAST node to visit.
Source code in src/arx/codegen/ast_output.py
195
196
197
198
199
200
201
202
203
204
205
def visit_return_stmt(self, expr: ast.ReturnStmtAST) -> None:
    """
    Visit a ast.ReturnStmtAST node.

    Parameters
    ----------
        expr: The ast.ReturnStmtAST node to visit.
    """
    self.visit(expr.value)
    node = {"RETURN": self.result_stack.pop()}
    self.result_stack.append(node)

visit_unary_expr(expr)

Visit a ast.UnaryExprAST node.

Parameters
expr: The ast.UnaryExprAST node to visit.
Source code in src/arx/codegen/ast_output.py
207
208
209
210
211
212
213
214
215
216
217
def visit_unary_expr(self, expr: ast.UnaryExprAST) -> None:
    """
    Visit a ast.UnaryExprAST node.

    Parameters
    ----------
        expr: The ast.UnaryExprAST node to visit.
    """
    self.visit(expr.operand)
    node = {f"UNARY[{expr.op_code}]": self.result_stack.pop()}
    self.result_stack.append(node)

visit_var_expr(expr)

Visit a ast.VarExprAST node.

Parameters
expr: The ast.VarExprAST node to visit.
Source code in src/arx/codegen/ast_output.py
219
220
221
222
223
224
225
226
227
def visit_var_expr(self, expr: ast.VarExprAST) -> None:
    """
    Visit a ast.VarExprAST node.

    Parameters
    ----------
        expr: The ast.VarExprAST node to visit.
    """
    raise Exception("Variable declaration will be changed soon.")

visit_variable_expr(expr)

Visit a ast.VariableExprAST node.

Parameters
expr: The ast.VariableExprAST node to visit.
Source code in src/arx/codegen/ast_output.py
229
230
231
232
233
234
235
236
237
def visit_variable_expr(self, expr: ast.VariableExprAST) -> None:
    """
    Visit a ast.VariableExprAST node.

    Parameters
    ----------
        expr: The ast.VariableExprAST node to visit.
    """
    self.result_stack.append(f"VARIABLE[{expr.name, expr.type_name}]")