Annotation of researchv10no/cmd/prefer/prefawk/maketab.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include "awk.h"
                      3: #include "y.tab.h"
                      4: 
                      5: struct xx
                      6: {      int token;
                      7:        char *name;
                      8:        char *pname;
                      9: } proc[] = {
                     10:        { PROGRAM, "program", NULL },
                     11:        { BOR, "boolop", " || " },
                     12:        { AND, "boolop", " && " },
                     13:        { NOT, "boolop", " !" },
                     14:        { NE, "relop", " != " },
                     15:        { EQ, "relop", " == " },
                     16:        { LE, "relop", " <= " },
                     17:        { LT, "relop", " < " },
                     18:        { GE, "relop", " >= " },
                     19:        { GT, "relop", " > " },
                     20:        { ARRAY, "array", NULL },
                     21:        { INDIRECT, "indirect", "$(" },
                     22:        { SUBSTR, "substr", "substr" },
                     23:        { SUB, "sub", "sub" },
                     24:        { GSUB, "gsub", "gsub" },
                     25:        { INDEX, "sindex", "sindex" },
                     26:        { SPRINTF, "asprintf", "sprintf " },
                     27:        { ADD, "arith", " + " },
                     28:        { MINUS, "arith", " - " },
                     29:        { MULT, "arith", " * " },
                     30:        { DIVIDE, "arith", " / " },
                     31:        { MOD, "arith", " % " },
                     32:        { UMINUS, "arith", " -" },
                     33:        { POWER, "arith", " **" },
                     34:        { PREINCR, "incrdecr", "++" },
                     35:        { POSTINCR, "incrdecr", "++" },
                     36:        { PREDECR, "incrdecr", "--" },
                     37:        { POSTDECR, "incrdecr", "--" },
                     38:        { CAT, "cat", " " },
                     39:        { PASTAT, "pastat", NULL },
                     40:        { PASTAT2, "dopa2", NULL },
                     41:        { MATCH, "matchop", " ~ " },
                     42:        { NOTMATCH, "matchop", " !~ " },
                     43:        { MATCHFCN, "matchop", "matchop" },
                     44:        { INTEST, "intest", "intest" },
                     45:        { PRINTF, "aprintf", "printf" },
                     46:        { PRINT, "print", "print" },
                     47:        { CLOSE, "closefile", "closefile" },
                     48:        { DELETE, "delete", "delete" },
                     49:        { SPLIT, "split", "split" },
                     50:        { ASSIGN, "assign", " = " },
                     51:        { ADDEQ, "assign", " += " },
                     52:        { SUBEQ, "assign", " -= " },
                     53:        { MULTEQ, "assign", " *= " },
                     54:        { DIVEQ, "assign", " /= " },
                     55:        { MODEQ, "assign", " %= " },
                     56:        { POWEQ, "assign", " ^= " },
                     57:        { CONDEXPR, "condexpr", " ?: " },
                     58:        { IF, "ifstat", "if(" },
                     59:        { WHILE, "whilestat", "while(" },
                     60:        { FOR, "forstat", "for(" },
                     61:        { DO, "dostat", "do" },
                     62:        { IN, "instat", "instat" },
                     63:        { NEXT, "jump", "next" },
                     64:        { EXIT, "jump", "exit" },
                     65:        { BREAK, "jump", "break" },
                     66:        { CONTINUE, "jump", "continue" },
                     67:        { RETURN, "jump", "ret" },
                     68:        { BLTIN, "bltin", "bltin" },
                     69:        { CALL, "call", "call" },
                     70:        { ARG, "arg", "arg" },
                     71:        { VARNF, "getnf", "NF" },
                     72:        { GETLINE, "getline", "getline" },
                     73:        { 0, "", "" },
                     74: };
                     75: 
                     76: #define SIZE   LASTTOKEN - FIRSTTOKEN + 1
                     77: char *table[SIZE];
                     78: char *names[SIZE];
                     79: 
                     80: main()
                     81: {
                     82:        struct xx *p;
                     83:        int i, tok;
                     84:        char c;
                     85:        FILE *fp;
                     86:        char buf[100], name[100], def[100];
                     87: 
                     88:        printf("#include \"awk.h\"\n");
                     89:        printf("#include \"y.tab.h\"\n\n");
                     90:        printf("Cell *nullproc();\n");
                     91:        for (i = SIZE; --i >= 0; )
                     92:                names[i] = "";
                     93:        for (p=proc; p->token!=0; p++)
                     94:                if (p == proc || strcmp(p->name, (p-1)->name))
                     95:                        printf("extern Cell *%s();\n", p->name);
                     96: 
                     97:        if ((fp = fopen("y.tab.h", "r")) == NULL) {
                     98:                fprintf(stderr, "maketab can't open y.tab.h!\n");
                     99:                exit(1);
                    100:        }
                    101:        printf("static uchar *printname[%d] = {\n", SIZE);
                    102:        i = 0;
                    103:        while (fgets(buf, sizeof buf, fp) != NULL) {
                    104:                sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
                    105:                if (c != '#')   /* not #define */
                    106:                        continue;
                    107:                if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
                    108:                        fprintf(stderr, "maketab funny token %d %s\n", tok, buf);
                    109:                        exit(1);
                    110:                }
                    111:                names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
                    112:                strcpy(names[tok-FIRSTTOKEN], name);
                    113:                printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok);
                    114:                i++;
                    115:        }
                    116:        printf("};\n\n");
                    117: 
                    118:        for (p=proc; p->token!=0; p++)
                    119:                table[p->token-FIRSTTOKEN] = p->name;
                    120:        printf("\nCell *(*proctab[%d])() = {\n", SIZE);
                    121:        for (i=0; i<SIZE; i++)
                    122:                if (table[i]==0)
                    123:                        printf("\tnullproc,\t/* %s */\n", names[i]);
                    124:                else
                    125:                        printf("\t%s,\t/* %s */\n", table[i], names[i]);
                    126:        printf("};\n\n");
                    127: 
                    128:        printf("uchar *tokname(n)\n");  /* print a tokname() function */
                    129:        printf("{\n");
                    130:        printf("        static uchar buf[100];\n\n");
                    131:        printf("        if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
                    132:        printf("                sprintf(buf, \"token %%d\", n);\n");
                    133:        printf("                return buf;\n");
                    134:        printf("        }\n");
                    135:        printf("        return printname[n-257];\n");
                    136:        printf("}\n");
                    137:        exit(0);
                    138: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.