Repository navigation
Expand file tree
/
Copy path_compiler_runtime.py
More file actions
286 lines (275 loc) · 14.3 KB
/
Copy path_compiler_runtime.py
File metadata and controls
286 lines (275 loc) · 14.3 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Original, dependency-selected ECMAScript helpers for python-browser/1."""
from functools import lru_cache
import json
HELPERS = {
'error': ((), r'''
class $PyError extends Error {
constructor(name, message, location) { super(message); this.name = name; this.location = location; }
}
function $fail(name, message, location) { throw new $PyError(name, message, location); }
'''),
'binding': (('error',), r'''
const $UNBOUND = Symbol('unbound');
function $get(value, name, local, location) {
if (value === $UNBOUND) $fail(local ? 'UnboundLocalError' : 'NameError',
local ? `cannot access local variable '${name}' where it is not associated with a value` : `name '${name}' is not defined`, location);
return value;
}
'''),
'str': ((), r'''
class $Str {
constructor(codepoints) { this.type = 'str'; this.codepoints = Object.freeze(codepoints); Object.freeze(this); }
}
function $str(points) { return new $Str(points); }
function $text(value) { return value.codepoints.map(point => String.fromCodePoint(point)).join(''); }
'''),
'tuple': ((), r'''
class $Tuple {
constructor(items) { this.type = 'tuple'; this.items = Object.freeze(items); Object.freeze(this); }
}
const $EMPTY = new $Tuple([]);
function $tuple(items) { return items.length ? new $Tuple(items) : $EMPTY; }
'''),
'numeric': (('error',), r'''
function $number(value, location) {
if (typeof value === 'bigint') return value;
if (typeof value === 'boolean') return value ? 1n : 0n;
$fail('TypeError', 'Operation requires an adopted Python integer.', location);
}
'''),
'truth': (('error', 'str', 'tuple'), r'''
function $truth(value, location) {
if (value === null || typeof value === 'boolean' || typeof value === 'bigint') return Boolean(value);
if (value instanceof $Str) return value.codepoints.length !== 0;
if (value instanceof $Tuple) return value.items.length !== 0;
$fail('TypeError', 'Truth testing this value is outside python-browser/1.', location);
}
'''),
'binary': (('numeric', 'str'), r'''
function $binary(operator, left, right, location) {
if (operator === '+' && left instanceof $Str && right instanceof $Str) return $str([...left.codepoints, ...right.codepoints]);
left = $number(left, location); right = $number(right, location);
if (operator === '+') return left + right;
if (operator === '-') return left - right;
if (operator === '*') return left * right;
if (right === 0n) $fail('ZeroDivisionError', operator === '//' ? 'division by zero' : 'integer modulo by zero', location);
let quotient = left / right;
if (left % right !== 0n && (left < 0n) !== (right < 0n)) quotient -= 1n;
return operator === '//' ? quotient : left - quotient * right;
}
'''),
'unary': (('numeric', 'truth'), r'''
function $unary(operator, value, location) {
if (operator === 'not') return !$truth(value, location);
value = $number(value, location); return operator === '-' ? -value : value;
}
'''),
'equal': (('numeric', 'str', 'tuple'), r'''
function $equal(left, right, location) {
if (left === right) return true;
if (['bigint', 'boolean'].includes(typeof left) && ['bigint', 'boolean'].includes(typeof right)) return $number(left, location) === $number(right, location);
if (left instanceof $Str && right instanceof $Str) return left.codepoints.length === right.codepoints.length && left.codepoints.every((point, index) => point === right.codepoints[index]);
if (left instanceof $Tuple && right instanceof $Tuple) return left.items.length === right.items.length && left.items.every((value, index) => $equal(value, right.items[index], location));
return false;
}
function $order(left, right, location) {
if (['bigint', 'boolean'].includes(typeof left) && ['bigint', 'boolean'].includes(typeof right)) {
left = $number(left, location); right = $number(right, location); return left < right ? -1 : left > right ? 1 : 0;
}
const strings = left instanceof $Str && right instanceof $Str;
const tuples = left instanceof $Tuple && right instanceof $Tuple;
if (!strings && !tuples) $fail('TypeError', 'Ordering requires matching adopted sequence or numeric values.', location);
const a = strings ? left.codepoints : left.items, b = strings ? right.codepoints : right.items;
for (let index = 0; index < Math.min(a.length, b.length); index++) {
if (strings ? a[index] !== b[index] : !$equal(a[index], b[index], location)) return strings ? (a[index] < b[index] ? -1 : 1) : $order(a[index], b[index], location);
}
return a.length < b.length ? -1 : a.length > b.length ? 1 : 0;
}
'''),
'compare': (('equal',), r'''
function $compare(operator, left, right, location) {
if (operator === '==' || operator === '!=') return $equal(left, right, location) === (operator === '==');
if (operator === 'is' || operator === 'is not') {
if (left !== null && right !== null && typeof left !== 'boolean' && typeof right !== 'boolean' &&
(typeof left === 'bigint' || typeof right === 'bigint' || left instanceof $Str || right instanceof $Str || typeof left === 'number' || typeof right === 'number')) {
$fail('TypeError', 'Integer/string interning identity is outside python-browser/1.', location);
}
return (left === right) === (operator === 'is');
}
if (operator === 'in' || operator === 'not in') {
let found = false;
if (right instanceof $Tuple) found = right.items.some(value => $equal(left, value, location));
else if (right instanceof $Str && left instanceof $Str) {
outer: for (let start = 0; start <= right.codepoints.length - left.codepoints.length; start++) {
for (let offset = 0; offset < left.codepoints.length; offset++) if (left.codepoints[offset] !== right.codepoints[start + offset]) continue outer;
found = true; break;
}
} else $fail('TypeError', 'Membership requires adopted string/tuple values.', location);
return found === (operator === 'in');
}
const order = $order(left, right, location);
return operator === '<' ? order < 0 : operator === '<=' ? order <= 0 : operator === '>' ? order > 0 : order >= 0;
}
'''),
'index': (('numeric', 'str', 'tuple'), r'''
function $index(value, index, location) {
const string = value instanceof $Str;
if (!string && !(value instanceof $Tuple)) $fail('TypeError', 'Indexing requires a Python str or tuple.', location);
const items = string ? value.codepoints : value.items;
index = $number(index, location); if (index < 0n) index += BigInt(items.length);
if (index < 0n || index >= BigInt(items.length)) $fail('IndexError', string ? 'string index out of range' : 'tuple index out of range', location);
return string ? $str([items[Number(index)]]) : items[Number(index)];
}
'''),
'iterate': (('error', 'str', 'tuple'), r'''
function $iterate(value, location) {
if (value instanceof $Str) return value.codepoints.map(point => $str([point]));
if (value instanceof $Tuple) return value.items;
$fail('TypeError', 'Iteration requires a Python str or tuple.', location);
}
'''),
'call': (('error',), r'''
class $Callable {
constructor(name, count, defaults, body) { Object.assign(this, {name, count, defaults, body}); Object.freeze(this); }
}
let $depth = 0;
function $fn(name, count, defaults, body) { return new $Callable(name, count, defaults, body); }
function $call(fn, args, location) {
if (!(fn instanceof $Callable)) $fail('TypeError', 'Value is not an adopted callable.', location);
if (fn.count !== null) {
if (args.length < fn.count - fn.defaults.length || args.length > fn.count) $fail('TypeError', `${fn.name}() received an unsupported argument count`, location);
for (let index = args.length; index < fn.count; index++) args.push(fn.defaults[index - (fn.count - fn.defaults.length)]);
}
if ($depth >= 128) $fail('RecursionError', 'Maximum compiled call depth (128) exceeded.', location);
$depth++;
try { return fn.body(args, location); } finally { $depth--; }
}
'''),
'len': (('call', 'str', 'tuple'), r'''
const $len = $fn('len', 1, [], (args, location) => {
const value = args[0];
if (value instanceof $Str) return BigInt(value.codepoints.length);
if (value instanceof $Tuple) return BigInt(value.items.length);
$fail('TypeError', 'len() requires an adopted Python str or tuple.', location);
});
'''),
'types': (('call', 'str', 'tuple'), r'''
const $types = Object.freeze({str: Symbol('str'), int: Symbol('int'), bool: Symbol('bool'), tuple: Symbol('tuple')});
const $isinstance = $fn('isinstance', 2, [], (args, location) => {
const [value, type] = args;
if (type === $types.str) return value instanceof $Str;
if (type === $types.tuple) return value instanceof $Tuple;
if (type === $types.int) return typeof value === 'bigint' || typeof value === 'boolean';
if (type === $types.bool) return typeof value === 'boolean';
$fail('TypeError', 'isinstance() requires an adopted builtin type.', location);
});
'''),
'exception': (('call', 'str'), r'''
const $TypeError = $fn('TypeError', 1, [], (args, location) => {
if (!(args[0] instanceof $Str)) $fail('TypeError', 'TypeError() requires a str message in this profile.', location);
return new $PyError('TypeError', $text(args[0]), location);
});
'''),
'repr': (('error', 'str', 'tuple'), r'''
const $nonprintable = __NONPRINTABLE__;
function $printable(point) {
let low = 0, high = $nonprintable.length;
while (low < high) { const mid = (low + high) >> 1; if ($nonprintable[mid][1] < point) low = mid + 1; else high = mid; }
return low === $nonprintable.length || point < $nonprintable[low][0];
}
function $repr(value, location, quoteString = false) {
if (value === null) return 'None';
if (typeof value === 'boolean') return value ? 'True' : 'False';
if (typeof value === 'bigint' || typeof value === 'number') return value.toString();
if (value instanceof $Str) {
if (!quoteString) {
if (value.codepoints.some(point => point >= 0xd800 && point <= 0xdfff)) $fail('TypeError', 'Surrogate text output requires a separately adopted sink.', location);
return $text(value);
}
const quote = !value.codepoints.includes(39) || value.codepoints.includes(34) ? "'" : '"';
return quote + value.codepoints.map(point => {
if (point === 92 || point === quote.codePointAt(0)) return '\\' + String.fromCodePoint(point);
if (point === 9) return '\\t'; if (point === 10) return '\\n'; if (point === 13) return '\\r';
if (!$printable(point)) return (point < 256 ? '\\x' : point < 65536 ? '\\u' : '\\U') + point.toString(16).padStart(point < 256 ? 2 : point < 65536 ? 4 : 8, '0');
return String.fromCodePoint(point);
}).join('') + quote;
}
if (value instanceof $Tuple) return '(' + value.items.map(item => $repr(item, location, true)).join(', ') + (value.items.length === 1 ? ',' : '') + ')';
if (value instanceof $PyError) return value.message;
$fail('TypeError', 'Printing this value is outside python-browser/1.', location);
}
'''),
'print': (('call', 'repr'), r'''
const $print = $fn('print', null, [], (args, location) => { write(args.map(value => $repr(value, location)).join(' ') + '\n'); return null; });
'''),
'javascript': (('call',), r'''
class $JSArray { constructor(values) { this.values = values; } }
const $of = $fn('Array.of', null, [], args => new $JSArray(args));
const $JArray = Object.freeze({of: $of});
const $JS = Object.freeze({Array: $JArray});
function $attribute(value, name, location) {
if (value === $JS && name === 'Array') return $JArray;
if (value === $JArray && name === 'of') return $of;
if (value instanceof $JSArray && name === 'length') return value.values.length;
$fail('TypeError', 'Attribute is outside the approved JavaScript registry.', location);
}
'''),
'input': (('call', 'str', 'tuple'), r'''
function $input(value, location, depth = 0, seen = new Set()) {
if (depth > 64) $fail('TypeError', 'Input nesting exceeds 64.', location);
if (value === null || ['boolean', 'bigint'].includes(typeof value) || value instanceof $Str || value instanceof $Tuple) return value;
if (typeof value === 'string') return $str(Array.from(value, character => character.codePointAt(0)));
if (typeof value !== 'object' || seen.has(value)) $fail('TypeError', 'Input requires an adopted Python value.', location);
if (value.type === 'str' && Array.isArray(value.codepoints) && value.codepoints.length <= 1048576 && Array.from(value.codepoints).every(point => Number.isInteger(point) && point >= 0 && point <= 0x10ffff)) return $str([...value.codepoints]);
if (value.type === 'tuple' && Array.isArray(value.items)) {
seen.add(value); const items = Array.from(value.items, item => $input(item, location, depth + 1, seen)); seen.delete(value); return $tuple(items);
}
$fail('TypeError', 'Use BigInt, string, bool, null or explicit str/tuple input; JavaScript Number/Array is not Python int/tuple.', location);
}
function $expose(value, location) {
if (!(value instanceof $Callable)) return value;
if (!$exposed.has(value)) $exposed.set(value, (...args) => $call(value, args.map(argument => $input(argument, location)), location));
return $exposed.get(value);
}
const $exposed = new WeakMap();
'''),
}
HELPER_BINDINGS = {
'error': ('$PyError', '$fail'), 'binding': ('$UNBOUND', '$get'),
'str': ('$str',), 'tuple': ('$tuple',), 'numeric': ('$number',),
'truth': ('$truth',), 'binary': ('$binary',), 'unary': ('$unary',),
'equal': ('$equal', '$order'), 'compare': ('$compare',), 'index': ('$index',),
'iterate': ('$iterate',), 'call': ('$fn', '$call'), 'len': ('$len',),
'types': ('$types', '$isinstance'), 'exception': ('$TypeError',),
'repr': ('$repr',), 'print': ('$print',),
'javascript': ('$JS', '$JArray', '$attribute'), 'input': ('$input', '$expose'),
}
@lru_cache(maxsize=1)
def _nonprintable_ranges():
ranges, start = [], None
for point in range(0x110000):
if not chr(point).isprintable():
if start is None:
start = point
elif start is not None:
ranges.append([start, point - 1])
start = None
if start is not None:
ranges.append([start, 0x10ffff])
return json.dumps(ranges, separators=(',', ':'))
def helper_closure(required):
ordered = []
def visit(name):
if name in ordered:
return
for dependency in HELPERS[name][0]:
visit(dependency)
ordered.append(name)
for name in sorted(required):
visit(name)
return tuple(ordered)
def helper_source(name):
source = HELPERS[name][1].strip()
if name == 'repr':
source = source.replace('__NONPRINTABLE__', _nonprintable_ranges())
return source