summaryrefslogtreecommitdiff
path: root/prettyprint.c
blob: ec00583fb4dc87a59404bd8104efa6f4e2b159fc (plain) (blame)
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
#include <u.h>
#include <libc.h>
#include <bio.h>

#include "dat.h"
#include "fns.h"

Rune *prettyprintlist(Term *, Rune *, int, int, int, int, Module *);
Rune *printlist(Term *, int, int, int, Module *);
int islist(Term *);
int needsquotes(Rune *);

Rune *
prettyprint(Term *t, int quoted, int ignoreops, int numbervars, Module *mod)
{
	Rune *result;
	Rune *args;
	if(mod == nil)
		mod = usermodule;

	switch(t->tag){
	case CompoundTerm:
		if(numbervars && t->arity == 1 
		    && t->children->tag == IntegerTerm 
		    && t->children->ival >= 0 
		    && runestrcmp(t->text, L"$VAR") == 0){
			vlong n = t->children->ival;
			Rune i = L'A' + (n % 26);
			vlong j = n / 26;
			if(j == 0)
				result = runesmprint("%C", i);
			else
				result = runesmprint("%C%lld", i, j);
			break;
		}
		args = printlist(t, quoted, ignoreops, numbervars, mod);
		if(args && !ignoreops){
			result = runesmprint("[%S]", args);
			free(args);
			break;
		}
		Operator *op = getoperator(t->text, mod);
		if(op == nil || ignoreops || !(t->arity == 1 || t->arity == 2)){
			Rune *functor = prettyprint(mkatom(t->text), quoted, ignoreops, numbervars, mod);
			args = prettyprintlist(t->children, L", ", 0, quoted, ignoreops, numbervars, mod);
			result = runesmprint("%S(%S)", functor, args);
			free(functor);
			free(args);
			break;
		}else{
			/* TODO:
				1) Only print spacing between op and args when needed
				2) currectly add () around args in special cases (see 7.10.5.h.2 in spec)
			*/
			Rune *functor = prettyprint(mkatom(t->text), quoted, ignoreops, numbervars, mod);
			Rune *arg1 = prettyprint(t->children, quoted, ignoreops, numbervars, mod);
			Rune *arg2 = t->arity == 2 ? prettyprint(t->children->next, quoted, ignoreops, numbervars, mod) : nil;
			if(t->arity == 2)
				result = runesmprint("%S %S %S", arg1, functor, arg2);
			else{
				if(op->type == Xf || op->type == Yf)
					result = runesmprint("%S %S", arg1, functor);
				else
					result = runesmprint("%S %S", functor, arg1);
			}
			free(functor);
			free(arg1);
			free(arg2);
		}
		break;
	case AtomTerm:
		if(quoted && needsquotes(t->text))
			result = runesmprint("'%S'", t->text);
		else
			result = runesmprint("%S", t->text);
		break;
	case VariableTerm:
		result = runesmprint("_%S", t->text);
		break;
	case FloatTerm:
		result = runesmprint("%f", t->dval);
		break;
	case IntegerTerm:
		result = runesmprint("%lld", t->ival);
		break;
	default:
		result = runesmprint("cant print term with tag %d", t->tag);
		break;
	}

	return result;
}

Rune *
prettyprintlist(Term *t, Rune *sep, int end, int quoted, int ignoreops, int numbervars, Module *mod)
{
	if(t == nil){
		if(end)
			return runesmprint("%S", sep);
		else
			return runesmprint("");
	}

	Rune *str = prettyprint(t, quoted, ignoreops, numbervars, mod);
	Rune *rest = prettyprintlist(t->next, sep, end, quoted, ignoreops, numbervars, mod);
	Rune *result;

	if(t->next != nil)
		result = runesmprint("%S%S%S", str, sep, rest);
	else
		result = runesmprint("%S%S", str, end ? rest : L"");

	free(str);
	free(rest);
	return result;
}

/* printlist prints a list's elements but not the surrounding [ and ] */
Rune *
printlist(Term *list, int quoted, int ignoreops, int numbervars, Module *mod)
{
	if(list->tag != CompoundTerm || list->arity != 2 || runestrcmp(L".", list->text) != 0)
		return nil;

	Term *head = list->children;
	Term *tail = head->next;

	Rune *headstr = prettyprint(head, quoted, ignoreops, numbervars, mod);
	Rune *tailstr = nil;
	Rune *result;

	if(tail->tag == CompoundTerm && tail->arity == 2 && runestrcmp(L".", tail->text) == 0){
		tailstr = printlist(tail, quoted, ignoreops, numbervars, mod);
		result = runesmprint("%S, %S", headstr, tailstr);
	}else if(tail->tag == AtomTerm && runestrcmp(L"[]", tail->text) == 0){
		result = runesmprint("%S", headstr);
	}else{
		tailstr = prettyprint(tail, quoted, ignoreops, numbervars, mod);
		result = runesmprint("%S | %S", headstr, tailstr);
	}
	free(headstr);
	free(tailstr);
	return result;
}

int
needsquotes(Rune *text)
{
	Rune *graphics = L"#$&*+-./:<=>?@^~\\"; /* keep in sync with lexer */
	int len = runestrlen(text);
	int i;

	if(runestrcmp(text, L"") == 0)
		return 1;

	if(runestrchr(graphics, text[0])){
		for(i = 0; i < len; i++){
			if(!runestrchr(graphics, text[i]))
				return 1;
		}
		return 0;
	}

	if(len == 1 && runestrchr(L";!,", text[0]))
		return 0;

	if(len > 0 && !islowerrune(text[0]))
		return 1;

	for(i = 0; i < len; i++)
		if(!isalpharune(text[i]))
			return 1;
	return 0;
}