Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revisão | 281b35b96aa71fb93481e651086810f70fe87a04 (tree) |
---|---|
Hora | 2024-04-19 06:15:52 |
Autor | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
Addeds aigr.Become(statement)
@@ -4,9 +4,10 @@ | ||
4 | 4 | PYREVERSE_PKGS = castle/aigr castle/aigr.machinery |
5 | 5 | |
6 | 6 | LAST = \ |
7 | + pytst/statements/test_if.py \ | |
7 | 8 | # |
8 | 9 | CURRENT = \ |
9 | - pytst/test_4_statements.py \ | |
10 | + pytst/statements/test_become.py \ | |
10 | 11 | # |
11 | 12 | TODO = \ |
12 | 13 | pytst/test_3_namespaces.py \ |
@@ -19,3 +19,20 @@ | ||
19 | 19 | test: AIGR # Boolean Expr |
20 | 20 | body: AIGR # Typical: `AIGR.Body` |
21 | 21 | orelse: PTH.Optional[AIGR]=None # Typical: `AIGR.Body | AIGR.If` |
22 | + | |
23 | +@dataclass | |
24 | +class Become(_statement): | |
25 | + """ It can be a single assignment as `a:=2`; or `b=foo()`; or more complex ones ... | |
26 | + | |
27 | + * Multiple assignment: `a,b := b,a` -- works as expected: swap the values | |
28 | + * For multiple assignment `len(targets)==len(values)` | |
29 | + * Tuple assignment `a := 1,2,3` is also possible -- then the two tuples do not have the same length | |
30 | + * Un/Packing as in Python `a,*b,c= 1,2,3,4,5;` is also possible -- here 'b' becomes '2,3,4' | |
31 | + | |
32 | + Currently, only single assignment are supported -- so both tuples have len==1 | |
33 | + | |
34 | + """ | |
35 | + _kids = ('targets', 'values') | |
36 | + _ : KW_ONLY | |
37 | + targets: tuple[AIGR] # LHS: (sequence of) Variables etc | |
38 | + values: tuple[AIGR] # RGS: (sequence of) Values |
@@ -0,0 +1,37 @@ | ||
1 | +# (C) Albert Mietus, 2024. Part of Castle/CCastle project | |
2 | + | |
3 | +import logging; logger = logging.getLogger(__name__) | |
4 | +import pytest | |
5 | +import typing as PTH # Python TypeHints | |
6 | + | |
7 | +from castle.aigr import AIGR | |
8 | +from castle.aigr import If | |
9 | + | |
10 | +from dataclasses import dataclass | |
11 | + | |
12 | + | |
13 | +@dataclass | |
14 | +class Dummy(AIGR): | |
15 | + mark: PTH.Any | |
16 | + | |
17 | + def __repr__(self): | |
18 | + return f'<Dummy.{self.mark}>' | |
19 | + | |
20 | + | |
21 | +def verifyMark(dummy, mark): | |
22 | + logger.debug("verifyMark: dummy=%s, mark=%s", dummy, mark) | |
23 | + if mark is None: | |
24 | + assert dummy is None | |
25 | + else: | |
26 | + assert isinstance(dummy, Dummy) | |
27 | + assert dummy.mark == mark, f"Expecting mark: {mark}, but got {dummy.mark}" | |
28 | + | |
29 | + | |
30 | +def verifyKids(s): | |
31 | + logger.debug("verifyKids: statements: %s", s) | |
32 | + unique = Dummy('unique') | |
33 | + for k in s._kids: | |
34 | + logger.debug("verifyKid getattr(s,%s,unique) (%s) != unique (%s) %s", | |
35 | + k, getattr(s,k,unique), unique, getattr(s,k,unique)!=unique) | |
36 | + assert unique != getattr(s,k, unique), f"Kid `{k}` should exist in {s}, but doesn't" | |
37 | + |
@@ -0,0 +1,22 @@ | ||
1 | +# (C) Albert Mietus, 2024. Part of Castle/CCastle project | |
2 | + | |
3 | +import pytest | |
4 | +from . import Dummy, verifyMark, verifyKids | |
5 | + | |
6 | +from castle.aigr import Become | |
7 | + | |
8 | + | |
9 | + | |
10 | + | |
11 | +def test_1_simpleAssign(): | |
12 | + "a:=1" | |
13 | + s=Become(targets=(Dummy('a'),),values=(Dummy(1),)) | |
14 | + verifyMark(s.targets[0],'a') | |
15 | + verifyMark(s.values[0],1) | |
16 | + | |
17 | + | |
18 | + | |
19 | +def test_if_kids(): | |
20 | + verifyKids(Become(targets=(),values=())) | |
21 | + | |
22 | + |
@@ -0,0 +1,30 @@ | ||
1 | +# (C) Albert Mietus, 2024. Part of Castle/CCastle project | |
2 | + | |
3 | +import pytest | |
4 | +from . import Dummy, verifyMark, verifyKids | |
5 | + | |
6 | +from castle.aigr import If | |
7 | + | |
8 | + | |
9 | +def test_1a_if3(): | |
10 | + s = If(test=Dummy('if-test'),body=Dummy('then-body'), orelse=Dummy('else')) | |
11 | + verifyMark(s.test, 'if-test') | |
12 | + verifyMark(s.body, 'then-body') | |
13 | + verifyMark(s.orelse,'else', ) | |
14 | + | |
15 | +def test_1a_if2(): | |
16 | + s = If(test=Dummy('if-test'),body=Dummy('then-body')) | |
17 | + verifyMark(s.test,'if-test') | |
18 | + verifyMark(s.body, 'then-body') | |
19 | + verifyMark(s.orelse, None) | |
20 | + | |
21 | +def test_2_if_missing(): | |
22 | + with pytest.raises(TypeError): If(test=Dummy('WRONG')) | |
23 | + with pytest.raises(TypeError): If(body=Dummy('WRONG')) | |
24 | + with pytest.raises(TypeError): If() | |
25 | + | |
26 | + | |
27 | +def test_if_kids(): | |
28 | + verifyKids(If(test=Dummy('1/3'), body=Dummy('2/3'), orelse=Dummy('3/3'))) | |
29 | + verifyKids(If(test=Dummy('1/2'), body=Dummy('2/2'))) | |
30 | + |
@@ -1,57 +0,0 @@ | ||
1 | -# (C) Albert Mietus, 2024. Part of Castle/CCastle project | |
2 | - | |
3 | -import logging; logger = logging.getLogger(__name__) | |
4 | -import pytest | |
5 | -import typing as PTH # Python TypeHints | |
6 | - | |
7 | -from castle.aigr import AIGR | |
8 | -from castle.aigr import If | |
9 | - | |
10 | -from dataclasses import dataclass | |
11 | - | |
12 | - | |
13 | -@dataclass | |
14 | -class Dummy(AIGR): | |
15 | - mark: PTH.Any | |
16 | - | |
17 | - def __repr__(self): | |
18 | - return f'<Dummy.{self.mark}>' | |
19 | - | |
20 | - | |
21 | -def verifyMark(dummy, mark): | |
22 | - if mark is None: | |
23 | - assert dummy is None | |
24 | - else: | |
25 | - assert isinstance(dummy, Dummy) | |
26 | - assert dummy.mark == mark, f"Expecting mark: {mark}, but got {dummy.mark}" | |
27 | - | |
28 | -def test_1a_if3(): | |
29 | - s = If(test=Dummy('if-test'),body=Dummy('then-body'), orelse=Dummy('else')) | |
30 | - verifyMark(s.test, 'if-test') | |
31 | - verifyMark(s.body, 'then-body') | |
32 | - verifyMark(s.orelse,'else', ) | |
33 | - | |
34 | - | |
35 | -def test_1a_if2(): | |
36 | - s = If(test=Dummy('if-test'),body=Dummy('then-body')) | |
37 | - verifyMark(s.test,'if-test') | |
38 | - verifyMark(s.body, 'then-body') | |
39 | - verifyMark(s.orelse, None) | |
40 | - | |
41 | -def test_2_if_missing(): | |
42 | - with pytest.raises(TypeError): If(test=Dummy('WRONG')) | |
43 | - with pytest.raises(TypeError): If(body=Dummy('WRONG')) | |
44 | - with pytest.raises(TypeError): If() | |
45 | - | |
46 | -def verifyKids(s): | |
47 | - logger.debug("verifyKids: statements: %s", s) | |
48 | - unique = Dummy('unique') | |
49 | - for k in s._kids: | |
50 | - logger.debug("verifyKid getattr(s,%s,unique) (%s) != unique (%s) %s", | |
51 | - k, getattr(s,k,unique), unique, getattr(s,k,unique)!=unique) | |
52 | - assert unique != getattr(s,k, unique), f"Kid `{k}` should exist in {s}, but doesn't" | |
53 | - | |
54 | -def test_if_kids(): | |
55 | - verifyKids(If(test=Dummy('1/3'), body=Dummy('2/3'), orelse=Dummy('3/3'))) | |
56 | - verifyKids(If(test=Dummy('1/2'), body=Dummy('2/2'))) | |
57 | - |