-
Notifications
You must be signed in to change notification settings - Fork 59
/
inspector.ts
175 lines (161 loc) · 3.59 KB
/
inspector.ts
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
175
import {
U,
DOUBLE,
STR,
SYM,
CONS,
TENSOR,
NUM,
iscons,
car,
cdr,
Cons,
Num,
Double,
Sym,
Str,
Tensor,
issymbol,
BaseAtom,
} from './runtime/defs';
import { length } from './sources/misc';
class AtomFormatter {
header(object: unknown) {
if (!this.isValidAtom(object)) {
return null;
}
return ['span', {}, this.atomTypeNode(object), this.summary(object)];
}
hasBody(object: U) {
switch (object.k) {
case DOUBLE:
case STR:
case SYM:
return false;
case CONS:
case TENSOR:
case NUM:
return true;
}
}
body(x: U) {
if (x.k == NUM) {
return propertyList([
{ name: 'a', value: x.q.a.toString() },
{ name: 'b', value: x.q.b.toString() },
]);
} else if (x.k == CONS) {
const items = iscons(x) ? [...x] : [];
return propertyList(items);
} else if (x.k == TENSOR) {
const elems = split_tensor(x, 0, 0)[1];
return propertyList(elems);
}
return null;
}
isValidAtom(x: unknown): x is U {
switch (x.constructor) {
case Cons:
case Num:
case Double:
case Sym:
case Str:
case Tensor:
return true;
}
return false;
}
atomTypeNode(a: U) {
return ['span', { style: 'font-weight: bold' }, this.atomType(a), ' '];
}
atomType(a: U) {
switch (a.k) {
case CONS:
return 'Cons';
case NUM:
return 'Num';
case DOUBLE:
return 'Double';
case STR:
return 'String';
case TENSOR:
return 'Tensor';
case SYM:
return 'Sym';
}
}
summary(a: U) {
switch (a.k) {
case SYM:
case NUM:
case DOUBLE:
case STR:
return a.toString();
case TENSOR:
const dims = (a.tensor.dim as number[]).slice(0, a.tensor.ndim);
return `size = ${dims.join('x')}`;
case CONS:
let name = issymbol(a.cons.car) ? `${a.cons.car} ` : '';
return `${name}length = ${length(a)}`;
}
}
}
type Atoms = U | Atoms[];
interface Property {
name: string;
value: Atoms | string;
}
function isProperty(x: Atoms | Property): x is Property {
if (x instanceof BaseAtom || Array.isArray(x)) {
return false;
}
return true;
}
type TemplateTag =
| [
'div' | 'span' | 'ol' | 'li' | 'table' | 'tr' | 'td',
{ style?: string },
...(TemplateTag | string)[]
]
| ['object', { object: any }];
function propertyList(items: (Atoms | Property)[]): TemplateTag {
const ol: TemplateTag = [
'ol',
{
style:
'list-style-type:none; padding-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-left: 12px',
},
];
items.forEach((x, i) => {
const name = isProperty(x) ? x.name : `${i}`;
const nameSpan: TemplateTag = [
'span',
{ style: '"color: rgb(136, 19, 145); background-color: #bada55"' },
`${name}: `,
];
const child: TemplateTag = [
'object',
{ object: isProperty(x) ? x.value : x },
];
ol.push(['li', {}, nameSpan, child]);
});
return ol;
}
function split_tensor(p: Tensor, j: number, k: number): [number, Atoms[]] {
// based on print_tensor_inner
let accumulator = [];
if (j < p.tensor.ndim - 1) {
for (let i = 0; i < p.tensor.dim[j]; i++) {
let result: Atoms[];
[k, result] = split_tensor(p, j + 1, k);
accumulator.push(result);
}
} else {
for (let i = 0; i < p.tensor.dim[j]; i++) {
accumulator.push(p.tensor.elem[k]);
k++;
}
}
return [k, accumulator];
}
(globalThis as any)['devtoolsFormatters'] = [new AtomFormatter()];