2 17e5fb89 2004-04-21 devnull #include <u.h>
3 17e5fb89 2004-04-21 devnull #include <libc.h>
4 17e5fb89 2004-04-21 devnull #include <bio.h>
6 17e5fb89 2004-04-21 devnull #define bsp_max 5000
8 17e5fb89 2004-04-21 devnull Biobuf *in;
9 0aae8d3d 2005-10-31 devnull Biobuf bstdin;
10 0aae8d3d 2005-10-31 devnull Biobuf bstdout;
11 17e5fb89 2004-04-21 devnull char cary[1000];
12 17e5fb89 2004-04-21 devnull char* cp = { cary };
13 17e5fb89 2004-04-21 devnull char string[1000];
14 17e5fb89 2004-04-21 devnull char* str = { string };
15 17e5fb89 2004-04-21 devnull int crs = 128;
16 17e5fb89 2004-04-21 devnull int rcrs = 128; /* reset crs */
17 17e5fb89 2004-04-21 devnull int bindx = 0;
18 17e5fb89 2004-04-21 devnull int lev = 0;
20 0aae8d3d 2005-10-31 devnull char* ttp;
21 17e5fb89 2004-04-21 devnull char* ss = "";
22 17e5fb89 2004-04-21 devnull int bstack[10] = { 0 };
23 17e5fb89 2004-04-21 devnull char* numb[15] =
25 17e5fb89 2004-04-21 devnull " 0", " 1", " 2", " 3", " 4", " 5",
26 17e5fb89 2004-04-21 devnull " 6", " 7", " 8", " 9", " 10", " 11",
27 17e5fb89 2004-04-21 devnull " 12", " 13", " 14"
29 0aae8d3d 2005-10-31 devnull char* pre;
30 0aae8d3d 2005-10-31 devnull char* post;
32 17e5fb89 2004-04-21 devnull long peekc = -1;
33 17e5fb89 2004-04-21 devnull int sargc;
34 17e5fb89 2004-04-21 devnull int ifile;
35 17e5fb89 2004-04-21 devnull char** sargv;
37 17e5fb89 2004-04-21 devnull char *funtab[] =
39 17e5fb89 2004-04-21 devnull "<1>","<2>","<3>","<4>","<5>",
40 17e5fb89 2004-04-21 devnull "<6>","<7>","<8>","<9>","<10>",
41 17e5fb89 2004-04-21 devnull "<11>","<12>","<13>","<14>","<15>",
42 17e5fb89 2004-04-21 devnull "<16>","<17>","<18>","<19>","<20>",
43 17e5fb89 2004-04-21 devnull "<21>","<22>","<23>","<24>","<25>",
46 17e5fb89 2004-04-21 devnull char *atab[] =
48 17e5fb89 2004-04-21 devnull "<221>","<222>","<223>","<224>","<225>",
49 17e5fb89 2004-04-21 devnull "<226>","<227>","<228>","<229>","<230>",
50 17e5fb89 2004-04-21 devnull "<231>","<232>","<233>","<234>","<235>",
51 17e5fb89 2004-04-21 devnull "<236>","<237>","<238>","<239>","<240>",
52 17e5fb89 2004-04-21 devnull "<241>","<242>","<243>","<244>","<245>",
55 17e5fb89 2004-04-21 devnull char* letr[26] =
57 17e5fb89 2004-04-21 devnull "a","b","c","d","e","f","g","h","i","j",
58 17e5fb89 2004-04-21 devnull "k","l","m","n","o","p","q","r","s","t",
59 17e5fb89 2004-04-21 devnull "u","v","w","x","y","z"
61 17e5fb89 2004-04-21 devnull char* dot = { "." };
62 0aae8d3d 2005-10-31 devnull char* bspace[bsp_max];
63 0aae8d3d 2005-10-31 devnull char** bsp_nxt = bspace;
64 17e5fb89 2004-04-21 devnull int bdebug = 0;
65 17e5fb89 2004-04-21 devnull int lflag;
66 17e5fb89 2004-04-21 devnull int cflag;
67 17e5fb89 2004-04-21 devnull int sflag;
69 0aae8d3d 2005-10-31 devnull char* bundle(int, ...);
70 0aae8d3d 2005-10-31 devnull void conout(char*, char*);
71 17e5fb89 2004-04-21 devnull int cpeek(int, int, int);
72 17e5fb89 2004-04-21 devnull int getch(void);
73 0aae8d3d 2005-10-31 devnull char* geta(char*);
74 0aae8d3d 2005-10-31 devnull char* getf(char*);
75 17e5fb89 2004-04-21 devnull void getout(void);
76 0aae8d3d 2005-10-31 devnull void output(char*);
77 17e5fb89 2004-04-21 devnull void pp(char*);
78 0aae8d3d 2005-10-31 devnull void routput(char*);
79 17e5fb89 2004-04-21 devnull void tp(char*);
80 17e5fb89 2004-04-21 devnull void yyerror(char*, ...);
81 17e5fb89 2004-04-21 devnull int yyparse(void);
83 17e5fb89 2004-04-21 devnull typedef void* pointer;
84 0aae8d3d 2005-10-31 devnull #pragma varargck type "lx" pointer
89 17e5fb89 2004-04-21 devnull char* cptr;
93 0aae8d3d 2005-10-31 devnull %type <cptr> pstat stat stat1 def slist dlets e ase nase
94 0aae8d3d 2005-10-31 devnull %type <cptr> slist re fprefix cargs eora cons constant lora
95 17e5fb89 2004-04-21 devnull %type <cptr> crs
97 17e5fb89 2004-04-21 devnull %token <cptr> LETTER EQOP _AUTO DOT
98 17e5fb89 2004-04-21 devnull %token <cc> DIGIT SQRT LENGTH _IF FFF EQ
99 17e5fb89 2004-04-21 devnull %token <cc> _PRINT _WHILE _FOR NE LE GE INCR DECR
100 17e5fb89 2004-04-21 devnull %token <cc> _RETURN _BREAK _DEFINE BASE OBASE SCALE
101 17e5fb89 2004-04-21 devnull %token <cc> QSTR ERROR
103 17e5fb89 2004-04-21 devnull %right '=' EQOP
104 17e5fb89 2004-04-21 devnull %left '+' '-'
105 17e5fb89 2004-04-21 devnull %left '*' '/' '%'
106 17e5fb89 2004-04-21 devnull %right '^'
107 17e5fb89 2004-04-21 devnull %left UMINUS
111 17e5fb89 2004-04-21 devnull start stuff
115 17e5fb89 2004-04-21 devnull pstat tail
117 17e5fb89 2004-04-21 devnull output($1);
119 17e5fb89 2004-04-21 devnull | def dargs ')' '{' dlist slist '}'
121 17e5fb89 2004-04-21 devnull ttp = bundle(6, pre, $6, post , "0", numb[lev], "Q");
122 17e5fb89 2004-04-21 devnull conout(ttp, (char*)$1);
123 17e5fb89 2004-04-21 devnull rcrs = crs;
124 0aae8d3d 2005-10-31 devnull output("");
125 17e5fb89 2004-04-21 devnull lev = bindx = 0;
130 17e5fb89 2004-04-21 devnull | dlist _AUTO dlets tail
136 17e5fb89 2004-04-21 devnull if(sflag)
137 17e5fb89 2004-04-21 devnull bundle(2, $1, "s.");
143 17e5fb89 2004-04-21 devnull if(sflag)
144 17e5fb89 2004-04-21 devnull bundle(2, $1, "0");
148 17e5fb89 2004-04-21 devnull if(!sflag)
149 17e5fb89 2004-04-21 devnull bundle(2, $1, "ps.");
154 17e5fb89 2004-04-21 devnull bundle(1, "");
158 17e5fb89 2004-04-21 devnull bundle(2, $1, "s.");
160 17e5fb89 2004-04-21 devnull | SCALE '=' e
162 17e5fb89 2004-04-21 devnull bundle(2, $3, "k");
164 17e5fb89 2004-04-21 devnull | SCALE EQOP e
166 17e5fb89 2004-04-21 devnull bundle(4, "K", $3, $2, "k");
168 17e5fb89 2004-04-21 devnull | BASE '=' e
170 17e5fb89 2004-04-21 devnull bundle(2, $3, "i");
172 17e5fb89 2004-04-21 devnull | BASE EQOP e
174 17e5fb89 2004-04-21 devnull bundle(4, "I", $3, $2, "i");
176 17e5fb89 2004-04-21 devnull | OBASE '=' e
178 17e5fb89 2004-04-21 devnull bundle(2, $3, "o");
180 17e5fb89 2004-04-21 devnull | OBASE EQOP e
182 17e5fb89 2004-04-21 devnull bundle(4, "O", $3, $2, "o");
186 17e5fb89 2004-04-21 devnull bundle(3, "[", $1, "]P");
188 17e5fb89 2004-04-21 devnull | _BREAK
190 17e5fb89 2004-04-21 devnull bundle(2, numb[lev-bstack[bindx-1]], "Q");
192 17e5fb89 2004-04-21 devnull | _PRINT e
194 17e5fb89 2004-04-21 devnull bundle(2, $2, "ps.");
196 17e5fb89 2004-04-21 devnull | _RETURN e
198 17e5fb89 2004-04-21 devnull bundle(4, $2, post, numb[lev], "Q");
200 17e5fb89 2004-04-21 devnull | _RETURN
202 17e5fb89 2004-04-21 devnull bundle(4, "0", post, numb[lev], "Q");
204 17e5fb89 2004-04-21 devnull | '{' slist '}'
206 17e5fb89 2004-04-21 devnull $$ = $2;
210 17e5fb89 2004-04-21 devnull bundle(1, "fY");
212 17e5fb89 2004-04-21 devnull | _IF crs BLEV '(' re ')' stat
214 17e5fb89 2004-04-21 devnull conout($7, $2);
215 17e5fb89 2004-04-21 devnull bundle(3, $5, $2, " ");
217 17e5fb89 2004-04-21 devnull | _WHILE crs '(' re ')' stat BLEV
219 17e5fb89 2004-04-21 devnull bundle(3, $6, $4, $2);
220 17e5fb89 2004-04-21 devnull conout($$, $2);
221 17e5fb89 2004-04-21 devnull bundle(3, $4, $2, " ");
223 17e5fb89 2004-04-21 devnull | fprefix crs re ';' e ')' stat BLEV
225 17e5fb89 2004-04-21 devnull bundle(5, $7, $5, "s.", $3, $2);
226 17e5fb89 2004-04-21 devnull conout($$, $2);
227 17e5fb89 2004-04-21 devnull bundle(5, $1, "s.", $3, $2, " ");
229 17e5fb89 2004-04-21 devnull | '~' LETTER '=' e
231 17e5fb89 2004-04-21 devnull bundle(3, $4, "S", $2);
234 17e5fb89 2004-04-21 devnull fprefix:
235 17e5fb89 2004-04-21 devnull _FOR '(' e ';'
237 17e5fb89 2004-04-21 devnull $$ = $3;
243 17e5fb89 2004-04-21 devnull --bindx;
248 17e5fb89 2004-04-21 devnull | slist tail stat
250 17e5fb89 2004-04-21 devnull bundle(2, $1, $3);
263 17e5fb89 2004-04-21 devnull $$ = bundle(3, $1, $3, "=");
265 17e5fb89 2004-04-21 devnull | e '<' e
267 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, ">");
269 17e5fb89 2004-04-21 devnull | e '>' e
271 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "<");
273 17e5fb89 2004-04-21 devnull | e NE e
275 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "!=");
277 17e5fb89 2004-04-21 devnull | e GE e
279 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "!>");
281 17e5fb89 2004-04-21 devnull | e LE e
283 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "!<");
287 17e5fb89 2004-04-21 devnull bundle(2, $1, " 0!=");
291 17e5fb89 2004-04-21 devnull '(' e ')'
293 17e5fb89 2004-04-21 devnull $$ = $2;
297 17e5fb89 2004-04-21 devnull bundle(3, " ", $1, " ");
299 17e5fb89 2004-04-21 devnull | DOT cons
301 17e5fb89 2004-04-21 devnull bundle(3, " .", $2, " ");
303 17e5fb89 2004-04-21 devnull | cons DOT cons
305 17e5fb89 2004-04-21 devnull bundle(5, " ", $1, ".", $3, " ");
307 17e5fb89 2004-04-21 devnull | cons DOT
309 17e5fb89 2004-04-21 devnull bundle(4, " ", $1, ".", " ");
313 17e5fb89 2004-04-21 devnull $<cptr>$ = "l.";
315 17e5fb89 2004-04-21 devnull | LETTER '[' e ']'
317 17e5fb89 2004-04-21 devnull bundle(3, $3, ";", geta($1));
319 17e5fb89 2004-04-21 devnull | LETTER INCR
321 17e5fb89 2004-04-21 devnull bundle(4, "l", $1, "d1+s", $1);
323 17e5fb89 2004-04-21 devnull | INCR LETTER
325 17e5fb89 2004-04-21 devnull bundle(4, "l", $2, "1+ds", $2);
327 17e5fb89 2004-04-21 devnull | DECR LETTER
329 17e5fb89 2004-04-21 devnull bundle(4, "l", $2, "1-ds", $2);
331 17e5fb89 2004-04-21 devnull | LETTER DECR
333 17e5fb89 2004-04-21 devnull bundle(4, "l", $1, "d1-s", $1);
335 17e5fb89 2004-04-21 devnull | LETTER '[' e ']' INCR
337 17e5fb89 2004-04-21 devnull bundle(7, $3, ";", geta($1), "d1+" ,$3, ":" ,geta($1));
339 17e5fb89 2004-04-21 devnull | INCR LETTER '[' e ']'
341 17e5fb89 2004-04-21 devnull bundle(7, $4, ";", geta($2), "1+d", $4, ":", geta($2));
343 17e5fb89 2004-04-21 devnull | LETTER '[' e ']' DECR
345 17e5fb89 2004-04-21 devnull bundle(7, $3, ";", geta($1), "d1-", $3, ":", geta($1));
347 17e5fb89 2004-04-21 devnull | DECR LETTER '[' e ']'
349 17e5fb89 2004-04-21 devnull bundle(7, $4, ";", geta($2), "1-d", $4, ":" ,geta($2));
351 17e5fb89 2004-04-21 devnull | SCALE INCR
353 17e5fb89 2004-04-21 devnull bundle(1, "Kd1+k");
355 17e5fb89 2004-04-21 devnull | INCR SCALE
357 17e5fb89 2004-04-21 devnull bundle(1, "K1+dk");
359 17e5fb89 2004-04-21 devnull | SCALE DECR
361 17e5fb89 2004-04-21 devnull bundle(1, "Kd1-k");
363 17e5fb89 2004-04-21 devnull | DECR SCALE
365 17e5fb89 2004-04-21 devnull bundle(1, "K1-dk");
367 17e5fb89 2004-04-21 devnull | BASE INCR
369 17e5fb89 2004-04-21 devnull bundle(1, "Id1+i");
371 17e5fb89 2004-04-21 devnull | INCR BASE
373 17e5fb89 2004-04-21 devnull bundle(1, "I1+di");
375 17e5fb89 2004-04-21 devnull | BASE DECR
377 17e5fb89 2004-04-21 devnull bundle(1, "Id1-i");
379 17e5fb89 2004-04-21 devnull | DECR BASE
381 17e5fb89 2004-04-21 devnull bundle(1, "I1-di");
383 17e5fb89 2004-04-21 devnull | OBASE INCR
385 17e5fb89 2004-04-21 devnull bundle(1, "Od1+o");
387 17e5fb89 2004-04-21 devnull | INCR OBASE
389 17e5fb89 2004-04-21 devnull bundle(1, "O1+do");
391 17e5fb89 2004-04-21 devnull | OBASE DECR
393 17e5fb89 2004-04-21 devnull bundle(1, "Od1-o");
395 17e5fb89 2004-04-21 devnull | DECR OBASE
397 17e5fb89 2004-04-21 devnull bundle(1, "O1-do");
399 17e5fb89 2004-04-21 devnull | LETTER '(' cargs ')'
401 17e5fb89 2004-04-21 devnull bundle(4, $3, "l", getf($1), "x");
403 17e5fb89 2004-04-21 devnull | LETTER '(' ')'
405 17e5fb89 2004-04-21 devnull bundle(3, "l", getf($1), "x");
407 17e5fb89 2004-04-21 devnull | LETTER = {
408 17e5fb89 2004-04-21 devnull bundle(2, "l", $1);
410 17e5fb89 2004-04-21 devnull | LENGTH '(' e ')'
412 17e5fb89 2004-04-21 devnull bundle(2, $3, "Z");
414 17e5fb89 2004-04-21 devnull | SCALE '(' e ')'
416 17e5fb89 2004-04-21 devnull bundle(2, $3, "X");
420 17e5fb89 2004-04-21 devnull bundle(1, "?");
422 17e5fb89 2004-04-21 devnull | SQRT '(' e ')'
424 17e5fb89 2004-04-21 devnull bundle(2, $3, "v");
426 17e5fb89 2004-04-21 devnull | '~' LETTER
428 17e5fb89 2004-04-21 devnull bundle(2, "L", $2);
432 17e5fb89 2004-04-21 devnull bundle(1, "K");
436 17e5fb89 2004-04-21 devnull bundle(1, "I");
440 17e5fb89 2004-04-21 devnull bundle(1, "O");
444 17e5fb89 2004-04-21 devnull bundle(3, " 0", $2, "-");
446 17e5fb89 2004-04-21 devnull | e '+' e
448 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "+");
450 17e5fb89 2004-04-21 devnull | e '-' e
452 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "-");
454 17e5fb89 2004-04-21 devnull | e '*' e
456 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "*");
458 17e5fb89 2004-04-21 devnull | e '/' e
460 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "/");
462 17e5fb89 2004-04-21 devnull | e '%' e
464 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "%%");
466 17e5fb89 2004-04-21 devnull | e '^' e
468 17e5fb89 2004-04-21 devnull bundle(3, $1, $3, "^");
472 17e5fb89 2004-04-21 devnull LETTER '=' e
474 17e5fb89 2004-04-21 devnull bundle(3, $3, "ds", $1);
476 17e5fb89 2004-04-21 devnull | LETTER '[' e ']' '=' e
478 17e5fb89 2004-04-21 devnull bundle(5, $6, "d", $3, ":", geta($1));
480 17e5fb89 2004-04-21 devnull | LETTER EQOP e
482 17e5fb89 2004-04-21 devnull bundle(6, "l", $1, $3, $2, "ds", $1);
484 17e5fb89 2004-04-21 devnull | LETTER '[' e ']' EQOP e
486 17e5fb89 2004-04-21 devnull bundle(9, $3, ";", geta($1), $6, $5, "d", $3, ":", geta($1));
495 17e5fb89 2004-04-21 devnull | cargs ',' eora
497 17e5fb89 2004-04-21 devnull bundle(2, $1, $3);
502 17e5fb89 2004-04-21 devnull | LETTER '[' ']'
504 17e5fb89 2004-04-21 devnull bundle(2, "l", geta($1));
508 17e5fb89 2004-04-21 devnull constant
510 17e5fb89 2004-04-21 devnull *cp++ = 0;
513 17e5fb89 2004-04-21 devnull constant:
516 17e5fb89 2004-04-21 devnull $<cptr>$ = cp;
517 17e5fb89 2004-04-21 devnull *cp++ = '_';
521 17e5fb89 2004-04-21 devnull $<cptr>$ = cp;
522 17e5fb89 2004-04-21 devnull *cp++ = $1;
524 17e5fb89 2004-04-21 devnull | constant DIGIT
526 17e5fb89 2004-04-21 devnull *cp++ = $2;
532 17e5fb89 2004-04-21 devnull $$ = cp;
533 17e5fb89 2004-04-21 devnull *cp++ = '<';
534 17e5fb89 2004-04-21 devnull *cp++ = crs/100+'0';
535 17e5fb89 2004-04-21 devnull *cp++ = (crs%100)/10+'0';
536 17e5fb89 2004-04-21 devnull *cp++ = crs%10+'0';
537 17e5fb89 2004-04-21 devnull *cp++ = '>';
538 17e5fb89 2004-04-21 devnull *cp++ = '\0';
539 17e5fb89 2004-04-21 devnull if(crs++ >= 220) {
540 17e5fb89 2004-04-21 devnull yyerror("program too big");
541 17e5fb89 2004-04-21 devnull getout();
543 17e5fb89 2004-04-21 devnull bstack[bindx++] = lev++;
547 17e5fb89 2004-04-21 devnull _DEFINE LETTER '('
549 17e5fb89 2004-04-21 devnull $$ = getf($2);
550 0aae8d3d 2005-10-31 devnull pre = (char*)"";
551 0aae8d3d 2005-10-31 devnull post = (char*)"";
552 17e5fb89 2004-04-21 devnull lev = 1;
553 17e5fb89 2004-04-21 devnull bindx = 0;
554 17e5fb89 2004-04-21 devnull bstack[bindx] = 0;
560 17e5fb89 2004-04-21 devnull pp((char*)$1);
562 17e5fb89 2004-04-21 devnull | dargs ',' lora
564 17e5fb89 2004-04-21 devnull pp((char*)$3);
570 17e5fb89 2004-04-21 devnull tp((char*)$1);
572 17e5fb89 2004-04-21 devnull | dlets ',' lora
574 17e5fb89 2004-04-21 devnull tp((char*)$3);
580 17e5fb89 2004-04-21 devnull $<cptr>$=$1;
582 17e5fb89 2004-04-21 devnull | LETTER '[' ']'
584 17e5fb89 2004-04-21 devnull $$ = geta($1);
590 17e5fb89 2004-04-21 devnull yylex(void)
592 17e5fb89 2004-04-21 devnull int c, ch;
594 17e5fb89 2004-04-21 devnull restart:
595 17e5fb89 2004-04-21 devnull c = getch();
596 17e5fb89 2004-04-21 devnull peekc = -1;
597 17e5fb89 2004-04-21 devnull while(c == ' ' || c == '\t')
598 17e5fb89 2004-04-21 devnull c = getch();
599 17e5fb89 2004-04-21 devnull if(c == '\\') {
600 17e5fb89 2004-04-21 devnull getch();
601 17e5fb89 2004-04-21 devnull goto restart;
603 17e5fb89 2004-04-21 devnull if(c >= 'a' && c <= 'z') {
604 17e5fb89 2004-04-21 devnull /* look ahead to look for reserved words */
605 17e5fb89 2004-04-21 devnull peekc = getch();
606 17e5fb89 2004-04-21 devnull if(peekc >= 'a' && peekc <= 'z') { /* must be reserved word */
607 17e5fb89 2004-04-21 devnull if(c=='p' && peekc=='r') {
608 17e5fb89 2004-04-21 devnull c = _PRINT;
609 17e5fb89 2004-04-21 devnull goto skip;
611 17e5fb89 2004-04-21 devnull if(c=='i' && peekc=='f') {
612 17e5fb89 2004-04-21 devnull c = _IF;
613 17e5fb89 2004-04-21 devnull goto skip;
615 17e5fb89 2004-04-21 devnull if(c=='w' && peekc=='h') {
616 17e5fb89 2004-04-21 devnull c = _WHILE;
617 17e5fb89 2004-04-21 devnull goto skip;
619 17e5fb89 2004-04-21 devnull if(c=='f' && peekc=='o') {
620 17e5fb89 2004-04-21 devnull c = _FOR;
621 17e5fb89 2004-04-21 devnull goto skip;
623 17e5fb89 2004-04-21 devnull if(c=='s' && peekc=='q') {
624 17e5fb89 2004-04-21 devnull c = SQRT;
625 17e5fb89 2004-04-21 devnull goto skip;
627 17e5fb89 2004-04-21 devnull if(c=='r' && peekc=='e') {
628 17e5fb89 2004-04-21 devnull c = _RETURN;
629 17e5fb89 2004-04-21 devnull goto skip;
631 17e5fb89 2004-04-21 devnull if(c=='b' && peekc=='r') {
632 17e5fb89 2004-04-21 devnull c = _BREAK;
633 17e5fb89 2004-04-21 devnull goto skip;
635 17e5fb89 2004-04-21 devnull if(c=='d' && peekc=='e') {
636 17e5fb89 2004-04-21 devnull c = _DEFINE;
637 17e5fb89 2004-04-21 devnull goto skip;
639 17e5fb89 2004-04-21 devnull if(c=='s' && peekc=='c') {
640 17e5fb89 2004-04-21 devnull c = SCALE;
641 17e5fb89 2004-04-21 devnull goto skip;
643 17e5fb89 2004-04-21 devnull if(c=='b' && peekc=='a') {
644 17e5fb89 2004-04-21 devnull c = BASE;
645 17e5fb89 2004-04-21 devnull goto skip;
647 17e5fb89 2004-04-21 devnull if(c=='i' && peekc=='b') {
648 17e5fb89 2004-04-21 devnull c = BASE;
649 17e5fb89 2004-04-21 devnull goto skip;
651 17e5fb89 2004-04-21 devnull if(c=='o' && peekc=='b') {
652 17e5fb89 2004-04-21 devnull c = OBASE;
653 17e5fb89 2004-04-21 devnull goto skip;
655 17e5fb89 2004-04-21 devnull if(c=='d' && peekc=='i') {
656 17e5fb89 2004-04-21 devnull c = FFF;
657 17e5fb89 2004-04-21 devnull goto skip;
659 17e5fb89 2004-04-21 devnull if(c=='a' && peekc=='u') {
660 17e5fb89 2004-04-21 devnull c = _AUTO;
661 17e5fb89 2004-04-21 devnull goto skip;
663 17e5fb89 2004-04-21 devnull if(c=='l' && peekc=='e') {
664 17e5fb89 2004-04-21 devnull c = LENGTH;
665 17e5fb89 2004-04-21 devnull goto skip;
667 17e5fb89 2004-04-21 devnull if(c=='q' && peekc=='u')
668 17e5fb89 2004-04-21 devnull getout();
669 17e5fb89 2004-04-21 devnull /* could not be found */
670 17e5fb89 2004-04-21 devnull return ERROR;
672 17e5fb89 2004-04-21 devnull skip: /* skip over rest of word */
673 17e5fb89 2004-04-21 devnull peekc = -1;
674 17e5fb89 2004-04-21 devnull for(;;) {
675 17e5fb89 2004-04-21 devnull ch = getch();
676 17e5fb89 2004-04-21 devnull if(ch < 'a' || ch > 'z')
679 17e5fb89 2004-04-21 devnull peekc = ch;
680 17e5fb89 2004-04-21 devnull return c;
683 17e5fb89 2004-04-21 devnull /* usual case; just one single letter */
684 17e5fb89 2004-04-21 devnull yylval.cptr = letr[c-'a'];
685 17e5fb89 2004-04-21 devnull return LETTER;
687 17e5fb89 2004-04-21 devnull if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
688 17e5fb89 2004-04-21 devnull yylval.cc = c;
689 17e5fb89 2004-04-21 devnull return DIGIT;
691 17e5fb89 2004-04-21 devnull switch(c) {
692 17e5fb89 2004-04-21 devnull case '.':
693 17e5fb89 2004-04-21 devnull return DOT;
694 17e5fb89 2004-04-21 devnull case '*':
695 17e5fb89 2004-04-21 devnull yylval.cptr = "*";
696 17e5fb89 2004-04-21 devnull return cpeek('=', EQOP, c);
697 17e5fb89 2004-04-21 devnull case '%':
698 17e5fb89 2004-04-21 devnull yylval.cptr = "%%";
699 17e5fb89 2004-04-21 devnull return cpeek('=', EQOP, c);
700 17e5fb89 2004-04-21 devnull case '^':
701 17e5fb89 2004-04-21 devnull yylval.cptr = "^";
702 17e5fb89 2004-04-21 devnull return cpeek('=', EQOP, c);
703 17e5fb89 2004-04-21 devnull case '+':
704 17e5fb89 2004-04-21 devnull ch = cpeek('=', EQOP, c);
705 17e5fb89 2004-04-21 devnull if(ch == EQOP) {
706 17e5fb89 2004-04-21 devnull yylval.cptr = "+";
707 17e5fb89 2004-04-21 devnull return ch;
709 17e5fb89 2004-04-21 devnull return cpeek('+', INCR, c);
710 17e5fb89 2004-04-21 devnull case '-':
711 17e5fb89 2004-04-21 devnull ch = cpeek('=', EQOP, c);
712 17e5fb89 2004-04-21 devnull if(ch == EQOP) {
713 17e5fb89 2004-04-21 devnull yylval.cptr = "-";
714 17e5fb89 2004-04-21 devnull return ch;
716 17e5fb89 2004-04-21 devnull return cpeek('-', DECR, c);
717 17e5fb89 2004-04-21 devnull case '=':
718 17e5fb89 2004-04-21 devnull return cpeek('=', EQ, '=');
719 17e5fb89 2004-04-21 devnull case '<':
720 17e5fb89 2004-04-21 devnull return cpeek('=', LE, '<');
721 17e5fb89 2004-04-21 devnull case '>':
722 17e5fb89 2004-04-21 devnull return cpeek('=', GE, '>');
723 17e5fb89 2004-04-21 devnull case '!':
724 17e5fb89 2004-04-21 devnull return cpeek('=', NE, '!');
725 17e5fb89 2004-04-21 devnull case '/':
726 17e5fb89 2004-04-21 devnull ch = cpeek('=', EQOP, c);
727 17e5fb89 2004-04-21 devnull if(ch == EQOP) {
728 17e5fb89 2004-04-21 devnull yylval.cptr = "/";
729 17e5fb89 2004-04-21 devnull return ch;
731 17e5fb89 2004-04-21 devnull if(peekc == '*') {
732 17e5fb89 2004-04-21 devnull peekc = -1;
733 17e5fb89 2004-04-21 devnull for(;;) {
734 17e5fb89 2004-04-21 devnull ch = getch();
735 17e5fb89 2004-04-21 devnull if(ch == '*') {
736 17e5fb89 2004-04-21 devnull peekc = getch();
737 17e5fb89 2004-04-21 devnull if(peekc == '/') {
738 17e5fb89 2004-04-21 devnull peekc = -1;
739 17e5fb89 2004-04-21 devnull goto restart;
744 17e5fb89 2004-04-21 devnull return c;
745 17e5fb89 2004-04-21 devnull case '"':
746 17e5fb89 2004-04-21 devnull yylval.cptr = str;
747 17e5fb89 2004-04-21 devnull while((c=getch()) != '"'){
748 17e5fb89 2004-04-21 devnull *str++ = c;
749 17e5fb89 2004-04-21 devnull if(str >= &string[999]){
750 17e5fb89 2004-04-21 devnull yyerror("string space exceeded");
751 17e5fb89 2004-04-21 devnull getout();
754 17e5fb89 2004-04-21 devnull *str++ = 0;
755 17e5fb89 2004-04-21 devnull return QSTR;
756 17e5fb89 2004-04-21 devnull default:
757 17e5fb89 2004-04-21 devnull return c;
762 17e5fb89 2004-04-21 devnull cpeek(int c, int yes, int no)
765 17e5fb89 2004-04-21 devnull peekc = getch();
766 17e5fb89 2004-04-21 devnull if(peekc == c) {
767 17e5fb89 2004-04-21 devnull peekc = -1;
768 17e5fb89 2004-04-21 devnull return yes;
770 17e5fb89 2004-04-21 devnull return no;
774 17e5fb89 2004-04-21 devnull getch(void)
776 17e5fb89 2004-04-21 devnull long ch;
779 17e5fb89 2004-04-21 devnull ch = peekc;
780 17e5fb89 2004-04-21 devnull if(ch < 0){
781 17e5fb89 2004-04-21 devnull if(in == 0)
782 17e5fb89 2004-04-21 devnull ch = -1;
784 17e5fb89 2004-04-21 devnull ch = Bgetc(in);
786 17e5fb89 2004-04-21 devnull peekc = -1;
787 17e5fb89 2004-04-21 devnull if(ch >= 0)
788 17e5fb89 2004-04-21 devnull return ch;
789 17e5fb89 2004-04-21 devnull ifile++;
790 17e5fb89 2004-04-21 devnull if(ifile > sargc) {
791 17e5fb89 2004-04-21 devnull if(ifile >= sargc+2)
792 17e5fb89 2004-04-21 devnull getout();
793 0aae8d3d 2005-10-31 devnull in = &bstdin;
794 17e5fb89 2004-04-21 devnull Binit(in, 0, OREAD);
796 17e5fb89 2004-04-21 devnull goto loop;
799 0aae8d3d 2005-10-31 devnull Bterm(in);
800 17e5fb89 2004-04-21 devnull if((in = Bopen(sargv[ifile], OREAD)) != 0){
802 17e5fb89 2004-04-21 devnull ss = sargv[ifile];
803 17e5fb89 2004-04-21 devnull goto loop;
805 17e5fb89 2004-04-21 devnull yyerror("cannot open input file");
806 17e5fb89 2004-04-21 devnull return 0; /* shut up ken */
810 17e5fb89 2004-04-21 devnull bundle(int a, ...)
813 0aae8d3d 2005-10-31 devnull char **q;
814 0aae8d3d 2005-10-31 devnull va_list arg;
817 0aae8d3d 2005-10-31 devnull va_start(arg, a);
818 17e5fb89 2004-04-21 devnull q = bsp_nxt;
819 17e5fb89 2004-04-21 devnull if(bdebug)
820 17e5fb89 2004-04-21 devnull fprint(2, "bundle %d elements at %lx\n", i, q);
821 17e5fb89 2004-04-21 devnull while(i-- > 0) {
822 17e5fb89 2004-04-21 devnull if(bsp_nxt >= &bspace[bsp_max])
823 17e5fb89 2004-04-21 devnull yyerror("bundling space exceeded");
824 0aae8d3d 2005-10-31 devnull *bsp_nxt++ = va_arg(arg, char*);
826 17e5fb89 2004-04-21 devnull *bsp_nxt++ = 0;
827 0aae8d3d 2005-10-31 devnull va_end(arg);
828 0aae8d3d 2005-10-31 devnull yyval.cptr = (char*)q;
829 0aae8d3d 2005-10-31 devnull return (char*)q;
833 0aae8d3d 2005-10-31 devnull routput(char *p)
835 0aae8d3d 2005-10-31 devnull char **pp;
837 17e5fb89 2004-04-21 devnull if(bdebug)
838 17e5fb89 2004-04-21 devnull fprint(2, "routput(%lx)\n", p);
839 0aae8d3d 2005-10-31 devnull if((char**)p >= &bspace[0] && (char**)p < &bspace[bsp_max]) {
840 17e5fb89 2004-04-21 devnull /* part of a bundle */
841 0aae8d3d 2005-10-31 devnull pp = (char**)p;
842 0aae8d3d 2005-10-31 devnull while(*pp != 0)
843 0aae8d3d 2005-10-31 devnull routput(*pp++);
845 0aae8d3d 2005-10-31 devnull Bprint(&bstdout, p); /* character string */
849 0aae8d3d 2005-10-31 devnull output(char *p)
851 17e5fb89 2004-04-21 devnull routput(p);
852 17e5fb89 2004-04-21 devnull bsp_nxt = &bspace[0];
853 0aae8d3d 2005-10-31 devnull Bprint(&bstdout, "\n");
854 0aae8d3d 2005-10-31 devnull Bflush(&bstdout);
855 17e5fb89 2004-04-21 devnull cp = cary;
856 17e5fb89 2004-04-21 devnull crs = rcrs;
860 0aae8d3d 2005-10-31 devnull conout(char *p, char *s)
862 0aae8d3d 2005-10-31 devnull Bprint(&bstdout, "[");
863 17e5fb89 2004-04-21 devnull routput(p);
864 0aae8d3d 2005-10-31 devnull Bprint(&bstdout, "]s%s\n", s);
865 0aae8d3d 2005-10-31 devnull Bflush(&bstdout);
870 17e5fb89 2004-04-21 devnull yyerror(char *s, ...)
872 17e5fb89 2004-04-21 devnull if(ifile > sargc)
873 17e5fb89 2004-04-21 devnull ss = "teletype";
874 0aae8d3d 2005-10-31 devnull Bprint(&bstdout, "c[%s on line %d, %s]pc\n", s, ln+1, ss);
875 0aae8d3d 2005-10-31 devnull Bflush(&bstdout);
876 17e5fb89 2004-04-21 devnull cp = cary;
877 17e5fb89 2004-04-21 devnull crs = rcrs;
878 17e5fb89 2004-04-21 devnull bindx = 0;
879 17e5fb89 2004-04-21 devnull lev = 0;
880 17e5fb89 2004-04-21 devnull bsp_nxt = &bspace[0];
884 17e5fb89 2004-04-21 devnull pp(char *s)
886 17e5fb89 2004-04-21 devnull /* puts the relevant stuff on pre and post for the letter s */
887 17e5fb89 2004-04-21 devnull bundle(3, "S", s, pre);
888 0aae8d3d 2005-10-31 devnull pre = yyval.cptr;
889 17e5fb89 2004-04-21 devnull bundle(4, post, "L", s, "s.");
890 0aae8d3d 2005-10-31 devnull post = yyval.cptr;
894 17e5fb89 2004-04-21 devnull tp(char *s)
896 17e5fb89 2004-04-21 devnull /* same as pp, but for temps */
897 17e5fb89 2004-04-21 devnull bundle(3, "0S", s, pre);
898 0aae8d3d 2005-10-31 devnull pre = yyval.cptr;
899 17e5fb89 2004-04-21 devnull bundle(4, post, "L", s, "s.");
900 0aae8d3d 2005-10-31 devnull post = yyval.cptr;
904 17e5fb89 2004-04-21 devnull yyinit(int argc, char **argv)
906 0aae8d3d 2005-10-31 devnull Binit(&bstdout, 1, OWRITE);
907 17e5fb89 2004-04-21 devnull sargv = argv;
908 17e5fb89 2004-04-21 devnull sargc = argc - 1;
909 17e5fb89 2004-04-21 devnull if(sargc == 0) {
910 0aae8d3d 2005-10-31 devnull in = &bstdin;
911 17e5fb89 2004-04-21 devnull Binit(in, 0, OREAD);
912 17e5fb89 2004-04-21 devnull } else if((in = Bopen(sargv[1], OREAD)) == 0)
913 17e5fb89 2004-04-21 devnull yyerror("cannot open input file");
914 17e5fb89 2004-04-21 devnull ifile = 1;
916 17e5fb89 2004-04-21 devnull ss = sargv[1];
920 17e5fb89 2004-04-21 devnull getout(void)
922 0aae8d3d 2005-10-31 devnull Bprint(&bstdout, "q");
923 0aae8d3d 2005-10-31 devnull Bflush(&bstdout);
924 17e5fb89 2004-04-21 devnull exits(0);
928 17e5fb89 2004-04-21 devnull getf(char *p)
930 0aae8d3d 2005-10-31 devnull return funtab[*p - 'a'];
934 17e5fb89 2004-04-21 devnull geta(char *p)
936 0aae8d3d 2005-10-31 devnull return atab[*p - 'a'];
940 17e5fb89 2004-04-21 devnull main(int argc, char **argv)
942 17e5fb89 2004-04-21 devnull int p[2];
944 17e5fb89 2004-04-21 devnull while(argc > 1 && *argv[1] == '-') {
945 17e5fb89 2004-04-21 devnull switch(argv[1][1]) {
946 17e5fb89 2004-04-21 devnull case 'd':
947 17e5fb89 2004-04-21 devnull bdebug++;
949 17e5fb89 2004-04-21 devnull case 'c':
950 17e5fb89 2004-04-21 devnull cflag++;
952 17e5fb89 2004-04-21 devnull case 'l':
953 17e5fb89 2004-04-21 devnull lflag++;
955 17e5fb89 2004-04-21 devnull case 's':
956 17e5fb89 2004-04-21 devnull sflag++;
958 17e5fb89 2004-04-21 devnull default:
959 17e5fb89 2004-04-21 devnull fprint(2, "Usage: bc [-l] [-c] [file ...]\n");
960 17e5fb89 2004-04-21 devnull exits("usage");
965 17e5fb89 2004-04-21 devnull if(lflag) {
968 c8b6342d 2005-01-13 devnull argv[1] = unsharp("#9/lib/bclib");
970 17e5fb89 2004-04-21 devnull if(cflag) {
971 17e5fb89 2004-04-21 devnull yyinit(argc, argv);
973 17e5fb89 2004-04-21 devnull yyparse();
974 0aae8d3d 2005-10-31 devnull exits(0);
976 17e5fb89 2004-04-21 devnull pipe(p);
977 17e5fb89 2004-04-21 devnull if(fork() == 0) {
978 17e5fb89 2004-04-21 devnull dup(p[1], 1);
979 17e5fb89 2004-04-21 devnull close(p[0]);
980 17e5fb89 2004-04-21 devnull close(p[1]);
981 17e5fb89 2004-04-21 devnull yyinit(argc, argv);
983 17e5fb89 2004-04-21 devnull yyparse();
985 17e5fb89 2004-04-21 devnull dup(p[0], 0);
986 17e5fb89 2004-04-21 devnull close(p[0]);
987 17e5fb89 2004-04-21 devnull close(p[1]);
988 0aae8d3d 2005-10-31 devnull execl("dc", "dc", nil);