-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcradle.c
248 lines (204 loc) · 3.44 KB
/
cradle.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
236
237
238
239
240
241
242
243
244
245
246
247
/* -*- compile-command: "cc -Wall -Wextra -lbsd cradle.c"; -*- */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "fns.h"
#include "dat.h"
int Look; /* Lookahead Character */
/* Read New Character From Input Stream */
void
GetChar(void)
{
extern int Look;
Look = getchar();
}
/* Report an Error */
void
Error(char *s)
{
char error_str[BUFSIZ];
strlcpy(error_str, "\n Error ", BUFSIZ);
strlcat(error_str, s, BUFSIZ);
strlcat(error_str, " .", BUFSIZ);
fputs(error_str, stderr);
}
/* Report Error and Halt */
void
Abort(char *s)
{
Error(s);
exit(1);
}
/* Report What Was Expected */
void
Expected(char *s)
{
char error_str[BUFSIZ];
strlcpy(error_str, s, BUFSIZ);
strlcat(error_str, " Expected", BUFSIZ);
Abort(error_str);
}
/* Match a Specific Input Character */
void
Match(int x)
{
extern int Look;
char s[BUFSIZ];
if (Look != x){
snprintf(s, BUFSIZ, "'''' %c ''''", x);
Expected(s);
}
GetChar();
}
/* Get an Identifier */
int
GetName(void){
extern int Look;
int ch;
if (isalpha(Look) == 0)
Expected("Name");
ch = toupper(Look);
GetChar();
return ch;
}
/* Get a Number */
int
GetNum(void)
{
extern int Look;
int ch;
if (isdigit(Look) == 0)
Expected("Integer");
ch = Look;
GetChar();
return ch;
}
/* Output a String with Tab */
void
Emit(char *s)
{
putchar('\t');
fputs(s, stdout);
}
/* Output a String with Tab and LF */
void
EmitLn(char *s)
{
Emit(s);
putchar('\n');
}
/* Parse and Translate a Math Term */
void
Term(void)
{
Factor();
while (Look == '/' || Look == '*'){
EmitLn("push r16");
switch(Look){
case '*':
Multiply();
break;
case '/':
Divide();
break;
default:
Expected("Mulop");
break;
}
}
}
/* Parse and Translate an Expression */
void
Expression(void)
{
if (IsAddop(Look) == true ){
EmitLn("clr r16");
} else {
Term();
}
while (IsAddop(Look) == true){
EmitLn("push r16");
switch (Look) {
case '+':
Add();
break;
case '-':
Substract();
break;
default:
Expected("Addop");
break;
}
}
}
/* Recognize and Translate an Add */
void
Add(void)
{
Match('+');
Term();
EmitLn("pop r17");
EmitLn("add r17, r16");
}
/* Recognize and Translate a Substract */
void
Substract(void)
{
Match('-');
Term();
EmitLn("pop r17");
EmitLn("sub r17, r16");
}
/* Parse and Translate a Math Factor */
void
Factor(void)
{
char s[BUFSIZ];
int num;
if (Look == '('){
Match('(');
Expression();
Match(')');
} else {
num = GetNum();
snprintf(s, BUFSIZ, "ldi r16, %c", num);
EmitLn(s);
}
}
void
Multiply(void)
{
Match('*');
Factor();
EmitLn("pop r17");
EmitLn("muls r17, r16");
}
void
Divide(void)
{
Match('/');
Factor();
EmitLn(";; THERE IS NO DIV in AVR");
EmitLn(";; think how to implement subprogram");
}
bool
IsAddop(char ch)
{
return ch == '+' || ch == '-' ? true : false;
}
/* Initialize and Main Program of Cradle */
int
main(void)
{
#if 0
EmitLn(";; Setting stack for function calls");
EmitLn("ldi r16, high(RAMEND)");
EmitLn("out SPH, r16");
EmitLn("ldi r16, low(RAMEND)");
EmitLn("out SPL, r16)");
#endif
GetChar();
Expression();
}