-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathir_printer.c
More file actions
224 lines (203 loc) · 6.57 KB
/
ir_printer.c
File metadata and controls
224 lines (203 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
#include "ir_printer.h"
#include "ir.h"
#include <stdio.h>
// --- Forward Declarations ---
static void print_indent(int level);
static void print_expr(IRExpr* expr);
static void print_expr_list(IRExprNode* head, int indent_level);
static void print_object_list(IRObject* head, int indent_level);
static void print_with_block_list(IRWithBlock* head, int indent_level);
static void print_node(IRNode* node, int indent_level);
// --- Helper Functions ---
static void print_indent(int level) {
for (int i = 0; i < level; ++i) {
printf(" "); // 2 spaces per indent level
}
}
static void print_expr(IRExpr* expr) {
if (!expr) {
printf("NULL_EXPR");
return;
}
switch(expr->base.type) {
case IR_EXPR_LITERAL: {
IRExprLiteral* lit = (IRExprLiteral*)expr;
if (lit->is_string) printf("\"%s\"", lit->value);
else printf("%s", lit->value);
break;
}
case IR_EXPR_STATIC_STRING: {
printf("!\"%s\"", ((IRExprStaticString*)expr)->value);
break;
}
case IR_EXPR_ENUM: {
printf("%s", ((IRExprEnum*)expr)->symbol);
break;
}
case IR_EXPR_REGISTRY_REF: {
printf("%s", ((IRExprRegistryRef*)expr)->name);
break;
}
case IR_EXPR_CONTEXT_VAR: {
printf("$%s", ((IRExprContextVar*)expr)->name);
break;
}
case IR_EXPR_FUNCTION_CALL: {
IRExprFunctionCall* call = (IRExprFunctionCall*)expr;
printf("%s(", call->func_name);
IRExprNode* arg_node = call->args;
while(arg_node) {
print_expr(arg_node->expr);
if (arg_node->next) printf(", ");
arg_node = arg_node->next;
}
printf(")");
break;
}
case IR_EXPR_ARRAY: {
IRExprArray* arr = (IRExprArray*)expr;
printf("[");
IRExprNode* elem_node = arr->elements;
while(elem_node) {
print_expr(elem_node->expr);
if (elem_node->next) printf(", ");
elem_node = elem_node->next;
}
printf("]");
break;
}
case IR_EXPR_RUNTIME_REG_ADD: {
IRExprRuntimeRegAdd* reg = (IRExprRuntimeRegAdd*)expr;
printf("register(\"%s\", ", reg->id);
print_expr(reg->object_expr);
printf(")");
break;
}
case IR_EXPR_IF_BACKEND: {
IRIfBackend* if_node = (IRIfBackend*)expr;
printf("if_backend(static=");
print_expr(if_node->static_expr);
printf(", dynamic=");
print_expr(if_node->dynamic_expr);
printf(")");
break;
}
default:
printf("UNKNOWN_EXPR");
break;
}
}
static void print_node(IRNode* node, int indent_level) {
print_indent(indent_level);
if (!node) {
printf("NULL_NODE\n");
return;
}
switch(node->type) {
case IR_NODE_OBJECT:
print_object_list((IRObject*)node, indent_level);
break;
case IR_NODE_WARNING:
printf("[HINT] %s\n", ((IRWarning*)node)->message);
break;
case IR_NODE_OBSERVER: {
IRObserver* obs = (IRObserver*)node;
printf("observes(\"%s\", type=%d, config=", obs->state_name, obs->update_type);
print_expr(obs->config_expr);
printf(")\n");
break;
}
case IR_NODE_ACTION: {
IRAction* act = (IRAction*)node;
printf("action(\"%s\", type=%d, data=", act->action_name, act->action_type);
if(act->data_expr) {
print_expr(act->data_expr);
} else {
printf("NULL");
}
printf(")\n");
break;
}
default:
// Must be an expression type
print_expr((IRExpr*)node);
printf("\n");
break;
}
}
static void print_expr_list(IRExprNode* head, int indent_level) {
IRExprNode* current = head;
while(current) {
print_node((IRNode*)current->expr, indent_level);
current = current->next;
}
}
static void print_object_list(IRObject* head, int indent_level) {
IRObject* current = head;
while(current) {
print_indent(indent_level);
printf("[OBJECT c_name=\"%s\" json_type=\"%s\"", current->c_name, current->json_type);
if (current->registered_id) {
printf(" id=\"%s\"", current->registered_id);
}
printf("]\n");
if (current->constructor_expr) {
print_indent(indent_level + 1);
printf("CONSTRUCTOR: ");
print_expr(current->constructor_expr);
printf("\n");
} else {
print_indent(indent_level + 1);
printf("CONSTRUCTOR: NULL (declare variable, do not assign from call)\n");
}
if (current->operations) {
IROperationNode* op_node = current->operations;
while (op_node) {
print_node(op_node->op_node, indent_level + 1);
op_node = op_node->next;
}
}
if (current->with_blocks) {
print_with_block_list(current->with_blocks, indent_level + 1);
}
current = current->next;
}
}
static void print_with_block_list(IRWithBlock* head, int indent_level) {
IRWithBlock* current = head;
while (current) {
print_indent(indent_level);
printf("[WITH target=");
print_expr(current->target_expr);
printf("]\n");
if (current->setup_calls) {
print_indent(indent_level + 1);
printf("SETUP_CALLS:\n");
print_expr_list(current->setup_calls, indent_level + 2);
}
if (current->children_root) {
print_indent(indent_level + 1);
printf("[DO]\n");
print_object_list(current->children_root, indent_level + 2);
}
current = current->next;
}
}
// --- Main Backend Function ---
void ir_print_backend(IRRoot* root, const ApiSpec* api_spec) {
(void)api_spec; // Not used in this backend
if (!root) {
printf("[IR_PRINTER] IR Root is NULL.\n");
return;
}
printf("[ROOT]\n");
if (root->components) {
// TODO: Implement printing for component definitions if needed
}
if (root->root_objects) {
print_object_list(root->root_objects, 1);
} else {
print_indent(1);
printf("(No root objects)\n");
}
}