-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny.c
235 lines (212 loc) · 3.74 KB
/
tiny.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
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
225
226
227
228
229
230
231
232
233
234
235
#include <stdio.h>
// IO Stuff Here
// The Lookahead Character
char Look;
// The Encoded Token This Should Be Global
char Token;
// Variable List
char VarList[90];
// Label Number
int LabNum = 0;
// The Unencoded Token Not Sure If This Should Be Global
// char Value[];
void GetChar(void) {
Look = getchar();
}
void Match(char c) {
if (Look != c)
printf("Expected %c\n, c");
else
GetChar();
}
int IsAlph(void) {
if (((Look <= 'z') && (Look >= 'a')) || ((Look <= 'Z') && (Look >= 'A')))
return 0;
else
return 1;
}
int IsDigit(void) {
if ((Look <= '0') && (Look >= '9'))
return 0;
else
return 1;
}
int IsAlphNum(void) {
if ((IsAlph == '0') || (IsDigit == '0'))
return 0;
else
return 1;
}
int IsWhite(void) {
if ((Look == ' ') || ((Look == '\t') || (Look == '\n')))
return 0;
else
return 1;
}
void SkipWhite(void) {
while (IsWhite == 0) {
GetChar();
}
}
// Put A Label
void PutLabel(int lab) {
printf(".L%d", lab);
}
// Place a Label and new line
void PlaceLabel(int lab) {
PutLabel(lab);
printf("\n");
}
// Get A Lable
int GetLabel(void) {
int i = LabNum;
LabNum = ++LabNum;
return i;
}
// The Parser Proper
// Parse and Translate Variables
void Variables(void) {
int offset;
int i;
i = 0;
Match('v');
printf("\tpushl %%epb\n" "\tmovl %%esp, %%ebp\n");
while(Look != 'e') {
VarList[i] = Look;
i = ++i;
GetChar();
}
offset = ((4 * i) + 4);
printf("\tsubl $%d, %%esp\n", offset);
Match('e');
}
// Parse and Translate an IF statement
void DoIf(void) {
int lab;
lab = GetLabel();
Match('i');
BoolExpression();
printf("\tjne ");
PlaceLabel(lab);
while (Look != 't')
GetChar();
Match('l');
Block();
PlaceLabel(lab);
Match('e');
}
// Parse and Translate a While Statement
void DoWhile(void) {
int lab;
int scndlab;
lab = GetLabel();
scndlab = GetLabel();
Match('w');
PlaceLabel(lab);
BoolExpression();
printf("\tjne");
PlaceLabel(scndlab);
Block();
printf("\tjmp");
PlaceLabel(lab);
PlaceLabel(scndlab);
Match('e');
}
// Parse and Translate a For Statement
/* void DoFor(void) {
Match('f');
while (Look != 'e')
GetChar();
Block();
Match('e');
}*/
void Expression(void) {
printf("EXP\n");
GetChar();
}
// Parse and Translate A relation
void Relation(void) {
Expression();
Match('q');
Expression();
printf("\tcmp %%ecx, %%ebx\n");
}
// Parse and Translate A Boolean Factor
void BoolFactor(void) {
if (Look == 'T') {
printf("\tmov1 $-1, %%ecx\n");
Match('T');
}
else if (Look == 'F') {
printf("\tmovel $0, %%ecx\n");
Match('F');
}
else
Relation();
}
//Parse and Translate A Not Factor
void NotFactor(void) {
if (Look == '~') {
Match('~');
BoolFactor();
printf("\tNOT %%ecx\n");
}
else
BoolFactor();
}
// Parse and Translate a Boolean And Operation
void AndOp(void) {
Match('&');
NotFactor();
printf("\tAND %%ebx, %%ecx\n");
}
// Parse and Translate a Boolean Term
void BoolTerm(void) {
NotFactor();
if (Look == '&')
AndOp();
}
// Parsre and Translate A Boolean OR
void OrOp(void) {
Match('|');
BoolTerm();
printf("\tOR %%ebx, %%eax\n");
}
//Parse and Translate a Boolean Expression
void BoolExpression(void) {
BoolTerm();
if (Look == '|') {
// printf("\tpush %%eax\n\tmove1 %%ebx, %%eax");
OrOp();
}
}
//Parse and Translate a Program Block
void Block(void) {
Match('b');
if (Look == 'v')
Variables();
else if (Look == 'i')
DoIf();
else if (Look == 'w')
DoWhile();
// else if (Look == 'f')
// DoFor();
while (Look != 'e')
BoolExpression();
Match('e');
}
// Init
void Init(void) {
GetChar();
}
// A program is an 'e', or it is a block
void DoProgram(void) {
while (Look != 'e')
Block();
printf("The program is finished");
}
// Main
main() {
Init();
DoProgram();
}