1 /**********************************************************************
4 * Syntax-related Utility Functions *
7 **********************************************************************/
13 #include "constants.h"
22 This file, syntax.c, is used both for the regular parser
23 and for parseint; however, we use the tab.h file from
24 the regular parser. This could get us in trouble...
27 #include "hsparser-DPH.tab.h"
29 #include "hsparser.tab.h"
30 #endif /* Data Parallel Haskell */
33 extern short icontexts;
35 extern unsigned endlineno, startlineno;
36 extern BOOLEAN hashIds, etags;
38 /* Forward Declarations */
40 char *ineg PROTO((char *));
41 tree unparen PROTO((tree));
43 tree fns[MAX_CONTEXTS] = { NULL };
44 short samefn[MAX_CONTEXTS] = { 0 };
45 tree prevpatt[MAX_CONTEXTS] = { NULL };
47 BOOLEAN inpat = FALSE;
51 check infix value in range 0..9
60 sscanf(vals,"%d",&value);
62 if (value < 0 || value > 9)
65 value = value < 0 ? 0 : 9;
66 fprintf(stderr,"Precedence must be between 0 and 9 (value given: %d, changed to %d)\n",
74 Check Previous Pattern usage
81 hsperror("\"'\" used before a function definition");
88 SAMEFN = (hashIds && fn == (char *)FN) || (FN != NULL && strcmp(fn,gident(FN)) == 0);
91 printf("%u\n",startlineno);
93 fprintf(stderr,"%u\tchecksamefn:%s\n",startlineno,fn);
99 Check that a list of types is a list of contexts
103 checkcontext(context)
109 while (tlist(context) == lcons)
111 ty = (ttype) lhd(context);
112 valid = tttype(ty) == tname;
116 valid = tlist(tl) != lnil && tlist(ltl(tl)) == lnil && tttype((ttype) lhd(tl)) == namedtvar;
120 hsperror("Not a valid context");
122 context = ltl(context);
130 hsperror("syntax error");
144 switch (tliteral(glit(e))) {
156 hsperror("not a valid literal pattern");
161 if (ttree(gnexp(e)) != lit) {
162 hsperror("syntax error: \"-\" applied to a non-literal");
164 literal l = glit(gnexp(e));
166 if (tliteral(l) != integer && tliteral(l) != floatr) {
167 hsperror("syntax error: \"-\" applied to a non-number");
186 checkpatt(glazyp(e));
190 checkpatt(gplusp(e));
195 tree f = ginfun((struct Sap *)e),
196 a1 = ginarg1((struct Sap *)e),
197 a2 = ginarg2((struct Sap *)e);
199 struct Splusp *e_plus;
203 if (ttree(f) == ident && strcmp(id_to_string(gident(f)),"+")==0)
205 if(ttree(a2) != lit || tliteral((literal) ttree(a2)) != integer)
206 hsperror("syntax error: non-integer in (n+k) pattern");
208 if(ttree(a1) == wildp || (ttree(a1) == ident && !isconstr(gident(a1))))
211 e_plus = (struct Splusp *) e;
212 *Rgplusp(e_plus) = a1;
213 *Rgplusi(e_plus) = glit(a2);
216 hsperror("syntax error: non-variable in (n+k) pattern");
220 if(ttree(f) == ident && !isconstr(gident(f)))
221 hsperror("syntax error: variable application in pattern");
229 list tup = gtuplelist(e);
230 while (tlist(tup) == lcons)
245 while (tlist(l) == lcons)
256 list pids = gprocid(e);
257 while (tlist(pids) == lcons)
259 checkpatt(lhd(pids));
262 checkpatt(gprocdata(e));
268 hsperror("not a pattern");
273 BOOLEAN /* return TRUE if LHS is a pattern; FALSE if a function */
274 is_patt_or_fun(e, outer_level)
277 /* only needed because x+y is a *function* at
278 the "outer level", but an n+k *pattern* at
279 any "inner" level. Sigh. */
284 switch (tliteral(glit(e))) {
296 hsperror("Literal is not a valid LHS");
316 /* This change might break ap infixop below. BEWARE.
317 return (isconstr(gident(e)));
323 /* do not "unparen", otherwise the error
324 fromInteger ((x,y) {-no comma-} z)
327 tree fn = function(e);
329 /*fprintf(stderr,"ap:f=%d %s (%d),a=%d %s\n",ttree(gfun(e)),(ttree(gfun(e)) == ident) ? (gident(gfun(e))) : "",ttree(fn),ttree(garg(e)),(ttree(garg(e)) == ident) ? (gident(garg(e))) : "");*/
332 if(ttree(fn) == ident)
333 return(isconstr(gident(fn)));
335 else if(ttree(fn) == tinfixop)
336 return(is_patt_or_fun(fn, TRUE/*still at "outer level"*/));
339 hsperror("Not a legal pattern binding in LHS");
344 tree f = ginfun((struct Sap *)e),
345 a1 = unparen(ginarg1((struct Sap *)e)),
346 a2 = unparen(ginarg2((struct Sap *)e));
348 struct Splusp *e_plus;
350 /* Even function definitions must have pattern arguments */
354 if (ttree(f) == ident)
356 if(strcmp(id_to_string(gident(f)),"+")==0 && ttree(a1) == ident)
358 /* n+k is a function at the top level */
359 if(outer_level || ttree(a2) != lit || tliteral((literal) ttree(a2)) != integer)
363 e_plus = (struct Splusp *) e;
364 *Rgplusp(e_plus) = a1;
365 *Rgplusi(e_plus) = glit(a2);
369 return(isconstr(gident(f)));
373 hsperror("Strange infix op");
377 return(is_patt_or_fun(gpare(e), FALSE /*no longer at "outer level"*/));
379 /* Anything else must be an illegal LHS */
381 hsperror("Not a valid LHS");
384 abort(); /* should never get here */
388 /* interface for the outside world */
393 return(is_patt_or_fun(e, TRUE /*outer-level*/));
397 Return the function at the root of a series of applications.
408 return(function(gfun(e)));
411 return(function(gpare(e)));
423 while (ttree(e) == par)
436 if (isconstr(gident(f)))
442 char errbuf[ERR_BUF_SIZE];
443 sprintf(errbuf,"syntax error: not a constructor application -- %s",gident(f));
448 checkconap(gfun(f), garg(f));
453 checkconap(gpare(f), a);
458 char errbuf[ERR_BUF_SIZE];
459 sprintf(errbuf,"syntax error: tuple pattern `applied' to arguments (missing comma?)");
465 hsperror("syntax error: not a constructor application");
471 Extend a function by adding a new definition to its list of bindings.
479 /* fprintf(stderr,"extending binding (%d)\n",tbinding(bind));*/
480 if(tbinding(bind) == abind)
481 bind = gabindsnd(bind);
483 if(tbinding(bind) == pbind)
484 gpbindl(bind) = lconc(gpbindl(bind), gpbindl(rule));
485 else if(tbinding(bind) == fbind)
486 gfbindl(bind) = lconc(gfbindl(bind), gfbindl(rule));
488 fprintf(stderr,"bind error in decl (%d)\n",tbinding(bind));
493 Precedence Parser for Haskell. By default operators are left-associative,
494 so it is only necessary to rearrange the parse tree where the new operator
495 has a greater precedence than the existing one, or where two operators have
496 the same precedence and are both right-associative. Error conditions are
499 Note: Prefix negation has the same precedence as infix minus.
500 The algorithm must thus take account of explicit negates.
509 fprintf(stderr,"precparse %x\n",ttree(t));
512 if(ttree(t) == tinfixop)
514 tree left = ginarg1((struct Sap *)t);
518 fprintf(stderr,"precparse:t=");ptree(t);printf("\nleft=");ptree(left);printf("\n");
522 if(ttree(left) == negate)
524 id tid = gident(ginfun((struct Sap *)t));
525 struct infix *ttabpos = infixlookup(tid);
526 struct infix *ntabpos = infixlookup(install_literal("-")); /* This should be static, but C won't allow that. */
528 if(pprecedence(ntabpos) < pprecedence(ttabpos))
530 tree right = ginarg2((struct Sap *)t);
532 gnexp(t) = mkinfixop(tid,gnexp(left),right);
536 else if(ttree(left) == tinfixop)
538 id lid = gident(ginfun((struct Sap *)left)),
539 tid = gident(ginfun((struct Sap *)t));
541 struct infix *lefttabpos = infixlookup(lid),
542 *ttabpos = infixlookup(tid);
546 fprintf(stderr,"precparse: lid=%s; tid=%s,ltab=%d,ttab=%d\n",
547 id_to_string(lid),id_to_string(tid),pprecedence(lefttabpos),pprecedence(ttabpos));
551 if (pprecedence(lefttabpos) < pprecedence(ttabpos))
552 rearrangeprec(left,t);
554 else if (pprecedence(lefttabpos) == pprecedence(ttabpos))
556 if(pfixity(lefttabpos) == INFIXR && pfixity(ttabpos) == INFIXR)
557 rearrangeprec(left,t);
559 else if(pfixity(lefttabpos) == INFIXL && pfixity(ttabpos) == INFIXL)
564 char errbuf[ERR_BUF_SIZE];
565 sprintf(errbuf,"Cannot mix %s and %s in the same infix expression",
566 id_to_string(lid), id_to_string(tid));
576 Rearrange a tree to effectively insert an operator in the correct place.
577 The recursive call to precparse ensures this filters down as necessary.
584 tree arg3 = ginarg2((struct Sap *)t2);
585 id id1 = gident(ginfun((struct Sap *)t1)),
586 id2 = gident(ginfun((struct Sap *)t2));
587 gident(ginfun((struct Sap *)t1)) = id2;
588 gident(ginfun((struct Sap *)t2)) = id1;
590 ginarg2((struct Sap *)t2) = t1;
591 ginarg1((struct Sap *)t2) = ginarg1((struct Sap *)t1);
592 ginarg1((struct Sap *)t1) = ginarg2((struct Sap *)t1);
593 ginarg2((struct Sap *)t1) = arg3;
598 createpat(guards,where)
607 func = install_literal("");
609 /* I don't think I need to allocate func here -- KH */
610 return(mkpgrhs(PREVPATT,guards,where,func,endlineno));
618 /* partain: want a more magical symbol ???
619 return(ldub(mkbool(1),expr));
621 return(ldub(mkident(install_literal("__o")),expr)); /* __otherwise */
629 char *p = xmalloc(strlen(i)+2);
637 checkmodname(import,interface)
638 id import, interface;
640 if(strcmp(import,interface) != 0)
642 char errbuf[ERR_BUF_SIZE];
643 sprintf(errbuf,"interface name (%s) does not agree with import name (%s)",interface,import);
649 Check the ordering of declarations in a cbody.
650 All signatures must appear before any declarations.
657 /* The ordering must be correct for a singleton */
658 if(tbinding(decls)!=abind)
661 checkorder2(decls,TRUE);
665 checkorder2(decls,sigs)
669 while(tbinding(decls)==abind)
671 /* Perform a left-traversal if necessary */
672 binding left = gabindfst(decls);
673 if(tbinding(left)==abind)
674 sigs = checkorder2(left,sigs);
676 sigs = checksig(sigs,left);
677 decls = gabindsnd(decls);
680 return(checksig(sigs,decls));
689 BOOLEAN issig = tbinding(decl) == sbind || tbinding(decl) == nullbind;
691 hsperror("Signature appears after definition in class body");
698 Check the precedence of a pattern or expression to ensure that
699 sections and function definitions have the correct parse.
703 checkprec(exp,fn,right)
708 if(ttree(exp) == tinfixop)
710 struct infix *ftabpos = infixlookup(fn);
711 struct infix *etabpos = infixlookup(gident(ginfun((struct Sap *)exp)));
713 if (pprecedence(etabpos) > pprecedence(ftabpos) ||
714 (pprecedence(etabpos) == pprecedence(ftabpos) &&
715 ((pfixity(etabpos) == INFIXR && pfixity(ftabpos) == INFIXR && right) ||
716 ((pfixity(etabpos) == INFIXL && pfixity(ftabpos) == INFIXL && !right)))))
721 char errbuf[ERR_BUF_SIZE];
722 sprintf(errbuf,"Cannot mix %s and %s on a LHS or in a section",
723 id_to_string(fn), id_to_string(gident(ginfun((struct Sap *)exp))));