default.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* $Header$ */
  2. /* Language dependant support; this one is default */
  3. #include <stdio.h>
  4. #include <alloc.h>
  5. #include "position.h"
  6. #include "class.h"
  7. #include "langdep.h"
  8. #include "Lpars.h"
  9. #include "idf.h"
  10. #include "token.h"
  11. #include "expr.h"
  12. #include "tree.h"
  13. #include "operator.h"
  14. extern FILE *db_out, *db_in;
  15. extern int
  16. get_name();
  17. extern double
  18. atof();
  19. static int
  20. print_string(),
  21. get_number(),
  22. get_string(),
  23. get_token(),
  24. print_op(),
  25. op_prio();
  26. static long
  27. array_elsize();
  28. static struct langdep def = {
  29. 0,
  30. "%ld",
  31. "0%lo",
  32. "0x%lX",
  33. "%lu",
  34. "0x%lX",
  35. "%g",
  36. "'\\%o'",
  37. "[",
  38. "]",
  39. "(",
  40. ")",
  41. "{",
  42. "}",
  43. print_string,
  44. array_elsize,
  45. op_prio,
  46. get_string,
  47. get_name,
  48. get_number,
  49. get_token,
  50. print_op
  51. };
  52. struct langdep *def_dep = &def;
  53. static int
  54. print_string(s, len)
  55. char *s;
  56. int len;
  57. {
  58. register char *str = s;
  59. int delim = '\'';
  60. while (*str) {
  61. if (*str++ == '\'') delim = '"';
  62. }
  63. fprintf(db_out, "%c%.*s%c", delim, len, s, delim);
  64. }
  65. extern long int_size;
  66. static long
  67. array_elsize(size)
  68. long size;
  69. {
  70. if (! (int_size % size)) return size;
  71. if (! (size % int_size)) return size;
  72. return ((size + int_size - 1) / int_size) * int_size;
  73. }
  74. /*ARGSUSED*/
  75. static int
  76. op_prio(op)
  77. int op;
  78. {
  79. return 1;
  80. }
  81. static int
  82. val_in_base(c, base)
  83. register int c;
  84. {
  85. return is_dig(c)
  86. ? c - '0'
  87. : base != 16
  88. ? -1
  89. : is_hex(c)
  90. ? (c - 'a' + 10) & 017
  91. : -1;
  92. }
  93. static int
  94. get_number(c)
  95. register int c;
  96. {
  97. char buf[512+1];
  98. register int base = 10;
  99. register char *p = &buf[0];
  100. register long val = 0;
  101. register int val_c;
  102. if (c == '0') {
  103. /* check if next char is an 'x' or an 'X' */
  104. c = getc(db_in);
  105. if (c == 'x' || c == 'X') {
  106. base = 16;
  107. c = getc(db_in);
  108. }
  109. else base = 8;
  110. }
  111. while (val_c = val_in_base(c, base), val_c >= 0) {
  112. val = val * base + val_c;
  113. if (p - buf < 512) *p++ = c;
  114. c = getc(db_in);
  115. }
  116. if (base == 16 || !((c == '.' || c == 'e' || c == 'E'))) {
  117. ungetc(c, db_in);
  118. tok.ival = val;
  119. return INTEGER;
  120. }
  121. if (c == '.') {
  122. if (p - buf < 512) *p++ = c;
  123. c = getc(db_in);
  124. }
  125. while (is_dig(c)) {
  126. if (p - buf < 512) *p++ = c;
  127. c = getc(db_in);
  128. }
  129. if (c == 'e' || c == 'E') {
  130. if (p - buf < 512) *p++ = c;
  131. c = getc(db_in);
  132. if (c == '+' || c == '-') {
  133. if (p - buf < 512) *p++ = c;
  134. c = getc(db_in);
  135. }
  136. if (! is_dig(c)) {
  137. error("malformed floating constant");
  138. }
  139. while (is_dig(c)) {
  140. if (p - buf < 512) *p++ = c;
  141. c = getc(db_in);
  142. }
  143. }
  144. ungetc(c, db_in);
  145. *p++ = 0;
  146. if (p == &buf[512+1]) {
  147. error("floating point constant too long");
  148. }
  149. tok.fval = atof(buf);
  150. return REAL;
  151. }
  152. static int
  153. get_token(c)
  154. register int c;
  155. {
  156. switch(c) {
  157. case '`':
  158. case ':':
  159. case ',':
  160. return c;
  161. case '.':
  162. return get_number(c);
  163. default:
  164. error("illegal character 0%o", c);
  165. return LLlex();
  166. }
  167. }
  168. static int
  169. quoted(ch)
  170. int ch;
  171. {
  172. /* quoted() replaces an escaped character sequence by the
  173. character meant.
  174. */
  175. /* first char after backslash already in ch */
  176. if (!is_oct(ch)) { /* a quoted char */
  177. switch (ch) {
  178. case 'n':
  179. ch = '\n';
  180. break;
  181. case 't':
  182. ch = '\t';
  183. break;
  184. case 'b':
  185. ch = '\b';
  186. break;
  187. case 'r':
  188. ch = '\r';
  189. break;
  190. case 'f':
  191. ch = '\f';
  192. break;
  193. }
  194. }
  195. else { /* a quoted octal */
  196. register int oct = 0, cnt = 0;
  197. do {
  198. oct = oct*8 + (ch-'0');
  199. ch = getc(db_in);
  200. } while (is_oct(ch) && ++cnt < 3);
  201. ungetc(ch, db_in);
  202. ch = oct;
  203. }
  204. return ch&0377;
  205. }
  206. static int
  207. get_string(c)
  208. int c;
  209. {
  210. register int ch;
  211. char buf[512];
  212. register int len = 0;
  213. while (ch = getc(db_in), ch != c) {
  214. if (ch == '\n') {
  215. error("newline in string");
  216. break;
  217. }
  218. if (ch == '\\') {
  219. ch = getc(db_in);
  220. ch = quoted(ch);
  221. }
  222. buf[len++] = ch;
  223. }
  224. buf[len++] = 0;
  225. tok.str = Salloc(buf, (unsigned) len);
  226. return STRING;
  227. }
  228. static int
  229. print_op(p)
  230. p_tree p;
  231. {
  232. switch(p->t_oper) {
  233. case OP_UNOP:
  234. switch(p->t_whichoper) {
  235. case E_MIN:
  236. fputs("-", db_out);
  237. print_node(p->t_args[0], 0);
  238. break;
  239. case E_PLUS:
  240. fputs("+", db_out);
  241. print_node(p->t_args[0], 0);
  242. break;
  243. case E_NOT:
  244. fputs("~", db_out);
  245. print_node(p->t_args[0], 0);
  246. break;
  247. case E_DEREF:
  248. fputs("*", db_out);
  249. print_node(p->t_args[0], 0);
  250. break;
  251. }
  252. break;
  253. case OP_BINOP:
  254. fputs("(", db_out);
  255. print_node(p->t_args[0], 0);
  256. switch(p->t_whichoper) {
  257. case E_AND:
  258. fputs("&&", db_out);
  259. break;
  260. case E_OR:
  261. fputs("||", db_out);
  262. break;
  263. case E_ZDIV:
  264. fputs("/", db_out);
  265. break;
  266. case E_ZMOD:
  267. fputs("%", db_out);
  268. break;
  269. case E_DIV:
  270. fputs(" div ", db_out);
  271. break;
  272. case E_MOD:
  273. fputs(" mod ", db_out);
  274. break;
  275. case E_IN:
  276. fputs(" in ", db_out);
  277. break;
  278. case E_PLUS:
  279. fputs("+", db_out);
  280. break;
  281. case E_MIN:
  282. fputs("-", db_out);
  283. break;
  284. case E_MUL:
  285. fputs("*", db_out);
  286. break;
  287. case E_EQUAL:
  288. fputs("==", db_out);
  289. break;
  290. case E_NOTEQUAL:
  291. fputs("!=", db_out);
  292. break;
  293. case E_LTEQUAL:
  294. fputs("<=", db_out);
  295. break;
  296. case E_GTEQUAL:
  297. fputs(">=", db_out);
  298. break;
  299. case E_LT:
  300. fputs("<", db_out);
  301. break;
  302. case E_GT:
  303. fputs(">", db_out);
  304. break;
  305. case E_SELECT:
  306. fputs(".", db_out);
  307. break;
  308. }
  309. print_node(p->t_args[1], 0);
  310. fputs(")", db_out);
  311. break;
  312. }
  313. }