]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/awk/maketab.c
merge
[plan9front.git] / sys / src / cmd / awk / maketab.c
1 /****************************************************************
2 Copyright (C) Lucent Technologies 1997
3 All Rights Reserved
4
5 Permission to use, copy, modify, and distribute this software and
6 its documentation for any purpose and without fee is hereby
7 granted, provided that the above copyright notice appear in all
8 copies and that both that the copyright notice and this
9 permission notice and warranty disclaimer appear in supporting
10 documentation, and that the name Lucent Technologies or any of
11 its entities not be used in advertising or publicity pertaining
12 to distribution of the software without specific, written prior
13 permission.
14
15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 THIS 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 <u.h>
32 #include <libc.h>
33 #include <bio.h>
34 #include "awk.h"
35 #include "y.tab.h"
36
37 struct xx
38 {       int token;
39         char *name;
40         char *pname;
41 } proc[] = {
42         { PROGRAM, "program", nil },
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", nil },
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", nil },
72         { PASTAT2, "dopa2", nil },
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, "getline", "getline" },
106         { 0, "", "" },
107 };
108
109 #define SIZE    (LASTTOKEN - FIRSTTOKEN + 1)
110 char *table[SIZE];
111 char *names[SIZE];
112
113 void main(int, char**)
114 {
115         struct xx *p;
116         int i, tok;
117         Biobuf *fp;
118         char *buf, *toks[3];
119
120         print("#include <u.h>\n");
121         print("#include <libc.h>\n");
122         print("#include <bio.h>\n");
123         print("#include \"awk.h\"\n");
124         print("#include \"y.tab.h\"\n\n");
125         for (i = SIZE; --i >= 0; )
126                 names[i] = "";
127
128         if ((fp = Bopen("y.tab.h", OREAD)) == nil) {
129                 fprint(2, "maketab can't open y.tab.h!\n");
130                 exits("can't open y.tab.h");
131         }
132         print("static char *printname[%d] = {\n", SIZE);
133         i = 0;
134         while ((buf = Brdline(fp, '\n')) != nil) {
135                 buf[Blinelen(fp)-1] = '\0';
136                 tokenize(buf, toks, 3);
137                 if (toks[0] == nil || strcmp("#define", toks[0]) != 0)  /* not a valid #define */
138                         continue;
139                 tok = strtol(toks[2], nil, 10);
140                 if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
141                         fprint(2, "maketab funny token %d %s\n", tok, buf);
142                         exits("funny token");
143                 }
144                 names[tok-FIRSTTOKEN] = (char *) malloc(strlen(toks[1])+1);
145                 strcpy(names[tok-FIRSTTOKEN], toks[1]);
146                 print("\t(char *) \"%s\",\t/* %d */\n", toks[1], tok);
147                 i++;
148         }
149         print("};\n\n");
150
151         for (p=proc; p->token!=0; p++)
152                 table[p->token-FIRSTTOKEN] = p->name;
153         print("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
154         for (i=0; i<SIZE; i++)
155                 if (table[i]==0)
156                         print("\tnullproc,\t/* %s */\n", names[i]);
157                 else
158                         print("\t%s,\t/* %s */\n", table[i], names[i]);
159         print("};\n\n");
160
161         print("char *tokname(int n)\n");        /* print a tokname() function */
162         print("{\n");
163         print(" static char buf[100];\n\n");
164         print(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
165         print("         sprint(buf, \"token %%d\", n);\n");
166         print("         return buf;\n");
167         print(" }\n");
168         print(" return printname[n-FIRSTTOKEN];\n");
169         print("}\n");
170         exits(0);
171 }