xref: /plan9/sys/src/cmd/awk/maketab.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 /*
2 Copyright (c) 1989 AT&T
3 	All Rights Reserved
4 
5 THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T.
6 
7 The copyright notice above does not evidence any
8 actual or intended publication of such source code.
9 */
10 
11 /*
12  * this program makes the table to link function names
13  * and type indices that is used by execute() in run.c.
14  * it finds the indices in y.tab.h, produced by yacc.
15  */
16 
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include "awk.h"
21 #include "y.tab.h"
22 
23 struct xx
24 {	int token;
25 	char *name;
26 	char *pname;
27 } proc[] = {
28 	{ PROGRAM, "program", NULL },
29 	{ BOR, "boolop", " || " },
30 	{ AND, "boolop", " && " },
31 	{ NOT, "boolop", " !" },
32 	{ NE, "relop", " != " },
33 	{ EQ, "relop", " == " },
34 	{ LE, "relop", " <= " },
35 	{ LT, "relop", " < " },
36 	{ GE, "relop", " >= " },
37 	{ GT, "relop", " > " },
38 	{ ARRAY, "array", NULL },
39 	{ INDIRECT, "indirect", "$(" },
40 	{ SUBSTR, "substr", "substr" },
41 	{ SUB, "sub", "sub" },
42 	{ GSUB, "gsub", "gsub" },
43 	{ INDEX, "sindex", "sindex" },
44 	{ SPRINTF, "asprintf", "sprintf " },
45 	{ ADD, "arith", " + " },
46 	{ MINUS, "arith", " - " },
47 	{ MULT, "arith", " * " },
48 	{ DIVIDE, "arith", " / " },
49 	{ MOD, "arith", " % " },
50 	{ UMINUS, "arith", " -" },
51 	{ POWER, "arith", " **" },
52 	{ PREINCR, "incrdecr", "++" },
53 	{ POSTINCR, "incrdecr", "++" },
54 	{ PREDECR, "incrdecr", "--" },
55 	{ POSTDECR, "incrdecr", "--" },
56 	{ CAT, "cat", " " },
57 	{ PASTAT, "pastat", NULL },
58 	{ PASTAT2, "dopa2", NULL },
59 	{ MATCH, "matchop", " ~ " },
60 	{ NOTMATCH, "matchop", " !~ " },
61 	{ MATCHFCN, "matchop", "matchop" },
62 	{ INTEST, "intest", "intest" },
63 	{ PRINTF, "aprintf", "printf" },
64 	{ PRINT, "printstat", "print" },
65 	{ CLOSE, "closefile", "closefile" },
66 	{ DELETE, "adelete", "adelete" },
67 	{ SPLIT, "split", "split" },
68 	{ ASSIGN, "assign", " = " },
69 	{ ADDEQ, "assign", " += " },
70 	{ SUBEQ, "assign", " -= " },
71 	{ MULTEQ, "assign", " *= " },
72 	{ DIVEQ, "assign", " /= " },
73 	{ MODEQ, "assign", " %= " },
74 	{ POWEQ, "assign", " ^= " },
75 	{ CONDEXPR, "condexpr", " ?: " },
76 	{ IF, "ifstat", "if(" },
77 	{ WHILE, "whilestat", "while(" },
78 	{ FOR, "forstat", "for(" },
79 	{ DO, "dostat", "do" },
80 	{ IN, "instat", "instat" },
81 	{ NEXT, "jump", "next" },
82 	{ EXIT, "jump", "exit" },
83 	{ BREAK, "jump", "break" },
84 	{ CONTINUE, "jump", "continue" },
85 	{ RETURN, "jump", "ret" },
86 	{ BLTIN, "bltin", "bltin" },
87 	{ CALL, "call", "call" },
88 	{ ARG, "arg", "arg" },
89 	{ VARNF, "getnf", "NF" },
90 	{ GETLINE, "getline", "getline" },
91 	{ 0, "", "" },
92 };
93 
94 #define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
95 char *table[SIZE];
96 char *names[SIZE];
97 
98 main(int argc, char *argv[])
99 {
100 	struct xx *p;
101 	int i, n, tok;
102 	char c;
103 	FILE *fp;
104 	char buf[200], name[200], def[200];
105 
106 	printf("#include <stdio.h>\n");
107 	printf("#include \"awk.h\"\n");
108 	printf("#include \"y.tab.h\"\n\n");
109 	for (i = SIZE; --i >= 0; )
110 		names[i] = "";
111 
112 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
113 		fprintf(stderr, "maketab can't open y.tab.h!\n");
114 		exit(1);
115 	}
116 	printf("static uchar *printname[%d] = {\n", SIZE);
117 	i = 0;
118 	while (fgets(buf, sizeof buf, fp) != NULL) {
119 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
120 		if (c != '#' || n != 4 && strcmp(def,"define") != 0)	/* not a valid #define */
121 			continue;
122 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
123 			fprintf(stderr, "maketab funny token %d %s\n", tok, buf);
124 			exit(1);
125 		}
126 		names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
127 		strcpy(names[tok-FIRSTTOKEN], name);
128 		printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok);
129 		i++;
130 	}
131 	printf("};\n\n");
132 
133 	for (p=proc; p->token!=0; p++)
134 		table[p->token-FIRSTTOKEN] = p->name;
135 	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
136 	for (i=0; i<SIZE; i++)
137 		if (table[i]==0)
138 			printf("\tnullproc,\t/* %s */\n", names[i]);
139 		else
140 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
141 	printf("};\n\n");
142 
143 	printf("uchar *tokname(int n)\n");	/* print a tokname() function */
144 	printf("{\n");
145 	printf("	static uchar buf[100];\n\n");
146 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
147 	printf("		sprintf(buf, \"token %%d\", n);\n");
148 	printf("		return buf;\n");
149 	printf("	}\n");
150 	printf("	return printname[n-FIRSTTOKEN];\n");
151 	printf("}\n");
152 	return 0;
153 }
154