parse.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /****************************************************************
  2. Copyright (C) Lucent Technologies 1997
  3. All Rights Reserved
  4. Permission to use, copy, modify, and distribute this software and
  5. its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the name Lucent Technologies or any of
  10. its entities not be used in advertising or publicity pertaining
  11. to distribution of the software without specific, written prior
  12. permission.
  13. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  14. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  15. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  16. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  18. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  20. THIS SOFTWARE.
  21. ****************************************************************/
  22. #define DEBUG
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "awk.h"
  27. #include "y.tab.h"
  28. Node *nodealloc(int n)
  29. {
  30. Node *x;
  31. x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
  32. if (x == NULL)
  33. FATAL("out of space in nodealloc");
  34. x->nnext = NULL;
  35. x->lineno = lineno;
  36. return(x);
  37. }
  38. Node *exptostat(Node *a)
  39. {
  40. a->ntype = NSTAT;
  41. return(a);
  42. }
  43. Node *node1(int a, Node *b)
  44. {
  45. Node *x;
  46. x = nodealloc(1);
  47. x->nobj = a;
  48. x->narg[0]=b;
  49. return(x);
  50. }
  51. Node *node2(int a, Node *b, Node *c)
  52. {
  53. Node *x;
  54. x = nodealloc(2);
  55. x->nobj = a;
  56. x->narg[0] = b;
  57. x->narg[1] = c;
  58. return(x);
  59. }
  60. Node *node3(int a, Node *b, Node *c, Node *d)
  61. {
  62. Node *x;
  63. x = nodealloc(3);
  64. x->nobj = a;
  65. x->narg[0] = b;
  66. x->narg[1] = c;
  67. x->narg[2] = d;
  68. return(x);
  69. }
  70. Node *node4(int a, Node *b, Node *c, Node *d, Node *e)
  71. {
  72. Node *x;
  73. x = nodealloc(4);
  74. x->nobj = a;
  75. x->narg[0] = b;
  76. x->narg[1] = c;
  77. x->narg[2] = d;
  78. x->narg[3] = e;
  79. return(x);
  80. }
  81. Node *stat1(int a, Node *b)
  82. {
  83. Node *x;
  84. x = node1(a,b);
  85. x->ntype = NSTAT;
  86. return(x);
  87. }
  88. Node *stat2(int a, Node *b, Node *c)
  89. {
  90. Node *x;
  91. x = node2(a,b,c);
  92. x->ntype = NSTAT;
  93. return(x);
  94. }
  95. Node *stat3(int a, Node *b, Node *c, Node *d)
  96. {
  97. Node *x;
  98. x = node3(a,b,c,d);
  99. x->ntype = NSTAT;
  100. return(x);
  101. }
  102. Node *stat4(int a, Node *b, Node *c, Node *d, Node *e)
  103. {
  104. Node *x;
  105. x = node4(a,b,c,d,e);
  106. x->ntype = NSTAT;
  107. return(x);
  108. }
  109. Node *op1(int a, Node *b)
  110. {
  111. Node *x;
  112. x = node1(a,b);
  113. x->ntype = NEXPR;
  114. return(x);
  115. }
  116. Node *op2(int a, Node *b, Node *c)
  117. {
  118. Node *x;
  119. x = node2(a,b,c);
  120. x->ntype = NEXPR;
  121. return(x);
  122. }
  123. Node *op3(int a, Node *b, Node *c, Node *d)
  124. {
  125. Node *x;
  126. x = node3(a,b,c,d);
  127. x->ntype = NEXPR;
  128. return(x);
  129. }
  130. Node *op4(int a, Node *b, Node *c, Node *d, Node *e)
  131. {
  132. Node *x;
  133. x = node4(a,b,c,d,e);
  134. x->ntype = NEXPR;
  135. return(x);
  136. }
  137. Node *celltonode(Cell *a, int b)
  138. {
  139. Node *x;
  140. a->ctype = OCELL;
  141. a->csub = b;
  142. x = node1(0, (Node *) a);
  143. x->ntype = NVALUE;
  144. return(x);
  145. }
  146. Node *rectonode(void) /* make $0 into a Node */
  147. {
  148. extern Cell *literal0;
  149. return op1(INDIRECT, celltonode(literal0, CUNK));
  150. }
  151. Node *makearr(Node *p)
  152. {
  153. Cell *cp;
  154. if (isvalue(p)) {
  155. cp = (Cell *) (p->narg[0]);
  156. if (isfcn(cp))
  157. SYNTAX( "%s is a function, not an array", cp->nval );
  158. else if (!isarr(cp)) {
  159. xfree(cp->sval);
  160. cp->sval = (char *) makesymtab(NSYMTAB);
  161. cp->tval = ARR;
  162. }
  163. }
  164. return p;
  165. }
  166. #define PA2NUM 50 /* max number of pat,pat patterns allowed */
  167. int paircnt; /* number of them in use */
  168. int pairstack[PA2NUM]; /* state of each pat,pat */
  169. Node *pa2stat(Node *a, Node *b, Node *c) /* pat, pat {...} */
  170. {
  171. Node *x;
  172. x = node4(PASTAT2, a, b, c, itonp(paircnt));
  173. if (paircnt++ >= PA2NUM)
  174. SYNTAX( "limited to %d pat,pat statements", PA2NUM );
  175. x->ntype = NSTAT;
  176. return(x);
  177. }
  178. Node *linkum(Node *a, Node *b)
  179. {
  180. Node *c;
  181. if (errorflag) /* don't link things that are wrong */
  182. return a;
  183. if (a == NULL)
  184. return(b);
  185. else if (b == NULL)
  186. return(a);
  187. for (c = a; c->nnext != NULL; c = c->nnext)
  188. ;
  189. c->nnext = b;
  190. return(a);
  191. }
  192. void defn(Cell *v, Node *vl, Node *st) /* turn on FCN bit in definition, */
  193. { /* body of function, arglist */
  194. Node *p;
  195. int n;
  196. if (isarr(v)) {
  197. SYNTAX( "`%s' is an array name and a function name", v->nval );
  198. return;
  199. }
  200. v->tval = FCN;
  201. v->sval = (char *) st;
  202. n = 0; /* count arguments */
  203. for (p = vl; p; p = p->nnext)
  204. n++;
  205. v->fval = n;
  206. dprintf( ("defining func %s (%d args)\n", v->nval, n) );
  207. }
  208. int isarg(char *s) /* is s in argument list for current function? */
  209. { /* return -1 if not, otherwise arg # */
  210. extern Node *arglist;
  211. Node *p = arglist;
  212. int n;
  213. for (n = 0; p != 0; p = p->nnext, n++)
  214. if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
  215. return n;
  216. return -1;
  217. }
  218. int ptoi(void *p) /* convert pointer to integer */
  219. {
  220. return (int) (long) p; /* swearing that p fits, of course */
  221. }
  222. Node *itonp(int i) /* and vice versa */
  223. {
  224. return (Node *) (long) i;
  225. }