-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeneration.c
29 lines (29 loc) · 982 Bytes
/
Generation.c
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
void generateAssembly(ASTNode* node) {
if (!node) return;
switch (node->type) {
case NODE_VAR_DECL:
printf("; Declare variable %s\n", node->text);
break;
case NODE_ASSIGN:
printf("; Assign to variable %s\n", node->text);
generateAssembly(node->left);
break;
case NODE_ARITH_OP:
generateAssembly(node->left);
generateAssembly(node->right);
if (strcmp(node->text, "+") == 0) {
printf("ADD A, B\n");
} else if (strcmp(node->text, "-") == 0) {
printf("SUB A, B\n");
}
break;
case NODE_CONDITIONAL:
printf("; If condition\n");
generateAssembly(node->left);
printf("CMP A, B\n");
printf("JNZ END_IF\n");
generateAssembly(node->right);
printf("END_IF:\n");
break;
}
}