Plan 9 from Bell Labs’s /usr/web/sources/contrib/boyd/sys/src/cmd/awk/maketab.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name Lucent Technologies or any of
its entities not be used in advertising or publicity pertaining
to distribution of the software without specific, written prior
permission.

LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/

/*
 * this program makes the table to link function names
 * and type indices that is used by execute() in run.c.
 * it finds the indices in y.tab.h, produced by yacc.
 */

#include "awk.h"
#include "y.tab.h"

struct xx
{	int token;
	char *name;
	char *pname;
} proc[] = {
	{ PROGRAM, "program", 0 },
	{ BOR, "boolop", " || " },
	{ AND, "boolop", " && " },
	{ NOT, "boolop", " !" },
	{ NE, "relop", " != " },
	{ EQ, "relop", " == " },
	{ LE, "relop", " <= " },
	{ LT, "relop", " < " },
	{ GE, "relop", " >= " },
	{ GT, "relop", " > " },
	{ ARRAY, "array", 0 },
	{ INDIRECT, "indirect", "$(" },
	{ SUBSTR, "substr", "substr" },
	{ SUB, "sub", "sub" },
	{ GSUB, "gsub", "gsub" },
	{ INDEX, "sindex", "sindex" },
	{ SPRINTF, "awksprintf", "sprintf " },
	{ ADD, "arith", " + " },
	{ MINUS, "arith", " - " },
	{ MULT, "arith", " * " },
	{ DIVIDE, "arith", " / " },
	{ MOD, "arith", " % " },
	{ UMINUS, "arith", " -" },
	{ POWER, "arith", " **" },
	{ PREINCR, "incrdecr", "++" },
	{ POSTINCR, "incrdecr", "++" },
	{ PREDECR, "incrdecr", "--" },
	{ POSTDECR, "incrdecr", "--" },
	{ CAT, "cat", " " },
	{ PASTAT, "pastat", 0 },
	{ PASTAT2, "dopa2", 0 },
	{ MATCH, "matchop", " ~ " },
	{ NOTMATCH, "matchop", " !~ " },
	{ MATCHFCN, "matchop", "matchop" },
	{ INTEST, "intest", "intest" },
	{ PRINTF, "awkprintf", "printf" },
	{ PRINT, "printstat", "print" },
	{ CLOSE, "closefile", "closefile" },
	{ DELETE, "awkdelete", "awkdelete" },
	{ SPLIT, "split", "split" },
	{ ASSIGN, "assign", " = " },
	{ ADDEQ, "assign", " += " },
	{ SUBEQ, "assign", " -= " },
	{ MULTEQ, "assign", " *= " },
	{ DIVEQ, "assign", " /= " },
	{ MODEQ, "assign", " %= " },
	{ POWEQ, "assign", " ^= " },
	{ CONDEXPR, "condexpr", " ?: " },
	{ IF, "ifstat", "if(" },
	{ WHILE, "whilestat", "while(" },
	{ FOR, "forstat", "for(" },
	{ DO, "dostat", "do" },
	{ IN, "instat", "instat" },
	{ NEXT, "jump", "next" },
	{ NEXTFILE, "jump", "nextfile" },
	{ EXIT, "jump", "exit" },
	{ BREAK, "jump", "break" },
	{ CONTINUE, "jump", "continue" },
	{ RETURN, "jump", "ret" },
	{ BLTIN, "bltin", "bltin" },
	{ CALL, "call", "call" },
	{ ARG, "arg", "arg" },
	{ VARNF, "getnf", "NF" },
	{ GETLINE, "getline", "getline" },
	{ 0, "", "" },
};

#define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
char *table[SIZE];
char *names[SIZE];

/* this replaces that horrible sscanf */
static int smash(char *s, char *c, char *def, char *name, int *tok)
{
	char	*args[3];
	int	n;
	
	if ((n = getfields(&s[1], args, nelem(args), 0, " \t")) != nelem(args))
		return n;

	*c = *s;
	strcpy(def, args[0]);
	strcpy(name, args[1]);
	*tok = atoi(args[2]);

	return n;
}

int main(int argc, char *argv[])
{
	struct xx *p;
	int i, n, tok;
	char c;
	Biobuf *bp;
	char *buf, name[200], def[200];

	USED(argc);
	USED(argv);

	print("#include \"awk.h\"\n");
	print("#include \"y.tab.h\"\n\n");
	for (i = SIZE; --i >= 0; )
		names[i] = "";

	if ((bp = Bopen("y.tab.h", OREAD)) == 0) {
		fprint(2, "maketab can't open y.tab.h!\n");
		exits("y.tab.h");
	}
	print("static char *printname[%d] = {\n", SIZE);
	i = 0;
	while ((buf = Brdstr(bp, '\n', 1)) != 0) {
		n = smash(buf, &c, def, name, &tok);
		if (c != '#' || (n != 4 && strcmp(def,"define") != 0))	/* not a valid #define */
			continue;
		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
			fprint(2, "maketab funny token %d %s\n", tok, buf);
			exits("funny token");
		}
		names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
		strcpy(names[tok-FIRSTTOKEN], name);
		print("\t(char *) \"%s\",\t/* %d */\n", name, tok);
		free(buf);
		i++;
	}
	print("};\n\n");

	for (p=proc; p->token!=0; p++)
		table[p->token-FIRSTTOKEN] = p->name;
	print("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
	for (i=0; i<SIZE; i++)
		if (table[i]==0)
			print("\tnullproc,\t/* %s */\n", names[i]);
		else
			print("\t%s,\t/* %s */\n", table[i], names[i]);
	print("};\n\n");

	print("char *tokname(int n)\n");	/* print a tokname() function */
	print("{\n");
	print("	static char buf[100];\n\n");
	print("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
	print("		sprint(buf, \"token %%d\", n);\n");
	print("		return buf;\n");
	print("	}\n");
	print("	return printname[n-FIRSTTOKEN];\n");
	print("}\n");
	exits(0);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].