comm5.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* @(#)comm5.c 1.1 */
  7. #include "comm0.h"
  8. #include "comm1.h"
  9. #include "y.tab.h"
  10. extern YYSTYPE yylval;
  11. int yylex()
  12. {
  13. int c;
  14. if (pass == PASS_1) {
  15. /* scan the input file */
  16. do
  17. c = nextchar();
  18. while (isspace(c) && c != '\n');
  19. if (ISALPHA(c))
  20. c = inident(c);
  21. else if (isdigit(c))
  22. c = innumber(c);
  23. else switch (c) {
  24. case '=':
  25. case '<':
  26. case '>':
  27. case '|':
  28. case '&':
  29. c = induo(c); break;
  30. case ASC_SQUO:
  31. case ASC_DQUO:
  32. c = instring(c); break;
  33. case ASC_COMM:
  34. do
  35. c = nextchar();
  36. while (c != '\n' && c != '\0');
  37. break;
  38. case CTRL('A'):
  39. c = CODE1; readcode(1); break;
  40. case CTRL('B'):
  41. c = CODE2; readcode(2); break;
  42. case CTRL('C'):
  43. c = CODE4; readcode(4); break;
  44. }
  45. /* produce the intermediate token file */
  46. if (c <= 0)
  47. return(0);
  48. if (c <= 127)
  49. putc(c, tempfile);
  50. else
  51. putval(c);
  52. } else {
  53. /* read from intermediate token file */
  54. c = getc(tempfile);
  55. if (c == EOF)
  56. return(0);
  57. if (c > 127) {
  58. c += 128;
  59. c = getval(c);
  60. }
  61. }
  62. curr_token = c;
  63. return(c);
  64. }
  65. void putval(int c)
  66. {
  67. valu_t v;
  68. int n = 0;
  69. char *p = 0;
  70. assert(c >= 256 && c < 256+128);
  71. switch (c) {
  72. case CODE1:
  73. n = 1; goto putnum;
  74. case CODE2:
  75. n = 2; goto putnum;
  76. case CODE4:
  77. n = 4; goto putnum;
  78. case NUMBER:
  79. v = yylval.y_valu;
  80. for (n = 0; n < sizeof(v); n++) {
  81. if (v == 0)
  82. break;
  83. v >>= 8;
  84. }
  85. c = NUMBER0 + n;
  86. putnum:
  87. putc(c-128, tempfile);
  88. v = yylval.y_valu;
  89. while (--n >= 0)
  90. putc((int) (v >> (n*8)), tempfile);
  91. return;
  92. case IDENT:
  93. case FBSYM:
  94. n = sizeof(item_t *);
  95. p = (char *) &yylval.y_item; break;
  96. #ifdef ASLD
  97. case MODULE:
  98. n = sizeof(char *);
  99. p = (char *) &yylval.y_strp; break;
  100. #endif
  101. case STRING:
  102. v = stringlen;
  103. putc(c-128, tempfile);
  104. for (n = 0; n < sizeof(v); n++) {
  105. if (v == 0)
  106. break;
  107. v >>= 8;
  108. }
  109. c = NUMBER0 + n;
  110. putc(c-128, tempfile);
  111. v = stringlen;
  112. while (--n >= 0)
  113. putc((int) (v >> (n*8)), tempfile);
  114. p = stringbuf;
  115. n = stringlen;
  116. while (--n >= 0)
  117. putc(*p++, tempfile);
  118. return;
  119. case OP_EQ:
  120. case OP_NE:
  121. case OP_LE:
  122. case OP_GE:
  123. case OP_LL:
  124. case OP_RR:
  125. case OP_OO:
  126. case OP_AA:
  127. break;
  128. default:
  129. n = sizeof(word_t);
  130. p = (char *) &yylval.y_word; break;
  131. }
  132. putc(c-128, tempfile);
  133. while (--n >= 0)
  134. putc(*p++, tempfile);
  135. }
  136. int getval(int c)
  137. {
  138. int n = 0;
  139. valu_t v;
  140. char *p = 0;
  141. switch (c) {
  142. case CODE1:
  143. n = 1; goto getnum;
  144. case CODE2:
  145. n = 2; goto getnum;
  146. case CODE4:
  147. n = 4; goto getnum;
  148. case NUMBER0:
  149. c = NUMBER; goto getnum;
  150. case NUMBER1:
  151. n = 1; c = NUMBER; goto getnum;
  152. case NUMBER2:
  153. n = 2; c = NUMBER; goto getnum;
  154. case NUMBER3:
  155. n = 3; c = NUMBER; goto getnum;
  156. case NUMBER:
  157. n = 4;
  158. getnum:
  159. v = 0;
  160. while (--n >= 0) {
  161. v <<= 8;
  162. v |= getc(tempfile);
  163. }
  164. yylval.y_valu = v;
  165. return(c);
  166. case IDENT:
  167. case FBSYM:
  168. n = sizeof(item_t *);
  169. p = (char *) &yylval.y_item; break;
  170. #ifdef ASLD
  171. case MODULE:
  172. n = sizeof(char *);
  173. p = (char *) &yylval.y_strp; break;
  174. #endif
  175. case STRING:
  176. getval(getc(tempfile)+128);
  177. stringlen = n = yylval.y_valu;
  178. p = stringbuf;
  179. p[n] = '\0'; break;
  180. case OP_EQ:
  181. case OP_NE:
  182. case OP_LE:
  183. case OP_GE:
  184. case OP_LL:
  185. case OP_RR:
  186. case OP_OO:
  187. case OP_AA:
  188. break;
  189. default:
  190. n = sizeof(word_t);
  191. p = (char *) &yylval.y_word; break;
  192. }
  193. while (--n >= 0)
  194. *p++ = getc(tempfile);
  195. return(c);
  196. }
  197. /* ---------- lexical scan in pass 1 ---------- */
  198. int nextchar()
  199. {
  200. int c;
  201. if (peekc != -1) {
  202. c = peekc;
  203. peekc = -1;
  204. return(c);
  205. }
  206. #ifdef ASLD
  207. if (archmode && --archsize < 0)
  208. return(0);
  209. #endif
  210. if ((c = getc(input)) == EOF)
  211. return(0);
  212. if (isascii(c) == 0)
  213. fatal("non-ascii character");
  214. #ifdef LISTING
  215. if (listflag & 0440)
  216. putc(c, listfile);
  217. #endif
  218. return(c);
  219. }
  220. void readcode(int n)
  221. {
  222. int c;
  223. yylval.y_valu = 0;
  224. do {
  225. if (
  226. #ifdef ASLD
  227. (archmode && --archsize < 0)
  228. ||
  229. #endif
  230. (c = getc(input)) == EOF
  231. )
  232. fatal("unexpected EOF in compact input");
  233. yylval.y_valu <<= 8;
  234. yylval.y_valu |= c;
  235. } while (--n);
  236. }
  237. int induo(int c)
  238. {
  239. static short duo[] = {
  240. ('='<<8) | '=', OP_EQ,
  241. ('<'<<8) | '>', OP_NE,
  242. ('<'<<8) | '=', OP_LE,
  243. ('>'<<8) | '=', OP_GE,
  244. ('<'<<8) | '<', OP_LL,
  245. ('>'<<8) | '>', OP_RR,
  246. ('|'<<8) | '|', OP_OO,
  247. ('&'<<8) | '&', OP_AA,
  248. };
  249. short *p;
  250. c = (c<<8) | nextchar();
  251. for (p = duo; *p; p++)
  252. if (*p++ == c)
  253. return(*p++);
  254. peekc = c & 0377;
  255. return(c>>8);
  256. }
  257. static char name[NAMEMAX+1];
  258. int inident(int c)
  259. {
  260. char *p = name;
  261. item_t *ip;
  262. int n = NAMEMAX;
  263. do {
  264. if (--n >= 0)
  265. *p++ = c;
  266. c = nextchar();
  267. } while (ISALNUM(c));
  268. *p = '\0';
  269. peekc = c;
  270. ip = item_search(name);
  271. if (ip == 0) {
  272. ip = item_alloc(S_UND);
  273. ip->i_name = remember(name);
  274. /* printf("ident %s %o\n", ip->i_name, ip); */
  275. unresolved++;
  276. item_insert(ip, H_LOCAL + (hashindex%H_SIZE));
  277. } else if (hashindex < H_SIZE) {
  278. assert(H_KEY == 0);
  279. yylval.y_word = (word_t) ip->i_valu;
  280. return(ip->i_type);
  281. }
  282. yylval.y_item = ip;
  283. return(IDENT);
  284. }
  285. #ifdef ASLD
  286. char *readident(int c)
  287. {
  288. int n = NAMEMAX;
  289. char *p = name;
  290. do {
  291. if (--n >= 0)
  292. *p++ = c;
  293. c = nextchar();
  294. } while (ISALNUM(c));
  295. *p++ = '\0';
  296. peekc = c;
  297. return(name);
  298. }
  299. #endif
  300. int innumber(int c)
  301. {
  302. char *p;
  303. int radix;
  304. static char num[20+1];
  305. p = num;
  306. radix = 20;
  307. do {
  308. if (--radix < 0)
  309. fatal("number too long");
  310. if (isupper(c))
  311. c += ('a' - 'A');
  312. *p++ = c;
  313. c = nextchar();
  314. } while (isalnum(c));
  315. peekc = c;
  316. *p = '\0';
  317. c = *--p;
  318. p = num;
  319. radix = 10;
  320. if (*p == '0') {
  321. radix = 8;
  322. p++;
  323. if (*p == 'x') {
  324. radix = 16;
  325. p++;
  326. } else if (*p == 'b') {
  327. radix = 2;
  328. p++;
  329. }
  330. }
  331. if (radix != 16 && (c == 'f' || c == 'b'))
  332. return(infbsym(num));
  333. yylval.y_valu = 0;
  334. while ( (c = *p++) ) {
  335. if (c > '9')
  336. c -= ('a' - '9' - 1);
  337. c -= '0';
  338. if ((unsigned)c >= radix)
  339. serror("digit exceeds radix");
  340. yylval.y_valu = yylval.y_valu * radix + c;
  341. }
  342. return(NUMBER);
  343. }
  344. int instring(int termc)
  345. {
  346. char *p;
  347. int c;
  348. static int maxstring = 0;
  349. if (! maxstring) {
  350. maxstring = STRINGMAX;
  351. if ((stringbuf = malloc(maxstring)) == 0) {
  352. fatal("out of memory");
  353. }
  354. }
  355. p = stringbuf;
  356. for (;;) {
  357. c = nextchar();
  358. if (c == '\n' || c == '\0') {
  359. peekc = c;
  360. serror("non-terminated string");
  361. break;
  362. }
  363. if (c == termc)
  364. break;
  365. if (c == '\\')
  366. c = inescape();
  367. if (p >= &stringbuf[maxstring - 1]) {
  368. int cnt = p - stringbuf;
  369. if ((stringbuf = realloc(stringbuf, maxstring += 256)) == 0) {
  370. fatal("out of memory");
  371. }
  372. p = stringbuf + cnt;
  373. }
  374. *p++ = c;
  375. }
  376. stringlen = p - stringbuf;
  377. *p = '\0';
  378. return(STRING);
  379. }
  380. int inescape()
  381. {
  382. int c, j, r;
  383. c = nextchar();
  384. if (c >= '0' && c <= '7') {
  385. r = c - '0';
  386. for (j = 0; j < 2; j++) {
  387. c = nextchar();
  388. if (c < '0' || c > '7') {
  389. peekc = c;
  390. return(r);
  391. }
  392. r <<= 3;
  393. r += (c - '0');
  394. }
  395. return(r);
  396. }
  397. switch (c) {
  398. case 'b': return('\b');
  399. case 'f': return('\f');
  400. case 'n': return('\n');
  401. case 'r': return('\r');
  402. case 't': return('\t');
  403. case '\'': return('\'');
  404. case '"': return('"');
  405. }
  406. return(c);
  407. }
  408. int infbsym(char *p)
  409. {
  410. int lab;
  411. item_t *ip;
  412. lab = *p++ - '0';
  413. if ((unsigned)lab < 10) {
  414. if (*p++ == 'f') {
  415. ip = fb_ptr[FB_FORW+lab];
  416. if (ip == 0) {
  417. ip = fb_alloc(lab);
  418. fb_ptr[FB_FORW+lab] = ip;
  419. }
  420. goto ok;
  421. }
  422. ip = fb_ptr[FB_BACK+lab];
  423. if (ip != 0 && *p == 0)
  424. goto ok;
  425. }
  426. serror("bad numeric label");
  427. ip = fb_alloc(0);
  428. ok:
  429. yylval.y_item = ip;
  430. return(FBSYM);
  431. }
  432. int hash(char *p)
  433. {
  434. unsigned short h;
  435. int c;
  436. h = 0;
  437. while ( (c = *p++) ) {
  438. h <<= 2;
  439. h += c;
  440. }
  441. return(h % H_SIZE);
  442. }
  443. item_t *item_search(char *p)
  444. {
  445. int h;
  446. item_t *ip;
  447. for (h = hash(p); h < H_TOTAL; h += H_SIZE) {
  448. ip = hashtab[h];
  449. while (ip != 0) {
  450. if (strcmp(p, ip->i_name) == 0)
  451. goto done;
  452. ip = ip->i_next;
  453. }
  454. }
  455. done:
  456. hashindex = h;
  457. return(ip);
  458. }
  459. void item_insert(item_t *ip, int h)
  460. {
  461. ip->i_next = hashtab[h];
  462. hashtab[h] = ip;
  463. }
  464. item_t *item_alloc(int typ)
  465. {
  466. item_t *ip;
  467. static nleft = 0;
  468. static item_t *next;
  469. if (--nleft < 0) {
  470. next = (item_t *) malloc(MEMINCR);
  471. if (next == 0)
  472. fatal("out of memory");
  473. nleft += (MEMINCR / sizeof(item_t));
  474. }
  475. ip = next++;
  476. ip->i_next = 0;
  477. ip->i_type = typ;
  478. ip->i_name = 0;
  479. ip->i_valu = 0;
  480. return(ip);
  481. }
  482. item_t *fb_alloc(int lab)
  483. {
  484. item_t *ip, *p;
  485. ip = item_alloc(S_UND);
  486. p = fb_ptr[FB_TAIL+lab];
  487. if (p == 0)
  488. fb_ptr[FB_HEAD+lab] = ip;
  489. else
  490. p->i_next = ip;
  491. fb_ptr[FB_TAIL+lab] = ip;
  492. return(ip);
  493. }
  494. item_t *fb_shift(int lab)
  495. {
  496. item_t *ip;
  497. ip = fb_ptr[FB_FORW+lab];
  498. if (ip == 0)
  499. {
  500. if (pass == PASS_1)
  501. {
  502. ip = fb_alloc(lab);
  503. }
  504. else
  505. {
  506. ip = fb_ptr[FB_HEAD+lab];
  507. }
  508. }
  509. fb_ptr[FB_BACK+lab] = ip;
  510. fb_ptr[FB_FORW+lab] = ip->i_next;
  511. return(ip);
  512. }