-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdcpuCompiler.c
62 lines (47 loc) · 1.31 KB
/
dcpuCompiler.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
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "ast.h"
#include "parse_utils.h"
#include "dcpuIR.h"
void compile_dcpu(node_t* node) {
switch (node->type) {
case NUM:
emit_num(((num_node_t*)node)->value);
break;
case ADD:
compile_dcpu(((binary_node_t*)node)->left);
compile_dcpu(((binary_node_t*)node)->right);
emit_add();
break;
case SUB:
compile_dcpu(((binary_node_t*)node)->left);
compile_dcpu(((binary_node_t*)node)->right);
emit_sub();
break;
case MUL:
compile_dcpu(((binary_node_t*)node)->left);
compile_dcpu(((binary_node_t*)node)->right);
emit_mul();
break;
//case DIV:
case UMINUS:
compile_dcpu(((unary_node_t*)node)->child);
emit_uminus();
break;
default:
fprintf(stderr, "ERROR: Undefined eval for operation %d\n!", node->type);
exit(1);
//return NULL;
}
}
int main(int argc, char *argv[]) {
printf("Input an expression to compile: ");
node_t* root = parse_root();
printf("Result:\n");
compile_dcpu(root);
dcpu_print();
dcpu_free();
free_ast(root);
return 0;
}