rapydscript-ns 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/.agignore +1 -0
  2. package/.gitattributes +4 -0
  3. package/.github/workflows/ci.yml +38 -0
  4. package/.github/workflows/web-repl-page-deploy.yml +42 -0
  5. package/=template.pyj +5 -0
  6. package/CHANGELOG.md +456 -0
  7. package/CONTRIBUTORS +13 -0
  8. package/HACKING.md +103 -0
  9. package/LICENSE +24 -0
  10. package/README.md +2512 -0
  11. package/TODO.md +327 -0
  12. package/add-toc-to-readme +2 -0
  13. package/bin/export +75 -0
  14. package/bin/rapydscript +70 -0
  15. package/bin/web-repl-export +102 -0
  16. package/build +3 -0
  17. package/package.json +46 -0
  18. package/publish.py +37 -0
  19. package/release/baselib-plain-pretty.js +4370 -0
  20. package/release/baselib-plain-ugly.js +3 -0
  21. package/release/compiler.js +18394 -0
  22. package/release/signatures.json +31 -0
  23. package/session.vim +4 -0
  24. package/setup.cfg +2 -0
  25. package/src/ast.pyj +1356 -0
  26. package/src/baselib-builtins.pyj +279 -0
  27. package/src/baselib-containers.pyj +723 -0
  28. package/src/baselib-errors.pyj +37 -0
  29. package/src/baselib-internal.pyj +421 -0
  30. package/src/baselib-itertools.pyj +97 -0
  31. package/src/baselib-str.pyj +798 -0
  32. package/src/compiler.pyj +36 -0
  33. package/src/errors.pyj +30 -0
  34. package/src/lib/aes.pyj +646 -0
  35. package/src/lib/collections.pyj +695 -0
  36. package/src/lib/elementmaker.pyj +83 -0
  37. package/src/lib/encodings.pyj +126 -0
  38. package/src/lib/functools.pyj +148 -0
  39. package/src/lib/gettext.pyj +569 -0
  40. package/src/lib/itertools.pyj +580 -0
  41. package/src/lib/math.pyj +193 -0
  42. package/src/lib/numpy.pyj +2101 -0
  43. package/src/lib/operator.pyj +11 -0
  44. package/src/lib/pythonize.pyj +20 -0
  45. package/src/lib/random.pyj +118 -0
  46. package/src/lib/re.pyj +470 -0
  47. package/src/lib/traceback.pyj +63 -0
  48. package/src/lib/uuid.pyj +77 -0
  49. package/src/monaco-language-service/analyzer.js +526 -0
  50. package/src/monaco-language-service/builtins.js +543 -0
  51. package/src/monaco-language-service/completions.js +498 -0
  52. package/src/monaco-language-service/diagnostics.js +643 -0
  53. package/src/monaco-language-service/dts.js +550 -0
  54. package/src/monaco-language-service/hover.js +121 -0
  55. package/src/monaco-language-service/index.js +386 -0
  56. package/src/monaco-language-service/scope.js +162 -0
  57. package/src/monaco-language-service/signature.js +144 -0
  58. package/src/output/__init__.pyj +0 -0
  59. package/src/output/classes.pyj +296 -0
  60. package/src/output/codegen.pyj +492 -0
  61. package/src/output/comments.pyj +45 -0
  62. package/src/output/exceptions.pyj +105 -0
  63. package/src/output/functions.pyj +491 -0
  64. package/src/output/literals.pyj +109 -0
  65. package/src/output/loops.pyj +444 -0
  66. package/src/output/modules.pyj +329 -0
  67. package/src/output/operators.pyj +429 -0
  68. package/src/output/statements.pyj +463 -0
  69. package/src/output/stream.pyj +309 -0
  70. package/src/output/treeshake.pyj +182 -0
  71. package/src/output/utils.pyj +72 -0
  72. package/src/parse.pyj +3106 -0
  73. package/src/string_interpolation.pyj +72 -0
  74. package/src/tokenizer.pyj +702 -0
  75. package/src/unicode_aliases.pyj +576 -0
  76. package/src/utils.pyj +192 -0
  77. package/test/_import_one.pyj +37 -0
  78. package/test/_import_two/__init__.pyj +11 -0
  79. package/test/_import_two/level2/__init__.pyj +0 -0
  80. package/test/_import_two/level2/deep.pyj +4 -0
  81. package/test/_import_two/other.pyj +6 -0
  82. package/test/_import_two/sub.pyj +13 -0
  83. package/test/aes_vectors.pyj +421 -0
  84. package/test/annotations.pyj +80 -0
  85. package/test/baselib.pyj +319 -0
  86. package/test/classes.pyj +452 -0
  87. package/test/collections.pyj +152 -0
  88. package/test/decorators.pyj +77 -0
  89. package/test/dict_spread.pyj +76 -0
  90. package/test/docstrings.pyj +39 -0
  91. package/test/elementmaker_test.pyj +45 -0
  92. package/test/ellipsis.pyj +49 -0
  93. package/test/functions.pyj +151 -0
  94. package/test/generators.pyj +41 -0
  95. package/test/generic.pyj +370 -0
  96. package/test/imports.pyj +72 -0
  97. package/test/internationalization.pyj +73 -0
  98. package/test/lint.pyj +164 -0
  99. package/test/loops.pyj +85 -0
  100. package/test/numpy.pyj +734 -0
  101. package/test/omit_function_metadata.pyj +20 -0
  102. package/test/regexp.pyj +55 -0
  103. package/test/repl.pyj +121 -0
  104. package/test/scoped_flags.pyj +76 -0
  105. package/test/starargs.pyj +506 -0
  106. package/test/starred_assign.pyj +104 -0
  107. package/test/str.pyj +198 -0
  108. package/test/subscript_tuple.pyj +53 -0
  109. package/test/unit/fixtures/fibonacci_expected.js +46 -0
  110. package/test/unit/index.js +2989 -0
  111. package/test/unit/language-service-builtins.js +815 -0
  112. package/test/unit/language-service-completions.js +1067 -0
  113. package/test/unit/language-service-dts.js +543 -0
  114. package/test/unit/language-service-hover.js +455 -0
  115. package/test/unit/language-service-scope.js +833 -0
  116. package/test/unit/language-service-signature.js +458 -0
  117. package/test/unit/language-service.js +705 -0
  118. package/test/unit/run-language-service.js +41 -0
  119. package/test/unit/web-repl.js +484 -0
  120. package/tools/build-language-service.js +190 -0
  121. package/tools/cli.js +547 -0
  122. package/tools/compile.js +219 -0
  123. package/tools/compiler.js +108 -0
  124. package/tools/completer.js +131 -0
  125. package/tools/embedded_compiler.js +251 -0
  126. package/tools/export.js +316 -0
  127. package/tools/gettext.js +185 -0
  128. package/tools/ini.js +65 -0
  129. package/tools/lint.js +705 -0
  130. package/tools/msgfmt.js +187 -0
  131. package/tools/repl.js +223 -0
  132. package/tools/self.js +162 -0
  133. package/tools/test.js +118 -0
  134. package/tools/utils.js +128 -0
  135. package/tools/web_repl.js +95 -0
  136. package/try +41 -0
  137. package/web-repl/env.js +74 -0
  138. package/web-repl/index.html +163 -0
  139. package/web-repl/language-service.js +4084 -0
  140. package/web-repl/main.js +254 -0
  141. package/web-repl/prism.css +139 -0
  142. package/web-repl/prism.js +113 -0
  143. package/web-repl/rapydscript.js +435 -0
  144. package/web-repl/sha1.js +25 -0
@@ -0,0 +1,37 @@
1
+ # vim:fileencoding=utf-8
2
+ # License: BSD
3
+ # Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
4
+ # globals: ρσ_str
5
+
6
+ NameError = ReferenceError
7
+
8
+ class Exception(Error):
9
+
10
+ def __init__(self, message):
11
+ self.message = message
12
+ self.stack = Error().stack
13
+ self.name = self.constructor.name
14
+
15
+ def __repr__(self):
16
+ return self.name + ': ' + self.message
17
+
18
+ class AttributeError(Exception):
19
+ pass
20
+
21
+ class IndexError(Exception):
22
+ pass
23
+
24
+ class KeyError(Exception):
25
+ pass
26
+
27
+ class ValueError(Exception):
28
+ pass
29
+
30
+ class UnicodeDecodeError(Exception):
31
+ pass
32
+
33
+ class AssertionError(Exception):
34
+ pass
35
+
36
+ class ZeroDivisionError(Exception):
37
+ pass
@@ -0,0 +1,421 @@
1
+ # vim:fileencoding=utf-8
2
+ # License: BSD
3
+
4
+ # globals: exports, console, ρσ_iterator_symbol, ρσ_kwargs_symbol, ρσ_arraylike, ρσ_list_contains, ρσ_list_constructor, ρσ_str, ρσ_int, ρσ_float
5
+
6
+ def ρσ_eslice(arr, step, start, end):
7
+ if jstype(arr) is 'string' or v'arr instanceof String':
8
+ is_string = True
9
+ arr = arr.split('')
10
+
11
+ if step < 0:
12
+ step = -step
13
+ arr = arr.slice().reverse()
14
+ if jstype(start) is not "undefined": start = arr.length - start - 1
15
+ if jstype(end) is not "undefined": end = arr.length - end - 1
16
+ if jstype(start) is "undefined": start = 0
17
+ if jstype(end) is "undefined": end = arr.length
18
+
19
+ arr = arr.slice(start, end).filter(def(e, i): return i % step is 0;)
20
+ if is_string:
21
+ arr = arr.join('')
22
+ return arr
23
+
24
+ def ρσ_delslice(arr, step, start, end):
25
+ if jstype(arr) is 'string' or v'arr instanceof String':
26
+ is_string = True
27
+ arr = arr.split('')
28
+ if step < 0:
29
+ if jstype(start) is "undefined": start = arr.length
30
+ if jstype(end) is "undefined": end = 0
31
+ start, end, step = end, start, -step
32
+ if jstype(start) is "undefined": start = 0
33
+ if jstype(end) is "undefined": end = arr.length
34
+
35
+ if step is 1:
36
+ arr.splice(start, end - start)
37
+ else:
38
+ if end > start:
39
+ indices = v'[]'
40
+ for v'var i = start; i < end; i += step':
41
+ indices.push(i)
42
+ for v'var i = indices.length - 1; i >= 0; i--':
43
+ arr.splice(indices[i], 1)
44
+
45
+ if is_string:
46
+ arr = arr.join('')
47
+ return arr
48
+
49
+ def ρσ_flatten(arr):
50
+ ans = []
51
+ for v'var i=0; i < arr.length; i++':
52
+ value = arr[i] # noqa:undef
53
+ if Array.isArray(value):
54
+ ans = ans.concat(ρσ_flatten(value))
55
+ else:
56
+ ans.push(value)
57
+ return ans
58
+
59
+ def ρσ_unpack_asarray(num, iterable):
60
+ if ρσ_arraylike(iterable):
61
+ return iterable
62
+ ans = v'[]'
63
+ if jstype(iterable[ρσ_iterator_symbol]) is 'function':
64
+ iterator = iterable.keys() if jstype(Map) is 'function' and v'iterable instanceof Map' else iterable[ρσ_iterator_symbol]()
65
+ result = iterator.next()
66
+ while not result.done and ans.length < num:
67
+ ans.push(result.value)
68
+ result = iterator.next()
69
+ return ans
70
+
71
+ def ρσ_unpack_starred_asarray(iterable):
72
+ if jstype(iterable) is 'string' or v'iterable instanceof String':
73
+ return iterable.split('')
74
+ if ρσ_arraylike(iterable):
75
+ return iterable
76
+ ans = v'[]'
77
+ if jstype(iterable[ρσ_iterator_symbol]) is 'function':
78
+ iterator = iterable.keys() if jstype(Map) is 'function' and v'iterable instanceof Map' else iterable[ρσ_iterator_symbol]()
79
+ result = iterator.next()
80
+ while not result.done:
81
+ ans.push(result.value)
82
+ result = iterator.next()
83
+ return ans
84
+
85
+ def ρσ_extends(child, parent):
86
+ child.prototype = Object.create(parent.prototype)
87
+ child.prototype.constructor = child
88
+ Object.setPrototypeOf(child, parent)
89
+
90
+ ρσ_in = (def ():
91
+ if jstype(Map) is 'function' and jstype(Set) is 'function':
92
+ return def(val, arr):
93
+ if jstype(arr) is 'string':
94
+ return arr.indexOf(val) is not -1
95
+ if jstype(arr.__contains__) is 'function':
96
+ return arr.__contains__(val)
97
+ if v'arr instanceof Map || arr instanceof Set':
98
+ return arr.has(val)
99
+ if ρσ_arraylike(arr):
100
+ return ρσ_list_contains.call(arr, val)
101
+ return Object.prototype.hasOwnProperty.call(arr, val)
102
+ return def(val, arr):
103
+ if jstype(arr) is 'string':
104
+ return arr.indexOf(val) is not -1
105
+ if jstype(arr.__contains__) is 'function':
106
+ return arr.__contains__(val)
107
+ if ρσ_arraylike(arr):
108
+ return ρσ_list_contains.call(arr, val)
109
+ return Object.prototype.hasOwnProperty.call(arr, val)
110
+ )()
111
+
112
+ def ρσ_Iterable(iterable):
113
+ # Once ES6 is mature, change AST_ForIn to use the iterator protocol and get
114
+ # rid of this function entirely
115
+ if ρσ_arraylike(iterable):
116
+ return iterable
117
+ if jstype(iterable[ρσ_iterator_symbol]) is 'function':
118
+ iterator = iterable.keys() if jstype(Map) is 'function' and v'iterable instanceof Map' else iterable[ρσ_iterator_symbol]()
119
+ ans = []
120
+ result = iterator.next()
121
+ while not result.done:
122
+ ans.push(result.value)
123
+ result = iterator.next()
124
+ return ans
125
+ # so we can use 'for ... in' syntax with objects, as we would with dicts in python
126
+ return Object.keys(iterable)
127
+
128
+ ρσ_desugar_kwargs = (def ():
129
+ if jstype(Object.assign) is 'function':
130
+ return def():
131
+ ans = Object.create(None)
132
+ ans[ρσ_kwargs_symbol] = True
133
+ for v'var i = 0; i < arguments.length; i++':
134
+ Object.assign(ans, arguments[i])
135
+ return ans
136
+ return def():
137
+ ans = Object.create(None)
138
+ ans[ρσ_kwargs_symbol] = True
139
+ for v'var i = 0; i < arguments.length; i++':
140
+ keys = Object.keys(arguments[i])
141
+ for v'var j = 0; j < keys.length; j++':
142
+ ans[keys[j]] = arguments[i][keys[j]]
143
+ return ans
144
+ )()
145
+
146
+ def ρσ_interpolate_kwargs(f, supplied_args):
147
+ if not f.__argnames__:
148
+ return f.apply(this, supplied_args)
149
+ has_prop = Object.prototype.hasOwnProperty
150
+ kwobj = supplied_args.pop()
151
+ if f.__handles_kwarg_interpolation__:
152
+ args = Array(Math.max(supplied_args.length, f.__argnames__.length) + 1)
153
+ args[-1] = kwobj
154
+ for v'var i = 0; i < args.length - 1; i++':
155
+ if i < f.__argnames__.length:
156
+ prop = f.__argnames__[i]
157
+ if has_prop.call(kwobj, prop):
158
+ args[i] = kwobj[prop]
159
+ v'delete kwobj[prop]'
160
+ elif i < supplied_args.length:
161
+ args[i] = supplied_args[i]
162
+ else:
163
+ args[i] = supplied_args[i]
164
+ return f.apply(this, args)
165
+
166
+ for v'var i = 0; i < f.__argnames__.length; i++':
167
+ prop = f.__argnames__[i]
168
+ if has_prop.call(kwobj, prop):
169
+ supplied_args[i] = kwobj[prop]
170
+ return f.apply(this, supplied_args)
171
+
172
+ def ρσ_interpolate_kwargs_constructor(apply, f, supplied_args):
173
+ if apply:
174
+ f.apply(this, supplied_args)
175
+ else:
176
+ ρσ_interpolate_kwargs.call(this, f, supplied_args)
177
+ return this
178
+
179
+ def ρσ_getitem(obj, key):
180
+ if obj.__getitem__:
181
+ return obj.__getitem__(key)
182
+ if jstype(key) is 'number' and key < 0:
183
+ key += obj.length
184
+ return obj[key]
185
+
186
+ def ρσ_setitem(obj, key, val):
187
+ if obj.__setitem__:
188
+ obj.__setitem__(key, val)
189
+ else:
190
+ if jstype(key) is 'number' and key < 0:
191
+ key += obj.length
192
+ obj[key] = val
193
+ return val
194
+
195
+ def ρσ_delitem(obj, key):
196
+ if obj.__delitem__:
197
+ obj.__delitem__(key)
198
+ elif jstype(obj.splice) is 'function':
199
+ obj.splice(key, 1)
200
+ else:
201
+ if jstype(key) is 'number' and key < 0:
202
+ key += obj.length
203
+ v'delete obj[key]'
204
+
205
+ def ρσ_bound_index(idx, arr):
206
+ if jstype(idx) is 'number' and idx < 0:
207
+ idx += arr.length
208
+ return idx
209
+
210
+ def ρσ_splice(arr, val, start, end):
211
+ start = start or 0
212
+ if start < 0:
213
+ start += arr.length
214
+ if end is undefined:
215
+ end = arr.length
216
+ if end < 0:
217
+ end += arr.length
218
+ Array.prototype.splice.apply(arr, v'[start, end - start].concat(val)')
219
+
220
+ ρσ_exists = {
221
+ 'n': def(expr):
222
+ return expr is not undefined and expr is not None
223
+ ,'d': def(expr):
224
+ if expr is undefined or expr is None:
225
+ return Object.create(None)
226
+ return expr
227
+ ,'c': def(expr):
228
+ if jstype(expr) is 'function':
229
+ return expr
230
+ return def():
231
+ return undefined
232
+ ,'g': def(expr):
233
+ if expr is undefined or expr is None or jstype(expr.__getitem__) is not 'function':
234
+ return {'__getitem__': def(): return undefined;}
235
+ ,'e': def(expr, alt):
236
+ return alt if expr is undefined or expr is None else expr
237
+ }
238
+
239
+ def ρσ_mixin():
240
+ # Implement a depth-first left-to-right method resolution order This is not
241
+ # the same as python's MRO, but I really dont feel like implementing the C3
242
+ # linearization right now, if that is even possible with prototypical
243
+ # inheritance.
244
+ seen = Object.create(None)
245
+ # Ensure the following special properties are never copied
246
+ seen.__argnames__ = seen.__handles_kwarg_interpolation__ = seen.__init__ = seen.__annotations__ = seen.__doc__ = seen.__bind_methods__ = seen.__bases__ = seen.constructor = seen.__class__ = True
247
+ resolved_props = {}
248
+ p = target = arguments[0].prototype
249
+ while p and p is not Object.prototype:
250
+ props = Object.getOwnPropertyNames(p)
251
+ for v'var i = 0; i < props.length; i++':
252
+ seen[props[i]] = True
253
+ p = Object.getPrototypeOf(p)
254
+ for v'var c = 1; c < arguments.length; c++':
255
+ p = arguments[c].prototype
256
+ while p and p is not Object.prototype:
257
+ props = Object.getOwnPropertyNames(p)
258
+ for v'var i = 0; i < props.length; i++':
259
+ name = props[i]
260
+ if seen[name]:
261
+ continue
262
+ seen[name] = True
263
+ resolved_props[name] = Object.getOwnPropertyDescriptor(p, name)
264
+ p = Object.getPrototypeOf(p)
265
+ Object.defineProperties(target, resolved_props)
266
+
267
+ # Arithmetic/bitwise operator overloading helpers.
268
+ # Each checks for the forward dunder method, then the reflected one, then
269
+ # falls back to the native JS operation (with special handling for // and **).
270
+ def ρσ_op_add(a, b):
271
+ if a is not None and jstype(a.__add__) is 'function': return a.__add__(b)
272
+ if b is not None and jstype(b.__radd__) is 'function': return b.__radd__(a)
273
+ return a + b
274
+
275
+ def ρσ_op_sub(a, b):
276
+ if a is not None and jstype(a.__sub__) is 'function': return a.__sub__(b)
277
+ if b is not None and jstype(b.__rsub__) is 'function': return b.__rsub__(a)
278
+ return a - b
279
+
280
+ def ρσ_op_mul(a, b):
281
+ if a is not None and jstype(a.__mul__) is 'function': return a.__mul__(b)
282
+ if b is not None and jstype(b.__rmul__) is 'function': return b.__rmul__(a)
283
+ if (jstype(a) is 'string' or v'a instanceof String') and jstype(b) is 'number': return a.repeat(b)
284
+ if (jstype(b) is 'string' or v'b instanceof String') and jstype(a) is 'number': return b.repeat(a)
285
+ return a * b
286
+
287
+ def ρσ_op_truediv(a, b):
288
+ if a is not None and jstype(a.__truediv__) is 'function': return a.__truediv__(b)
289
+ if b is not None and jstype(b.__rtruediv__) is 'function': return b.__rtruediv__(a)
290
+ return a / b
291
+
292
+ def ρσ_op_floordiv(a, b):
293
+ if a is not None and jstype(a.__floordiv__) is 'function': return a.__floordiv__(b)
294
+ if b is not None and jstype(b.__rfloordiv__) is 'function': return b.__rfloordiv__(a)
295
+ return Math.floor(a / b)
296
+
297
+ def ρσ_op_mod(a, b):
298
+ if a is not None and jstype(a.__mod__) is 'function': return a.__mod__(b)
299
+ if b is not None and jstype(b.__rmod__) is 'function': return b.__rmod__(a)
300
+ return a % b
301
+
302
+ def ρσ_op_pow(a, b):
303
+ if a is not None and jstype(a.__pow__) is 'function': return a.__pow__(b)
304
+ if b is not None and jstype(b.__rpow__) is 'function': return b.__rpow__(a)
305
+ return Math.pow(a, b)
306
+
307
+ def ρσ_op_and(a, b):
308
+ if a is not None and jstype(a.__and__) is 'function': return a.__and__(b)
309
+ if b is not None and jstype(b.__rand__) is 'function': return b.__rand__(a)
310
+ return a & b
311
+
312
+ def ρσ_op_or(a, b):
313
+ if a is not None and jstype(a.__or__) is 'function': return a.__or__(b)
314
+ if b is not None and jstype(b.__ror__) is 'function': return b.__ror__(a)
315
+ return a | b
316
+
317
+ def ρσ_op_xor(a, b):
318
+ if a is not None and jstype(a.__xor__) is 'function': return a.__xor__(b)
319
+ if b is not None and jstype(b.__rxor__) is 'function': return b.__rxor__(a)
320
+ return a ^ b
321
+
322
+ def ρσ_op_lshift(a, b):
323
+ if a is not None and jstype(a.__lshift__) is 'function': return a.__lshift__(b)
324
+ if b is not None and jstype(b.__rlshift__) is 'function': return b.__rlshift__(a)
325
+ return a << b
326
+
327
+ def ρσ_op_rshift(a, b):
328
+ if a is not None and jstype(a.__rshift__) is 'function': return a.__rshift__(b)
329
+ if b is not None and jstype(b.__rrshift__) is 'function': return b.__rrshift__(a)
330
+ return a >> b
331
+
332
+ # Unary operator overloading helpers
333
+ def ρσ_op_neg(a):
334
+ if a is not None and jstype(a.__neg__) is 'function': return a.__neg__()
335
+ return -a
336
+
337
+ def ρσ_op_pos(a):
338
+ if a is not None and jstype(a.__pos__) is 'function': return a.__pos__()
339
+ return +a
340
+
341
+ def ρσ_op_invert(a):
342
+ if a is not None and jstype(a.__invert__) is 'function': return a.__invert__()
343
+ return ~a
344
+
345
+ # Augmented-assignment helpers: try __i<op>__ first, fall back to binary op
346
+ def ρσ_op_iadd(a, b):
347
+ if a is not None and jstype(a.__iadd__) is 'function': return a.__iadd__(b)
348
+ return ρσ_op_add(a, b)
349
+
350
+ def ρσ_op_isub(a, b):
351
+ if a is not None and jstype(a.__isub__) is 'function': return a.__isub__(b)
352
+ return ρσ_op_sub(a, b)
353
+
354
+ def ρσ_op_imul(a, b):
355
+ if a is not None and jstype(a.__imul__) is 'function': return a.__imul__(b)
356
+ return ρσ_op_mul(a, b)
357
+
358
+ def ρσ_op_itruediv(a, b):
359
+ if a is not None and jstype(a.__itruediv__) is 'function': return a.__itruediv__(b)
360
+ return ρσ_op_truediv(a, b)
361
+
362
+ def ρσ_op_ifloordiv(a, b):
363
+ if a is not None and jstype(a.__ifloordiv__) is 'function': return a.__ifloordiv__(b)
364
+ return ρσ_op_floordiv(a, b)
365
+
366
+ def ρσ_op_imod(a, b):
367
+ if a is not None and jstype(a.__imod__) is 'function': return a.__imod__(b)
368
+ return ρσ_op_mod(a, b)
369
+
370
+ def ρσ_op_ipow(a, b):
371
+ if a is not None and jstype(a.__ipow__) is 'function': return a.__ipow__(b)
372
+ return ρσ_op_pow(a, b)
373
+
374
+ def ρσ_op_iand(a, b):
375
+ if a is not None and jstype(a.__iand__) is 'function': return a.__iand__(b)
376
+ return ρσ_op_and(a, b)
377
+
378
+ def ρσ_op_ior(a, b):
379
+ if a is not None and jstype(a.__ior__) is 'function': return a.__ior__(b)
380
+ return ρσ_op_or(a, b)
381
+
382
+ def ρσ_op_ixor(a, b):
383
+ if a is not None and jstype(a.__ixor__) is 'function': return a.__ixor__(b)
384
+ return ρσ_op_xor(a, b)
385
+
386
+ def ρσ_op_ilshift(a, b):
387
+ if a is not None and jstype(a.__ilshift__) is 'function': return a.__ilshift__(b)
388
+ return ρσ_op_lshift(a, b)
389
+
390
+ def ρσ_op_irshift(a, b):
391
+ if a is not None and jstype(a.__irshift__) is 'function': return a.__irshift__(b)
392
+ return ρσ_op_rshift(a, b)
393
+
394
+ def ρσ_instanceof():
395
+ obj = arguments[0]
396
+ bases = ''
397
+ if obj and obj.constructor and obj.constructor.prototype:
398
+ bases = obj.constructor.prototype.__bases__ or ''
399
+ for v'var i = 1; i < arguments.length; i++':
400
+ q = arguments[i]
401
+ if v'obj instanceof q':
402
+ return True
403
+ if (q is Array or q is ρσ_list_constructor) and Array.isArray(obj):
404
+ return True
405
+ if q is ρσ_str and (jstype(obj) is 'string' or v'obj instanceof String'):
406
+ return True
407
+ if q is ρσ_int and (jstype(obj) is 'number' and Number.isInteger(obj)):
408
+ return True
409
+ if q is ρσ_float and (jstype(obj) is 'number' and not Number.isInteger(obj)):
410
+ return True
411
+ if bases.length > 1:
412
+ for v'var c = 1; c < bases.length; c++':
413
+ cls = bases[c]
414
+ while cls:
415
+ if q is cls:
416
+ return True
417
+ p = Object.getPrototypeOf(cls.prototype)
418
+ if not p:
419
+ break
420
+ cls = p.constructor
421
+ return False
@@ -0,0 +1,97 @@
1
+ # vim:fileencoding=utf-8
2
+ # License: BSD
3
+ # Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
4
+
5
+ # globals: ρσ_iterator_symbol, ρσ_bool
6
+
7
+ def sum(iterable, start):
8
+ if Array.isArray(iterable):
9
+ return iterable.reduce(
10
+ def(prev, cur): return prev+cur
11
+ ,
12
+ start or 0
13
+ )
14
+ ans = start or 0
15
+ iterator = iter(iterable)
16
+ r = iterator.next()
17
+ while not r.done:
18
+ ans += r.value
19
+ r = iterator.next()
20
+ return ans
21
+
22
+ def map():
23
+ iterators = new Array(arguments.length - 1)
24
+ func = arguments[0] # noqa: unused-local
25
+ args = new Array(arguments.length - 1) # noqa: unused-local
26
+ for v'var i = 1; i < arguments.length; i++':
27
+ iterators[i - 1] = iter(arguments[i]) # noqa:undef
28
+ ans = v"{'_func':func, '_iterators':iterators, '_args':args}"
29
+ ans[ρσ_iterator_symbol] = def():
30
+ return this
31
+ ans['next'] = def():
32
+ for v'var i = 0; i < this._iterators.length; i++':
33
+ r = this._iterators[i].next()
34
+ if r.done:
35
+ return v"{'done':true}"
36
+ this._args[i] = r.value # noqa:undef
37
+ return v"{'done':false, 'value':this._func.apply(undefined, this._args)}"
38
+ return ans
39
+
40
+ def filter(func_or_none, iterable):
41
+ func = ρσ_bool if func_or_none is None else func_or_none # noqa: unused-local
42
+ ans = v"{'_func':func, '_iterator':ρσ_iter(iterable)}"
43
+ ans[ρσ_iterator_symbol] = def():
44
+ return this
45
+ ans['next'] = def():
46
+ r = this._iterator.next()
47
+ while not r.done:
48
+ if this._func(r.value):
49
+ return r
50
+ r = this._iterator.next()
51
+ return v"{'done':true}"
52
+ return ans
53
+
54
+ def zip():
55
+ iterators = new Array(arguments.length)
56
+ for v'var i = 0; i < arguments.length; i++':
57
+ iterators[i] = iter(arguments[i]) # noqa:undef
58
+ ans = v"{'_iterators':iterators}"
59
+ ans[ρσ_iterator_symbol] = def():
60
+ return this
61
+ ans['next'] = def():
62
+ args = new Array(this._iterators.length)
63
+ for v'var i = 0; i < this._iterators.length; i++':
64
+ r = this._iterators[i].next()
65
+ if r.done:
66
+ return v"{'done':true}"
67
+ args[i] = r.value # noqa:undef
68
+ return v"{'done':false, 'value':args}"
69
+ return ans
70
+
71
+ def any(iterable):
72
+ if Array.isArray(iterable) or jstype(iterable) is 'string':
73
+ for v'var i = 0; i < iterable.length; i++':
74
+ if iterable[i]: # noqa:undef
75
+ return True
76
+ return False
77
+ iterator = iter(iterable)
78
+ r = iterator.next()
79
+ while not r.done:
80
+ if r.value:
81
+ return True
82
+ r = iterator.next()
83
+ return False
84
+
85
+ def all(iterable):
86
+ if Array.isArray(iterable) or jstype(iterable) is 'string':
87
+ for v'var i = 0; i < iterable.length; i++':
88
+ if not iterable[i]: # noqa:undef
89
+ return False
90
+ return True
91
+ iterator = iter(iterable)
92
+ r = iterator.next()
93
+ while not r.done:
94
+ if not r.value:
95
+ return False
96
+ r = iterator.next()
97
+ return True