9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

maketab.c (5102B)


      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 <stdio.h>
     32 #include <string.h>
     33 #include <stdlib.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", 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, "getline", "getline" },
    106 	{ 0, "", "" },
    107 };
    108 
    109 #define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
    110 char *table[SIZE];
    111 char *names[SIZE];
    112 
    113 int main(int argc, char *argv[])
    114 {
    115 	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 \"y.tab.h\"\n\n");
    124 	for (i = SIZE; --i >= 0; )
    125 		names[i] = "";
    126 
    127 	if ((fp = fopen("y.tab.h", "r")) == NULL) {
    128 		fprintf(stderr, "maketab can't open y.tab.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\n", tok, buf);
    139 			exit(1);
    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 }
    169