maketab.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /****************************************************************
  10. Copyright (C) Lucent Technologies 1997
  11. All Rights Reserved
  12. Permission to use, copy, modify, and distribute this software and
  13. its documentation for any purpose and without fee is hereby
  14. granted, provided that the above copyright notice appear in all
  15. copies and that both that the copyright notice and this
  16. permission notice and warranty disclaimer appear in supporting
  17. documentation, and that the name Lucent Technologies or any of
  18. its entities not be used in advertising or publicity pertaining
  19. to distribution of the software without specific, written prior
  20. permission.
  21. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  22. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  23. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  24. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  25. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  26. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  27. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  28. THIS SOFTWARE.
  29. ****************************************************************/
  30. /*
  31. * this program makes the table to link function names
  32. * and type indices that is used by execute() in run.c.
  33. * it finds the indices in y.tab.h, produced by yacc.
  34. */
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include "awk.h"
  39. #include "y.tab.h"
  40. struct xx
  41. { int token;
  42. char *name;
  43. char *pname;
  44. } proc[] = {
  45. { PROGRAM, "program", NULL },
  46. { BOR, "boolop", " || " },
  47. { AND, "boolop", " && " },
  48. { NOT, "boolop", " !" },
  49. { NE, "relop", " != " },
  50. { EQ, "relop", " == " },
  51. { LE, "relop", " <= " },
  52. { LT, "relop", " < " },
  53. { GE, "relop", " >= " },
  54. { GT, "relop", " > " },
  55. { ARRAY, "array", NULL },
  56. { INDIRECT, "indirect", "$(" },
  57. { SUBSTR, "substr", "substr" },
  58. { SUB, "sub", "sub" },
  59. { GSUB, "gsub", "gsub" },
  60. { INDEX, "sindex", "sindex" },
  61. { SPRINTF, "awksprintf", "sprintf " },
  62. { ADD, "arith", " + " },
  63. { MINUS, "arith", " - " },
  64. { MULT, "arith", " * " },
  65. { DIVIDE, "arith", " / " },
  66. { MOD, "arith", " % " },
  67. { UMINUS, "arith", " -" },
  68. { POWER, "arith", " **" },
  69. { PREINCR, "incrdecr", "++" },
  70. { POSTINCR, "incrdecr", "++" },
  71. { PREDECR, "incrdecr", "--" },
  72. { POSTDECR, "incrdecr", "--" },
  73. { CAT, "cat", " " },
  74. { PASTAT, "pastat", NULL },
  75. { PASTAT2, "dopa2", NULL },
  76. { MATCH, "matchop", " ~ " },
  77. { NOTMATCH, "matchop", " !~ " },
  78. { MATCHFCN, "matchop", "matchop" },
  79. { INTEST, "intest", "intest" },
  80. { PRINTF, "awkprintf", "printf" },
  81. { PRINT, "printstat", "print" },
  82. { CLOSE, "closefile", "closefile" },
  83. { DELETE, "awkdelete", "awkdelete" },
  84. { SPLIT, "split", "split" },
  85. { ASSIGN, "assign", " = " },
  86. { ADDEQ, "assign", " += " },
  87. { SUBEQ, "assign", " -= " },
  88. { MULTEQ, "assign", " *= " },
  89. { DIVEQ, "assign", " /= " },
  90. { MODEQ, "assign", " %= " },
  91. { POWEQ, "assign", " ^= " },
  92. { CONDEXPR, "condexpr", " ?: " },
  93. { IF, "ifstat", "if(" },
  94. { WHILE, "whilestat", "while(" },
  95. { FOR, "forstat", "for(" },
  96. { DO, "dostat", "do" },
  97. { IN, "instat", "instat" },
  98. { NEXT, "jump", "next" },
  99. { NEXTFILE, "jump", "nextfile" },
  100. { EXIT, "jump", "exit" },
  101. { BREAK, "jump", "break" },
  102. { CONTINUE, "jump", "continue" },
  103. { RETURN, "jump", "ret" },
  104. { BLTIN, "bltin", "bltin" },
  105. { CALL, "call", "call" },
  106. { ARG, "arg", "arg" },
  107. { VARNF, "getnf", "NF" },
  108. { GETLINE, "getline", "getline" },
  109. { 0, "", "" },
  110. };
  111. #define SIZE (LASTTOKEN - FIRSTTOKEN + 1)
  112. char *table[SIZE];
  113. char *names[SIZE];
  114. int main(int argc, char *argv[])
  115. {
  116. struct xx *p;
  117. int i, n, tok;
  118. char c;
  119. FILE *fp;
  120. char buf[200], name[200], def[200];
  121. printf("#include <stdio.h>\n");
  122. printf("#include \"awk.h\"\n");
  123. printf("#include \"y.tab.h\"\n\n");
  124. for (i = SIZE; --i >= 0; )
  125. names[i] = "";
  126. if ((fp = fopen("y.tab.h", "r")) == NULL) {
  127. fprintf(stderr, "maketab can't open y.tab.h!\n");
  128. exit(1);
  129. }
  130. printf("static char *printname[%d] = {\n", SIZE);
  131. i = 0;
  132. while (fgets(buf, sizeof buf, fp) != NULL) {
  133. n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
  134. if (c != '#' || (n != 4 && strcmp(def,"define") != 0)) /* not a valid #define */
  135. continue;
  136. if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
  137. fprintf(stderr, "maketab funny token %d %s\n", tok, buf);
  138. exit(1);
  139. }
  140. names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
  141. strcpy(names[tok-FIRSTTOKEN], name);
  142. printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
  143. i++;
  144. }
  145. printf("};\n\n");
  146. for (p=proc; p->token!=0; p++)
  147. table[p->token-FIRSTTOKEN] = p->name;
  148. printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
  149. for (i=0; i<SIZE; i++)
  150. if (table[i]==0)
  151. printf("\tnullproc,\t/* %s */\n", names[i]);
  152. else
  153. printf("\t%s,\t/* %s */\n", table[i], names[i]);
  154. printf("};\n\n");
  155. printf("char *tokname(int n)\n"); /* print a tokname() function */
  156. printf("{\n");
  157. printf(" static char buf[100];\n\n");
  158. printf(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
  159. printf(" sprintf(buf, \"token %%d\", n);\n");
  160. printf(" return buf;\n");
  161. printf(" }\n");
  162. printf(" return printname[n-FIRSTTOKEN];\n");
  163. printf("}\n");
  164. return 0;
  165. }