2 /* --------------------------------------------------------------------------
3 * Unparse expressions and types - for use in error messages, type checker
6 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
7 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
8 * Technology, 1994-1999, All rights reserved. It is distributed as
9 * free software under the license in the file "License", which is
10 * included in the distribution.
12 * $RCSfile: output.c,v $
14 * $Date: 2000/04/25 17:43:50 $
15 * ------------------------------------------------------------------------*/
17 #include "hugsbasictypes.h"
23 #define DEPTH_LIMIT 15
25 /* --------------------------------------------------------------------------
26 * Local function prototypes:
27 * ------------------------------------------------------------------------*/
29 static Void local put ( Int,Cell );
30 static Void local putFlds ( Cell,List );
31 static Void local putComp ( Cell,List );
32 static Void local putQual ( Cell );
33 static Bool local isDictVal ( Cell );
34 static Cell local maySkipDict ( Cell );
35 static Void local putAp ( Int,Cell );
36 static Void local putOverInfix ( Int,Text,Syntax,Cell );
37 static Void local putInfix ( Int,Text,Syntax,Cell,Cell );
38 static Void local putSimpleAp ( Cell,Int );
39 static Void local putTuple ( Int,Cell );
40 static Int local unusedTups ( Int,Cell );
41 static Void local unlexOp ( Text );
43 static Void local putSigType ( Cell );
44 static Void local putContext ( List,List,Int );
45 static Void local putPred ( Cell,Int );
46 static Void local putType ( Cell,Int,Int );
47 static Void local putTyVar ( Int );
48 static Bool local putTupleType ( Cell,Int );
49 static Void local putApType ( Type,Int,Int );
51 static Void local putKind ( Kind );
52 static Void local putKinds ( Kinds );
55 /* --------------------------------------------------------------------------
56 * Basic output routines:
57 * ------------------------------------------------------------------------*/
59 FILE *outputStream; /* current output stream */
60 Int outColumn = 0; /* current output column number */
62 #define OPEN(b) if (b) putChr('(');
63 #define CLOSE(b) if (b) putChr(')');
65 Void putChr(c) /* print single character */
71 Void putStr(s) /* print string */
74 Putc(*s,outputStream);
79 Void putInt(n) /* print integer */
81 static char intBuf[16];
82 sprintf(intBuf,"%d",n);
86 Void putPtr(p) /* print pointer */
88 static char intBuf[16];
89 sprintf(intBuf,"%p",p);
93 /* --------------------------------------------------------------------------
94 * Precedence values (See Haskell 1.3 report, p.12):
95 * ------------------------------------------------------------------------*/
97 #define ALWAYS FUN_PREC /* Always use parens (unless atomic)*/
98 /* User defined operators have prec */
99 /* in the range MIN_PREC..MAX_PREC */
100 #define ARROW_PREC MAX_PREC /* for printing -> in type exprs */
101 #define COCO_PREC (MIN_PREC-1) /* :: is left assoc, low precedence */
102 #define COND_PREC (MIN_PREC-2) /* conditional expressions */
103 #define WHERE_PREC (MIN_PREC-3) /* where expressions */
104 #define LAM_PREC (MIN_PREC-4) /* lambda abstraction */
105 #define NEVER LAM_PREC /* Never use parentheses */
108 /* --------------------------------------------------------------------------
109 * Print an expression (used to display context of type errors):
110 * ------------------------------------------------------------------------*/
112 static Int putDepth = 0; /* limits depth of printing DBG */
114 static Void local put(d,e) /* print expression e in context of */
115 Int d; /* operator of precedence d */
119 if (putDepth>DEPTH_LIMIT) {
127 case FINLIST : putChr('[');
131 while (nonNull(xs=tl(xs))) {
139 case AP : putAp(d,e);
142 case NAME : unlexVar(name(e).text);
149 case CONOPCELL : unlexVar(textOf(e));
153 case IPVAR : putChr('?');
157 case WITHEXP : OPEN(d>WHERE_PREC);
158 putStr("dlet {...} in ");
159 put(WHERE_PREC+1,fst(snd(e)));
165 case RECSEL : putChr('#');
166 unlexVar(extText(snd(e)));
170 case FREECELL : putStr("{free!}");
173 case TUPLE : putTuple(tupleOf(e),e);
176 case WILDCARD : putChr('_');
179 case ASPAT : put(NEVER,fst(snd(e)));
181 put(ALWAYS,snd(snd(e)));
184 case LAZYPAT : putChr('~');
188 case DOCOMP : putStr("do {...}");
191 case MDOCOMP : putStr("do {...}");
194 case COMP : putComp(fst(snd(e)),snd(snd(e)));
197 case MONADCOMP : putComp(fst(snd(snd(e))),snd(snd(snd(e))));
200 case CHARCELL : unlexCharConst(charOf(e));
203 case INTCELL : { Int i = intOf(e);
204 if (i<0 && d>=UMINUS_PREC) putChr('(');
206 if (i<0 && d>=UMINUS_PREC) putChr(')');
210 case FLOATCELL : { Float f = floatOf(e);
211 if (f<0 && d>=UMINUS_PREC) putChr('(');
212 putStr(floatToString(e));
213 if (f<0 && d>=UMINUS_PREC) putChr(')');
217 case STRCELL : unlexStrConst(textOf(e));
220 case LETREC : OPEN(d>WHERE_PREC);
223 put(NEVER,fst(snd(e)));
226 putStr("let {...} in ");
228 put(WHERE_PREC+1,snd(snd(e)));
232 case COND : OPEN(d>COND_PREC);
234 put(COND_PREC+1,fst3(snd(e)));
236 put(COND_PREC+1,snd3(snd(e)));
238 put(COND_PREC+1,thd3(snd(e)));
242 case LAMBDA : xs = fst(snd(e));
243 if (whatIs(xs)==BIGLAM)
245 while (nonNull(xs) && isDictVal(hd(xs)))
248 put(d,snd(snd(snd(e))));
255 while (nonNull(xs=tl(xs))) {
261 put(LAM_PREC,snd(snd(snd(e))));
265 case ESIGN : OPEN(d>COCO_PREC);
266 put(COCO_PREC,fst(snd(e)));
268 putSigType(snd(snd(e)));
272 case BIGLAM : put(d,snd(snd(e)));
275 case CASE : putStr("case ");
276 put(NEVER,fst(snd(e)));
279 put(NEVER,snd(snd(e)));
286 case CONFLDS : putFlds(fst(snd(e)),snd(snd(e)));
289 case UPDFLDS : putFlds(fst3(snd(e)),thd3(snd(e)));
292 default : /*internal("put");*/
300 static Void local putFlds(exp,fs) /* Output exp using labelled fields*/
305 for (; nonNull(fs); fs=tl(fs)) {
312 Text t = isName(f) ? name(f).text :
313 isVar(f) ? textOf(f) : inventText();
314 Text s = isName(e) ? name(e).text :
315 isVar(e) ? textOf(e) : inventText();
318 if (haskell98 || s!=t) {
329 static Void local putComp(e,qs) /* print comprehension */
337 while (nonNull(qs=tl(qs))) {
345 static Void local putQual(q) /* print list comp qualifier */
348 case BOOLQUAL : put(NEVER,snd(q));
351 case QWHERE : putStr("let {...}");
354 case FROMQUAL : put(ALWAYS,fst(snd(q)));
356 put(NEVER,snd(snd(q)));
361 static Bool local isDictVal(e) /* Look for dictionary value */
363 #if 0 /* was !DEBUG_CODE -- is it necessary? */
366 case DICTVAR : return TRUE;
367 case NAME : return isDfun(h);
373 static Cell local maySkipDict(e) /* descend function application, */
374 Cell e; { /* ignoring dict aps */
375 while (isAp(e) && isDictVal(arg(e)))
380 static Void local putAp(d,e) /* print application (args>=1) */
384 Text t = 0; /* bogus init to keep gcc -O happy */
388 for (h=e; isAp(h); h=fun(h)) /* find head of expression, looking*/
389 if (!isDictVal(arg(h))) /* for dictionary arguments */
392 if (args==0) { /* Special case when *all* args */
393 put(d,h); /* are dictionary values */
398 case ADDPAT : if (args==1)
399 putInfix(d,textPlus,syntaxOf(namePlus),
400 arg(e),mkInt(intValOf(fun(e))));
405 case TUPLE : OPEN(args>tupleOf(h) && d>=FUN_PREC);
406 putTuple(tupleOf(h),e);
407 CLOSE(args>tupleOf(h) && d>=FUN_PREC);
410 case NAME : if (args==1 &&
411 ((h==nameFromInt && isInt(arg(e))) ||
412 (h==nameFromDouble && isFloat(arg(e))))) {
424 case CONOPCELL : sy = defaultSyntax(t = textOf(h));
428 case EXT : if (args==2) {
433 putStr(textToStr(extText(h)));
435 put(NEVER,extField(e));
438 for (h=e; isAp(h); h=fun(h))
439 if (!isDictVal(arg(h)))
441 } while (isExt(h) && args==2);
456 default : sy = APPLIC;
462 if (sy==APPLIC) { /* print simple application */
468 else if (args==1) { /* print section of the form (e+) */
470 put(FUN_PREC-1,arg(e));
475 else if (args==2) /* infix expr of the form e1 + e2 */
476 putInfix(d,t,sy,arg(maySkipDict(fun(e))),arg(e));
477 else { /* o/w (e1 + e2) e3 ... en (n>=3) */
479 putOverInfix(args,t,sy,e);
484 static Void local putOverInfix(args,t,sy,e)
485 Int args; /* infix applied to >= 3 arguments */
490 putOverInfix(args-1,t,sy,maySkipDict(fun(e)));
492 put(FUN_PREC,arg(e));
495 putInfix(ALWAYS,t,sy,arg(maySkipDict(fun(e))),arg(e));
498 static Void local putInfix(d,t,sy,e,f) /* print infix expression */
500 Text t; /* Infix operator symbol */
501 Syntax sy; /* with name t, syntax s */
502 Cell e, f; { /* Left and right operands */
503 Syntax a = assocOf(sy);
507 put((a==LEFT_ASS ? p : 1+p), e);
511 put((a==RIGHT_ASS ? p : 1+p), f);
515 static Void local putSimpleAp(e,n) /* print application e0 e1 ... en */
519 putSimpleAp(maySkipDict(fun(e)),n-1);
521 put(FUN_PREC,arg(e));
527 static Void local putTuple(ts,e) /* Print tuple expression, allowing*/
528 Int ts; /* for possibility of either too */
529 Cell e; { /* few or too many args to constr */
532 if ((i=unusedTups(ts,e))>0) {
539 static Int local unusedTups(ts,e) /* print first part of tuple expr */
540 Int ts; /* returning number of constructor */
541 Cell e; { /* args not yet printed ... */
543 if ((ts=unusedTups(ts,fun(e))-1)>=0) {
545 putChr(ts>0?',':')');
549 put(FUN_PREC,arg(e));
557 if ((isascii((int)(s[0])) && isalpha((int)(s[0])))
558 || s[0]=='_' || s[0]=='[' || s[0]=='('
560 || (s[0]==':' && s[1]=='D')
570 Void unlexVar(t) /* print text as a variable name */
571 Text t; { /* operator symbols must be enclosed*/
572 unlexVarStr(textToStr(t)); /* in parentheses... except [] ... */
575 static Void local unlexOp(t) /* print text as operator name */
576 Text t; { /* alpha numeric symbols must be */
577 String s = textToStr(t); /* enclosed by backquotes */
579 if (isascii((int)(s[0])) && isalpha((int)(s[0]))) {
588 Void unlexCharConst(c)
591 putStr(unlexChar(c,'\''));
595 Void unlexStrConst(t)
597 String s = textToStr(t);
598 static Char SO = 14; /* ASCII code for '\SO' */
599 Bool lastWasSO = FALSE;
600 Bool lastWasDigit = FALSE;
601 Bool lastWasEsc = FALSE;
605 String ch = unlexChar(*s,'\"');
608 if ((lastWasSO && *ch=='H') ||
609 (lastWasEsc && lastWasDigit
610 && isascii((int)(*ch)) && isdigit((int)(*ch))))
613 lastWasEsc = (*ch=='\\');
614 lastWasSO = (*s==SO);
615 for (; *ch; c = *ch++)
617 lastWasDigit = (isascii(c) && isdigit(c));
622 /* --------------------------------------------------------------------------
623 * Print type expression:
624 * ------------------------------------------------------------------------*/
626 static Void local putSigType(t) /* print (possibly) generic type */
630 Kinds ks = polySigOf(t);
631 for (; isAp(ks); ks=tl(ks))
636 putType(t,NEVER,fr); /* Finally, print rest of type ... */
639 static Void local putContext(ps,qs,fr) /* print context list */
643 Int len = length(ps) + length(qs);
646 Bool useParens = len!=1 || isIP(fun(hd(ps)));
648 Bool useParens = len!=1;
652 for (; nonNull(ps); ps=tl(ps)) {
658 for (; nonNull(qs); qs=tl(qs)) {
668 static Void local putPred(pi,fr) /* Output predicate */
673 if (isExt(fun(pi))) {
674 putType(arg(pi),ALWAYS,fr);
676 putStr(textToStr(extText(fun(pi))));
681 if (whatIs(fun(pi)) == IPCELL) {
685 putType(arg(pi),NEVER,fr);
691 putType(arg(pi),ALWAYS,fr);
693 else if (isClass(pi))
694 putStr(textToStr(cclass(pi).text));
696 putStr(textToStr(textOf(pi)));
698 else if (whatIs(pi) == IPCELL)
699 unlexVar(textOf(pi));
702 putStr("<unknownPredicate>");
705 static Void local putType(t,prec,fr) /* print nongeneric type expression*/
710 case TYCON : putStr(textToStr(tycon(t).text));
713 case TUPLE : { Int n = tupleOf(t);
721 case POLYTYPE : { Kinds ks = polySigOf(t);
722 OPEN(prec>=ARROW_PREC);
724 for (; isAp(ks); ks=tl(ks)) {
730 putType(monotypeOf(t),NEVER,fr);
731 CLOSE(prec>=ARROW_PREC);
736 case QUAL : OPEN(prec>=ARROW_PREC);
737 if (whatIs(snd(snd(t)))==CDICTS) {
738 putContext(fst(snd(t)),fst(snd(snd(snd(t)))),fr);
740 putType(snd(snd(snd(snd(t)))),NEVER,fr);
742 putContext(fst(snd(t)),NIL,fr);
744 putType(snd(snd(t)),NEVER,fr);
746 CLOSE(prec>=ARROW_PREC);
750 case RANK2 : putType(snd(snd(t)),prec,fr);
753 case OFFSET : putTyVar(offsetOf(t));
757 case VAROPCELL : putChr('_');
761 case INTCELL : putChr('_');
765 case AP : { Cell typeHead = getHead(t);
766 Bool brackets = (argCount!=0 && prec>=ALWAYS);
769 if (typeHead==typeList) {
772 putType(arg(t),NEVER,fr);
777 else if (typeHead==typeArrow) {
779 OPEN(prec>=ARROW_PREC);
780 putType(arg(fun(t)),ARROW_PREC,fr);
782 putType(arg(t),NEVER,fr);
783 CLOSE(prec>=ARROW_PREC);
787 else if (argCount==1) {
789 putType(arg(t),ARROW_PREC,fr);
795 else if (isTuple(typeHead)) {
796 if (argCount==tupleOf(typeHead)) {
804 else if (isExt(typeHead)) {
810 putStr(textToStr(extText(typeHead)));
812 putType(extField(t),NEVER,fr);
814 typeHead = getHead(t);
815 } while (isExt(typeHead) && argCount==2);
830 putApType(t,args,fr);
835 default : putStr("(bad type)");
839 static Void local putTyVar(n) /* print type variable */
841 static String alphabet /* for the benefit of EBCDIC :-) */
842 ="abcdefghijklmnopqrstuvwxyz";
843 putChr(alphabet[n%26]);
844 if (n /= 26) /* just in case there are > 26 vars*/
848 static Bool local putTupleType(e,fr) /* print tuple of types, returning */
849 Cell e; /* TRUE if something was printed, */
850 Int fr; { /* FALSE otherwise; used to control*/
851 if (isAp(e)) { /* printing of intermed. commas */
852 if (putTupleType(fun(e),fr))
854 putType(arg(e),NEVER,fr);
860 static Void local putApType(t,n,fr) /* print type application */
865 putApType(fun(t),n-1,fr);
867 putType(arg(t),ALWAYS,fr);
870 putType(t,ALWAYS,fr);
873 /* --------------------------------------------------------------------------
874 * Print kind expression:
875 * ------------------------------------------------------------------------*/
877 static Void local putKind(k) /* print kind expression */
880 case AP : if (isAp(fst(k))) {
892 case ROW : putStr("row");
896 case STAR : putChr('*');
899 case OFFSET : putTyVar(offsetOf(k));
902 case INTCELL : putChr('_');
906 default : putStr("(bad kind)");
910 static Void local putKinds(ks) /* Print list of kinds */
914 else if (nonNull(tl(ks))) {
917 while (nonNull(ks=tl(ks))) {
927 /* --------------------------------------------------------------------------
929 * ------------------------------------------------------------------------*/
931 FILE *mystdout ( Void ) {
932 /* We use this from the gdb command line when debugging */
936 Void printExp(fp,e) /* print expr on specified stream */
944 Void printType(fp,t) /* print type on specified stream */
951 Void printContext(fp,qs) /* print context on spec. stream */
955 putContext(qs,NIL,0);
958 Void printPred(fp,pi) /* print predicate pi on stream */
965 Void printKind(fp,k) /* print kind k on stream */
972 Void printKinds(fp,ks) /* print list of kinds on stream */
979 Void printFD(fp,fd) /* print functional dependency */
984 for (us=fst(fd); nonNull(us); us=tl(us)) {
985 putTyVar(offsetOf(hd(us)));
986 if (nonNull(tl(us))) {
991 for (us=snd(fd); nonNull(us); us=tl(us)) {
992 putTyVar(offsetOf(hd(us)));
993 if (nonNull(tl(us))) {
999 /*-------------------------------------------------------------------------*/