-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmp1.c
70 lines (57 loc) · 1.08 KB
/
cmp1.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
63
64
65
66
67
68
69
70
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
enum {
ADD,
CONSTANT,
MULTIPLY,
};
typedef struct node {
int type;
int value;
struct node *node1, *node2;
} node;
node* nnode(int type) {
node* n = (node*) (malloc(sizeof(node)));
n->type = type;
return n;
}
node* sample() {
node *x, *y, *z;
y = nnode(CONSTANT);
y->value = 32;
z = nnode(CONSTANT);
z->value = 53;
x = nnode(ADD);
x->node1 = y;
x->node2 = z;
node *p, *q;
p = nnode(CONSTANT);
p->value = 90;
q = nnode(MULTIPLY);
q->node1 = x;
q->node2 = p;
return q;
}
void compile(node *n) {
switch (n->type) {
case CONSTANT:
printf("SET %d\n", n->value);
break;
case ADD:
compile(n->node1);
compile(n->node2);
printf("ADD\n");
break;
case MULTIPLY:
compile(n->node1);
compile(n->node2);
printf("MUL\n");
break;
}
}
int main() {
compile(sample());
return 0;
}