-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcradle.c
203 lines (164 loc) · 3.45 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
// adapted to c/RISCV 2022 by hirosh dabui <[email protected]>
#include "cradle.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char labelName[MAX_LABEL_LEN];
static int LCount = 0;
char *token_buf;
char Value[MAX_TOKEN_LEN];
char Look = 0;
char Token = 0;
Symbol KWList[] = {{"IF"}, {"ELSE"}, {"ENDIF"}, {"END"},
{"WHILE"}, {"LOOP"}, {"REPEAT"}, {"FOR"},
{"DO"}, {"BREAK"}, {"UNTIL"}};
const char KWCode[] = "xileewprfdbu";
void Halt() {
printf("\n");
for (;;)
;
// exit(1);
}
void GetChar() { Look = getchar(); }
void Error(char *s) { printf("\nError: %s.", s); }
void Abort(char *s) {
Error(s);
Halt();
}
void Expected(char *s) {
char tmp[MAX_SPRINTF_LEN];
snprintf(tmp, MAX_SPRINTF_LEN, "%s Expected", s);
Abort(tmp);
}
char *NewLabel() {
snprintf(labelName, MAX_SPRINTF_LEN, "L%02d", LCount);
LCount++;
return labelName;
}
void PostLabel(char *L) { printf("%s:\n", L); }
void Fin() {
if (Look == '\r')
GetChar();
if (Look == '\n')
GetChar();
}
void Match(char x) {
if (Look == x) {
GetChar();
SkipWhite();
} else {
char tmp[MAX_SPRINTF_LEN];
snprintf(tmp, MAX_SPRINTF_LEN, "' %c ' ", x);
Expected(tmp);
}
}
void MatchString(char *x) {
if (strncmp(Value, x, MAX_TOKEN_LEN) != 0) {
char tmp[MAX_SPRINTF_LEN];
snprintf(tmp, MAX_SPRINTF_LEN, "' %s ' ", x);
Expected(tmp);
}
}
int IsAlpha(char c) {
char uc = UpCase(c);
return (uc >= 'A' && uc <= 'Z') || (uc >= 'a' && uc <= 'z');
}
int IsDigit(char c) { return (c >= '0' && c <= '9'); }
int IsAlNum(char c) { return IsAlpha(c) || IsDigit(c); }
// \f\n\r\t\v
int IsWhite(char c) { return strchr(" \t", c) != 0; }
void SkipWhite() {
while (IsWhite(Look)) {
GetChar();
}
}
void GetName() {
token_buf = Value;
while (Look == '\n')
Fin();
if (!IsAlpha(Look)) {
Expected("Name");
}
while (IsAlNum(Look)) {
*token_buf = UpCase(Look);
token_buf++;
GetChar();
}
*token_buf = '\0';
SkipWhite();
}
void Scan() {
GetName();
Token =
KWCode[Lookup(&KWList[0], Value, sizeof(KWList) / sizeof(Symbol)) + 1];
SkipWhite();
}
void GetOp() {
token_buf = Value;
if (!IsOp(Look)) {
Expected("Operand");
}
*token_buf = Look;
token_buf++;
GetChar();
*token_buf = '\0';
Token = '?';
}
void GetNum() {
token_buf = Value;
if (!IsDigit(Look))
Expected("Integer");
while (IsDigit(Look)) {
*token_buf = Look;
token_buf++;
GetChar();
}
*token_buf = '\0';
Token = '#';
SkipWhite();
}
int IsMulop(char c) { return strchr("*/", c) != 0; }
int IsAddop(char c) { return strchr("+-", c) != 0; }
void Emit(char *s) { printf("\t%s", s); }
void EmitLn(char *s) {
Emit(s);
printf("\n");
}
int IsBoolean(char c) {
char uc = UpCase(c);
return strchr("TF", uc) != 0;
}
int IsOp(char c) { return strchr("+-*/<>:=", c) != 0; }
int IsOrOp(char c) { return strchr("|~", c) != 0; }
int IsRelop(char c) { return strchr("=#<>", c) != 0; }
int Lookup(TabPtr T, char *s, int n) {
int found = 0;
int i = n - 1;
while (i >= 0 && !found) {
if (strncmp(s, T[i], SYMBOL_LEN) == 0) {
found = ~0;
} else {
i--;
}
}
return i;
}
int GetBoolean() {
char c = UpCase(Look);
if (!IsBoolean(Look)) {
Expected("Boolean Literal");
}
GetChar();
return (c == 'T');
}
void SkipComma() {
SkipWhite();
if (Look == ',') {
GetChar();
SkipWhite();
}
}
void Init() {
LCount = 0;
GetChar();
}