2 /* --------------------------------------------------------------------------
3 * Unparse expressions and types - for use in error messages, type checker
6 * Hugs 98 is Copyright (c) Mark P Jones, Alastair Reid and the Yale
7 * Haskell Group 1994-99, and is distributed as Open Source software
8 * under the Artistic License; see the file "Artistic" that is included
9 * in the distribution for details.
11 * $RCSfile: output.c,v $
13 * $Date: 1999/03/01 14:46:50 $
14 * ------------------------------------------------------------------------*/
22 #define DEPTH_LIMIT 15
24 /* --------------------------------------------------------------------------
25 * Local function prototypes:
26 * ------------------------------------------------------------------------*/
28 static Void local put Args((Int,Cell));
29 static Void local putFlds Args((Cell,List));
30 static Void local putComp Args((Cell,List));
31 static Void local putQual Args((Cell));
32 static Bool local isDictVal Args((Cell));
33 static Cell local maySkipDict Args((Cell));
34 static Void local putAp Args((Int,Cell));
35 static Void local putOverInfix Args((Int,Text,Syntax,Cell));
36 static Void local putInfix Args((Int,Text,Syntax,Cell,Cell));
37 static Void local putSimpleAp Args((Cell,Int));
38 static Void local putTuple Args((Int,Cell));
39 static Int local unusedTups Args((Int,Cell));
40 static Void local unlexOp Args((Text));
42 static Void local putSigType Args((Cell));
43 static Void local putContext Args((List,List,Int));
44 static Void local putPred Args((Cell,Int));
45 static Void local putType Args((Cell,Int,Int));
46 static Void local putTyVar Args((Int));
47 static Bool local putTupleType Args((Cell,Int));
48 static Void local putApType Args((Type,Int,Int));
50 static Void local putKind Args((Kind));
51 static Void local putKinds Args((Kinds));
53 /* --------------------------------------------------------------------------
54 * Basic output routines:
55 * ------------------------------------------------------------------------*/
57 FILE *outputStream; /* current output stream */
58 Int outColumn = 0; /* current output column number */
60 #define OPEN(b) if (b) putChr('(');
61 #define CLOSE(b) if (b) putChr(')');
63 Void putChr(c) /* print single character */
69 Void putStr(s) /* print string */
72 Putc(*s,outputStream);
77 Void putInt(n) /* print integer */
79 static char intBuf[16];
80 sprintf(intBuf,"%d",n);
84 Void putPtr(p) /* print pointer */
86 static char intBuf[16];
87 sprintf(intBuf,"%p",p);
91 /* --------------------------------------------------------------------------
92 * Precedence values (See Haskell 1.3 report, p.12):
93 * ------------------------------------------------------------------------*/
95 #define ALWAYS FUN_PREC /* Always use parens (unless atomic)*/
96 /* User defined operators have prec */
97 /* in the range MIN_PREC..MAX_PREC */
98 #define ARROW_PREC MAX_PREC /* for printing -> in type exprs */
99 #define COCO_PREC (MIN_PREC-1) /* :: is left assoc, low precedence */
100 #define COND_PREC (MIN_PREC-2) /* conditional expressions */
101 #define WHERE_PREC (MIN_PREC-3) /* where expressions */
102 #define LAM_PREC (MIN_PREC-4) /* lambda abstraction */
103 #define NEVER LAM_PREC /* Never use parentheses */
106 /* --------------------------------------------------------------------------
107 * Print an expression (used to display context of type errors):
108 * ------------------------------------------------------------------------*/
110 static Int putDepth = 0; /* limits depth of printing DBG */
112 static Void local put(d,e) /* print expression e in context of */
113 Int d; /* operator of precedence d */
117 if (putDepth>DEPTH_LIMIT) {
125 case FINLIST : putChr('[');
129 while (nonNull(xs=tl(xs))) {
137 case AP : putAp(d,e);
140 case NAME : unlexVar(name(e).text);
147 case CONOPCELL : unlexVar(textOf(e));
151 case RECSEL : putChr('#');
152 unlexVar(extText(snd(e)));
156 case FREECELL : putStr("{free!}");
159 case TUPLE : putTuple(tupleOf(e),e);
162 case WILDCARD : putChr('_');
165 case ASPAT : put(NEVER,fst(snd(e)));
167 put(ALWAYS,snd(snd(e)));
170 case LAZYPAT : putChr('~');
174 case DOCOMP : putStr("do {...}");
177 case COMP : putComp(fst(snd(e)),snd(snd(e)));
180 case MONADCOMP : putComp(fst(snd(snd(e))),snd(snd(snd(e))));
183 case CHARCELL : unlexCharConst(charOf(e));
186 case INTCELL : { Int i = intOf(e);
187 if (i<0 && d>=UMINUS_PREC) putChr('(');
189 if (i<0 && d>=UMINUS_PREC) putChr(')');
196 case POSNUM : xs = bigOut(e,NIL,d>=UMINUS_PREC);
197 for (; nonNull(xs); xs=tl(xs))
198 putChr(charOf(arg(hd(xs))));
202 case FLOATCELL : { Float f = floatOf(e);
203 if (f<0 && d>=UMINUS_PREC) putChr('(');
204 putStr(floatToString(f));
205 if (f<0 && d>=UMINUS_PREC) putChr(')');
209 case STRCELL : unlexStrConst(textOf(e));
212 case LETREC : OPEN(d>WHERE_PREC);
215 put(NEVER,fst(snd(e)));
218 putStr("let {...} in ");
220 put(WHERE_PREC+1,snd(snd(e)));
224 case COND : OPEN(d>COND_PREC);
226 put(COND_PREC+1,fst3(snd(e)));
228 put(COND_PREC+1,snd3(snd(e)));
230 put(COND_PREC+1,thd3(snd(e)));
234 case LAMBDA : xs = fst(snd(e));
235 if (whatIs(xs)==BIGLAM)
237 while (nonNull(xs) && isDictVal(hd(xs)))
240 put(d,snd(snd(snd(e))));
247 while (nonNull(xs=tl(xs))) {
253 put(LAM_PREC,snd(snd(snd(e))));
257 case ESIGN : OPEN(d>COCO_PREC);
258 put(COCO_PREC,fst(snd(e)));
260 putSigType(snd(snd(e)));
264 case BIGLAM : put(d,snd(snd(e)));
267 case CASE : putStr("case ");
268 put(NEVER,fst(snd(e)));
271 put(NEVER,snd(snd(e)));
278 case CONFLDS : putFlds(fst(snd(e)),snd(snd(e)));
281 case UPDFLDS : putFlds(fst3(snd(e)),thd3(snd(e)));
284 default : /*internal("put");*/
292 static Void local putFlds(exp,fs) /* Output exp using labelled fields*/
297 for (; nonNull(fs); fs=tl(fs)) {
304 Text t = isName(f) ? name(f).text :
305 isVar(f) ? textOf(f) : inventText();
306 Text s = isName(e) ? name(e).text :
307 isVar(e) ? textOf(e) : inventText();
310 if (haskell98 || s!=t) {
321 static Void local putComp(e,qs) /* print comprehension */
329 while (nonNull(qs=tl(qs))) {
337 static Void local putQual(q) /* print list comp qualifier */
340 case BOOLQUAL : put(NEVER,snd(q));
343 case QWHERE : putStr("let {...}");
346 case FROMQUAL : put(ALWAYS,fst(snd(q)));
348 put(NEVER,snd(snd(q)));
353 static Bool local isDictVal(e) /* Look for dictionary value */
358 case DICTVAR : return TRUE;
359 case NAME : return isDfun(h);
365 static Cell local maySkipDict(e) /* descend function application, */
366 Cell e; { /* ignoring dict aps */
367 while (isAp(e) && isDictVal(arg(e)))
372 static Void local putAp(d,e) /* print application (args>=1) */
380 for (h=e; isAp(h); h=fun(h)) /* find head of expression, looking*/
381 if (!isDictVal(arg(h))) /* for dictionary arguments */
384 if (args==0) { /* Special case when *all* args */
385 put(d,h); /* are dictionary values */
391 case ADDPAT : if (args==1)
392 putInfix(d,textPlus,syntaxOf(namePlus),
393 arg(e),mkInt(intValOf(fun(e))));
399 case TUPLE : OPEN(args>tupleOf(h) && d>=FUN_PREC);
400 putTuple(tupleOf(h),e);
401 CLOSE(args>tupleOf(h) && d>=FUN_PREC);
404 case NAME : if (args==1 &&
405 ((h==nameFromInt && isInt(arg(e))) ||
407 (h==nameFromInteger && isBignum(arg(e))) ||
409 (h==nameFromDouble && isFloat(arg(e))))) {
421 case CONOPCELL : sy = defaultSyntax(t = textOf(h));
425 case EXT : if (args==2) {
430 putStr(textToStr(extText(h)));
432 put(NEVER,extField(e));
435 for (h=e; isAp(h); h=fun(h))
436 if (!isDictVal(arg(h)))
438 } while (isExt(h) && args==2);
453 default : sy = APPLIC;
459 if (sy==APPLIC) { /* print simple application */
465 else if (args==1) { /* print section of the form (e+) */
467 put(FUN_PREC-1,arg(e));
472 else if (args==2) /* infix expr of the form e1 + e2 */
473 putInfix(d,t,sy,arg(maySkipDict(fun(e))),arg(e));
474 else { /* o/w (e1 + e2) e3 ... en (n>=3) */
476 putOverInfix(args,t,sy,e);
481 static Void local putOverInfix(args,t,sy,e)
482 Int args; /* infix applied to >= 3 arguments */
487 putOverInfix(args-1,t,sy,maySkipDict(fun(e)));
489 put(FUN_PREC,arg(e));
492 putInfix(ALWAYS,t,sy,arg(maySkipDict(fun(e))),arg(e));
495 static Void local putInfix(d,t,sy,e,f) /* print infix expression */
497 Text t; /* Infix operator symbol */
498 Syntax sy; /* with name t, syntax s */
499 Cell e, f; { /* Left and right operands */
500 Syntax a = assocOf(sy);
504 put((a==LEFT_ASS ? p : 1+p), e);
508 put((a==RIGHT_ASS ? p : 1+p), f);
512 static Void local putSimpleAp(e,n) /* print application e0 e1 ... en */
516 putSimpleAp(maySkipDict(fun(e)),n-1);
518 put(FUN_PREC,arg(e));
524 static Void local putTuple(ts,e) /* Print tuple expression, allowing*/
525 Int ts; /* for possibility of either too */
526 Cell e; { /* few or too many args to constr */
529 if ((i=unusedTups(ts,e))>0) {
536 static Int local unusedTups(ts,e) /* print first part of tuple expr */
537 Int ts; /* returning number of constructor */
538 Cell e; { /* args not yet printed ... */
540 if ((ts=unusedTups(ts,fun(e))-1)>=0) {
542 putChr(ts>0?',':')');
546 put(FUN_PREC,arg(e));
552 Void unlexVar(t) /* print text as a variable name */
553 Text t; { /* operator symbols must be enclosed*/
554 String s = textToStr(t); /* in parentheses... except [] ... */
556 if ((isascii((int)(s[0])) && isalpha((int)(s[0])))
557 || s[0]=='_' || s[0]=='[' || s[0]=='(')
566 static Void local unlexOp(t) /* print text as operator name */
567 Text t; { /* alpha numeric symbols must be */
568 String s = textToStr(t); /* enclosed by backquotes */
570 if (isascii((int)(s[0])) && isalpha((int)(s[0]))) {
579 Void unlexCharConst(c)
582 putStr(unlexChar(c,'\''));
586 Void unlexStrConst(t)
588 String s = textToStr(t);
589 static Char SO = 14; /* ASCII code for '\SO' */
590 Bool lastWasSO = FALSE;
591 Bool lastWasDigit = FALSE;
592 Bool lastWasEsc = FALSE;
596 String ch = unlexChar(*s,'\"');
599 if ((lastWasSO && *ch=='H') ||
600 (lastWasEsc && lastWasDigit
601 && isascii((int)(*ch)) && isdigit((int)(*ch))))
604 lastWasEsc = (*ch=='\\');
605 lastWasSO = (*s==SO);
606 for (; *ch; c = *ch++)
608 lastWasDigit = (isascii(c) && isdigit(c));
613 /* --------------------------------------------------------------------------
614 * Print type expression:
615 * ------------------------------------------------------------------------*/
617 static Void local putSigType(t) /* print (possibly) generic type */
621 Kinds ks = polySigOf(t);
622 for (; isAp(ks); ks=tl(ks))
627 putType(t,NEVER,fr); /* Finally, print rest of type ... */
630 static Void local putContext(ps,qs,fr) /* print context list */
634 Int len = length(ps) + length(qs);
639 for (; nonNull(ps); ps=tl(ps)) {
645 for (; nonNull(qs); qs=tl(qs)) {
656 static Void local putPred(pi,fr) /* Output predicate */
661 if (isExt(fun(pi))) {
662 putType(arg(pi),ALWAYS,fr);
664 putStr(textToStr(extText(fun(pi))));
670 putType(arg(pi),ALWAYS,fr);
672 else if (isClass(pi))
673 putStr(textToStr(cclass(pi).text));
675 putStr(textToStr(textOf(pi)));
677 putStr("<unknownPredicate>");
680 static Void local putType(t,prec,fr) /* print nongeneric type expression*/
685 case TYCON : putStr(textToStr(tycon(t).text));
688 case TUPLE : { Int n = tupleOf(t);
696 case POLYTYPE : { Kinds ks = polySigOf(t);
697 OPEN(prec>=ARROW_PREC);
699 for (; isAp(ks); ks=tl(ks)) {
705 putType(monotypeOf(t),NEVER,fr);
706 CLOSE(prec>=ARROW_PREC);
711 case QUAL : OPEN(prec>=ARROW_PREC);
712 if (whatIs(snd(snd(t)))==CDICTS) {
713 putContext(fst(snd(t)),fst(snd(snd(snd(t)))),fr);
715 putType(snd(snd(snd(snd(t)))),NEVER,fr);
717 putContext(fst(snd(t)),NIL,fr);
719 putType(snd(snd(t)),NEVER,fr);
721 CLOSE(prec>=ARROW_PREC);
725 case RANK2 : putType(snd(snd(t)),prec,fr);
728 case OFFSET : putTyVar(offsetOf(t));
732 case VAROPCELL : putChr('_');
736 case INTCELL : putChr('_');
740 case AP : { Cell typeHead = getHead(t);
741 Bool brackets = (argCount!=0 && prec>=ALWAYS);
744 if (typeHead==typeList) {
747 putType(arg(t),NEVER,fr);
752 else if (typeHead==typeArrow) {
754 OPEN(prec>=ARROW_PREC);
755 putType(arg(fun(t)),ARROW_PREC,fr);
757 putType(arg(t),NEVER,fr);
758 CLOSE(prec>=ARROW_PREC);
761 else if (argCount==1) {
763 putType(arg(t),ARROW_PREC,fr);
768 else if (isTuple(typeHead)) {
769 if (argCount==tupleOf(typeHead)) {
777 else if (isExt(typeHead)) {
783 putStr(textToStr(extText(typeHead)));
785 putType(extField(t),NEVER,fr);
787 typeHead = getHead(t);
788 } while (isExt(typeHead) && argCount==2);
803 putApType(t,args,fr);
808 default : putStr("(bad type)");
812 static Void local putTyVar(n) /* print type variable */
814 static String alphabet /* for the benefit of EBCDIC :-) */
815 ="abcdefghijklmnopqrstuvwxyz";
816 putChr(alphabet[n%26]);
817 if (n /= 26) /* just in case there are > 26 vars*/
821 static Bool local putTupleType(e,fr) /* print tuple of types, returning */
822 Cell e; /* TRUE if something was printed, */
823 Int fr; { /* FALSE otherwise; used to control*/
824 if (isAp(e)) { /* printing of intermed. commas */
825 if (putTupleType(fun(e),fr))
827 putType(arg(e),NEVER,fr);
833 static Void local putApType(t,n,fr) /* print type application */
838 putApType(fun(t),n-1,fr);
840 putType(arg(t),ALWAYS,fr);
843 putType(t,ALWAYS,fr);
846 /* --------------------------------------------------------------------------
847 * Print kind expression:
848 * ------------------------------------------------------------------------*/
850 static Void local putKind(k) /* print kind expression */
853 case AP : if (isAp(fst(k))) {
865 case ROW : putStr("row");
869 case STAR : putChr('*');
872 case OFFSET : putTyVar(offsetOf(k));
875 case INTCELL : putChr('_');
879 default : putStr("(bad kind)");
883 static Void local putKinds(ks) /* Print list of kinds */
887 else if (nonNull(tl(ks))) {
890 while (nonNull(ks=tl(ks))) {
900 /* --------------------------------------------------------------------------
902 * ------------------------------------------------------------------------*/
904 Void printExp(fp,e) /* print expr on specified stream */
912 Void printType(fp,t) /* print type on specified stream */
919 Void printContext(fp,qs) /* print context on spec. stream */
923 putContext(qs,NIL,0);
926 Void printPred(fp,pi) /* print predicate pi on stream */
933 Void printKind(fp,k) /* print kind k on stream */
940 Void printKinds(fp,ks) /* print list of kinds on stream */
947 /*-------------------------------------------------------------------------*/