1 /* $Id: pattern.c,v 1.5 1994/06/24 10:42:16 ceriel Exp $ */
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
8 * Deals with the pattern stuff.
9 * it maintains a table of information about the patterns
10 * Functions : addpattern() and printpatterns()
18 char *p_constraint; /* constraint of this pattern */
19 int p_lineno, /* line number of constraint */
20 p_npat, /* # of instructions in pattern */
21 p_nrepl; /* # of instructions in replacement */
24 static struct pattern *pattable, /* ptr to pattern array */
25 *current, /* ptr to first unoccupied el of
28 *maxpat; /* if beyond this, new space must
32 addpattern(str,l,np,nr) char *str; {
34 * Just add a pattern to the list.
35 * "str" is the constraint, "l" is the line number,
36 * "np" is the number of instructions in the pattern,
37 * "nr" is the number of instructions in the replacement
38 * Space is allocated in chunks of 50
40 char *malloc(), *realloc();
41 register struct pattern *p;
43 if (!pattable) { /* No space allocated yet */
44 pattable = (struct pattern *) malloc(50 * sizeof *pattable);
46 maxpat = pattable + 50;
48 if (current >= maxpat) { /* Allocate some new space */
50 pattable = (struct pattern *) realloc(
52 (unsigned) (sizeof *pattable * (50 + (maxpat - pattable))));
53 current = pattable + (current - p);
54 maxpat = pattable + (maxpat - p) + 50;
57 p->p_constraint = str;
64 prconstraint(str) char *str; {
66 * prints a constraint, with variable names replaced
74 if (isupper(*p) || islower(*p) || *p == '_') {
80 isupper(*q) || islower(*q) || isdigit(*q) || *q == '_')) {
85 /* Temporarily let it end with null byte */
86 name = findident(p,LOOKING,&idtable);
87 if (name) { /* yeah, it was a variable */
88 fprintf(genc,"var[%d].value", name->s_num);
90 else if (!strcmp(p, "ANY")) {
91 fputs("ANY.value", genc);
94 /* Now replace null byte with whatever used to be there */
107 * Prints the pattern_descr table and generates the routine
110 register struct pattern *p;
115 fputs("struct pattern_descr patterns[] = {\n", genc);
116 while (p != current) {
117 fprintf(genc," {%d,pat%d,%d,rep%d,},\n",
118 p->p_npat, i, p->p_nrepl, i);
123 fputs("int\ncheck_constraint(patno){\n\tint r;\n\tswitch(patno){\n",genc);
125 while (p < current) {
126 if (p->p_constraint) {
127 /* The pattern has a constraint */
128 fprintf(genc,"\tcase %d :\n",p - pattable);
129 fprintf(genc,linedir,p->p_lineno,inpfile); /* linedirective */
130 fputs("\tr = (",genc);
131 prconstraint(p->p_constraint);
132 fputs("); break;\n",genc);
136 fputs("\tdefault :\n\t\tr = 1;\n\t}\n\treturn r;\n}\n\n",genc);