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