BSD 4_4_Lite1 development
[unix-history] / usr / src / contrib / awk.research / maketab.c
CommitLineData
b43ea8de
C
1/****************************************************************
2Copyright (C) AT&T 1993
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 of AT&T or any of its entities
11not be used in advertising or publicity pertaining to
12distribution of the software without specific, written prior
13permission.
14
15AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17IN NO EVENT SHALL AT&T 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 y.tab.h, produced by yacc.
29 */
30
31#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include "awk.h"
35#include "y.tab.h"
36
37struct xx
38{ int token;
39 char *name;
40 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, "asprintf", "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, "aprintf", "printf" },
78 { PRINT, "printstat", "print" },
79 { CLOSE, "closefile", "closefile" },
80 { DELETE, "adelete", "adelete" },
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 { EXIT, "jump", "exit" },
97 { BREAK, "jump", "break" },
98 { CONTINUE, "jump", "continue" },
99 { RETURN, "jump", "ret" },
100 { BLTIN, "bltin", "bltin" },
101 { CALL, "call", "call" },
102 { ARG, "arg", "arg" },
103 { VARNF, "getnf", "NF" },
104 { GETLINE, "getline", "getline" },
105 { 0, "", "" },
106};
107
108#define SIZE (LASTTOKEN - FIRSTTOKEN + 1)
109char *table[SIZE];
110char *names[SIZE];
111
112main(int argc, char *argv[])
113{
114 struct xx *p;
115 int i, n, tok;
116 char c;
117 FILE *fp;
118 char buf[200], name[200], def[200];
119
120 printf("#include <stdio.h>\n");
121 printf("#include \"awk.h\"\n");
122 printf("#include \"y.tab.h\"\n\n");
123 for (i = SIZE; --i >= 0; )
124 names[i] = "";
125
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 uchar *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(uchar *) \"%s\",\t/* %d */\n", name, tok);
143 i++;
144 }
145 printf("};\n\n");
146
147 for (p=proc; p->token!=0; p++)
148 table[p->token-FIRSTTOKEN] = p->name;
149 printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
150 for (i=0; i<SIZE; i++)
151 if (table[i]==0)
152 printf("\tnullproc,\t/* %s */\n", names[i]);
153 else
154 printf("\t%s,\t/* %s */\n", table[i], names[i]);
155 printf("};\n\n");
156
157 printf("uchar *tokname(int n)\n"); /* print a tokname() function */
158 printf("{\n");
159 printf(" static uchar buf[100];\n\n");
160 printf(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
161 printf(" sprintf(buf, \"token %%d\", n);\n");
162 printf(" return buf;\n");
163 printf(" }\n");
164 printf(" return printname[n-FIRSTTOKEN];\n");
165 printf("}\n");
166 return 0;
167}