morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

maketab.c (5273B)


      1 /*	$OpenBSD: maketab.c,v 1.11 2010/06/13 17:58:19 millert Exp $	*/
      2 /****************************************************************
      3 Copyright (C) Lucent Technologies 1997
      4 All Rights Reserved
      5 
      6 Permission to use, copy, modify, and distribute this software and
      7 its documentation for any purpose and without fee is hereby
      8 granted, provided that the above copyright notice appear in all
      9 copies and that both that the copyright notice and this
     10 permission notice and warranty disclaimer appear in supporting
     11 documentation, and that the name Lucent Technologies or any of
     12 its entities not be used in advertising or publicity pertaining
     13 to distribution of the software without specific, written prior
     14 permission.
     15 
     16 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     17 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     18 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
     19 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     21 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
     22 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
     23 THIS SOFTWARE.
     24 ****************************************************************/
     25 
     26 /*
     27  * this program makes the table to link function names
     28  * and type indices that is used by execute() in run.c.
     29  * it finds the indices in ytab.h, produced by yacc.
     30  */
     31 
     32 #include <stdio.h>
     33 #include <string.h>
     34 #include <stdlib.h>
     35 #include "awk.h"
     36 #include "ytab.h"
     37 
     38 struct xx
     39 {	int token;
     40 	const char *name;
     41 	const char *pname;
     42 } proc[] = {
     43 	{ PROGRAM, "program", NULL },
     44 	{ BOR, "boolop", " || " },
     45 	{ AND, "boolop", " && " },
     46 	{ NOT, "boolop", " !" },
     47 	{ NE, "relop", " != " },
     48 	{ EQ, "relop", " == " },
     49 	{ LE, "relop", " <= " },
     50 	{ LT, "relop", " < " },
     51 	{ GE, "relop", " >= " },
     52 	{ GT, "relop", " > " },
     53 	{ ARRAY, "array", NULL },
     54 	{ INDIRECT, "indirect", "$(" },
     55 	{ SUBSTR, "substr", "substr" },
     56 	{ SUB, "sub", "sub" },
     57 	{ GSUB, "gsub", "gsub" },
     58 	{ INDEX, "sindex", "sindex" },
     59 	{ SPRINTF, "awksprintf", "sprintf " },
     60 	{ ADD, "arith", " + " },
     61 	{ MINUS, "arith", " - " },
     62 	{ MULT, "arith", " * " },
     63 	{ DIVIDE, "arith", " / " },
     64 	{ MOD, "arith", " % " },
     65 	{ UMINUS, "arith", " -" },
     66 	{ POWER, "arith", " **" },
     67 	{ PREINCR, "incrdecr", "++" },
     68 	{ POSTINCR, "incrdecr", "++" },
     69 	{ PREDECR, "incrdecr", "--" },
     70 	{ POSTDECR, "incrdecr", "--" },
     71 	{ CAT, "cat", " " },
     72 	{ PASTAT, "pastat", NULL },
     73 	{ PASTAT2, "dopa2", NULL },
     74 	{ MATCH, "matchop", " ~ " },
     75 	{ NOTMATCH, "matchop", " !~ " },
     76 	{ MATCHFCN, "matchop", "matchop" },
     77 	{ INTEST, "intest", "intest" },
     78 	{ PRINTF, "awkprintf", "printf" },
     79 	{ PRINT, "printstat", "print" },
     80 	{ CLOSE, "closefile", "closefile" },
     81 	{ DELETE, "awkdelete", "awkdelete" },
     82 	{ SPLIT, "split", "split" },
     83 	{ ASSIGN, "assign", " = " },
     84 	{ ADDEQ, "assign", " += " },
     85 	{ SUBEQ, "assign", " -= " },
     86 	{ MULTEQ, "assign", " *= " },
     87 	{ DIVEQ, "assign", " /= " },
     88 	{ MODEQ, "assign", " %= " },
     89 	{ POWEQ, "assign", " ^= " },
     90 	{ CONDEXPR, "condexpr", " ?: " },
     91 	{ IF, "ifstat", "if(" },
     92 	{ WHILE, "whilestat", "while(" },
     93 	{ FOR, "forstat", "for(" },
     94 	{ DO, "dostat", "do" },
     95 	{ IN, "instat", "instat" },
     96 	{ NEXT, "jump", "next" },
     97 	{ NEXTFILE, "jump", "nextfile" },
     98 	{ EXIT, "jump", "exit" },
     99 	{ BREAK, "jump", "break" },
    100 	{ CONTINUE, "jump", "continue" },
    101 	{ RETURN, "jump", "ret" },
    102 	{ BLTIN, "bltin", "bltin" },
    103 	{ CALL, "call", "call" },
    104 	{ ARG, "arg", "arg" },
    105 	{ VARNF, "getnf", "NF" },
    106 	{ GETLINE, "awkgetline", "getline" },
    107 	{ 0, "", "" },
    108 };
    109 
    110 #define SIZE	(LASTTOKEN - FIRSTTOKEN + 1)
    111 const char *table[SIZE];
    112 char *names[SIZE];
    113 
    114 int main(int argc, char *argv[])
    115 {
    116 	const struct xx *p;
    117 	int i, n, tok;
    118 	char c;
    119 	FILE *fp;
    120 	char buf[200], name[200], def[200];
    121 
    122 	printf("#include <stdio.h>\n");
    123 	printf("#include \"awk.h\"\n");
    124 	printf("#include \"ytab.h\"\n\n");
    125 	for (i = SIZE; --i >= 0; )
    126 		names[i] = "";
    127 
    128 	if ((fp = fopen("ytab.h", "r")) == NULL) {
    129 		fprintf(stderr, "maketab: can't open ytab.h!\n");
    130 		exit(1);
    131 	}
    132 	printf("static char *printname[%d] = {\n", SIZE);
    133 	i = 0;
    134 	while (fgets(buf, sizeof buf, fp) != NULL) {
    135 		n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
    136 		if (n != 4 || c != '#' || strcmp(def, "define") != 0)
    137 			continue;	/* not a valid #define */
    138 		if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
    139 			/* fprintf(stderr, "maketab: funny token %d %s ignored\n", tok, buf); */
    140 			continue;
    141 		}
    142 		names[tok-FIRSTTOKEN] = (char *) strdup(name);
    143 		if (names[tok-FIRSTTOKEN] == NULL) {
    144 			fprintf(stderr, "maketab: out of memory\n");
    145 			exit(1);
    146 		}
    147 		printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
    148 		i++;
    149 	}
    150 	printf("};\n\n");
    151 
    152 	for (p=proc; p->token!=0; p++)
    153 		table[p->token-FIRSTTOKEN] = p->name;
    154 	printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
    155 	for (i=0; i<SIZE; i++)
    156 		if (table[i]==0)
    157 			printf("\tnullproc,\t/* %s */\n", names[i]);
    158 		else
    159 			printf("\t%s,\t/* %s */\n", table[i], names[i]);
    160 	printf("};\n\n");
    161 
    162 	printf("char *tokname(int n)\n");	/* print a tokname() function */
    163 	printf("{\n");
    164 	printf("	static char buf[100];\n\n");
    165 	printf("	if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
    166 	printf("		snprintf(buf, sizeof buf, \"token %%d\", n);\n");
    167 	printf("		return buf;\n");
    168 	printf("	}\n");
    169 	printf("	return printname[n-FIRSTTOKEN];\n");
    170 	printf("}\n");
    171 	return 0;
    172 }