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,444 @@
1
+ # vim:fileencoding=utf-8
2
+ # License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3
+ # globals: regenerate
4
+ from __python__ import hash_literals
5
+
6
+ from ast import AST_BaseCall, AST_SymbolRef, AST_Array, AST_Unary, AST_Number, has_calls, AST_Seq, AST_ListComprehension, AST_Starred, is_node_type
7
+ from output.stream import OutputStream
8
+
9
+ def unpack_tuple(elems, output, in_statement):
10
+ starred_idx = -1
11
+ for i, elem in enumerate(elems):
12
+ if is_node_type(elem, AST_Starred):
13
+ starred_idx = i
14
+ break
15
+ after_count = elems.length - starred_idx - 1 if starred_idx >= 0 else 0
16
+
17
+ for i, elem in enumerate(elems):
18
+ output.indent()
19
+ is_starred = is_node_type(elem, AST_Starred)
20
+ actual_elem = elem.expression if is_starred else elem
21
+ output.assign(actual_elem)
22
+ output.print("ρσ_unpack")
23
+ if is_starred:
24
+ output.print('.slice(')
25
+ output.print(i)
26
+ if after_count > 0:
27
+ output.print(', ρσ_unpack.length - ')
28
+ output.print(after_count)
29
+ output.print(')')
30
+ elif starred_idx >= 0 and i > starred_idx:
31
+ from_end = i - elems.length
32
+ output.print('[ρσ_unpack.length + ')
33
+ output.print(from_end)
34
+ output.print(']')
35
+ else:
36
+ output.with_square(def():
37
+ output.print(i)
38
+ )
39
+ if not in_statement or i < elems.length - 1:
40
+ output.semicolon()
41
+ output.newline()
42
+
43
+ def print_do_loop(self, output):
44
+ output.print("do")
45
+ output.space()
46
+ self._do_print_body(output)
47
+ output.space()
48
+ output.print("while")
49
+ output.space()
50
+ output.with_parens(def(): self.condition.print(output);)
51
+ output.semicolon()
52
+
53
+ def print_while_loop(self, output):
54
+ output.print("while")
55
+ output.space()
56
+ output.with_parens(def(): self.condition.print(output);)
57
+ output.space()
58
+ self._do_print_body(output)
59
+
60
+ def is_simple_for_in(self):
61
+ # return true if this loop can be simplified into a basic for (i in j) loop
62
+ if is_node_type(self.object, AST_BaseCall)
63
+ and is_node_type(self.object.expression, AST_SymbolRef)
64
+ and self.object.expression.name is "dir" and self.object.args.length is 1:
65
+ return True
66
+ return False
67
+
68
+ def is_simple_for(self):
69
+ # returns true if this loop can be simplified into a basic for(i=n;i<h;i++) loop
70
+ if (is_node_type(self.object, AST_BaseCall) and
71
+ is_node_type(self.object.expression, AST_SymbolRef) and
72
+ self.object.expression.name is "range" and
73
+ not (is_node_type(self.init, AST_Array))):
74
+ a = self.object.args
75
+ l = a.length
76
+ if l < 3 or (
77
+ is_node_type(a[2], AST_Number) or (
78
+ is_node_type(a[2], AST_Unary) and a[2].operator is '-' and is_node_type(a[2].expression, AST_Number)
79
+ )):
80
+ if (l is 1 and not has_calls(a[0])) or (l > 1 and not has_calls(a[1])):
81
+ return True
82
+ return False
83
+
84
+ def print_for_loop_body(output):
85
+ self = this
86
+ output.with_block(def():
87
+ if not (self.simple_for_index or is_simple_for_in(self)):
88
+ # if we're using multiple iterators, unpack them
89
+ output.indent()
90
+ if output.options.js_version is 5:
91
+ itervar = "ρσ_Iter" + output.index_counter + "[ρσ_Index" + output.index_counter + "]"
92
+ else:
93
+ itervar = "ρσ_Index" + output.index_counter
94
+ if is_node_type(self.init, AST_Array):
95
+ flat = self.init.flatten()
96
+ output.assign("ρσ_unpack")
97
+ if flat.length > self.init.elements.length:
98
+ output.print('ρσ_flatten(' + itervar + ')')
99
+ else:
100
+ output.print(itervar)
101
+ output.end_statement()
102
+ unpack_tuple(flat, output)
103
+ else:
104
+ output.assign(self.init)
105
+ output.print(itervar)
106
+ output.end_statement()
107
+
108
+ output.index_counter += 1
109
+ if self.simple_for_index:
110
+ output.indent()
111
+ output.assign(self.init)
112
+ output.print(self.simple_for_index)
113
+ output.end_statement()
114
+
115
+ for stmt in self.body.body:
116
+ output.indent()
117
+ stmt.print(output)
118
+ output.newline()
119
+ )
120
+
121
+ def init_es6_itervar(output, itervar):
122
+ output.indent()
123
+ output.spaced(itervar, '=', '((typeof', itervar + '[Symbol.iterator]', '===', '"function")', '?',
124
+ '('+itervar, 'instanceof', 'Map', '?', itervar + '.keys()', ':', itervar+')', ':', 'Object.keys(' + itervar + '))')
125
+ output.end_statement()
126
+
127
+ def print_for_in(self, output):
128
+ def write_object():
129
+ if self.object.constructor is AST_Seq:
130
+ (new AST_Array({'elements':self.object.to_array()})).print(output)
131
+ else:
132
+ self.object.print(output)
133
+
134
+ if is_simple_for(self):
135
+ # optimize range() into a simple for loop
136
+ increment = None
137
+ args = self.object.args
138
+ tmp_ = args.length
139
+ if tmp_ is 1:
140
+ start = 0
141
+ end = args[0]
142
+ elif tmp_ is 2:
143
+ start = args[0]
144
+ end = args[1]
145
+ elif tmp_ is 3:
146
+ start = args[0]
147
+ end = args[1]
148
+ increment = args[2]
149
+
150
+ self.simple_for_index = idx = 'ρσ_Index' + output.index_counter
151
+ output.index_counter += 1
152
+ output.print("for")
153
+ output.space()
154
+ output.with_parens(def():
155
+ output.spaced('var', idx, '='), output.space()
156
+ start.print(output) if start.print else output.print(start)
157
+ output.semicolon()
158
+ output.space()
159
+ output.print(idx)
160
+ output.space()
161
+ output.print(">") if is_node_type(increment, AST_Unary) else output.print("<")
162
+ output.space()
163
+ end.print(output)
164
+ output.semicolon()
165
+ output.space()
166
+ output.print(idx)
167
+ if increment and (not (is_node_type(increment, AST_Unary)) or increment.expression.value is not "1"):
168
+ if is_node_type(increment, AST_Unary):
169
+ output.print("-=")
170
+ increment.expression.print(output)
171
+ else:
172
+ output.print("+=")
173
+ increment.print(output)
174
+ else:
175
+ if is_node_type(increment, AST_Unary):
176
+ output.print("--")
177
+ else:
178
+ output.print("++")
179
+ )
180
+ elif is_simple_for_in(self):
181
+ # optimize dir() into a simple for in loop
182
+ output.print("for")
183
+ output.space()
184
+ output.with_parens(def():
185
+ self.init.print(output)
186
+ output.space()
187
+ output.print('in')
188
+ output.space()
189
+ self.object.args[0].print(output)
190
+ )
191
+ else:
192
+ # regular loop
193
+ if output.options.js_version is 5:
194
+ output.assign("var ρσ_Iter" + output.index_counter)
195
+ output.print("ρσ_Iterable")
196
+ output.with_parens(write_object)
197
+ output.semicolon()
198
+ output.newline()
199
+ output.indent()
200
+ output.print("for")
201
+ output.space()
202
+ output.with_parens(def():
203
+ output.print("var")
204
+ output.space()
205
+ output.assign("ρσ_Index" + output.index_counter)
206
+ output.print("0")
207
+ output.semicolon()
208
+ output.space()
209
+ output.print("ρσ_Index" + output.index_counter)
210
+ output.space()
211
+ output.print("<")
212
+ output.space()
213
+ output.print("ρσ_Iter" + output.index_counter + ".length")
214
+ output.semicolon()
215
+ output.space()
216
+ output.print("ρσ_Index" + output.index_counter + "++")
217
+ )
218
+ else:
219
+ itervar = "ρσ_Iter" + output.index_counter
220
+ output.assign("var " + itervar)
221
+ write_object()
222
+ output.end_statement()
223
+ init_es6_itervar(output, itervar)
224
+ output.indent()
225
+ output.spaced('for', '(var', 'ρσ_Index' + output.index_counter, 'of', itervar + ')')
226
+
227
+ output.space()
228
+ self._do_print_body(output)
229
+
230
+ def print_list_comprehension(self, output):
231
+ tname = self.constructor.name.slice(4)
232
+ result_obj = {'ListComprehension':'[]', 'DictComprehension':('Object.create(null)' if self.is_jshash else '{}'), 'SetComprehension':'ρσ_set()'}[tname]
233
+ is_generator = tname is 'GeneratorComprehension'
234
+ es5 = output.options.js_version is 5
235
+ if tname is 'DictComprehension':
236
+ if self.is_pydict:
237
+ result_obj = 'ρσ_dict()'
238
+ add_to_result = def(output):
239
+ output.indent()
240
+ output.print('ρσ_Result.set')
241
+ output.with_parens(def():
242
+ self.statement.print(output)
243
+ output.space(), output.print(','), output.space()
244
+ output.with_parens(def():
245
+ if self.value_statement.constructor is AST_Seq:
246
+ output.with_square(def():self.value_statement.print(output);)
247
+ else:
248
+ self.value_statement.print(output)
249
+ )
250
+ )
251
+ output.end_statement()
252
+ else:
253
+ add_to_result = def(output):
254
+ output.indent()
255
+ output.print('ρσ_Result')
256
+ output.with_square(def():
257
+ self.statement.print(output)
258
+ )
259
+ output.space(), output.print('='), output.space()
260
+ output.with_parens(def():
261
+ if self.value_statement.constructor is AST_Seq:
262
+ output.with_square(def():self.value_statement.print(output);)
263
+ else:
264
+ self.value_statement.print(output)
265
+ )
266
+ output.end_statement()
267
+ else:
268
+ push_func = "ρσ_Result." + ('push' if self.constructor is AST_ListComprehension else 'add')
269
+ if is_generator:
270
+ push_func = 'yield '
271
+ add_to_result = def(output):
272
+ output.indent()
273
+ output.print(push_func)
274
+ output.with_parens(def():
275
+ if self.statement.constructor is AST_Seq:
276
+ output.with_square(def():self.statement.print(output);)
277
+ else:
278
+ self.statement.print(output)
279
+ )
280
+ output.end_statement()
281
+
282
+ # Collect additional for-clauses (nested comprehension support)
283
+ clauses = self.clauses if self.clauses else []
284
+
285
+ # Helper: emit the for-loop for one clause level, recursing into inner clauses.
286
+ # depth 0 = outermost for-clause (uses ρσ_Iter / ρσ_Index),
287
+ # depth N = inner clause N (uses ρσ_IterN / ρσ_IndexN).
288
+ def emit_loop(body_out, depth):
289
+ if depth == 0:
290
+ iv = 'ρσ_Iter'
291
+ idx = 'ρσ_Index'
292
+ init_node = self.init
293
+ cond_node = self.condition
294
+ else:
295
+ iv = 'ρσ_Iter' + str(depth)
296
+ idx = 'ρσ_Index' + str(depth)
297
+ clause = clauses[depth - 1]
298
+ init_node = clause.init
299
+ cond_node = clause.condition
300
+ # Declare and initialise the inner iterable inside the outer loop body
301
+ body_out.indent()
302
+ body_out.assign("var " + iv)
303
+ if es5:
304
+ body_out.print("ρσ_Iterable")
305
+ body_out.with_parens(def(): clause.object.print(body_out);)
306
+ else:
307
+ clause.object.print(body_out)
308
+ body_out.end_statement()
309
+ if not es5:
310
+ init_es6_itervar(body_out, iv)
311
+ body_out.indent()
312
+ body_out.print("for")
313
+ body_out.space()
314
+ body_out.with_parens(def():
315
+ if es5:
316
+ body_out.print("var")
317
+ body_out.space()
318
+ body_out.assign(idx)
319
+ body_out.print("0")
320
+ body_out.semicolon()
321
+ body_out.space()
322
+ body_out.print(idx)
323
+ body_out.space()
324
+ body_out.print("<")
325
+ body_out.space()
326
+ body_out.print(iv + ".length")
327
+ body_out.semicolon()
328
+ body_out.space()
329
+ body_out.print(idx + "++")
330
+ else:
331
+ body_out.spaced('var', idx, 'of', iv)
332
+ )
333
+ body_out.space()
334
+ body_out.with_block(def():
335
+ body_out.indent()
336
+ itervar_expr = iv + '[' + idx + ']' if es5 else idx
337
+ if is_node_type(init_node, AST_Array):
338
+ flat = init_node.flatten()
339
+ body_out.assign("ρσ_unpack")
340
+ if flat.length > init_node.elements.length:
341
+ body_out.print('ρσ_flatten(' + itervar_expr + ')')
342
+ else:
343
+ body_out.print(itervar_expr)
344
+ body_out.end_statement()
345
+ unpack_tuple(flat, body_out)
346
+ else:
347
+ body_out.assign(init_node)
348
+ body_out.print(itervar_expr)
349
+ body_out.end_statement()
350
+ is_last = (depth == clauses.length)
351
+ if cond_node:
352
+ body_out.indent()
353
+ body_out.print("if")
354
+ body_out.space()
355
+ body_out.with_parens(def(): cond_node.print(body_out);)
356
+ body_out.space()
357
+ if is_last:
358
+ body_out.with_block(def(): add_to_result(body_out);)
359
+ else:
360
+ body_out.with_block(def(): emit_loop(body_out, depth + 1);)
361
+ body_out.newline()
362
+ else:
363
+ if is_last:
364
+ add_to_result(body_out)
365
+ else:
366
+ emit_loop(body_out, depth + 1)
367
+ )
368
+ body_out.newline()
369
+
370
+ output.with_parens(def():
371
+ output.print("function")
372
+ output.print("()")
373
+ output.space()
374
+ output.with_block(def():
375
+ body_out = output
376
+ if is_generator:
377
+ if es5:
378
+ body_out = OutputStream({'beautify':True})
379
+ body_out.indent()
380
+ body_out.print('function* js_generator()'), body_out.space(), body_out.print('{')
381
+ body_out.newline()
382
+ previous_indentation = output.indentation()
383
+ output.set_indentation(output.next_indent())
384
+ body_out.indent()
385
+ body_out.assign("var ρσ_Iter")
386
+ if es5:
387
+ body_out.print("ρσ_Iterable")
388
+ body_out.with_parens(def():
389
+ self.object.print(body_out)
390
+ )
391
+ else:
392
+ self.object.print(body_out)
393
+
394
+ if result_obj:
395
+ body_out.comma()
396
+ body_out.assign("ρσ_Result")
397
+ body_out.print(result_obj)
398
+ # Locally scope all loop variables (outer and inner clauses)
399
+ def decl_loop_vars(init_node):
400
+ if is_node_type(init_node, AST_Array):
401
+ for i in init_node.elements:
402
+ body_out.comma()
403
+ i.print(body_out)
404
+ else:
405
+ body_out.comma()
406
+ init_node.print(body_out)
407
+ decl_loop_vars(self.init)
408
+ for clause in clauses:
409
+ decl_loop_vars(clause.init)
410
+ body_out.end_statement()
411
+
412
+ if not es5:
413
+ init_es6_itervar(body_out, 'ρσ_Iter')
414
+ emit_loop(body_out, 0)
415
+ if self.constructor is AST_ListComprehension:
416
+ body_out.indent()
417
+ body_out.spaced('ρσ_Result', '=', 'ρσ_list_constructor(ρσ_Result)')
418
+ body_out.end_statement()
419
+ if not is_generator:
420
+ body_out.indent()
421
+ body_out.print("return ρσ_Result")
422
+ body_out.end_statement()
423
+ if is_generator:
424
+ output.set_indentation(previous_indentation)
425
+ body_out.newline(), body_out.indent(), body_out.print('}') # end js_generator
426
+ if es5:
427
+ transpiled = regenerate(body_out.get(), output.options.beautify).replace(/regeneratorRuntime.(wrap|mark)/g, 'ρσ_regenerator.regeneratorRuntime.$1')
428
+ if output.options.beautify:
429
+ ci = output.make_indent(0)
430
+ transpiled = [ci + x for x in transpiled.split('\n')].join('\n')
431
+ output.print(transpiled)
432
+ output.newline(), output.indent()
433
+ output.spaced('var', 'result', '=', 'js_generator.call(this)')
434
+ output.end_statement()
435
+ # Python's generator objects use a separate method to send data to the generator
436
+ output.indent()
437
+ output.spaced('result.send', '=', 'result.next')
438
+ output.end_statement()
439
+ output.indent()
440
+ output.spaced('return', 'result')
441
+ output.end_statement()
442
+ )
443
+ )
444
+ output.print("()")