summaryrefslogtreecommitdiff
path: root/print.c
blob: 8c3dbbf88ba28c0524659020711683d18d539d34 (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
#include <u.h>
#include <libc.h>
#include <bio.h>

#include "apl9.h"

Rune *
ppdatum(Datum d)
{
	Rune *result;
	switch(d.tag){
	case ArrayTag: result = pparray(d.array); break;
	case FunctionTag:
		if(d.func.type == FunctypePrim)
			result = runesmprint("%C", primfuncnames[d.func.code]);
		else if(d.func.type == FunctypeDfn)
			result = runesmprint("{%S}", d.func.dfn);
		else
			result = runesmprint("%S", ppoperator(d.func.operator));
		break;
	case HybridTag: result = runesmprint("%C", primhybridnames[d.func.code]); break;
	case MonadicOpTag:
	case DyadicOpTag: result = ppoperator(d.operator); break;
	case BoundFunctionTag:
		if(d.func.type == FunctypePrim)
			result = runesmprint("%S∘%C", pparray(d.func.left), primfuncnames[d.func.code]);
		else if(d.func.type == FunctypeDfn)
			result = runesmprint("%S∘{%S}", pparray(d.func.left), d.func.dfn);
		else
			result = runesmprint("%S∘%S", pparray(d.func.left), ppoperator(d.func.operator));
		break;
	case LParTag: result = runestrdup(L"("); break;
	case RParTag: result = runestrdup(L")"); break;
	case LBracketTag: result = runestrdup(L"["); break;
	case RBracketTag: result = runestrdup(L"]"); break;
	case ArrowTag: result = runestrdup(L"←"); break;
	case AssignmentTag: result = runesmprint("%S←", d.symbol->name); break;
	case NameTag: result = runestrdup(d.symbol->name); break;
	default: result = runesmprint("<not printable %d>", d.tag);
	}
	return result;
}

Rune *
ppdatums(Datum *ds, int n)
{
	int i;
	Rune *res = runesmprint("");
	Rune *tmp;
	for(i = 0; i < n; i++){
		tmp = res;
		res = runesmprint("%S %S", res, ppdatum(ds[i]));
		free(tmp);
	}
	return res;
}

Rune *
pparray(Array *a)
{
	Rune **elemstrs = malloc(sizeof(Rune *) * a->size);
	for(int i = 0; i < a->size; i++){
		if(a->type == AtypeArray){
			Rune *arrstr = pparray(a->arraydata[i]);
			elemstrs[i] = runesmprint("[%S]", arrstr);
			free(arrstr);
		}else if(a->type == AtypeInt)
			elemstrs[i] = runesmprint("%lld", a->intdata[i]);
	}

	/* Should do some width and height padding here */
	Rune *res = runesmprint("");
	Rune *tmp;
	for(int i = 0; i < a->size; i++){
		tmp = res;
		res = runesmprint("%S%S", res, elemstrs[i]);
		free(tmp);
		free(elemstrs[i]);

		int j = 1;
		int spaceprinted = 0;
		for(int dim = 0; dim < a->rank && i+1 != a->size; dim++){
			j *= a->shape[a->rank-dim-1];
			tmp = res;
			if((i+1)%j == 0){
				spaceprinted = 1;
				res = runesmprint("%S\n", res);
				free(tmp);
			}else if(!spaceprinted){
				spaceprinted = 1;
				res = runesmprint("%S ", res);
				free(tmp);
			}
		}		
	}
	return res;
}

Rune *
ppoperator(Operator op)
{
	Rune *left = op.left ? ppdatum(*op.left) : runestrdup(L"");
	Rune *right = op.right ? ppdatum(*op.right) : runestrdup(L"");
	Rune *res;
	if(op.type == OperatortypeDop)
		res = runesmprint("(%S{%S}%S)", left, op.dop, right);
	else{
		res = runesmprint("(%S%C%S)",
			left,
			op.dyadic ? primdyadopnames[op.code] : primmonopnames[op.code],
			right);
	}
	free(left);
	free(right);
	return res;
}