2 * Infix operator stuff -- modified from LML
9 #include "hsparser-DPH.tab.h"
11 #include "hsparser.tab.h"
13 #include "constants.h"
16 static short iscope = 1;
23 } infixtab[INFIX_SCOPES][MAX_INFIX] =
26 Name Len Fixity Precedence
39 "notElem", 7, INFIX, 4,
52 ":%", 2, INFIXL, 7, /* possibly wrong; should be omitted? */
64 #define NFIX 31 /* The number of predefined operators */
65 #define ninfix (ninfixtab[iscope])
66 static int ninfixtab[INFIX_SCOPES] = {NFIX,0}; /* # of predefined operators */
67 static char infixstr[MAX_ISTR];
68 static char *infixp = infixstr;
70 /* An "iscope" is an "infix scope": the scope of infix declarations
71 (either the main module or an interface) */
76 if(++iscope > INFIX_SCOPES)
78 char errbuf[ERR_BUF_SIZE];
79 sprintf(errbuf,"Too many infix scopes (> %d)\n",INFIX_SCOPES);
96 for (i=0; i < ninfixtab[iscope+1]; ++i)
98 struct infix *ip = infixtab[iscope+1] + i;
99 makeinfix(install_literal(ip->iname),ip->ifixity,ip->iprecedence);
105 ionelookup(name,iscope)
110 char *iname = id_to_string(name);
112 for(i = 0; i < ninfixtab[iscope]; i++)
114 if(strcmp(iname,infixtab[iscope][i].iname)==0)
127 for (i=iscope; i >= 0; --i)
129 int n = ionelookup(name,i);
131 return (infixtab[i]+n);
146 return infixtab[iscope][n].iname;
153 switch(infixtab[iscope][n].ifixity) {
164 /* Why might it return 0 ?? (WDP 94/11) */
174 fprintf(stderr,"fixity of %s (at %d) is %d\n",infixtab[iscope][n].iname,n,infixtab[iscope][n].ifixity);
176 return(n < 0? INFIXL: infixtab[iscope][n].ifixity);
185 fprintf(stderr,"precedence of %s (at %d) is %d\n",infixtab[iscope][n].iname,n,infixtab[iscope][n].iprecedence);
187 return(n < 0? 9: infixtab[iscope][n].iprecedence);
196 fprintf(stderr,"fixity of %s is %d\n",ip->iname,ip->ifixity);
198 return(ip == NULL? INFIXL: ip->ifixity);
206 fprintf(stderr,"precedence of %s (at %d) is %d\n",ip->iname,ip->iprecedence);
208 return(ip == NULL? 9: ip->iprecedence);
213 makeinfix(ssi, fixity, precedence)
215 int fixity, precedence;
219 char *ss = id_to_string(ssi);
221 for(i=0; i < ninfix; ++i)
223 if(strcmp(ss,infixtab[iscope][i].iname)==0)
225 /* Allow duplicate definitions if they are identical */
226 if(infixtab[iscope][i].ifixity!=fixity ||
227 infixtab[iscope][i].iprecedence!=precedence )
229 char errbuf[ERR_BUF_SIZE];
230 sprintf(errbuf,"(%s) already declared to be %s %d\n",
233 infixtab[iscope][i].iprecedence);
244 if (ninfix >= MAX_INFIX || infixp+l+1 >= &infixstr[MAX_ISTR]) {
245 char errbuf[ERR_BUF_SIZE];
246 sprintf(errbuf,"Too many Infix identifiers (> %d)",MAX_INFIX);
251 fprintf(stderr,"adding %s (was %s), fixity=%d, prec=%d\n",s,ss,fixity,precedence);
253 infixtab[iscope][ninfix].iname = infixp;
256 infixtab[iscope][ninfix].ifixity = fixity;
257 infixtab[iscope][ninfix].iprecedence = precedence;
258 infixtab[iscope][ninfix].ilen = l-1;