-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic.c
378 lines (344 loc) · 5.42 KB
/
basic.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FS_MAXSIZE 8192
#define STACKSIZE 32
char *p;
int var[26];
int sp, err, size;
char *stack[STACKSIZE];
char buff[FS_MAXSIZE+1];
int parseExpression();
void parseStatment();
void skipSpaces();
int isDigit(char c){
if(c>='0' && c<='9') return 1;
else return 0;
}
int isAlpha(char c){
if(c>='A' && c<='Z') return 1;
if(c>='a' && c<='z') return 1;
return 0;
}
void error(char *msg){
int ln=0;
char *t=buff;
if(!err){
err=1;
while(t!=p && *t){
if(*t=='\n') ++ln;
++t;
}
printf("\nERROR: %s: line %d.\n", msg, ln+1);
}
}
void skipLabel(){
char *t=p;
skipSpaces();
while(isAlpha(*p) || isDigit(*p)) ++p;
if(*p==':'){
++p;
}else{
p=t;
}
}
void skipSpaces(){
while(*p==' ' || *p=='\t') ++p;
}
int parseNumber(){
int i=0;
char b[16];
while(*p>='0' && *p<='9'){
b[i++]=*p++;
}
b[i]=0;
return atoi(b);
}
int parseFactor(){
int a;
if(*p>='0' && *p<='9'){
a=parseNumber();
}else if(*p>='A' && *p<='Z'){
a=var[(int)(*p-'A')];
++p;
}else if(*p=='('){
++p;
a=parseExpression();
if(*p!=')') error("Missing bracket");
else ++p;
}else{
error("Syntax error");
}
return a;
}
int parseTerm(){
int a, b;
a=parseFactor();
while(1){
if(*p=='*'){
p++;
b=parseFactor();
a*=b;
}else if(*p=='/'){
p++;
b=parseFactor();
if(b!=0) a/=b;
else error("Divide by zero");
}else if(*p=='&'){
p++;
b=parseFactor();
a=a&&b;
}else if(*p=='|'){
p++;
b=parseFactor();
a=a||b;
}else if(*p=='%'){
p++;
b=parseFactor();
a=(!a && b) || (a && !b);
}else if(*p=='='){
p++;
b=parseFactor();
a=(a==b);
}else if(*p=='>'){
p++;
b=parseFactor();
a=(a>b);
}else if(*p=='<'){
p++;
b=parseFactor();
a=(a<b);
}else if(*p=='!'){
p++;
b=parseFactor();
a=(a!=b);
}else{
return a;
}
}
}
int parseExpression(){
int a, b;
if(*p=='-' || *p=='+') a=0;
else a=parseTerm();
while(1){
if(*p=='-'){
p++;
b=parseTerm();
a-=b;
}else if(*p=='+'){
p++;
b=parseTerm();
a+=b;
}else{
return a;
}
}
}
void parseCR(){
skipSpaces();
if(*p==0) error("Try to execute out of memory");
else if(*p=='\n') ++p;
else error("Syntax error");
}
void parseString(){
if(*p=='"'){
++p;
while(*p!='"' && *p!='\n' && *p!='\0'){
printf("%c", *p++);
}
if(*p!='"') error("Syntax error");
else ++p;
}
}
void parsePrint(){
while(1){
skipSpaces();
if(*p=='"') parseString();
else printf("%d", parseExpression());
skipSpaces();
if(*p==',') ++p;
else break;
}
if(*p==';') ++p;
else printf("\n");
}
void parseEnd(){
printf("\n");
if(*p!='\n' && *p!=0) error("Syntax error");
else err=1;
}
void parseInput(){
int n;
if(*p>='A' && *p<='Z'){
scanf("%d", &n);
var[(int)(*p-'A')]=n;
++p;
}else{
error("Syntax error");
}
}
void parseLet(){
int v;
if(*p>='A' && *p<='Z'){
v=(int)(*p-'A');
++p;
if(*p!='=') error("Syntax error");
++p;
var[v]=parseExpression();
}else{
error("Syntax error");
}
}
void parseIf(){
if(!parseExpression()){
while(*p!='\n' && *p!='\0') ++p;
return;
}
skipSpaces();
if(strncmp(p, "THEN ", 5)!=0){
error("Syntax error");
return;
}
p+=5;
skipSpaces();
parseStatment();
}
void parseGoto(){
int ln, i=0;
char *t=p;
char label[32];
while(isAlpha(*p) || isDigit(*p)){
label[i++]=*p++;
}
label[i++]=':';
label[i]=0;
skipSpaces();
if(*p!=0 && *p!='\n'){
error("Syntax error");
return;
}
p=buff;
ln=0;
while(1){
skipSpaces();
if(strncmp(p, label, i)==0){
skipLabel();
skipSpaces();
parseStatment();
return;
}
while(*p!='\n' && *p!='\0') ++p;
if(*p==0){
p=t;
error("Label not found");
break;
}else{
++ln;
++p;
}
}
}
void parseGosub(){
char *t=p;
while(*t!='\n' && *t!=0) ++t;
stack[sp++]=++t;
if(sp>=STACKSIZE){
error("Stack overflow");
return;
}
parseGoto();
}
void parseRet(){
if(*p!='\n' && *p!=0){
error("Syntax error");
return;
}
--sp;
if(sp<0){
error("RET without GOSUB");
return;
}
p=stack[sp];
skipLabel();
parseStatment();
}
void parseStatment(){
skipSpaces();
if(strncmp(p, "REM ", 4)==0){
p+=4;
while(*p!='\n' && *p!='\0') ++p;
}else if(strncmp(p, "PRINT ", 6)==0){
p+=6;
skipSpaces();
parsePrint();
}else if(strncmp(p, "INPUT ", 6)==0){
p+=6;
skipSpaces();
parseInput();
}else if(strncmp(p, "GOTO ", 5)==0){
p+=5;
skipSpaces();
parseGoto();
}else if(strncmp(p, "GOSUB ", 6)==0){
p+=6;
skipSpaces();
parseGosub();
}else if(strncmp(p, "IF ", 3)==0){
p+=3;
skipSpaces();
parseIf();
}else if(strncmp(p, "LET ", 4)==0){
p+=4;
skipSpaces();
parseLet();
}else if(strncmp(p, "RET", 3)==0){
p+=3;
skipSpaces();
parseRet();
}else if(strncmp(p, "END", 3)==0){
p+=3;
skipSpaces();
parseEnd();
}
}
int main(int argc, char **argv){
FILE *f;
if(argc!=2){
printf("WARNING: Illegal arguments. Usage: tb [file]\n");
return 1;
}
if(!(f=fopen(argv[1], "rb"))){
printf("WARNING: File not exist.\n");
return 2;
}
size=fread(buff, 1, 8192, f);
fclose(f);
buff[size]=0;
p=buff;
err=0;
sp=0;
while(!err){
skipLabel();
parseStatment();
parseCR();
}
return 0;
}
/* GRAMMAR
line := (label:)? statment? CR
statment := PRINT (str|expr) (, (str|expr))* ;?
IF expr THEN statment
GOTO label
INPUT var
LET var=expr
GOSUB label
REM ...
RET
END
expr := (+|-|e) term ((+|-) term)*
term := factor ((*|/) factor)*
factor := var|num|(expr)
str := "..."
label := ...
*/