Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
修订版 | 80e84a4d4dce9b2f1eb6f508be8882d341445552 (tree) |
---|---|
时间 | 2023-09-23 19:18:07 |
作者 | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
More aigr groundwork
@@ -9,9 +9,10 @@ | ||
9 | 9 | # TODO: Some test that are needed soon |
10 | 10 | rPY_LAST = \ |
11 | 11 | pytst/writers/RPy/test_0_templating.py \ |
12 | + pytst/aigr/test_0_AIGR.py \ | |
12 | 13 | # |
13 | 14 | rPY_CURRENT = \ |
14 | - pytst/aigr/test_0_AIGR.py \ | |
15 | + pytst/aigr/test_1_events.py \ | |
15 | 16 | # |
16 | 17 | CC2CPy_TODO = \ |
17 | 18 | pytst/writers/RPy/test_999.py \ |
@@ -8,4 +8,5 @@ | ||
8 | 8 | return super().__new__(cls) |
9 | 9 | |
10 | 10 | |
11 | - | |
11 | +from .events import * | |
12 | +from .protocols import * |
@@ -6,23 +6,24 @@ | ||
6 | 6 | - better |
7 | 7 | TODO: update the CC2Cpy parts to use this generic AIGR layer |
8 | 8 | """ |
9 | + | |
10 | +import typing as PTH # Python TypeHints | |
9 | 11 | from dataclasses import dataclass, KW_ONLY |
10 | 12 | from . import AIGR |
11 | -from .types import TypedParameter | |
13 | +from .types import TypedParameter # Castle/AIGR types | |
12 | 14 | |
13 | 15 | __all__ = ['Event'] |
14 | 16 | |
15 | -from dataclasses import dataclass, KW_ONLY | |
16 | 17 | |
17 | -from castle.auxiliary import AIGR | |
18 | 18 | |
19 | -@dataclass # pragma: no mutate | |
19 | + | |
20 | +@dataclass # pragma: no mutate | |
20 | 21 | class Event(AIGR): |
21 | 22 | """An event is like a (remote) function-call |
22 | 23 | |
23 | - It has a name, a return-type (can be void), and a sequence of typed parameters.""" | |
24 | + It has a name, a return-type (can be void), and an (inmutable) sequence of typed parameters.""" | |
24 | 25 | |
25 | 26 | name: str |
26 | - _: KW_ONLY # The field below must be passed as keywords, when initialising | |
27 | + _: KW_ONLY | |
27 | 28 | return_type: type=None |
28 | - typedParameters: Sequence[TypedParameter]=() ## A tuple `()` is inmutable | |
29 | + typedParameters: PTH.Sequence[TypedParameter]=() |
@@ -7,8 +7,10 @@ | ||
7 | 7 | TODO: update the CC2Cpy parts to use this generic AIGR layer |
8 | 8 | """ |
9 | 9 | |
10 | +import typing as PTH # Python TypeHints | |
10 | 11 | from enum import Enum |
11 | 12 | from dataclasses import dataclass, KW_ONLY |
13 | +from dataclasses import field as dc_field | |
12 | 14 | from . import AIGR |
13 | 15 | from .events import Event |
14 | 16 |
@@ -27,18 +29,18 @@ | ||
27 | 29 | Stream = 3 |
28 | 30 | |
29 | 31 | |
30 | -Protocol: TypeAlias = 'Protocol' # forward reference # pragma: no mutate | |
32 | +Protocol: PTH.TypeAlias = 'Protocol' # forward reference # pragma: no mutate | |
31 | 33 | @dataclass |
32 | 34 | class Protocol(AIGR): |
33 | 35 | """ .. note:: Use one of the subclasses -- Only Event is defined yet |
34 | 36 | .. todo:: Design: What is the `kind` self and the inherited ones are not the same? |
35 | 37 | overriding ProtocolKind.Unknown is always allowed |
36 | 38 | """ |
37 | - _BASE: ClassVar=None # pragma: no mutate | |
39 | + _BASE: PTH.ClassVar=None # pragma: no mutate | |
38 | 40 | |
39 | 41 | name: str |
40 | 42 | kind: ProtocolKind |
41 | - based_on: Optional[Protocol]=dc_field(default_factory= lambda :Protocol._BASE) | |
43 | + based_on: PTH.Optional[Protocol]=dc_field(default_factory= lambda :Protocol._BASE) | |
42 | 44 | |
43 | 45 | |
44 | 46 | @dataclass |
@@ -62,5 +64,5 @@ | ||
62 | 64 | """ |
63 | 65 | _: KW_ONLY |
64 | 66 | kind: ProtocolKind = ProtocolKind.Event |
65 | - events: Sequence[Event] | |
67 | + events: PTH.Sequence[Event] | |
66 | 68 |
@@ -0,0 +1,24 @@ | ||
1 | +# (C) Albert Mietus, 2023. Part of Castle/CCastle project | |
2 | + | |
3 | +""" This file is based (a fork) on `../writers/CC2Cpy/Protocol.py`, | |
4 | + - the rendering part is removed | |
5 | + - the prefixed are gone | |
6 | + - better | |
7 | +TODO: update the CC2Cpy parts to use this generic AIGR layer | |
8 | +""" | |
9 | + | |
10 | +import typing as PTH # Python TypeHints | |
11 | +from enum import Enum | |
12 | +from dataclasses import dataclass, KW_ONLY | |
13 | +from . import AIGR | |
14 | + | |
15 | +# XXX__all__ = [ | |
16 | +@dataclass | |
17 | +class TypedParameter(AIGR): | |
18 | + """This is many a helper class/struct to combine a parameter: a name and an type""" | |
19 | + name: str | |
20 | + type: type | |
21 | + | |
22 | + | |
23 | +TypedParameterTuple: PTH.TypeAlias = PTH.Sequence[TypedParameter] | |
24 | + |
@@ -0,0 +1,22 @@ | ||
1 | +# (C) Albert Mietus, 2022, 2023. Part of Castle/CCastle project | |
2 | + | |
3 | + | |
4 | +from castle.aigr import Event | |
5 | +from castle.aigr.types import TypedParameter | |
6 | + | |
7 | +def test_0_Event_empty(): | |
8 | + e = Event("leeg_event") | |
9 | + assert e.name == 'leeg_event' | |
10 | + assert e.return_type is None | |
11 | + assert len(e.typedParameters) == 0 | |
12 | + | |
13 | +def test_1_Event_small(): | |
14 | + e = Event("demo_int", typedParameters=[TypedParameter(name='p1', type=float )]) | |
15 | + assert e.return_type is None | |
16 | + assert len(e.typedParameters) == 1 | |
17 | + assert e.typedParameters[0].name == 'p1' | |
18 | + assert e.typedParameters[0].type == float | |
19 | + | |
20 | +def test_2_Event_retunInt(): | |
21 | + e = Event("an_event", return_type=int) | |
22 | + assert e.return_type == int |