pyscript-programming-language 1.12.0__py3-none-any.whl

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 (76) hide show
  1. pyscript/__init__.py +49 -0
  2. pyscript/__init__.pyi +96 -0
  3. pyscript/__main__.py +303 -0
  4. pyscript/core/__init__.py +61 -0
  5. pyscript/core/analyzer.py +531 -0
  6. pyscript/core/bases.py +2 -0
  7. pyscript/core/buffer.py +43 -0
  8. pyscript/core/cache.py +70 -0
  9. pyscript/core/checks.py +42 -0
  10. pyscript/core/constants.py +123 -0
  11. pyscript/core/context.py +63 -0
  12. pyscript/core/editor/__init__.py +15 -0
  13. pyscript/core/editor/bases.py +35 -0
  14. pyscript/core/editor/gui.py +144 -0
  15. pyscript/core/editor/terminal.py +175 -0
  16. pyscript/core/exceptions.py +123 -0
  17. pyscript/core/handlers.py +57 -0
  18. pyscript/core/highlight.py +552 -0
  19. pyscript/core/interpreter.py +1546 -0
  20. pyscript/core/lexer.py +863 -0
  21. pyscript/core/mapping.py +139 -0
  22. pyscript/core/nodes.py +663 -0
  23. pyscript/core/objects.py +213 -0
  24. pyscript/core/parser.py +2456 -0
  25. pyscript/core/position.py +114 -0
  26. pyscript/core/pysbuiltins.py +703 -0
  27. pyscript/core/results.py +186 -0
  28. pyscript/core/runner.py +363 -0
  29. pyscript/core/shell.py +287 -0
  30. pyscript/core/symtab.py +103 -0
  31. pyscript/core/token.py +25 -0
  32. pyscript/core/utils/__init__.py +27 -0
  33. pyscript/core/utils/ansi.py +127 -0
  34. pyscript/core/utils/debug.py +60 -0
  35. pyscript/core/utils/decorators.py +53 -0
  36. pyscript/core/utils/generic.py +46 -0
  37. pyscript/core/utils/jsdict.py +32 -0
  38. pyscript/core/utils/module.py +28 -0
  39. pyscript/core/utils/path.py +29 -0
  40. pyscript/core/utils/similarity.py +22 -0
  41. pyscript/core/utils/string.py +49 -0
  42. pyscript/core/version.py +120 -0
  43. pyscript/lib/__hello__.pys +7 -0
  44. pyscript/lib/ansi.pys +14 -0
  45. pyscript/lib/ast/__init__.pys +36 -0
  46. pyscript/lib/ast/ast_dump.py +433 -0
  47. pyscript/lib/ast/ast_literal_eval.py +80 -0
  48. pyscript/lib/ast/ast_unparse.py +540 -0
  49. pyscript/lib/ast/ast_walk.py +256 -0
  50. pyscript/lib/brainfuck.pys +190 -0
  51. pyscript/lib/dis.pys +7 -0
  52. pyscript/lib/explorer.pys +218 -0
  53. pyscript/lib/fpstimer/__init__.pys +6 -0
  54. pyscript/lib/fpstimer/py_fpstimer.py +54 -0
  55. pyscript/lib/getch.pys +28 -0
  56. pyscript/lib/inspect.pys +25 -0
  57. pyscript/lib/jsdict.pys +5 -0
  58. pyscript/lib/keyword.pys +2 -0
  59. pyscript/lib/opcode.pys +1 -0
  60. pyscript/lib/parser.pys +165 -0
  61. pyscript/lib/site.pys +55 -0
  62. pyscript/lib/symtable.pys +5 -0
  63. pyscript/lib/token.pys +12 -0
  64. pyscript/lib/tokenize/__init__.pys +14 -0
  65. pyscript/lib/tokenize/tok_untokenize.py +64 -0
  66. pyscript/other/.nomedia +0 -0
  67. pyscript/other/PyScript.ico +0 -0
  68. pyscript/other/copyright +2 -0
  69. pyscript/other/credits +2 -0
  70. pyscript/other/license +21 -0
  71. pyscript/site-packages/this.pys +19 -0
  72. pyscript/this.py +8 -0
  73. pyscript_programming_language-1.12.0.dist-info/METADATA +133 -0
  74. pyscript_programming_language-1.12.0.dist-info/RECORD +76 -0
  75. pyscript_programming_language-1.12.0.dist-info/WHEEL +5 -0
  76. pyscript_programming_language-1.12.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,540 @@
1
+ from pyscript.core.checks import is_keyword
2
+ from pyscript.core.constants import TOKENS
3
+ from pyscript.core.mapping import SYMBOLS_TOKEN_MAP
4
+ from pyscript.core.nodes import PysNode, PysStringNode
5
+ from pyscript.core.utils.string import indent as sindent
6
+
7
+ def indent(string):
8
+ return sindent(string, 4)
9
+
10
+ def identifier(name):
11
+ return f'${name}' if is_keyword(name) else name
12
+
13
+ def unparse(ast_obj):
14
+ return get_visitor(ast_obj.__class__)(ast_obj)
15
+
16
+ def visit_NumberNode(node):
17
+ return repr(node.value.value)
18
+
19
+ def visit_StringNode(node):
20
+ return repr(node.value.value)
21
+
22
+ def visit_KeywordNode(node):
23
+ return node.name.value
24
+
25
+ def visit_IdentifierNode(node):
26
+ return identifier(node.name.value)
27
+
28
+ def visit_DictionaryNode(node):
29
+ elements = []
30
+
31
+ if node.class_type is dict:
32
+ for key, value in node.pairs:
33
+ element_string = unparse(key)
34
+ element_string += ': '
35
+ element_string += unparse(value)
36
+ elements.append(element_string)
37
+ else:
38
+ for key, value in node.pairs:
39
+ element_string = (
40
+ key.value.value
41
+ if isinstance(key, PysStringNode) and key.value.value.isindentifier() else
42
+ f'[{unparse(key)}]'
43
+ )
44
+ element_string += ': '
45
+ element_string += unparse(value)
46
+ elements.append(element_string)
47
+
48
+ return '{' + ', '.join(elements) + '}'
49
+
50
+ def visit_SetNode(node):
51
+ return '{' + ', '.join(map(unparse, node.elements)) + '}'
52
+
53
+ def visit_ListNode(node):
54
+ return '[' + ', '.join(map(unparse, node.elements)) + ']'
55
+
56
+ def visit_TupleNode(node):
57
+ string = ', '.join(map(unparse, node.elements))
58
+ return '(' + (string + ',' if len(node.elements) == 1 else string) + ')'
59
+
60
+ def visit_AttributeNode(node):
61
+ return f'{unparse(node.target)}.{identifier(node.attribute.value)}'
62
+
63
+ def visit_SubscriptNode(node):
64
+ string = unparse(node.target)
65
+ string += '['
66
+
67
+ if isinstance(node.slice, slice):
68
+ if node.slice.start:
69
+ string += unparse(node.slice.start)
70
+ string += ':'
71
+ if node.slice.stop:
72
+ string += unparse(node.slice.stop)
73
+ string += ':'
74
+ if node.slice.step:
75
+ string += unparse(node.slice.step)
76
+
77
+ elif isinstance(node.slice, tuple):
78
+ indices = []
79
+
80
+ for index in node.slice:
81
+ index_string = ''
82
+
83
+ if isinstance(index, slice):
84
+ if index.start:
85
+ index_string += unparse(index.start)
86
+ index_string += ':'
87
+ if index.stop:
88
+ index_string += unparse(index.stop)
89
+ index_string += ':'
90
+ if index.step:
91
+ index_string += unparse(index.step)
92
+ else:
93
+ index_string += unparse(index)
94
+
95
+ indices.append(index_string)
96
+
97
+ string += indices[0] + ',' if len(indices) == 1 else ', '.join(indices)
98
+
99
+ else:
100
+ string += unparse(node.slice)
101
+
102
+ string += ']'
103
+
104
+ return string
105
+
106
+ def visit_CallNode(node):
107
+ arguments = []
108
+
109
+ for argument in node.arguments:
110
+ if isinstance(argument, tuple):
111
+ keyword, argument = argument
112
+ arguments.append(f'{identifier(keyword.value)}={unparse(argument)}')
113
+ else:
114
+ arguments.append(unparse(argument))
115
+
116
+ return f'{unparse(node.target)}({", ".join(arguments)})'
117
+
118
+ def visit_ChainOperatorNode(node):
119
+ string = unparse(node.expressions[0])
120
+
121
+ for i, operand in enumerate(node.operations, start=1):
122
+ string += ' '
123
+
124
+ if operand.match(TOKENS['KEYWORD'], 'in'):
125
+ string += 'in'
126
+ elif operand.match(TOKENS['KEYWORD'], 'is'):
127
+ string += 'is'
128
+ else:
129
+ string += SYMBOLS_TOKEN_MAP[operand.type]
130
+
131
+ string += ' '
132
+ string += unparse(node.expressions[i])
133
+
134
+ return f'({string})'
135
+
136
+ def visit_TernaryOperatorNode(node):
137
+ return f'({unparse(node.condition)} ? {unparse(node.valid)} : {unparse(node.invalid)})'
138
+
139
+ def visit_BinaryOperatorNode(node):
140
+ if node.operand.match(TOKENS['KEYWORD'], 'and'):
141
+ operand = 'and'
142
+ elif node.operand.match(TOKENS['KEYWORD'], 'or'):
143
+ operand = 'or'
144
+ else:
145
+ operand = SYMBOLS_TOKEN_MAP[node.operand.type]
146
+
147
+ return f'({unparse(node.left)} {operand} {unparse(node.right)})'
148
+
149
+ def visit_UnaryOperatorNode(node):
150
+ if node.operand.match(TOKENS['KEYWORD'], 'not'):
151
+ operand = 'not '
152
+ elif node.operand.match(TOKENS['KEYWORD'], 'typeof'):
153
+ operand = 'typeof '
154
+ else:
155
+ operand = SYMBOLS_TOKEN_MAP[node.operand.type]
156
+
157
+ return f'({operand}{unparse(node.value)})'
158
+
159
+ def visit_IncrementalNode(node):
160
+ operand = SYMBOLS_TOKEN_MAP[node.operand.type]
161
+ target = unparse(node.target)
162
+ return '(' + (operand + target if node.operand_position == 'left' else target + operand) + ')'
163
+
164
+ def visit_StatementsNode(node):
165
+ return '\n'.join(map(unparse, node.body))
166
+
167
+ def visit_AssignmentNode(node):
168
+ return f'{unparse(node.target)} {SYMBOLS_TOKEN_MAP[node.operand.type]} {unparse(node.value)}'
169
+
170
+ def visit_ImportNode(node):
171
+ string = ''
172
+
173
+ name, as_name = node.name
174
+ name_string = identifier(name.value) if name.type == TOKENS['IDENTIFIER'] else repr(name.value)
175
+
176
+ if as_name:
177
+ name_string += ' as '
178
+ name_string += identifier(as_name.value)
179
+
180
+ if node.packages == 'all':
181
+ string += 'from '
182
+ string += identifier(name_string)
183
+ string += ' import *'
184
+
185
+ elif node.packages:
186
+ packages = []
187
+
188
+ for package, as_package in node.packages:
189
+ package_string = identifier(package.value)
190
+
191
+ if as_package:
192
+ package_string += ' as '
193
+ package_string += identifier(as_package.value)
194
+
195
+ packages.append(package_string)
196
+
197
+ string += 'from '
198
+ string += name_string
199
+ string += ' import '
200
+ string += ', '.join(packages)
201
+
202
+ else:
203
+ string += 'import '
204
+ string += name_string
205
+
206
+ return string
207
+
208
+ def visit_IfNode(node):
209
+ cases = []
210
+
211
+ for i, (condition, body) in enumerate(node.cases_body):
212
+ case_string = 'if' if i == 0 else 'elif'
213
+ case_string += ' ('
214
+ case_string += unparse(condition)
215
+ case_string += ') {\n'
216
+ case_string += indent(unparse(body))
217
+ case_string += '\n}'
218
+
219
+ cases.append(case_string)
220
+
221
+ string = '\n'.join(cases)
222
+
223
+ if node.else_body:
224
+ string += '\nelse {\n'
225
+ string += indent(unparse(node.else_body))
226
+ string += '\n}'
227
+
228
+ return string
229
+
230
+ def visit_SwitchNode(node):
231
+ cases = []
232
+
233
+ for condition, body in node.case_cases:
234
+ case_string = 'case '
235
+ case_string += unparse(condition)
236
+ case_string += ':\n'
237
+ case_string += indent(unparse(body))
238
+
239
+ cases.append(case_string)
240
+
241
+ if node.default_body:
242
+ default_string = 'default:\n'
243
+ default_string += indent(unparse(node.default_body))
244
+
245
+ cases.append(default_string)
246
+
247
+ string = 'switch ('
248
+ string += unparse(node.target)
249
+ string += ') {\n'
250
+ string += '\n'.join(map(indent, cases))
251
+ string += '\n}'
252
+
253
+ return string
254
+
255
+ def visit_MatchNode(node):
256
+ string = 'match '
257
+
258
+ if node.target:
259
+ string += '('
260
+ string += unparse(node.target)
261
+ string += ') '
262
+
263
+ cases = []
264
+
265
+ for condition, value in node.cases:
266
+ case_string = unparse(condition)
267
+ case_string += ': '
268
+ case_string += unparse(value)
269
+
270
+ cases.append(case_string)
271
+
272
+ if node.default:
273
+ default_string = 'default: '
274
+ default_string += unparse(node.default)
275
+
276
+ cases.append(default_string)
277
+
278
+ string += '{\n'
279
+ string += indent(',\n'.join(cases))
280
+ string += '\n}'
281
+
282
+ return string
283
+
284
+ def visit_TryNode(node):
285
+ catch_cases = []
286
+
287
+ for (targets, parameter), body in node.catch_cases:
288
+ name_string = ''
289
+
290
+ if not (not targets and parameter is None):
291
+ name_string += ' ('
292
+
293
+ if targets:
294
+ name_string += ', '.join(identifier(target.name.value) for target in targets)
295
+ name_string += ' '
296
+
297
+ name_string += identifier(parameter.value)
298
+ name_string += ')'
299
+
300
+ catch_string = 'catch'
301
+ catch_string += name_string
302
+ catch_string += ' {\n'
303
+ catch_string += indent(unparse(body))
304
+ catch_string += '\n}'
305
+
306
+ catch_cases.append(catch_string)
307
+
308
+ string = 'try {\n'
309
+ string += indent(unparse(node.body))
310
+ string += '\n}\n'
311
+ string += '\n'.join(catch_cases)
312
+
313
+ if catch_cases:
314
+ string += '\n'
315
+
316
+ if node.else_body:
317
+ string += 'else {\n'
318
+ string += indent(unparse(node.else_body))
319
+ string += '\n}'
320
+
321
+ if node.finally_body:
322
+ string += 'finally {\n'
323
+ string += indent(unparse(node.finally_body))
324
+ string += '\n}'
325
+
326
+ return string
327
+
328
+ def visit_WithNode(node):
329
+ contexts = []
330
+
331
+ for context, alias in node.contexts:
332
+ context_string = unparse(context)
333
+
334
+ if alias:
335
+ context_string += ' as '
336
+ context_string += identifier(alias.value)
337
+
338
+ contexts.append(context_string)
339
+
340
+ string = 'with ('
341
+ string += ', '.join(contexts)
342
+ string += ') {\n'
343
+ string += indent(unparse(node.body))
344
+ string += '\n}'
345
+
346
+ return string
347
+
348
+ def visit_ForNode(node):
349
+ string = 'for ('
350
+
351
+ if len(node.header) == 2:
352
+ declaration, iteration = node.header
353
+
354
+ string += unparse(declaration)
355
+ string += ' of '
356
+ string += unparse(iteration)
357
+
358
+ elif len(node.header) == 3:
359
+ declaration, condition, update = node.header
360
+
361
+ if declaration:
362
+ string += unparse(declaration)
363
+ string += ';'
364
+ if condition:
365
+ string += unparse(condition)
366
+ string += ';'
367
+ if update:
368
+ string += unparse(update)
369
+
370
+ string += ') {\n'
371
+ string += indent(unparse(node.body))
372
+ string += '\n}'
373
+
374
+ if node.else_body:
375
+ string += '\nelse {\n'
376
+ string += indent(unparse(node.else_body))
377
+ string += '\n}'
378
+
379
+ return string
380
+
381
+ def visit_WhileNode(node):
382
+ string = 'while ('
383
+ string += unparse(node.condition)
384
+ string += ') {\n'
385
+ string += indent(unparse(node.body))
386
+ string += '\n}'
387
+
388
+ if node.else_body:
389
+ string += '\nelse {\n'
390
+ string += indent(unparse(node.else_body))
391
+ string += '\n}'
392
+
393
+ return string
394
+
395
+ def visit_DoWhileNode(node):
396
+ string = 'do {\n'
397
+ string += indent(unparse(node.body))
398
+ string += '\n} while ('
399
+ string += unparse(node.condition)
400
+ string += ')'
401
+
402
+ if node.else_body:
403
+ string += 'else {\n'
404
+ string += indent(unparse(node.else_body))
405
+ string += '\n}'
406
+
407
+ return string
408
+
409
+ def visit_RepeatNode(node):
410
+ string = 'repeat {\n'
411
+ string += indent(unparse(node.body))
412
+ string += '\n} until ('
413
+ string += unparse(node.condition)
414
+ string += ')'
415
+
416
+ if node.else_body:
417
+ string += 'else {\n'
418
+ string += indent(unparse(node.else_body))
419
+ string += '\n}'
420
+
421
+ return string
422
+
423
+ def visit_ClassNode(node):
424
+ bases = []
425
+ decorators = []
426
+
427
+ for decorator in node.decorators:
428
+ decorators.append(f'@{unparse(decorator)}')
429
+
430
+ for base in node.bases:
431
+ bases.append(unparse(base))
432
+
433
+ string = ''
434
+
435
+ if decorators:
436
+ string += '\n'.join(decorators)
437
+ string += '\n'
438
+
439
+ string += 'class '
440
+ string += identifier(node.name.value)
441
+
442
+ if bases:
443
+ string += '('
444
+ string += ', '.join(bases)
445
+ string += ')'
446
+
447
+ string += ' {\n'
448
+ string += indent(unparse(node.body))
449
+ string += '\n}'
450
+
451
+ return string
452
+
453
+ def visit_FunctionNode(node):
454
+ decorators = []
455
+ parameters = []
456
+
457
+ for decorator in node.decorators:
458
+ decorators.append(f'@{unparse(decorator)}')
459
+
460
+ for parameter in node.parameters:
461
+ if isinstance(parameter, tuple):
462
+ parameter, value = parameter
463
+ parameters.append(f'{identifier(parameter.value)}={unparse(value)}')
464
+ else:
465
+ parameters.append(parameter.value)
466
+
467
+ string = ''
468
+
469
+ if decorators:
470
+ string += '\n'.join(decorators)
471
+ string += '\n'
472
+
473
+ if node.constructor:
474
+ string += 'constructor'
475
+ else:
476
+ string += 'func'
477
+ if node.name:
478
+ string += ' '
479
+ string += identifier(node.name.value)
480
+
481
+ string += '('
482
+ string += ', '.join(parameters)
483
+ string += ') {\n'
484
+ string += indent(unparse(node.body))
485
+ string += '\n}'
486
+
487
+ return string
488
+
489
+ def visit_GlobalNode(node):
490
+ string = 'global '
491
+ string += ', '.join(identifier(name.value) for name in node.identifiers)
492
+ return string
493
+
494
+ def visit_ReturnNode(node):
495
+ string = 'return'
496
+
497
+ if node.value:
498
+ string += ' '
499
+ string += unparse(node.value)
500
+
501
+ return string
502
+
503
+ def visit_ThrowNode(node):
504
+ string = 'throw '
505
+ string += unparse(node.target)
506
+
507
+ if node.cause:
508
+ string += ' from '
509
+ string += unparse(node.cause)
510
+
511
+ return string
512
+
513
+ def visit_AssertNode(node):
514
+ string = 'assert '
515
+ string += unparse(node.condition)
516
+
517
+ if node.message:
518
+ string += ', '
519
+ string += unparse(node.message)
520
+
521
+ return string
522
+
523
+ def visit_DeleteNode(node):
524
+ string = 'del '
525
+ string += ', '.join(map(unparse, node.targets))
526
+ return string
527
+
528
+ def visit_EllipsisNode(node):
529
+ return '...'
530
+
531
+ def visit_ContinueNode(node):
532
+ return 'continue'
533
+
534
+ def visit_BreakNode(node):
535
+ return 'break'
536
+
537
+ get_visitor = {
538
+ class_node: globals()['visit_' + class_node.__name__.removeprefix('Pys')]
539
+ for class_node in PysNode.__subclasses__()
540
+ }.__getitem__