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);
99 for (i=0; i < ninfixtab[iscope+1]; ++i)
101 struct infix *ip = infixtab[iscope+1] + i;
102 makeinfix(install_literal(ip->iname),ip->ifixity,ip->iprecedence);
108 ionelookup(id name, int iscope)
111 char *iname = id_to_string(name);
113 for(i = 0; i < ninfixtab[iscope]; i++)
115 if(strcmp(iname,infixtab[iscope][i].iname)==0)
128 for (i=iscope; i >= 0; --i)
130 int n = ionelookup(name,i);
132 return (infixtab[i]+n);
146 return infixtab[iscope][n].iname;
152 switch(infixtab[iscope][n].ifixity) {
163 /* 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);
186 fprintf(stderr,"precedence of %s (at %d) is %d\n",infixtab[iscope][n].iname,n,infixtab[iscope][n].iprecedence);
188 return(n < 0? 9: infixtab[iscope][n].iprecedence);
197 fprintf(stderr,"fixity of %s is %d\n",ip->iname,ip->ifixity);
199 return(ip == NULL? INFIXL: ip->ifixity);
207 fprintf(stderr,"precedence of %s (at %d) is %d\n",ip->iname,ip->iprecedence);
209 return(ip == NULL? 9: ip->iprecedence);
214 makeinfix(ssi, fixity, precedence)
216 int fixity, precedence;
220 char *ss = id_to_string(ssi);
222 for(i=0; i < ninfix; ++i)
224 if(strcmp(ss,infixtab[iscope][i].iname)==0)
226 /* Allow duplicate definitions if they are identical */
227 if(infixtab[iscope][i].ifixity!=fixity ||
228 infixtab[iscope][i].iprecedence!=precedence )
230 char errbuf[ERR_BUF_SIZE];
231 sprintf(errbuf,"(%s) already declared to be %s %d\n",
234 infixtab[iscope][i].iprecedence);
245 if (ninfix >= MAX_INFIX || infixp+l+1 >= &infixstr[MAX_ISTR]) {
246 char errbuf[ERR_BUF_SIZE];
247 sprintf(errbuf,"Too many Infix identifiers (> %d)",MAX_INFIX);
252 fprintf(stderr,"adding %s (was %s), fixity=%d, prec=%d\n",s,ss,fixity,precedence);
254 infixtab[iscope][ninfix].iname = infixp;
257 infixtab[iscope][ninfix].ifixity = fixity;
258 infixtab[iscope][ninfix].iprecedence = precedence;
259 infixtab[iscope][ninfix].ilen = l-1;