summaryrefslogtreecommitdiff
path: root/ghc/compiler/parser/infix.c
blob: 627fb92473437b316daa6ef3d4495ad527da5c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 *	Infix operator stuff -- modified from LML
 */

#include <stdio.h>

#include "hspincl.h"
#include "hsparser.tab.h"
#include "constants.h"
#include "utils.h"

static struct infix {
    char *imod;
    char *iop;
    short thismod;
    short unqualok;
    short ifixity;
    short iprecedence;
} infixtab[MAX_INFIX];

static int ninfix = 0;

void
makeinfix(opid, fixity, precedence, modid, imported,
	  withas, impmodid, impasid, withqual,
	  withspec, withhiding, importspec)
  id opid;
  int fixity, precedence;
  long imported, withas, withqual, withspec, withhiding;
  id modid, impmodid, impasid;
  list importspec;
/*
  ToDo: Throw away infix operator if hidden by importspec!
*/
{
    int i;
    char *op = id_to_string(opid);
    char *mod = id_to_string(imported ? (withas ? impasid : impmodid) : modid);
    short thismod = ! imported;
    short unqualok = ! (imported && withqual);

    for(i=0; i < ninfix; ++i)
      {
	if(strcmp(op,infixtab[i].iop)==0 &&
	   strcmp(mod,infixtab[i].imod)==0 &&
	   unqualok==infixtab[i].unqualok)
	  {
	    /* Allow duplicate definitions if they are identical */
	    if (infixtab[i].ifixity==fixity && 
	        infixtab[i].iprecedence==precedence)
	      {
		return;
	      }

	    /* Allow local definition to override an import */
	    else if(thismod && !infixtab[i].thismod)
	      {
		/*continue*/
	      }

	    else
	      {
		char errbuf[ERR_BUF_SIZE];
		sprintf(errbuf,"%s.%s %s already declared to be %s %d\n",
			mod, op, unqualok ? "(unqualified)" : "(qualified)",
			infixstr(infixtab[i].ifixity),
			infixtab[i].iprecedence);
		hsperror(errbuf);
	      }
	  }
      }

    if (ninfix >= MAX_INFIX) {
        char errbuf[ERR_BUF_SIZE];
	sprintf(errbuf,"Too many Infix identifiers (> %d)",MAX_INFIX);
	hsperror(errbuf);
    }

#ifdef HSP_DEBUG
    fprintf(stderr,"makeinfix: %s.%s, fixity=%d prec=%d\n",mod,op,infixint(fixity),precedence);
#endif
    infixtab[ninfix].imod = mod;
    infixtab[ninfix].iop = op;
    infixtab[ninfix].thismod = thismod;
    infixtab[ninfix].unqualok = unqualok;
    infixtab[ninfix].ifixity = fixity;
    infixtab[ninfix].iprecedence = precedence;
    ninfix++;
}

struct infix *
infixlookup(name)
  qid name;
{
    int i;
    struct infix *found = NULL;
    char *op  = qid_to_string(name);
    char *mod = qid_to_mod(name);
    short unqual = mod == NULL;

    for(i = 0; i < ninfix; i++)
      {
	if(strcmp(op,infixtab[i].iop)==0 &&
	   ( (unqual && infixtab[i].unqualok) ||
	     (!unqual && strcmp(mod,infixtab[i].imod)==0)
	   ))
	  {
	    if (! found)
	      {
		/* first find */
		found = infixtab+i;
	      }
	    else if (found && ! found->thismod && infixtab[i].thismod)
	      {
		/* new find for this module; overrides */
		found = infixtab+i;
	      }
	    else if (found && found->thismod && ! infixtab[i].thismod)
	      {
		/* prev find for this module */
	      }
	    else if (found->ifixity == infixtab[i].ifixity &&
		     found->iprecedence == infixtab[i].iprecedence)
	      {
	        /* finds are identical */
	      }
	    else
	      {
		char errbuf[ERR_BUF_SIZE];
		sprintf(errbuf,"conflicting infix declarations for %s.%s\n  %s.%s %s (%s,%d) and %s.%s %s (%s,%d)\n",
			qid_to_pmod(name), op,
			found->imod, found->iop, found->unqualok ? "(unqualified)" : "(qualified)",
			   infixstr(found->ifixity),found->iprecedence,
			infixtab[i].imod, infixtab[i].iop, infixtab[i].unqualok ? "(unqualified)" : "(qualified)",
			   infixstr(infixtab[i].ifixity),infixtab[i].iprecedence);
		hsperror(errbuf);

	      }
	  }
      }

#ifdef HSP_DEBUG
  fprintf(stderr,"infixlookup: %s.%s = fixity=%d prec=%d\n",qid_to_pmod(name),op,infixint(pfixity(found)),pprecedence(found));
#endif

  return(found);
}

int
pfixity(ip)
  struct infix *ip;
{
  return(ip == NULL? INFIXL: ip->ifixity);
}

int
pprecedence(ip)
  struct infix *ip;
{
  return(ip == NULL? 9: ip->iprecedence);
}

char *
infixstr(n)
  int n;
{
  switch(n) {
    case INFIXL:
      return "infixl";
      
    case INFIXR:
      return "infixr";
	
    case INFIX:
      return "infix";

    default:
      hsperror("infixstr");
  }
}

long
infixint(n)
  int n;
{
  switch(n) {
    case INFIXL:
      return -1;
      
    case INFIX:
      return 0;

    case INFIXR:
      return 1;
	
    default:
      hsperror("infixint");
  }
}