rapydscript-ns 0.8.4 → 0.9.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.
- package/.agignore +1 -1
- package/.github/workflows/ci.yml +38 -38
- package/=template.pyj +5 -5
- package/CHANGELOG.md +18 -0
- package/HACKING.md +103 -103
- package/LICENSE +24 -24
- package/README.md +715 -169
- package/TODO.md +9 -2
- package/add-toc-to-readme +2 -2
- package/bin/export +75 -75
- package/bin/rapydscript +70 -70
- package/bin/web-repl-export +102 -102
- package/build +2 -2
- package/language-service/index.js +36 -27
- package/package.json +1 -1
- package/publish.py +37 -37
- package/release/baselib-plain-pretty.js +2358 -168
- package/release/baselib-plain-ugly.js +73 -3
- package/release/compiler.js +6282 -3092
- package/release/signatures.json +31 -30
- package/session.vim +4 -4
- package/setup.cfg +2 -2
- package/src/ast.pyj +1 -0
- package/src/baselib-builtins.pyj +340 -2
- package/src/baselib-bytes.pyj +664 -0
- package/src/baselib-errors.pyj +1 -1
- package/src/baselib-internal.pyj +267 -60
- package/src/baselib-itertools.pyj +110 -97
- package/src/baselib-str.pyj +22 -4
- package/src/compiler.pyj +36 -36
- package/src/errors.pyj +30 -30
- package/src/lib/abc.pyj +317 -0
- package/src/lib/aes.pyj +646 -646
- package/src/lib/copy.pyj +120 -120
- package/src/lib/dataclasses.pyj +532 -0
- package/src/lib/elementmaker.pyj +83 -83
- package/src/lib/encodings.pyj +126 -126
- package/src/lib/enum.pyj +125 -0
- package/src/lib/gettext.pyj +569 -569
- package/src/lib/itertools.pyj +580 -580
- package/src/lib/math.pyj +193 -193
- package/src/lib/operator.pyj +11 -11
- package/src/lib/pythonize.pyj +20 -20
- package/src/lib/random.pyj +118 -118
- package/src/lib/re.pyj +504 -470
- package/src/lib/react.pyj +74 -74
- package/src/lib/traceback.pyj +63 -63
- package/src/lib/typing.pyj +577 -0
- package/src/lib/uuid.pyj +77 -77
- package/src/monaco-language-service/builtins.js +14 -4
- package/src/monaco-language-service/diagnostics.js +19 -20
- package/src/monaco-language-service/dts.js +550 -550
- package/src/output/classes.pyj +62 -26
- package/src/output/comments.pyj +45 -45
- package/src/output/exceptions.pyj +201 -201
- package/src/output/functions.pyj +78 -5
- package/src/output/jsx.pyj +164 -164
- package/src/output/loops.pyj +5 -2
- package/src/output/operators.pyj +100 -34
- package/src/output/treeshake.pyj +182 -182
- package/src/output/utils.pyj +72 -72
- package/src/parse.pyj +80 -16
- package/src/string_interpolation.pyj +72 -72
- package/src/tokenizer.pyj +9 -4
- package/src/unicode_aliases.pyj +576 -576
- package/src/utils.pyj +192 -192
- package/test/_import_one.pyj +37 -37
- package/test/_import_two/__init__.pyj +11 -11
- package/test/_import_two/level2/deep.pyj +4 -4
- package/test/_import_two/other.pyj +6 -6
- package/test/_import_two/sub.pyj +13 -13
- package/test/abc.pyj +291 -0
- package/test/aes_vectors.pyj +421 -421
- package/test/annotations.pyj +80 -80
- package/test/arithmetic_nostrict.pyj +88 -0
- package/test/arithmetic_types.pyj +169 -0
- package/test/baselib.pyj +91 -0
- package/test/bytes.pyj +467 -0
- package/test/classes.pyj +1 -0
- package/test/comparison_ops.pyj +173 -0
- package/test/dataclasses.pyj +253 -0
- package/test/decorators.pyj +77 -77
- package/test/docstrings.pyj +39 -39
- package/test/elementmaker_test.pyj +45 -45
- package/test/enum.pyj +134 -0
- package/test/eval_exec.pyj +56 -0
- package/test/format.pyj +148 -0
- package/test/functions.pyj +151 -151
- package/test/generators.pyj +41 -41
- package/test/generic.pyj +370 -370
- package/test/imports.pyj +72 -72
- package/test/internationalization.pyj +73 -73
- package/test/lint.pyj +164 -164
- package/test/loops.pyj +85 -85
- package/test/numpy.pyj +734 -734
- package/test/object.pyj +64 -0
- package/test/omit_function_metadata.pyj +20 -20
- package/test/python_compat.pyj +17 -15
- package/test/python_features.pyj +70 -15
- package/test/regexp.pyj +83 -55
- package/test/repl.pyj +121 -121
- package/test/scoped_flags.pyj +76 -76
- package/test/tuples.pyj +96 -0
- package/test/typing.pyj +469 -0
- package/test/unit/index.js +116 -7
- package/test/unit/language-service-dts.js +543 -543
- package/test/unit/language-service-hover.js +455 -455
- package/test/unit/language-service.js +84 -0
- package/test/unit/web-repl.js +804 -1
- package/test/vars_locals_globals.pyj +94 -0
- package/tools/cli.js +558 -547
- package/tools/compile.js +224 -219
- package/tools/completer.js +131 -131
- package/tools/embedded_compiler.js +262 -251
- package/tools/gettext.js +185 -185
- package/tools/ini.js +65 -65
- package/tools/lint.js +16 -19
- package/tools/msgfmt.js +187 -187
- package/tools/repl.js +223 -223
- package/tools/test.js +118 -118
- package/tools/utils.js +128 -128
- package/tools/web_repl.js +95 -95
- package/try +41 -41
- package/web-repl/env.js +196 -196
- package/web-repl/index.html +163 -163
- package/web-repl/main.js +252 -252
- package/web-repl/prism.css +139 -139
- package/web-repl/prism.js +113 -113
- package/web-repl/rapydscript.js +224 -224
- package/web-repl/sha1.js +25 -25
- package/PYTHON_DIFFERENCES_REPORT.md +0 -291
- package/PYTHON_FEATURE_COVERAGE.md +0 -200
package/release/signatures.json
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
1
|
{
|
|
2
|
-
"ast": "
|
|
3
|
-
"baselib-builtins": "
|
|
2
|
+
"ast": "88027aa403dfe55fcf8d475115897d141717d17c",
|
|
3
|
+
"baselib-builtins": "284e14536cfdd0b497abeb6f110ae49a20f2a172",
|
|
4
|
+
"baselib-bytes": "057ab581c276ef03ce91fecdd4213e412198251c",
|
|
4
5
|
"baselib-containers": "d2f534e6530dfb309b074bf7fac082c65779e778",
|
|
5
|
-
"baselib-errors": "
|
|
6
|
-
"baselib-internal": "
|
|
7
|
-
"baselib-itertools": "
|
|
8
|
-
"baselib-str": "
|
|
9
|
-
"compiler": "
|
|
10
|
-
"errors": "
|
|
11
|
-
"output
|
|
12
|
-
"output
|
|
13
|
-
"output
|
|
14
|
-
"output
|
|
15
|
-
"output
|
|
16
|
-
"output
|
|
17
|
-
"output
|
|
18
|
-
"output
|
|
19
|
-
"output
|
|
20
|
-
"output
|
|
21
|
-
"output
|
|
22
|
-
"output
|
|
23
|
-
"output
|
|
24
|
-
"output
|
|
25
|
-
"output
|
|
26
|
-
"parse": "
|
|
27
|
-
"string_interpolation": "
|
|
28
|
-
"tokenizer": "
|
|
29
|
-
"unicode_aliases": "
|
|
30
|
-
"utils": "
|
|
31
|
-
"#compiler#": "
|
|
32
|
-
"#compiled_with#": "
|
|
6
|
+
"baselib-errors": "8efbb59deb88ede0995768f63f57103f2f0fd02d",
|
|
7
|
+
"baselib-internal": "bb1eff32c1c50d5d25ea3074a968e765a136108c",
|
|
8
|
+
"baselib-itertools": "268b7b7af79506e9a01023e8cd3b1741599bb6a5",
|
|
9
|
+
"baselib-str": "0e8d1ee658690412140cc138adf9681425de5084",
|
|
10
|
+
"compiler": "4c728d81b441b4b9c590f9330271f54b47cc11eb",
|
|
11
|
+
"errors": "d1b33848d19b141e19cc079cf78421f29b6d2c05",
|
|
12
|
+
"output/__init__": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
|
13
|
+
"output/classes": "469c47511692e04eb1bc2b079af3f65b47165865",
|
|
14
|
+
"output/codegen": "69151b2cd53ff8b3270a122c65e1a0f56735150e",
|
|
15
|
+
"output/comments": "78ec38d8b34da96c078653e2d0d4049d7003906b",
|
|
16
|
+
"output/exceptions": "d45bc62bc6b011bae182ed7c8df8ea5e392f6fb8",
|
|
17
|
+
"output/functions": "5fe055661a45cf9de8ef024c758bb8214a87cd80",
|
|
18
|
+
"output/jsx": "0a174c39378155c19dfa669e833c14d63190127b",
|
|
19
|
+
"output/literals": "287ec4222c79f1ebfa2c6efae925e794cbb931ab",
|
|
20
|
+
"output/loops": "8a0dd93b95e1b9c2a29344e6b41cb5cec6b7afc5",
|
|
21
|
+
"output/modules": "15735b9b07260d11ba231fc3439818bd1a54947e",
|
|
22
|
+
"output/operators": "14b23f4a3ab3b1997411b8ec9f0f57d3e76a7da0",
|
|
23
|
+
"output/statements": "94ee0cd084f22aea451b96eb1a9a831084cc4267",
|
|
24
|
+
"output/stream": "2723d80bc398468e0828f50fb0837095c52f45dc",
|
|
25
|
+
"output/treeshake": "c1d65269ff21cfd046e947755a3489d0e670a74a",
|
|
26
|
+
"output/utils": "595968f96f6fdcc51eb41c34d64c92bb595e3cb1",
|
|
27
|
+
"parse": "0643834ed3c98d350ceb9ec71179f8923974c08c",
|
|
28
|
+
"string_interpolation": "bff1cc76d772d24d35707f6c794f38734ca08376",
|
|
29
|
+
"tokenizer": "134f46d5a10576c6a27d4fe024c63f45e111fefe",
|
|
30
|
+
"unicode_aliases": "79ac6eaa5e6be44a5397d62c561f854a8fe7528e",
|
|
31
|
+
"utils": "c1666db819aa7c8db38697e34e18362e3cb45869",
|
|
32
|
+
"#compiler#": "d41c267ea92de4937495c7d35c8d50e1b48f550c",
|
|
33
|
+
"#compiled_with#": "d41c267ea92de4937495c7d35c8d50e1b48f550c"
|
|
33
34
|
}
|
package/session.vim
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
set wildignore+=*.pyj-cached
|
|
2
|
-
set wildignore+=node_modules
|
|
3
|
-
imap <F4> ρσ_
|
|
4
|
-
cmap <F4> ρσ_
|
|
1
|
+
set wildignore+=*.pyj-cached
|
|
2
|
+
set wildignore+=node_modules
|
|
3
|
+
imap <F4> ρσ_
|
|
4
|
+
cmap <F4> ρσ_
|
package/setup.cfg
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
[rapydscript]
|
|
2
|
-
globals=assert,RapydScript
|
|
1
|
+
[rapydscript]
|
|
2
|
+
globals=assert,RapydScript
|
package/src/ast.pyj
CHANGED
|
@@ -988,6 +988,7 @@ class AST_Binary(AST_Node):
|
|
|
988
988
|
'operator': "[string] the operator",
|
|
989
989
|
'right': "[AST_Node] right-hand side expression",
|
|
990
990
|
'overloaded': "[bool] Whether to use Python-style operator overloading dispatch",
|
|
991
|
+
'strict_arith': "[bool] Whether incompatible operand types raise TypeError (default True when overloaded; disable with no_strict_arithmetic)",
|
|
991
992
|
'python_truthiness': "[bool] Whether to use Python truthiness (from __python__ import truthiness)"
|
|
992
993
|
}
|
|
993
994
|
|
package/src/baselib-builtins.pyj
CHANGED
|
@@ -459,12 +459,350 @@ class ρσ_slice:
|
|
|
459
459
|
raise TypeError("unhashable type: 'slice'")
|
|
460
460
|
|
|
461
461
|
|
|
462
|
-
|
|
463
|
-
|
|
462
|
+
class ρσ_object:
|
|
463
|
+
def __init__(self):
|
|
464
|
+
pass
|
|
465
|
+
|
|
466
|
+
def __repr__(self):
|
|
467
|
+
v'if (this.ρσ_object_id === undefined) this.ρσ_object_id = ++ρσ_hash_id_counter'
|
|
468
|
+
return '<object object at 0x' + v'this.ρσ_object_id.toString(16)'+ '>'
|
|
469
|
+
|
|
470
|
+
def __str__(self):
|
|
471
|
+
return self.__repr__()
|
|
472
|
+
|
|
473
|
+
def __eq__(self, other):
|
|
474
|
+
return self is other
|
|
475
|
+
|
|
476
|
+
def __hash__(self):
|
|
477
|
+
v'if (this.ρσ_object_id === undefined) this.ρσ_object_id = ++ρσ_hash_id_counter'
|
|
478
|
+
return self.ρσ_object_id
|
|
479
|
+
|
|
480
|
+
ρσ_object.__name__ = 'object'
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
def ρσ_exec(code, globals, locals):
|
|
484
|
+
# Execute a string of JavaScript/RapydScript-compiled code, like Python's exec.
|
|
485
|
+
# Always returns None. Without globals/locals uses indirect eval (global scope).
|
|
486
|
+
# With globals/locals uses the Function constructor so the code can reference the
|
|
487
|
+
# supplied variables; mutable object arguments (lists, dicts) are passed by
|
|
488
|
+
# reference so side-effects (e.g. array.push) are visible in the caller.
|
|
489
|
+
# Handles both plain JS objects and Python ρσ_dict instances (jsmap-backed Map).
|
|
490
|
+
# ρσ_ helpers referenced in compiled strings are injected from the current scope.
|
|
491
|
+
if globals is undefined:
|
|
492
|
+
v'eval(code)'
|
|
493
|
+
return None
|
|
494
|
+
v'''
|
|
495
|
+
function _ρσ_to_obj(d) {
|
|
496
|
+
if (d == null) return {};
|
|
497
|
+
if (typeof ρσ_dict === "function" && d instanceof ρσ_dict) {
|
|
498
|
+
var _r = {}; d.jsmap.forEach(function(v, k) { _r[k] = v; }); return _r;
|
|
499
|
+
}
|
|
500
|
+
return Object.assign({}, d);
|
|
501
|
+
}
|
|
502
|
+
var _ctx = _ρσ_to_obj(globals);
|
|
503
|
+
if (locals !== undefined) Object.assign(_ctx, _ρσ_to_obj(locals));
|
|
504
|
+
var _rσ_refs = code.match(/ρσ_\w+/g) || [];
|
|
505
|
+
_rσ_refs.forEach(function(name) {
|
|
506
|
+
if (!Object.prototype.hasOwnProperty.call(_ctx, name)) {
|
|
507
|
+
try { var _v = eval(name); if (_v !== undefined) _ctx[name] = _v; } catch(e) {}
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
var _keys = Object.keys(_ctx);
|
|
511
|
+
var _vals = _keys.map(function(k) { return _ctx[k]; });
|
|
512
|
+
Function.apply(null, _keys.concat([code])).apply(null, _vals);
|
|
513
|
+
'''
|
|
514
|
+
return None
|
|
515
|
+
ρσ_exec.__name__ = 'exec'
|
|
516
|
+
|
|
517
|
+
def ρσ_eval(expr, globals, locals):
|
|
518
|
+
# Called only when 2 or 3 args are provided (scoped eval via Function constructor).
|
|
519
|
+
# The compiler emits ρσ_eval() only when globals/locals are supplied; the
|
|
520
|
+
# plain 1-arg eval(expr) stays as a native direct JS eval call.
|
|
521
|
+
# Handles both plain JS objects and Python ρσ_dict instances (jsmap-backed Map).
|
|
522
|
+
# ρσ_ helpers referenced in compiled strings are injected from the current scope.
|
|
523
|
+
v'''
|
|
524
|
+
function _ρσ_to_obj(d) {
|
|
525
|
+
if (d == null) return {};
|
|
526
|
+
if (typeof ρσ_dict === "function" && d instanceof ρσ_dict) {
|
|
527
|
+
var _r = {}; d.jsmap.forEach(function(v, k) { _r[k] = v; }); return _r;
|
|
528
|
+
}
|
|
529
|
+
return Object.assign({}, d);
|
|
530
|
+
}
|
|
531
|
+
var _ctx = (globals !== undefined) ? _ρσ_to_obj(globals) : {};
|
|
532
|
+
if (locals !== undefined) Object.assign(_ctx, _ρσ_to_obj(locals));
|
|
533
|
+
var _rσ_refs = expr.match(/ρσ_\w+/g) || [];
|
|
534
|
+
_rσ_refs.forEach(function(name) {
|
|
535
|
+
if (!Object.prototype.hasOwnProperty.call(_ctx, name)) {
|
|
536
|
+
try { var _v = eval(name); if (_v !== undefined) _ctx[name] = _v; } catch(e) {}
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
var _keys = Object.keys(_ctx);
|
|
540
|
+
var _vals = _keys.map(function(k) { return _ctx[k]; });
|
|
541
|
+
return Function.apply(null, _keys.concat(['return (' + expr + ')'])).apply(null, _vals);
|
|
542
|
+
'''
|
|
543
|
+
ρσ_eval.__name__ = 'eval'
|
|
544
|
+
|
|
545
|
+
def ρσ_vars(obj):
|
|
546
|
+
# vars(obj) -> dict of obj's own instance attributes (like Python's obj.__dict__)
|
|
547
|
+
# vars() -> compiler rewrites to vars(this) — see output/functions.pyj
|
|
548
|
+
v'''
|
|
549
|
+
var _d;
|
|
550
|
+
if (typeof ρσ_dict === "function") {
|
|
551
|
+
_d = new ρσ_dict();
|
|
552
|
+
if (obj !== undefined && obj !== null) {
|
|
553
|
+
Object.keys(obj).forEach(function(k) {
|
|
554
|
+
if (k.charCodeAt(0) !== 0x03c1) { _d.jsmap.set(k, obj[k]); }
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
} else {
|
|
558
|
+
_d = Object.create(null);
|
|
559
|
+
if (obj !== undefined && obj !== null) {
|
|
560
|
+
Object.keys(obj).forEach(function(k) {
|
|
561
|
+
if (k.charCodeAt(0) !== 0x03c1) { _d[k] = obj[k]; }
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
'''
|
|
566
|
+
return v'_d'
|
|
567
|
+
ρσ_vars.__name__ = 'vars'
|
|
568
|
+
|
|
569
|
+
def ρσ_locals():
|
|
570
|
+
# In JavaScript, local scope cannot be introspected at runtime.
|
|
571
|
+
# Returns an empty dict as an approximation.
|
|
572
|
+
v'''
|
|
573
|
+
var _d;
|
|
574
|
+
if (typeof ρσ_dict === "function") { _d = new ρσ_dict(); } else { _d = Object.create(null); }
|
|
575
|
+
'''
|
|
576
|
+
return v'_d'
|
|
577
|
+
ρσ_locals.__name__ = 'locals'
|
|
578
|
+
|
|
579
|
+
def ρσ_globals():
|
|
580
|
+
# Returns a snapshot dict of the JS global object's own enumerable properties.
|
|
581
|
+
# In a browser this covers window.*; in Node.js this covers globalThis/global.*.
|
|
582
|
+
# Note: module-level RapydScript variables compiled inside an IIFE/module
|
|
583
|
+
# wrapper will NOT appear here — use a plain dict to share state instead.
|
|
584
|
+
v'''
|
|
585
|
+
var _g = (typeof globalThis !== "undefined") ? globalThis : (typeof window !== "undefined" ? window : (typeof global !== "undefined" ? global : {}));
|
|
586
|
+
var _d;
|
|
587
|
+
if (typeof ρσ_dict === "function") {
|
|
588
|
+
_d = new ρσ_dict();
|
|
589
|
+
Object.getOwnPropertyNames(_g).forEach(function(k) { _d.jsmap.set(k, _g[k]); });
|
|
590
|
+
} else {
|
|
591
|
+
_d = Object.create(null);
|
|
592
|
+
Object.getOwnPropertyNames(_g).forEach(function(k) { _d[k] = _g[k]; });
|
|
593
|
+
}
|
|
594
|
+
'''
|
|
595
|
+
return v'_d'
|
|
596
|
+
ρσ_globals.__name__ = 'globals'
|
|
597
|
+
|
|
598
|
+
def ρσ_abs(x):
|
|
599
|
+
if x is not None and jstype(x.__abs__) is 'function':
|
|
600
|
+
return x.__abs__()
|
|
601
|
+
return Math.abs(x)
|
|
602
|
+
ρσ_abs.__name__ = 'abs'
|
|
603
|
+
|
|
604
|
+
class ρσ_complex:
|
|
605
|
+
def __init__(self, real, imag):
|
|
606
|
+
nargs = arguments.length
|
|
607
|
+
if nargs is 0:
|
|
608
|
+
self.real = 0.0
|
|
609
|
+
self.imag = 0.0
|
|
610
|
+
elif nargs >= 2:
|
|
611
|
+
if jstype(real) is not 'number' and jstype(real) is not 'boolean':
|
|
612
|
+
raise TypeError("complex() first argument must be a number, not '" + jstype(real) + "'")
|
|
613
|
+
if jstype(imag) is not 'number' and jstype(imag) is not 'boolean':
|
|
614
|
+
raise TypeError("complex() second argument must be a number, not '" + jstype(imag) + "'")
|
|
615
|
+
self.real = +real
|
|
616
|
+
self.imag = +imag
|
|
617
|
+
else:
|
|
618
|
+
x = real
|
|
619
|
+
if v'x instanceof ρσ_complex':
|
|
620
|
+
self.real = x.real
|
|
621
|
+
self.imag = x.imag
|
|
622
|
+
elif jstype(x) is 'number' or jstype(x) is 'boolean':
|
|
623
|
+
self.real = +x
|
|
624
|
+
self.imag = 0.0
|
|
625
|
+
elif jstype(x) is 'string' or v'x instanceof String':
|
|
626
|
+
s = String(x).trim()
|
|
627
|
+
m_mixed = v'/^([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)([+-](?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)?)[jJ]$/.exec(s)'
|
|
628
|
+
m_imag = v'/^([+-]?(?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)?)[jJ]$/.exec(s)'
|
|
629
|
+
m_real = v'/^([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)$/.exec(s)'
|
|
630
|
+
if m_mixed:
|
|
631
|
+
self.real = parseFloat(m_mixed[1])
|
|
632
|
+
ims = m_mixed[2]
|
|
633
|
+
if ims is '+': self.imag = 1.0
|
|
634
|
+
elif ims is '-': self.imag = -1.0
|
|
635
|
+
else: self.imag = parseFloat(ims)
|
|
636
|
+
elif m_imag:
|
|
637
|
+
self.real = 0.0
|
|
638
|
+
ims = m_imag[1]
|
|
639
|
+
if not ims or ims is '+': self.imag = 1.0
|
|
640
|
+
elif ims is '-': self.imag = -1.0
|
|
641
|
+
else: self.imag = parseFloat(ims)
|
|
642
|
+
elif m_real:
|
|
643
|
+
self.real = parseFloat(m_real[1])
|
|
644
|
+
self.imag = 0.0
|
|
645
|
+
else:
|
|
646
|
+
raise ValueError("complex() arg is a malformed string")
|
|
647
|
+
else:
|
|
648
|
+
raise TypeError("complex() argument must be a string or a number")
|
|
649
|
+
|
|
650
|
+
def __add__(self, other):
|
|
651
|
+
if v'other instanceof ρσ_complex':
|
|
652
|
+
return new ρσ_complex(self.real + other.real, self.imag + other.imag)
|
|
653
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
654
|
+
return new ρσ_complex(self.real + other, self.imag)
|
|
655
|
+
raise TypeError("unsupported operand type(s) for +: 'complex' and '" + jstype(other) + "'")
|
|
656
|
+
|
|
657
|
+
def __radd__(self, other):
|
|
658
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
659
|
+
return new ρσ_complex(other + self.real, self.imag)
|
|
660
|
+
if v'other instanceof ρσ_complex':
|
|
661
|
+
return new ρσ_complex(other.real + self.real, other.imag + self.imag)
|
|
662
|
+
raise TypeError("unsupported operand type(s) for +: '" + jstype(other) + "' and 'complex'")
|
|
663
|
+
|
|
664
|
+
def __sub__(self, other):
|
|
665
|
+
if v'other instanceof ρσ_complex':
|
|
666
|
+
return new ρσ_complex(self.real - other.real, self.imag - other.imag)
|
|
667
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
668
|
+
return new ρσ_complex(self.real - other, self.imag)
|
|
669
|
+
raise TypeError("unsupported operand type(s) for -: 'complex' and '" + jstype(other) + "'")
|
|
670
|
+
|
|
671
|
+
def __rsub__(self, other):
|
|
672
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
673
|
+
return new ρσ_complex(other - self.real, -self.imag)
|
|
674
|
+
if v'other instanceof ρσ_complex':
|
|
675
|
+
return new ρσ_complex(other.real - self.real, other.imag - self.imag)
|
|
676
|
+
raise TypeError("unsupported operand type(s) for -: '" + jstype(other) + "' and 'complex'")
|
|
677
|
+
|
|
678
|
+
def __mul__(self, other):
|
|
679
|
+
if v'other instanceof ρσ_complex':
|
|
680
|
+
return new ρσ_complex(
|
|
681
|
+
self.real * other.real - self.imag * other.imag,
|
|
682
|
+
self.real * other.imag + self.imag * other.real
|
|
683
|
+
)
|
|
684
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
685
|
+
return new ρσ_complex(self.real * other, self.imag * other)
|
|
686
|
+
raise TypeError("unsupported operand type(s) for *: 'complex' and '" + jstype(other) + "'")
|
|
687
|
+
|
|
688
|
+
def __rmul__(self, other):
|
|
689
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
690
|
+
return new ρσ_complex(other * self.real, other * self.imag)
|
|
691
|
+
if v'other instanceof ρσ_complex':
|
|
692
|
+
return new ρσ_complex(
|
|
693
|
+
other.real * self.real - other.imag * self.imag,
|
|
694
|
+
other.real * self.imag + other.imag * self.real
|
|
695
|
+
)
|
|
696
|
+
raise TypeError("unsupported operand type(s) for *: '" + jstype(other) + "' and 'complex'")
|
|
697
|
+
|
|
698
|
+
def __truediv__(self, other):
|
|
699
|
+
if v'other instanceof ρσ_complex':
|
|
700
|
+
denom = other.real * other.real + other.imag * other.imag
|
|
701
|
+
if denom is 0: raise ZeroDivisionError('complex division by zero')
|
|
702
|
+
return new ρσ_complex(
|
|
703
|
+
(self.real * other.real + self.imag * other.imag) / denom,
|
|
704
|
+
(self.imag * other.real - self.real * other.imag) / denom
|
|
705
|
+
)
|
|
706
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
707
|
+
if other is 0: raise ZeroDivisionError('complex division by zero')
|
|
708
|
+
return new ρσ_complex(self.real / other, self.imag / other)
|
|
709
|
+
raise TypeError("unsupported operand type(s) for /: 'complex' and '" + jstype(other) + "'")
|
|
710
|
+
|
|
711
|
+
def __rtruediv__(self, other):
|
|
712
|
+
denom = self.real * self.real + self.imag * self.imag
|
|
713
|
+
if denom is 0: raise ZeroDivisionError('complex division by zero')
|
|
714
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
715
|
+
return new ρσ_complex(other * self.real / denom, -other * self.imag / denom)
|
|
716
|
+
if v'other instanceof ρσ_complex':
|
|
717
|
+
return new ρσ_complex(
|
|
718
|
+
(other.real * self.real + other.imag * self.imag) / denom,
|
|
719
|
+
(other.imag * self.real - other.real * self.imag) / denom
|
|
720
|
+
)
|
|
721
|
+
raise TypeError("unsupported operand type(s) for /: '" + jstype(other) + "' and 'complex'")
|
|
722
|
+
|
|
723
|
+
def __pow__(self, other):
|
|
724
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
725
|
+
r = Math.sqrt(self.real * self.real + self.imag * self.imag)
|
|
726
|
+
if r is 0: return new ρσ_complex(0, 0)
|
|
727
|
+
theta = Math.atan2(self.imag, self.real)
|
|
728
|
+
rn = Math.pow(r, other)
|
|
729
|
+
return new ρσ_complex(rn * Math.cos(other * theta), rn * Math.sin(other * theta))
|
|
730
|
+
if v'other instanceof ρσ_complex':
|
|
731
|
+
r = Math.sqrt(self.real * self.real + self.imag * self.imag)
|
|
732
|
+
if r is 0: return new ρσ_complex(0, 0)
|
|
733
|
+
theta = Math.atan2(self.imag, self.real)
|
|
734
|
+
log_r = Math.log(r)
|
|
735
|
+
c = other.real
|
|
736
|
+
d = other.imag
|
|
737
|
+
re_prod = c * log_r - d * theta
|
|
738
|
+
im_prod = d * log_r + c * theta
|
|
739
|
+
exp_re = Math.exp(re_prod)
|
|
740
|
+
return new ρσ_complex(exp_re * Math.cos(im_prod), exp_re * Math.sin(im_prod))
|
|
741
|
+
raise TypeError("unsupported operand type(s) for **: 'complex' and '" + jstype(other) + "'")
|
|
742
|
+
|
|
743
|
+
def __rpow__(self, other):
|
|
744
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
745
|
+
base = new ρσ_complex(other, 0)
|
|
746
|
+
return base.__pow__(self)
|
|
747
|
+
if v'other instanceof ρσ_complex':
|
|
748
|
+
return other.__pow__(self)
|
|
749
|
+
raise TypeError("unsupported operand type(s) for **: '" + jstype(other) + "' and 'complex'")
|
|
750
|
+
|
|
751
|
+
def __neg__(self):
|
|
752
|
+
return new ρσ_complex(-self.real, -self.imag)
|
|
753
|
+
|
|
754
|
+
def __pos__(self):
|
|
755
|
+
return new ρσ_complex(self.real, self.imag)
|
|
756
|
+
|
|
757
|
+
def __abs__(self):
|
|
758
|
+
return Math.sqrt(self.real * self.real + self.imag * self.imag)
|
|
759
|
+
|
|
760
|
+
def __bool__(self):
|
|
761
|
+
return self.real is not 0 or self.imag is not 0
|
|
762
|
+
|
|
763
|
+
def __eq__(self, other):
|
|
764
|
+
if v'other instanceof ρσ_complex':
|
|
765
|
+
return self.real is other.real and self.imag is other.imag
|
|
766
|
+
if jstype(other) is 'number' or jstype(other) is 'boolean':
|
|
767
|
+
return self.imag is 0 and self.real is other
|
|
768
|
+
return False
|
|
769
|
+
|
|
770
|
+
def __hash__(self):
|
|
771
|
+
if self.imag is 0:
|
|
772
|
+
return self.real | 0
|
|
773
|
+
return v'(self.real * 1000003 ^ self.imag) | 0'
|
|
774
|
+
|
|
775
|
+
def conjugate(self):
|
|
776
|
+
return new ρσ_complex(self.real, -self.imag)
|
|
777
|
+
|
|
778
|
+
def __repr__(self):
|
|
779
|
+
r = self.real
|
|
780
|
+
i = self.imag
|
|
781
|
+
if r is 0 and i is 0: return '0j'
|
|
782
|
+
r_str = String(r)
|
|
783
|
+
i_str = String(i)
|
|
784
|
+
if r is 0:
|
|
785
|
+
return i_str + 'j'
|
|
786
|
+
if i >= 0 or isNaN(i):
|
|
787
|
+
return '(' + r_str + '+' + i_str + 'j)'
|
|
788
|
+
return '(' + r_str + i_str + 'j)'
|
|
789
|
+
|
|
790
|
+
def __str__(self):
|
|
791
|
+
return self.__repr__()
|
|
792
|
+
|
|
793
|
+
ρσ_complex.__name__ = 'complex'
|
|
794
|
+
|
|
795
|
+
v'var abs = ρσ_abs, max = ρσ_max.bind(Math.max), min = ρσ_max.bind(Math.min), bool = ρσ_bool, type = ρσ_type'
|
|
796
|
+
v'var float = ρσ_float, int = ρσ_int, complex = ρσ_complex, arraylike = ρσ_arraylike_creator(), ρσ_arraylike = arraylike'
|
|
464
797
|
v'var id = ρσ_id, get_module = ρσ_get_module, pow = ρσ_pow, divmod = ρσ_divmod, __import__ = ρσ__import__'
|
|
465
798
|
v'var dir = ρσ_dir, ord = ρσ_ord, chr = ρσ_chr, bin = ρσ_bin, hex = ρσ_hex, callable = ρσ_callable, round = ρσ_round'
|
|
466
799
|
v'var enumerate = ρσ_enumerate, iter = ρσ_iter, reversed = ρσ_reversed, len = ρσ_len'
|
|
467
800
|
v'var range = ρσ_range, getattr = ρσ_getattr, setattr = ρσ_setattr, hasattr = ρσ_hasattr, issubclass = ρσ_issubclass, hash = ρσ_hash, next = ρσ_next'
|
|
801
|
+
v'var exec = ρσ_exec'
|
|
802
|
+
v'var vars = ρσ_vars, locals = ρσ_locals, globals = ρσ_globals'
|
|
468
803
|
v'var ρσ_Ellipsis = Object.freeze({toString: function(){return "Ellipsis";}, __repr__: function(){return "Ellipsis";}})'
|
|
469
804
|
v'var Ellipsis = ρσ_Ellipsis'
|
|
470
805
|
v'var slice = ρσ_slice'
|
|
806
|
+
v'var object = ρσ_object'
|
|
807
|
+
v'Number.prototype.is_integer = function() { return isFinite(+this) && (+this) % 1 === 0; }'
|
|
808
|
+
v'Number.prototype.bit_length = function() { var n = Math.abs(Math.trunc(+this)); if (n === 0) return 0; return Math.floor(Math.log2(n)) + 1; }'
|