koatl 0.1.26__tar.gz → 0.1.28__tar.gz
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.
- {koatl-0.1.26 → koatl-0.1.28}/Cargo.lock +1 -1
- {koatl-0.1.26 → koatl-0.1.28}/PKG-INFO +1 -1
- {koatl-0.1.26 → koatl-0.1.28}/koatl/Cargo.toml +1 -1
- {koatl-0.1.26 → koatl-0.1.28/koatl}/python/koatl/prelude/functional/algebra.tl +5 -10
- {koatl-0.1.26 → koatl-0.1.28/koatl}/python/koatl/prelude/functional/list.tl +2 -3
- {koatl-0.1.26 → koatl-0.1.28/koatl}/python/koatl/prelude/functional/result.tl +5 -6
- {koatl-0.1.26 → koatl-0.1.28/koatl}/python/koatl/prelude/iterable.tl +22 -26
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/runtime/__init__.py +4 -1
- koatl-0.1.28/koatl/python/koatl/runtime/classes.py +92 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/runtime/helpers.py +0 -10
- koatl-0.1.28/koatl/python/koatl/runtime/virtual.py +103 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/src/emit_py.rs +28 -2
- {koatl-0.1.26 → koatl-0.1.28}/koatl/src/lib.rs +9 -5
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/coal.tl +9 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/destructure.tl +1 -7
- koatl-0.1.28/koatl/tests/e2e/base/record.tl +24 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/scopes.tl +12 -0
- koatl-0.1.28/koatl/tests/e2e/prelude/aug_assign.tl +23 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/memo.tl +17 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/reader.tl +1 -1
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/parser/src/ast.rs +3 -1
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/parser/src/lexer.rs +2 -1
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/parser/src/parser.rs +91 -64
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/parser/src/util.rs +2 -2
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/inference.rs +5 -1
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/py/ast.rs +1 -1
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/py/emit.rs +7 -2
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/py/util.rs +10 -1
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/resolve_scopes.rs +98 -105
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/transform.rs +204 -433
- {koatl-0.1.26/koatl → koatl-0.1.28}/python/koatl/prelude/functional/algebra.tl +5 -10
- {koatl-0.1.26/koatl → koatl-0.1.28}/python/koatl/prelude/functional/list.tl +2 -3
- {koatl-0.1.26/koatl → koatl-0.1.28}/python/koatl/prelude/functional/result.tl +5 -6
- {koatl-0.1.26/koatl → koatl-0.1.28}/python/koatl/prelude/iterable.tl +22 -26
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/runtime/__init__.py +4 -1
- koatl-0.1.28/python/koatl/runtime/classes.py +92 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/runtime/helpers.py +0 -10
- koatl-0.1.28/python/koatl/runtime/virtual.py +103 -0
- koatl-0.1.26/koatl/python/koatl/runtime/virtual.py +0 -139
- koatl-0.1.26/python/koatl/runtime/virtual.py +0 -139
- {koatl-0.1.26 → koatl-0.1.28}/Cargo.toml +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/README.md +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/.github/workflows/CI.yml +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/.gitignore +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/LICENSE +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/README.md +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/__init__.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/__main__.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/cli.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/notebook/__init__.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/notebook/magic.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/prelude/__init__.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/prelude/functional/__init__.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/prelude/functional/async.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/prelude/functional/async_util.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/prelude/functional/memo.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/prelude/functional/reader.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/runtime/meta_finder.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/python/koatl/runtime/record.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/requirements.txt +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/containers.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/decorators.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/destructure-for-and-fn.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/escape_ident.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/fstr.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/functions.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/generator.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/if_expr.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/imports.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/loops.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/match.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/nary-list.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/placeholder.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/precedence.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/semantic_whitespace.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/base/short_circuit.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/destructure.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/async.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/iterables.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/list.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/result.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/slice.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/try.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/prelude/virtual.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/util/__init__.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/util/module0.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/util/module1.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/e2e/util/module2.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/parse/arith.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/parse/assign.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/parse/block-comments.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/parse/deco.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/parse/func.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/parse/matches.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/test_e2e.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl/tests/test_parse.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/Cargo.toml +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/parser/Cargo.toml +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/parser/src/lib.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/parser/tests/lexer.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/lib.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/main.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/parse_timer.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/parser.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/py/mod.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/types.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/koatl-core/src/util.rs +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/pyproject.toml +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/__init__.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/__main__.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/cli.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/notebook/__init__.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/notebook/magic.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/prelude/__init__.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/prelude/functional/__init__.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/prelude/functional/async.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/prelude/functional/async_util.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/prelude/functional/memo.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/prelude/functional/reader.tl +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/runtime/meta_finder.py +0 -0
- {koatl-0.1.26 → koatl-0.1.28}/python/koatl/runtime/record.py +0 -0
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export Monad = class(abc.ABC):
|
|
1
|
+
export Monad = class(Trait):
|
|
4
2
|
"""
|
|
5
3
|
Abstract base class for monads.
|
|
6
4
|
|
|
@@ -9,17 +7,16 @@ export Monad = class(abc.ABC):
|
|
|
9
7
|
"""
|
|
10
8
|
__slots__ = ()
|
|
11
9
|
|
|
12
|
-
bind =
|
|
10
|
+
bind = Abstract& (self, f) => None
|
|
13
11
|
|
|
14
|
-
pure = staticmethod&
|
|
12
|
+
pure = staticmethod& Abstract& value => None
|
|
15
13
|
|
|
16
14
|
# Automatically generated implementations.
|
|
17
15
|
map = (self, f) => self.bind(x => self.pure(f(x)))
|
|
18
16
|
apply = (self, f) => self.bind(x => f.map(fn => fn(x)))
|
|
19
17
|
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
export MonadOnce = class(abc.ABC):
|
|
19
|
+
export MonadOnce = class(Monad, Trait):
|
|
23
20
|
"""
|
|
24
21
|
Abstract base class for single-use monads.
|
|
25
22
|
|
|
@@ -32,9 +29,7 @@ export MonadOnce = class(abc.ABC):
|
|
|
32
29
|
__slots__ = ()
|
|
33
30
|
|
|
34
31
|
# The default implementation required for `@` syntax that should be overridden by subclasses.
|
|
35
|
-
bind_once =
|
|
36
|
-
|
|
37
|
-
pure = staticmethod& abc.abstractmethod& value => None
|
|
32
|
+
bind_once = Abstract& (self, f) => None
|
|
38
33
|
|
|
39
34
|
# An optional, optimized implementation of `bind` that skips deep recursion.
|
|
40
35
|
# TODO: can this be automatically generated from bind_once?
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import collections.UserList
|
|
2
1
|
import itertools
|
|
3
2
|
|
|
4
3
|
import .Monad
|
|
5
4
|
import ..iterable.Iterable
|
|
6
5
|
|
|
7
|
-
export List = class(
|
|
6
|
+
export List = class(Monad):
|
|
8
7
|
bind_once = (self, f) =>
|
|
9
8
|
raise NotImplementedError(
|
|
10
9
|
"Binding List in a do-block doesn't work in Koatl " +
|
|
@@ -23,4 +22,4 @@ for name, method in List.__dict__:
|
|
|
23
22
|
if name.startswith("_"):
|
|
24
23
|
continue
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
ExtensionMethod(list, name)& method
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import functools.wraps
|
|
2
|
-
import koatl.runtime.virtual.register_global_attr
|
|
3
2
|
import .algebra.MonadOnce
|
|
4
3
|
|
|
5
4
|
export Result = class(MonadOnce):
|
|
@@ -118,14 +117,14 @@ export Err = class(Result):
|
|
|
118
117
|
coalesce = (self, f) => f()
|
|
119
118
|
|
|
120
119
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
120
|
+
ExtensionProperty(type(None), "ok")& _ => False
|
|
121
|
+
ExtensionProperty(BaseException, "ok")& _ => False
|
|
122
|
+
ExtensionProperty(object, "ok")& _ => True
|
|
124
123
|
|
|
125
|
-
|
|
124
|
+
ExtensionProperty(object, "result")& Result
|
|
126
125
|
|
|
127
126
|
# Enables @ operator for bare objects.
|
|
128
|
-
|
|
127
|
+
ExtensionMethod(object, "bind_gen")& Result.bind_gen
|
|
129
128
|
|
|
130
129
|
__tl__.Ok = Ok
|
|
131
130
|
__tl__.Err = Err
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import itertools
|
|
2
2
|
import builtins
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
export Iterable = ExtensionTrait& class(Trait):
|
|
5
|
+
iter = Abstract
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
map = (self, f) => builtins.map(f, self.iter)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
filter = (self, f) => builtins.filter(f, self.iter)
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
flat_map = (self, f) => itertools.chain.from_iterable(self.map(f))
|
|
12
|
+
|
|
13
|
+
traverse = (self, f) =>
|
|
14
|
+
let it = self.iter
|
|
13
15
|
let v
|
|
14
16
|
try:
|
|
15
17
|
v = next(it)
|
|
@@ -39,45 +41,39 @@ methods = {
|
|
|
39
41
|
acc = acc.apply(m.map(x => acc => [*acc, x]))
|
|
40
42
|
acc
|
|
41
43
|
|
|
42
|
-
fold
|
|
44
|
+
fold = (self, init, f) =>
|
|
43
45
|
let acc = init
|
|
44
|
-
for i in
|
|
46
|
+
for i in self:
|
|
45
47
|
acc = f(acc, i)
|
|
46
48
|
acc
|
|
47
49
|
|
|
48
|
-
first
|
|
49
|
-
for i in
|
|
50
|
+
first = (self, f) =>
|
|
51
|
+
for i in self:
|
|
50
52
|
if f(i):
|
|
51
53
|
return i
|
|
52
54
|
return None
|
|
53
55
|
|
|
54
|
-
last
|
|
56
|
+
last = (self, f) =>
|
|
55
57
|
let result = None
|
|
56
|
-
for i in
|
|
58
|
+
for i in self:
|
|
57
59
|
if f(i):
|
|
58
60
|
result = i
|
|
59
61
|
return result
|
|
60
62
|
|
|
61
|
-
at
|
|
62
|
-
for i in
|
|
63
|
+
at = (self, index) =>
|
|
64
|
+
for i in self:
|
|
63
65
|
if i == index:
|
|
64
66
|
return i
|
|
65
67
|
raise IndexError("Index out of range")
|
|
66
68
|
|
|
67
|
-
sum
|
|
69
|
+
sum = self =>
|
|
68
70
|
let acc = 0
|
|
69
|
-
for i in
|
|
71
|
+
for i in self:
|
|
70
72
|
acc = acc + i
|
|
71
73
|
acc
|
|
72
74
|
|
|
73
|
-
list
|
|
74
|
-
list(
|
|
75
|
-
|
|
76
|
-
record: x =>
|
|
77
|
-
Record(x.iter)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
# TODO merge this with typing.Iterable?
|
|
75
|
+
list = self =>
|
|
76
|
+
list(self.iter)
|
|
81
77
|
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
record = self =>
|
|
79
|
+
Record(self.iter)
|
|
@@ -19,6 +19,7 @@ meta_finder.install_hook()
|
|
|
19
19
|
from .virtual import *
|
|
20
20
|
from .record import *
|
|
21
21
|
from .helpers import *
|
|
22
|
+
from .classes import *
|
|
22
23
|
|
|
23
24
|
|
|
24
25
|
def dummy(name):
|
|
@@ -33,9 +34,9 @@ def dummy(name):
|
|
|
33
34
|
__tl__ = SimpleNamespace(
|
|
34
35
|
Exception=Exception,
|
|
35
36
|
slice=slice,
|
|
37
|
+
type=type,
|
|
36
38
|
vget=virtual.vget,
|
|
37
39
|
vhas=virtual.vhas,
|
|
38
|
-
unpack_record=helpers.unpack_record,
|
|
39
40
|
set_exports=helpers.set_exports,
|
|
40
41
|
do=helpers.do,
|
|
41
42
|
partial=functools.partial,
|
|
@@ -50,6 +51,7 @@ __tl__ = SimpleNamespace(
|
|
|
50
51
|
**{name: helpers.__dict__[name] for name in helpers.__all__},
|
|
51
52
|
**{name: record.__dict__[name] for name in record.__all__},
|
|
52
53
|
**{name: virtual.__dict__[name] for name in virtual.__all__},
|
|
54
|
+
**{name: classes.__dict__[name] for name in classes.__all__},
|
|
53
55
|
)
|
|
54
56
|
|
|
55
57
|
|
|
@@ -58,4 +60,5 @@ __all__ = [
|
|
|
58
60
|
*helpers.__all__,
|
|
59
61
|
*record.__all__,
|
|
60
62
|
*virtual.__all__,
|
|
63
|
+
*classes.__all__,
|
|
61
64
|
]
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
import collections
|
|
3
|
+
import inspect
|
|
4
|
+
|
|
5
|
+
from koatl.runtime.virtual import vhas
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@collections.abc.Mapping.register
|
|
9
|
+
class MappingMeta(type):
|
|
10
|
+
def __getitem__(self, key):
|
|
11
|
+
return getattr(self, key)
|
|
12
|
+
|
|
13
|
+
def __iter__(self):
|
|
14
|
+
return self.keys().__iter__()
|
|
15
|
+
|
|
16
|
+
def __len__(self):
|
|
17
|
+
return len(self.items())
|
|
18
|
+
|
|
19
|
+
def __contains__(self, key):
|
|
20
|
+
return hasattr(self, key)
|
|
21
|
+
|
|
22
|
+
def get(self, key, default=None):
|
|
23
|
+
if hasattr(self, key):
|
|
24
|
+
return getattr(self, key)
|
|
25
|
+
else:
|
|
26
|
+
return default
|
|
27
|
+
|
|
28
|
+
def keys(self):
|
|
29
|
+
return [k for k, _ in self.items()]
|
|
30
|
+
|
|
31
|
+
def values(self):
|
|
32
|
+
return [v for _, v in self.items()]
|
|
33
|
+
|
|
34
|
+
def items(self):
|
|
35
|
+
return [(k, v) for k, v in inspect.getmembers(self) if not k.startswith("_")]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class Class(metaclass=MappingMeta):
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# temporary value, since TraitMeta needs to reference Trait.
|
|
43
|
+
Trait = None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class TraitMeta(MappingMeta):
|
|
47
|
+
def __init__(cls, name, bases, namespace):
|
|
48
|
+
super().__init__(name, bases, namespace)
|
|
49
|
+
|
|
50
|
+
if Trait in bases:
|
|
51
|
+
cls._trait_reqs = []
|
|
52
|
+
cls._own_methods = []
|
|
53
|
+
|
|
54
|
+
for key, value in inspect.getmembers(cls):
|
|
55
|
+
if (
|
|
56
|
+
hasattr(value, "__isabstractmethod__")
|
|
57
|
+
and value.__isabstractmethod__
|
|
58
|
+
):
|
|
59
|
+
cls._trait_reqs.append(key)
|
|
60
|
+
|
|
61
|
+
for key, value in namespace.items():
|
|
62
|
+
if (
|
|
63
|
+
hasattr(value, "__isabstractmethod__")
|
|
64
|
+
and value.__isabstractmethod__
|
|
65
|
+
):
|
|
66
|
+
# Abstract methods are not considered "own methods".
|
|
67
|
+
continue
|
|
68
|
+
|
|
69
|
+
if callable(value) and not key.startswith("_"):
|
|
70
|
+
cls._own_methods.append(key)
|
|
71
|
+
|
|
72
|
+
def __instancecheck__(cls, instance):
|
|
73
|
+
# Check if the exact class has _trait_reqs, in which case it's a trait.
|
|
74
|
+
if "_trait_reqs" in cls.__dict__:
|
|
75
|
+
for req in cls._trait_reqs:
|
|
76
|
+
if not vhas(instance, req):
|
|
77
|
+
return False
|
|
78
|
+
|
|
79
|
+
return True
|
|
80
|
+
else:
|
|
81
|
+
return type.__instancecheck__(cls, instance)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class Trait(metaclass=TraitMeta):
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
Abstract = lambda value: abc.abstractmethod(value)
|
|
89
|
+
Abstract.__isabstractmethod__ = True
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
__all__ = ["Class", "Trait", "Abstract"]
|
|
@@ -35,16 +35,6 @@ def set_exports(package_name, globals_dict, exports, module_star_exports):
|
|
|
35
35
|
globals_dict["__all__"] = tuple(set(globals_dict["__all__"]) | exports)
|
|
36
36
|
|
|
37
37
|
|
|
38
|
-
def unpack_record(obj):
|
|
39
|
-
"""
|
|
40
|
-
used in record unpacking
|
|
41
|
-
"""
|
|
42
|
-
if hasattr(obj, "items"):
|
|
43
|
-
return Record(obj.items())
|
|
44
|
-
else:
|
|
45
|
-
return Record(obj.__dict__)
|
|
46
|
-
|
|
47
|
-
|
|
48
38
|
def ok(obj):
|
|
49
39
|
try:
|
|
50
40
|
return obj.ok
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
from functools import partial
|
|
2
|
+
from itertools import count
|
|
3
|
+
from .._rs import fast_vget, fast_vset, fast_vset_trait
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def vget(obj, name, ignore_traits=False):
|
|
7
|
+
try:
|
|
8
|
+
return getattr(obj, name)
|
|
9
|
+
except:
|
|
10
|
+
# special case for iter - this could be implemented using types and trait vtbls
|
|
11
|
+
# but this is simpler and probably faster
|
|
12
|
+
if name == "iter":
|
|
13
|
+
if isinstance(obj, slice):
|
|
14
|
+
start = obj.start if obj.start is not None else 0
|
|
15
|
+
step = obj.step if obj.step is not None else 1
|
|
16
|
+
if obj.stop is None:
|
|
17
|
+
return count(start, step)
|
|
18
|
+
else:
|
|
19
|
+
return range(start, obj.stop, step)
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
return obj.items()
|
|
23
|
+
except AttributeError:
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
return iter(obj)
|
|
28
|
+
except TypeError:
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
v = fast_vget(obj, name, ignore_traits)
|
|
32
|
+
if v is not None:
|
|
33
|
+
if hasattr(v, "_ext_prop"):
|
|
34
|
+
return v(obj)
|
|
35
|
+
|
|
36
|
+
return partial(v, obj)
|
|
37
|
+
|
|
38
|
+
raise AttributeError(
|
|
39
|
+
f"'{type(obj).__name__}' object has no v-attribute '{name}'"
|
|
40
|
+
) from None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def vhas(obj, name, ignore_traits=False):
|
|
44
|
+
if hasattr(obj, name):
|
|
45
|
+
return True
|
|
46
|
+
|
|
47
|
+
if name == "iter":
|
|
48
|
+
if isinstance(obj, slice):
|
|
49
|
+
return True
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
obj.items()
|
|
53
|
+
return True
|
|
54
|
+
except AttributeError:
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
try:
|
|
58
|
+
iter(obj)
|
|
59
|
+
return True
|
|
60
|
+
except TypeError:
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
v = fast_vget(obj, name, ignore_traits)
|
|
64
|
+
if v is not None:
|
|
65
|
+
return True
|
|
66
|
+
|
|
67
|
+
return False
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def ExtensionProperty(type, name):
|
|
71
|
+
def impl(value):
|
|
72
|
+
value._ext_prop = True
|
|
73
|
+
fast_vset(type, name, value)
|
|
74
|
+
return value
|
|
75
|
+
|
|
76
|
+
return impl
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def ExtensionMethod(type, name):
|
|
80
|
+
def impl(value):
|
|
81
|
+
fast_vset(type, name, value)
|
|
82
|
+
return value
|
|
83
|
+
|
|
84
|
+
return impl
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def ExtensionTrait(type):
|
|
88
|
+
for name in type._own_methods:
|
|
89
|
+
fast_vset_trait(type.__name__, type._trait_reqs, name, type.__dict__[name])
|
|
90
|
+
return type
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def TraitProperty(value):
|
|
94
|
+
value._ext_prop = True
|
|
95
|
+
return value
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
__all__ = [
|
|
99
|
+
"ExtensionProperty",
|
|
100
|
+
"ExtensionMethod",
|
|
101
|
+
"ExtensionTrait",
|
|
102
|
+
"TraitProperty",
|
|
103
|
+
]
|
|
@@ -279,10 +279,36 @@ impl<'src> PyStmtExt<'src> for SPyStmt<'src> {
|
|
|
279
279
|
.collect();
|
|
280
280
|
ctx.ast_node("Match", (subject_ast, cases_ast?), &self.tl_span)
|
|
281
281
|
}
|
|
282
|
-
PyStmt::Assign(target, value) => {
|
|
282
|
+
PyStmt::Assign(target, value, op) => {
|
|
283
283
|
let target_ast = target.emit_py(ctx)?;
|
|
284
284
|
let value_ast = value.emit_py(ctx)?;
|
|
285
|
-
|
|
285
|
+
|
|
286
|
+
if let Some(op) = op {
|
|
287
|
+
let py_op = match op {
|
|
288
|
+
PyBinaryOp::Add => "Add",
|
|
289
|
+
PyBinaryOp::Sub => "Sub",
|
|
290
|
+
PyBinaryOp::Mult => "Mult",
|
|
291
|
+
PyBinaryOp::Div => "Div",
|
|
292
|
+
_ => {
|
|
293
|
+
return Err(PyTlErr {
|
|
294
|
+
message: format!(
|
|
295
|
+
"Unsupported augmented assignment operator: {:?}",
|
|
296
|
+
op
|
|
297
|
+
),
|
|
298
|
+
py_err: None,
|
|
299
|
+
span: Some(self.tl_span),
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
ctx.ast_node(
|
|
305
|
+
"AugAssign",
|
|
306
|
+
(target_ast, ctx.ast_cls(py_op, ())?, value_ast),
|
|
307
|
+
&self.tl_span,
|
|
308
|
+
)
|
|
309
|
+
} else {
|
|
310
|
+
ctx.ast_node("Assign", ([target_ast], value_ast), &self.tl_span)
|
|
311
|
+
}
|
|
286
312
|
}
|
|
287
313
|
PyStmt::Return(expr) => {
|
|
288
314
|
let expr_ast = expr.emit_py(ctx)?;
|
|
@@ -178,11 +178,15 @@ fn fast_vset_trait<'py, 'ptr>(
|
|
|
178
178
|
vtbl.insert(name.clone(), Vec::new());
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
181
|
+
// TODO: what about conflicting traits?
|
|
182
|
+
vtbl.get_mut(&name).unwrap().insert(
|
|
183
|
+
0,
|
|
184
|
+
TraitAttr {
|
|
185
|
+
name: trait_name.to_string(),
|
|
186
|
+
requirements: reqs,
|
|
187
|
+
value: value.clone().unbind(),
|
|
188
|
+
},
|
|
189
|
+
);
|
|
186
190
|
|
|
187
191
|
Ok(())
|
|
188
192
|
}
|
|
@@ -48,11 +48,5 @@ assert_eq([a, b, c], ["a", [2, 3], {2: "2", 3: "3"}])
|
|
|
48
48
|
{a, **b} = {a: 42, b: 43}
|
|
49
49
|
assert_eq([a, b], [42, {b: 43}])
|
|
50
50
|
|
|
51
|
-
a, *b, c = 1, 2, 3, 4
|
|
52
|
-
assert_eq([a, b, c], [1, (2, 3), 4])
|
|
53
|
-
|
|
54
51
|
a, [*b], c, *d = 1, [2, 3], 4, 5, 6
|
|
55
|
-
assert_eq([a, b, c, d], [1, [2, 3], 4,
|
|
56
|
-
|
|
57
|
-
# destructuring modules
|
|
58
|
-
{assert_eq} = util
|
|
52
|
+
assert_eq([a, b, c, d], [1, [2, 3], 4, [5, 6]])
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import util.assert_eq
|
|
2
|
+
|
|
3
|
+
assert_eq(
|
|
4
|
+
{a: 0}.keys() | list
|
|
5
|
+
["a"]
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
assert_eq(
|
|
9
|
+
{(): 0}.keys() | list
|
|
10
|
+
[()]
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
assert_eq(
|
|
14
|
+
{(1): 0}.keys() | list
|
|
15
|
+
[1]
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
assert_eq(
|
|
19
|
+
{(
|
|
20
|
+
let x = 1
|
|
21
|
+
x
|
|
22
|
+
): 0}.keys() | list
|
|
23
|
+
[1]
|
|
24
|
+
)
|
|
@@ -89,4 +89,16 @@ f = () =>
|
|
|
89
89
|
|
|
90
90
|
A().f()
|
|
91
91
|
assert_eq(A, 2)
|
|
92
|
+
f()
|
|
93
|
+
|
|
94
|
+
# Shadowing should work
|
|
95
|
+
f = () =>
|
|
96
|
+
let x = 1
|
|
97
|
+
let x = x + 1
|
|
98
|
+
assert_eq(x, 2)
|
|
99
|
+
|
|
100
|
+
# but recursion should also work
|
|
101
|
+
let x = 3
|
|
102
|
+
let x = () => x.__name__
|
|
103
|
+
assert_eq(type(x()), str)
|
|
92
104
|
f()
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import util.assert_eq
|
|
2
|
+
|
|
3
|
+
x = 1
|
|
4
|
+
x += 2
|
|
5
|
+
assert_eq(x, 3)
|
|
6
|
+
|
|
7
|
+
x = None
|
|
8
|
+
x ??= 2
|
|
9
|
+
assert_eq(x, 2)
|
|
10
|
+
|
|
11
|
+
x = 4
|
|
12
|
+
x |= $ + 2
|
|
13
|
+
assert_eq(x, 6)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
counts = 0
|
|
17
|
+
f = () =>
|
|
18
|
+
counts += 1
|
|
19
|
+
{x: None}
|
|
20
|
+
f().x ??= 2
|
|
21
|
+
|
|
22
|
+
# should only call once
|
|
23
|
+
assert_eq(counts, 1)
|
|
@@ -29,3 +29,20 @@ assert_eq(f(10).run(ctx), 20)
|
|
|
29
29
|
assert_eq(f(5).run(ctx), 10)
|
|
30
30
|
assert_eq(f(5).run(ctx), 10)
|
|
31
31
|
assert_eq(counts, 2)
|
|
32
|
+
|
|
33
|
+
# Indirect dependencies should be tracked as well.
|
|
34
|
+
|
|
35
|
+
counts = 0
|
|
36
|
+
|
|
37
|
+
f = (x, y) =>
|
|
38
|
+
memo:
|
|
39
|
+
counts = counts + 1
|
|
40
|
+
let other = () => y
|
|
41
|
+
x * other()
|
|
42
|
+
|
|
43
|
+
ctx = Memo.Cache()
|
|
44
|
+
assert_eq(f(10, 20).run(ctx), 200)
|
|
45
|
+
assert_eq(f(10, 20).run(ctx), 200)
|
|
46
|
+
assert_eq(f(10, 30).run(ctx), 300)
|
|
47
|
+
assert_eq(f(10, 30).run(ctx), 300)
|
|
48
|
+
assert_eq(counts, 2)
|
|
@@ -121,7 +121,9 @@ pub enum DeclType {
|
|
|
121
121
|
#[derive(Debug, Clone)]
|
|
122
122
|
pub enum Stmt<'a, TTree: Tree> {
|
|
123
123
|
Decl(Vec<SIdent<'a>>, DeclType),
|
|
124
|
-
Assign(TTree::Expr, TTree::Expr, Option<
|
|
124
|
+
Assign(TTree::Expr, TTree::Expr, Option<BinaryOp>),
|
|
125
|
+
PatternAssign(TTree::Pattern, TTree::Expr, Option<DeclType>),
|
|
126
|
+
|
|
125
127
|
Expr(TTree::Expr),
|
|
126
128
|
|
|
127
129
|
Return(TTree::Expr),
|
|
@@ -272,7 +272,8 @@ where
|
|
|
272
272
|
|
|
273
273
|
fn parse_symbol(&mut self) -> TResult<'src, Spanned<Token<'src>>> {
|
|
274
274
|
const POLYGRAMS: &[&str] = &[
|
|
275
|
-
"
|
|
275
|
+
"+=", "-=", "*=", "/=", "|=", "??=", "===", "<=>", "=>", "..", "==", "<>", "<=", ">=",
|
|
276
|
+
"//", "**", "??", ".=",
|
|
276
277
|
];
|
|
277
278
|
const MONOGRAMS: &str = "[](){}<>.,;:!?@$%^&*+-=|\\/`~";
|
|
278
279
|
|