14 char* str = { string };
16 int rcrs = 128; /* reset crs */
22 int bstack[10] = { 0 };
25 " 0", " 1", " 2", " 3", " 4", " 5",
26 " 6", " 7", " 8", " 9", " 10", " 11",
39 "<1>","<2>","<3>","<4>","<5>",
40 "<6>","<7>","<8>","<9>","<10>",
41 "<11>","<12>","<13>","<14>","<15>",
42 "<16>","<17>","<18>","<19>","<20>",
43 "<21>","<22>","<23>","<24>","<25>",
48 "<221>","<222>","<223>","<224>","<225>",
49 "<226>","<227>","<228>","<229>","<230>",
50 "<231>","<232>","<233>","<234>","<235>",
51 "<236>","<237>","<238>","<239>","<240>",
52 "<241>","<242>","<243>","<244>","<245>",
57 "a","b","c","d","e","f","g","h","i","j",
58 "k","l","m","n","o","p","q","r","s","t",
59 "u","v","w","x","y","z"
62 char* bspace[bsp_max];
63 char** bsp_nxt = bspace;
69 char* bundle(int, ...);
70 void conout(char*, char*);
71 int cpeek(int, int, int);
80 void yyerror(char*, ...);
83 typedef void* pointer;
84 #pragma varargck type "lx" pointer
93 %type <cptr> pstat stat stat1 def slist dlets e ase nase
94 %type <cptr> slist re fprefix cargs eora cons constant lora
97 %token <cptr> LETTER EQOP _AUTO DOT
98 %token <cc> DIGIT SQRT LENGTH _IF FFF EQ
99 %token <cc> _PRINT _WHILE _FOR NE LE GE INCR DECR
100 %token <cc> _RETURN _BREAK _DEFINE BASE OBASE SCALE
101 %token <cc> QSTR ERROR
119 | def dargs ')' '{' dlist slist '}'
121 ttp = bundle(6, pre, $6, post , "0", numb[lev], "Q");
122 conout(ttp, (char*)$1);
130 | dlist _AUTO dlets tail
149 bundle(2, $1, "ps.");
166 bundle(4, "K", $3, $2, "k");
174 bundle(4, "I", $3, $2, "i");
182 bundle(4, "O", $3, $2, "o");
186 bundle(3, "[", $1, "]P");
190 bundle(2, numb[lev-bstack[bindx-1]], "Q");
194 bundle(2, $2, "ps.");
198 bundle(4, $2, post, numb[lev], "Q");
202 bundle(4, "0", post, numb[lev], "Q");
212 | _IF crs BLEV '(' re ')' stat
215 bundle(3, $5, $2, " ");
217 | _WHILE crs '(' re ')' stat BLEV
219 bundle(3, $6, $4, $2);
221 bundle(3, $4, $2, " ");
223 | fprefix crs re ';' e ')' stat BLEV
225 bundle(5, $7, $5, "s.", $3, $2);
227 bundle(5, $1, "s.", $3, $2, " ");
231 bundle(3, $4, "S", $2);
263 $$ = bundle(3, $1, $3, "=");
267 bundle(3, $1, $3, ">");
271 bundle(3, $1, $3, "<");
275 bundle(3, $1, $3, "!=");
279 bundle(3, $1, $3, "!>");
283 bundle(3, $1, $3, "!<");
287 bundle(2, $1, " 0!=");
297 bundle(3, " ", $1, " ");
301 bundle(3, " .", $2, " ");
305 bundle(5, " ", $1, ".", $3, " ");
309 bundle(4, " ", $1, ".", " ");
317 bundle(3, $3, ";", geta($1));
321 bundle(4, "l", $1, "d1+s", $1);
325 bundle(4, "l", $2, "1+ds", $2);
329 bundle(4, "l", $2, "1-ds", $2);
333 bundle(4, "l", $1, "d1-s", $1);
335 | LETTER '[' e ']' INCR
337 bundle(7, $3, ";", geta($1), "d1+" ,$3, ":" ,geta($1));
339 | INCR LETTER '[' e ']'
341 bundle(7, $4, ";", geta($2), "1+d", $4, ":", geta($2));
343 | LETTER '[' e ']' DECR
345 bundle(7, $3, ";", geta($1), "d1-", $3, ":", geta($1));
347 | DECR LETTER '[' e ']'
349 bundle(7, $4, ";", geta($2), "1-d", $4, ":" ,geta($2));
399 | LETTER '(' cargs ')'
401 bundle(4, $3, "l", getf($1), "x");
405 bundle(3, "l", getf($1), "x");
444 bundle(3, " 0", $2, "-");
448 bundle(3, $1, $3, "+");
452 bundle(3, $1, $3, "-");
456 bundle(3, $1, $3, "*");
460 bundle(3, $1, $3, "/");
464 bundle(3, $1, $3, "%%");
468 bundle(3, $1, $3, "^");
474 bundle(3, $3, "ds", $1);
476 | LETTER '[' e ']' '=' e
478 bundle(5, $6, "d", $3, ":", geta($1));
482 bundle(6, "l", $1, $3, $2, "ds", $1);
484 | LETTER '[' e ']' EQOP e
486 bundle(9, $3, ";", geta($1), $6, $5, "d", $3, ":", geta($1));
504 bundle(2, "l", geta($1));
535 *cp++ = (crs%100)/10+'0';
540 yyerror("program too big");
543 bstack[bindx++] = lev++;
597 while(c == ' ' || c == '\t')
603 if(c >= 'a' && c <= 'z') {
604 /* look ahead to look for reserved words */
606 if(peekc >= 'a' && peekc <= 'z') { /* must be reserved word */
607 if(c=='p' && peekc=='r') {
611 if(c=='i' && peekc=='f') {
615 if(c=='w' && peekc=='h') {
619 if(c=='f' && peekc=='o') {
623 if(c=='s' && peekc=='q') {
627 if(c=='r' && peekc=='e') {
631 if(c=='b' && peekc=='r') {
635 if(c=='d' && peekc=='e') {
639 if(c=='s' && peekc=='c') {
643 if(c=='b' && peekc=='a') {
647 if(c=='i' && peekc=='b') {
651 if(c=='o' && peekc=='b') {
655 if(c=='d' && peekc=='i') {
659 if(c=='a' && peekc=='u') {
663 if(c=='l' && peekc=='e') {
667 if(c=='q' && peekc=='u')
669 /* could not be found */
672 skip: /* skip over rest of word */
676 if(ch < 'a' || ch > 'z')
683 /* usual case; just one single letter */
684 yylval.cptr = letr[c-'a'];
687 if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
696 return cpeek('=', EQOP, c);
699 return cpeek('=', EQOP, c);
702 return cpeek('=', EQOP, c);
704 ch = cpeek('=', EQOP, c);
709 return cpeek('+', INCR, c);
711 ch = cpeek('=', EQOP, c);
716 return cpeek('-', DECR, c);
718 return cpeek('=', EQ, '=');
720 return cpeek('=', LE, '<');
722 return cpeek('=', GE, '>');
724 return cpeek('=', NE, '!');
726 ch = cpeek('=', EQOP, c);
747 while((c=getch()) != '"'){
749 if(str >= &string[999]){
750 yyerror("string space exceeded");
762 cpeek(int c, int yes, int no)
800 if((in = Bopen(sargv[ifile], OREAD)) != 0){
805 yyerror("cannot open input file");
806 return 0; /* shut up ken */
820 fprint(2, "bundle %d elements at %lx\n", i, q);
822 if(bsp_nxt >= &bspace[bsp_max])
823 yyerror("bundling space exceeded");
824 *bsp_nxt++ = va_arg(arg, char*);
828 yyval.cptr = (char*)q;
838 fprint(2, "routput(%lx)\n", p);
839 if((char**)p >= &bspace[0] && (char**)p < &bspace[bsp_max]) {
840 /* part of a bundle */
845 Bprint(&bstdout, p); /* character string */
852 bsp_nxt = &bspace[0];
853 Bprint(&bstdout, "\n");
860 conout(char *p, char *s)
862 Bprint(&bstdout, "[");
864 Bprint(&bstdout, "]s%s\n", s);
870 yyerror(char *s, ...)
874 Bprint(&bstdout, "c[%s:%d %s]pc\n", ss, ln+1, s);
880 bsp_nxt = &bspace[0];
886 /* puts the relevant stuff on pre and post for the letter s */
887 bundle(3, "S", s, pre);
889 bundle(4, post, "L", s, "s.");
896 /* same as pp, but for temps */
897 bundle(3, "0S", s, pre);
899 bundle(4, post, "L", s, "s.");
904 yyinit(int argc, char **argv)
906 Binit(&bstdout, 1, OWRITE);
912 } else if((in = Bopen(sargv[1], OREAD)) == 0)
913 yyerror("cannot open input file");
922 Bprint(&bstdout, "q");
930 return funtab[*p - 'a'];
936 return atab[*p - 'a'];
940 main(int argc, char **argv)
944 while(argc > 1 && *argv[1] == '-') {
959 fprint(2, "Usage: bc [-cdls] [file ...]\n");
968 argv[1] = "/sys/lib/bclib";
988 execl("/bin/dc", "dc", nil);