-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecrice1.c
190 lines (169 loc) · 3.45 KB
/
execrice1.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
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
#include <stdio.h>
#include <stdlib.h>
#define TODO() \
do{ \
printf("\nAdd your code here: file \"%s\", line %d\n", \
__FILE__, __LINE__); \
} while (0)
///////////////////////////////////////////////
// Data structures for the Sum language.
enum Exp_Kind_t { EXP_INT, EXP_SUM };
struct Exp_t
{
enum Exp_Kind_t kind;
};
struct Exp_Int
{
enum Exp_Kind_t kind;
int i;
};
struct Exp_Sum
{
enum Exp_Kind_t kind;
struct Exp_t *left;
struct Exp_t *right;
};
// "constructors"
struct Exp_t *Exp_Int_new(int i)
{
struct Exp_Int *p = malloc(sizeof(*p));
p->kind = EXP_INT;
p->i = i;
return (struct Exp_t *)p;
}
struct Exp_t *Exp_Sum_new(struct Exp_t *left, struct Exp_t *right)
{
struct Exp_Sum *p = malloc(sizeof(*p));
p->kind = EXP_SUM;
p->left = left;
p->right = right;
return (struct Exp_t *)p;
}
// "printer"
void Exp_print(struct Exp_t *exp)
{
switch (exp->kind){
case EXP_INT:{
struct Exp_Int *p = (struct Exp_Int *)exp;
printf("%d", p->i);
break;
}
case EXP_SUM:{
struct Exp_Sum *p = (struct Exp_Sum *)exp;
Exp_print(p->left);
printf("+");
Exp_print(p->right);
break;
}
default:
break;
}
}
//////////////////////////////////////////////
// Data structures for the Stack language.
enum Stack_Kind_t { STACK_ADD, STACK_PUSH };
struct Stack_t
{
enum Stack_Kind_t kind;
};
struct Stack_Add
{
enum Stack_Kind_t kind;
};
struct Stack_Push
{
enum Stack_Kind_t kind;
int i;
};
// "constructors"
struct Stack_t *Stack_Add_new()
{
struct Stack_Add *p = malloc(sizeof(*p));
p->kind = STACK_ADD;
return (struct Stack_t *)p;
}
struct Stack_t *Stack_Push_new(int i)
{
struct Stack_Push *p = malloc(sizeof(*p));
p->kind = STACK_PUSH;
p->i = i;
return (struct Stack_t *)p;
}
/// instruction list
struct List_t
{
struct Stack_t *instr;
struct List_t *next;
};
struct List_t *List_new(struct Stack_t *instr, struct List_t *next)
{
struct List_t *p = malloc(sizeof (*p));
p->instr = instr;
p->next = next;
return p;
}
// "printer"
void List_reverse_print(struct List_t *list)
{
printf("\n");
while (list != NULL) {
if (list->instr->kind == STACK_PUSH) {
struct Exp_Int *p = (struct Stack_Push *)list->instr;
printf("PUSH %d\n", p->i);
}
else if (list->instr->kind == STACK_ADD) {
printf("ADD\n");
}
list = list->next;
}
}
//////////////////////////////////////////////////
// a compiler from Sum to Stack
struct List_t *all = 0;
void emit(struct Stack_t *instr)
{
all = List_new(instr, all);
}
void compile(struct Exp_t *exp)
{
switch (exp->kind){
case EXP_INT:{
struct Exp_Int *p = (struct Exp_Int *)exp;
emit(Stack_Push_new(p->i));
break;
}
case EXP_SUM:{
struct Exp_Sum *p = (struct Exp_Sum *)exp;
compile(p->left);
compile(p->right);
emit(Stack_Add_new());
break;
}
default:
break;
}
}
//////////////////////////////////////////////////
// program entry
int main()
{
printf("Compile starting\n");
// build an expression tree:
// +
// / \
// + 4
// / \
// 2 3
struct Exp_t *exp = Exp_Sum_new(Exp_Sum_new(Exp_Int_new(2)
, Exp_Int_new(3))
, Exp_Int_new(4));
// print out this tree:
printf("the expression is:\n");
Exp_print(exp);
// compile this tree to Stack machine instructions
compile(exp);
// print out the generated Stack instructons:
List_reverse_print(all);
printf("\nCompile finished\n");
return 0;
}