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