pascal.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* $Id$ */
  2. /* Language dependant support; this one is for Pascal */
  3. #include <stdio.h>
  4. #include <alloc.h>
  5. #include <assert.h>
  6. #include <ctype.h>
  7. #include "position.h"
  8. #include "class.h"
  9. #include "langdep.h"
  10. #include "Lpars.h"
  11. #include "idf.h"
  12. #include "token.h"
  13. #include "expr.h"
  14. #include "tree.h"
  15. #include "operator.h"
  16. #include "misc.h"
  17. extern FILE *db_out, *db_in;
  18. extern double
  19. atof();
  20. extern long
  21. atol();
  22. static int
  23. print_string(),
  24. print_char(),
  25. get_number(),
  26. getname(),
  27. get_token(),
  28. getstring(),
  29. print_op(),
  30. binop_prio(),
  31. unop_prio(),
  32. fix_bin_to_pref();
  33. static long
  34. array_elsize();
  35. static struct langdep pascal = {
  36. 1,
  37. "%ld",
  38. "0%lo",
  39. "0x%lx",
  40. "%lu",
  41. "0x%lx",
  42. "%.14g",
  43. "[",
  44. "]",
  45. "(",
  46. ")",
  47. "[",
  48. "]",
  49. print_string,
  50. print_char,
  51. array_elsize,
  52. binop_prio,
  53. unop_prio,
  54. getstring,
  55. getname,
  56. get_number,
  57. get_token,
  58. print_op,
  59. fix_bin_to_pref
  60. };
  61. struct langdep *pascal_dep = &pascal;
  62. static
  63. print_char(c)
  64. int c;
  65. {
  66. c &= 0377;
  67. fprintf(db_out, (c >= 040 && c < 0177) ? "'%c'" : "chr(%d)", c);
  68. }
  69. static
  70. print_string(f, s, len)
  71. FILE *f;
  72. char *s;
  73. int len;
  74. {
  75. register char *str = s;
  76. putc('\'', f);
  77. while (*str && len > 0) {
  78. putc(*str, f);
  79. if (*str++ == '\'') putc('\'', f);
  80. len--;
  81. }
  82. putc('\'', f);
  83. }
  84. extern long int_size;
  85. static long
  86. array_elsize(size)
  87. long size;
  88. {
  89. if (! (int_size % size)) return size;
  90. if (! (size % int_size)) return size;
  91. return ((size + int_size - 1) / int_size) * int_size;
  92. }
  93. static int
  94. unop_prio(op)
  95. int op;
  96. {
  97. switch(op) {
  98. case E_NOT:
  99. return 8;
  100. case E_MIN:
  101. case E_PLUS:
  102. return 6;
  103. }
  104. return 1;
  105. }
  106. static int
  107. binop_prio(op)
  108. int op;
  109. {
  110. switch(op) {
  111. case E_SELECT:
  112. return 9;
  113. case E_ARRAY:
  114. return 9;
  115. case E_AND:
  116. case E_MUL:
  117. case E_DIV:
  118. case E_MOD:
  119. return 7;
  120. case E_PLUS:
  121. case E_MIN:
  122. case E_OR:
  123. return 6;
  124. case E_IN:
  125. case E_EQUAL:
  126. case E_NOTEQUAL:
  127. case E_LTEQUAL:
  128. case E_GTEQUAL:
  129. case E_LT:
  130. case E_GT:
  131. return 5;
  132. }
  133. return 1;
  134. }
  135. static int
  136. get_number(ch)
  137. register int ch;
  138. {
  139. char buf[512+1];
  140. register char *np = &buf[0];
  141. int real_mode = 0;
  142. while (is_dig(ch)) {
  143. if (np < &buf[512]) *np++ = ch;
  144. ch = getc(db_in);
  145. }
  146. if (ch == '.') {
  147. real_mode = 1;
  148. if (np < &buf[512]) *np++ = '.';
  149. ch = getc(db_in);
  150. while (is_dig(ch)) {
  151. /* Fractional part
  152. */
  153. if (np < &buf[512]) *np++ = ch;
  154. ch = getc(db_in);
  155. }
  156. }
  157. if (ch == 'E' || ch == 'e') {
  158. /* Scale factor
  159. */
  160. real_mode = 1;
  161. if (np < &buf[512]) *np++ = ch;
  162. ch = getc(db_in);
  163. if (ch == '+' || ch == '-') {
  164. /* Signed scalefactor
  165. */
  166. if (np < &buf[512]) *np++ = ch;
  167. ch = getc(db_in);
  168. }
  169. if (is_dig(ch)) {
  170. do {
  171. if (np < &buf[512]) *np++ = ch;
  172. ch = getc(db_in);
  173. } while (is_dig(ch));
  174. }
  175. else {
  176. error("bad scale factor");
  177. }
  178. }
  179. *np++ = '\0';
  180. ungetc(ch, db_in);
  181. if (np >= &buf[512]) {
  182. if (! real_mode) {
  183. tok.ival = 0;
  184. error("constant too long");
  185. }
  186. else {
  187. tok.fval = 0.0;
  188. error("real constant too long");
  189. }
  190. }
  191. else if (! real_mode) {
  192. tok.ival = atol(buf);
  193. return INTEGER;
  194. }
  195. tok.fval = atof(buf);
  196. return REAL;
  197. }
  198. static int
  199. getname(c)
  200. register int c;
  201. {
  202. char buf[512+1];
  203. register char *p = &buf[0];
  204. register struct idf *id;
  205. do {
  206. if (isupper(c)) c = tolower(c);
  207. if (p - buf < 512) *p++ = c;
  208. c = getc(db_in);
  209. } while (in_idf(c));
  210. ungetc(c, db_in);
  211. *p = 0;
  212. /* now recognize and, div, in, mod, not, or */
  213. switch(buf[0]) {
  214. case 'a':
  215. if (strcmp(buf, "and") == 0) {
  216. tok.ival = E_AND;
  217. return BIN_OP;
  218. }
  219. break;
  220. case 'd':
  221. if (strcmp(buf, "div") == 0) {
  222. tok.ival = E_DIV;
  223. return BIN_OP;
  224. }
  225. break;
  226. case 'i':
  227. if (strcmp(buf, "in") == 0) {
  228. tok.ival = E_IN;
  229. return BIN_OP;
  230. }
  231. break;
  232. case 'm':
  233. if (strcmp(buf, "mod") == 0) {
  234. tok.ival = E_MOD;
  235. return BIN_OP;
  236. }
  237. break;
  238. case 'n':
  239. if (strcmp(buf, "not") == 0) {
  240. tok.ival = E_NOT;
  241. return PREF_OP;
  242. }
  243. break;
  244. case 'o':
  245. if (strcmp(buf, "or") == 0) {
  246. tok.ival = E_OR;
  247. return BIN_OP;
  248. }
  249. break;
  250. }
  251. id = str2idf(buf, 1);
  252. tok.idf = id;
  253. tok.str = id->id_text;
  254. return id->id_reserved ? id->id_reserved : NAME;
  255. }
  256. static int
  257. get_token(c)
  258. register int c;
  259. {
  260. switch(c) {
  261. case '[':
  262. tok.ival = E_ARRAY;
  263. /* fall through */
  264. case '(':
  265. case ')':
  266. case ']':
  267. case '`':
  268. case '{':
  269. case '}':
  270. case ':':
  271. case ',':
  272. case '\\':
  273. return c;
  274. case '.':
  275. tok.ival = E_SELECT;
  276. return SEL_OP;
  277. case '+':
  278. tok.ival = E_PLUS;
  279. return PREF_OR_BIN_OP;
  280. case '-':
  281. tok.ival = E_MIN;
  282. return PREF_OR_BIN_OP;
  283. case '*':
  284. tok.ival = E_MUL;
  285. return BIN_OP;
  286. case '/':
  287. tok.ival = E_DIV;
  288. return BIN_OP;
  289. case '=':
  290. tok.ival = E_EQUAL;
  291. return BIN_OP;
  292. case '<':
  293. c = getc(db_in);
  294. if (c == '>') {
  295. tok.ival = E_NOTEQUAL;
  296. return BIN_OP;
  297. }
  298. if (c == '=') {
  299. tok.ival = E_LTEQUAL;
  300. return BIN_OP;
  301. }
  302. ungetc(c, db_in);
  303. tok.ival = E_LT;
  304. return BIN_OP;
  305. case '>':
  306. c = getc(db_in);
  307. if (c == '=') {
  308. tok.ival = E_GTEQUAL;
  309. return BIN_OP;
  310. }
  311. ungetc(c, db_in);
  312. tok.ival = E_GT;
  313. return BIN_OP;
  314. case '^':
  315. tok.ival = E_DEREF;
  316. return POST_OP;
  317. default:
  318. error((c >= 040 && c < 0177) ? "%s'%c'" : "%s'\\0%o'", "illegal character ", c);
  319. return LLlex();
  320. }
  321. }
  322. static int
  323. getstring(c)
  324. int c;
  325. {
  326. register int ch;
  327. char buf[512];
  328. register int len = 0;
  329. for (;;) {
  330. ch = getc(db_in);
  331. if (ch == c) {
  332. ch = getc(db_in);
  333. if (ch != c) {
  334. ungetc(ch, db_in);
  335. break;
  336. }
  337. }
  338. if (ch == '\n') {
  339. error("newline in string");
  340. ungetc(ch, db_in);
  341. break;
  342. }
  343. buf[len++] = ch;
  344. }
  345. buf[len++] = 0;
  346. tok.str = Salloc(buf, (unsigned) len);
  347. return STRING;
  348. }
  349. static
  350. print_op(f, p)
  351. FILE *f;
  352. p_tree p;
  353. {
  354. switch(p->t_oper) {
  355. case OP_UNOP:
  356. switch(p->t_whichoper) {
  357. case E_MIN:
  358. fputs("-", f);
  359. print_node(f, p->t_args[0], 0);
  360. break;
  361. case E_PLUS:
  362. fputs("+", f);
  363. print_node(f, p->t_args[0], 0);
  364. break;
  365. case E_NOT:
  366. fputs(" not ", f);
  367. print_node(f, p->t_args[0], 0);
  368. break;
  369. case E_DEREF:
  370. print_node(f, p->t_args[0], 0);
  371. fputs("^", f);
  372. break;
  373. }
  374. break;
  375. case OP_BINOP:
  376. if (p->t_whichoper == E_ARRAY) {
  377. print_node(f, p->t_args[0], 0);
  378. fputs("[", f);
  379. print_node(f, p->t_args[1], 0);
  380. fputs("]", f);
  381. break;
  382. }
  383. if (p->t_whichoper == E_SELECT) {
  384. print_node(f, p->t_args[0], 0);
  385. fputs(".", f);
  386. print_node(f, p->t_args[1], 0);
  387. break;
  388. }
  389. fputs("(", f);
  390. print_node(f, p->t_args[0], 0);
  391. switch(p->t_whichoper) {
  392. case E_AND:
  393. fputs(" and ", f);
  394. break;
  395. case E_OR:
  396. fputs(" or ", f);
  397. break;
  398. case E_DIV:
  399. fputs("/", f);
  400. break;
  401. case E_MOD:
  402. fputs(" mod ", f);
  403. break;
  404. case E_IN:
  405. fputs(" in ", f);
  406. break;
  407. case E_PLUS:
  408. fputs("+", f);
  409. break;
  410. case E_MIN:
  411. fputs("-", f);
  412. break;
  413. case E_MUL:
  414. fputs("*", f);
  415. break;
  416. case E_EQUAL:
  417. fputs("=", f);
  418. break;
  419. case E_NOTEQUAL:
  420. fputs("<>", f);
  421. break;
  422. case E_LTEQUAL:
  423. fputs("<=", f);
  424. break;
  425. case E_GTEQUAL:
  426. fputs(">=", f);
  427. break;
  428. case E_LT:
  429. fputs("<", f);
  430. break;
  431. case E_GT:
  432. fputs(">", f);
  433. break;
  434. }
  435. print_node(f, p->t_args[1], 0);
  436. fputs(")", f);
  437. break;
  438. }
  439. }
  440. static
  441. fix_bin_to_pref()
  442. {
  443. /* No problems of this kind in Pascal */
  444. }