• R/O
  • SSH

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2


Commit MetaInfo

Revisionfb3240ad99ab8cc046e3393637ed4d0dd1ef7dde (tree)
Zeit2023-10-05 05:31:58
AutorAlbert Mietus < albert AT mietus DOT nl >
CommiterAlbert Mietus < albert AT mietus DOT nl >

Log Message

Added ProtocolWrapper (and to many small txt changed)

Ändern Zusammenfassung

Diff

diff -r a1693dfcc1d9 -r fb3240ad99ab Makefile
--- a/Makefile Wed Oct 04 16:44:53 2023 +0200
+++ b/Makefile Wed Oct 04 22:31:58 2023 +0200
@@ -16,11 +16,11 @@
1616 pytst/aigr/test_2b_protocol.py \
1717 pytst/writers/RPy/test_0_templating.py \
1818 pytst/writers/RPy/test_1_EventIndexes.py \
19+ pytst/writers/RPy/test_2_ProtocolDataStructures.py \
20+ pytst/writers/RPy/test_99_SieveMoats.py \
1921 #
2022 rPY_CURRENT = \
21- pytst/writers/RPy/test_2_ProtocolDataStructures.py \
22- pytst/writers/RPy/test_99_SieveMoats.py \
23- pytst/aigr/test_2b_protocol.py \
23+ pytst/aigr/test_2c_GenericProtocols.py \
2424 #
2525 CC2CPy_TODO = \
2626 pytst/writers/RPy/test_999.py \
diff -r a1693dfcc1d9 -r fb3240ad99ab TestDoubles/AIGR/protocols/Sieve.py
--- a/TestDoubles/AIGR/protocols/Sieve.py Wed Oct 04 16:44:53 2023 +0200
+++ b/TestDoubles/AIGR/protocols/Sieve.py Wed Oct 04 22:31:58 2023 +0200
@@ -1,6 +1,6 @@
11 # (C) Albert Mietus, 2023. Part of Castle/CCastle project
22
3-from castle.aigr.types import TypedParameter
3+from castle.aigr.aid import TypedParameter
44 from castle.aigr import EventProtocol, Event
55
66 import pytest
diff -r a1693dfcc1d9 -r fb3240ad99ab TestDoubles/AIGR/protocols/__init__.py
--- a/TestDoubles/AIGR/protocols/__init__.py Wed Oct 04 16:44:53 2023 +0200
+++ b/TestDoubles/AIGR/protocols/__init__.py Wed Oct 04 22:31:58 2023 +0200
@@ -1,6 +1,6 @@
11 # (C) Albert Mietus, 2023. Part of Castle/CCastle project
22
3-from castle.aigr.types import TypedParameter
3+from castle.aigr.aid import TypedParameter
44 from castle.aigr import EventProtocol, Event
55
66 from .Sieve import *
diff -r a1693dfcc1d9 -r fb3240ad99ab castle/aigr/aid.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/castle/aigr/aid.py Wed Oct 04 22:31:58 2023 +0200
@@ -0,0 +1,36 @@
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+
17+@dataclass
18+class TypedParameter(AIGR):
19+ """This is many a helper class/struct to combine a parameter: a name and an type"""
20+ name: str
21+ type: type
22+
23+@dataclass
24+class Argument(AIGR):
25+ """This is many a helper class/struct to combine a argument: a value and optional a name"""
26+ value: PTH.Any
27+ _: KW_ONLY
28+ name: PTH.Optional[str]=None
29+
30+#@dataclass
31+#class Invoke(AIGR):
32+# """call a callable: a callable and a list of Arguments"""
33+# _: KW_ONLY
34+# callable: PTH.Any # XXX
35+# arguments: PTH.Sequence[Argument]=()
36+
diff -r a1693dfcc1d9 -r fb3240ad99ab castle/aigr/events.py
--- a/castle/aigr/events.py Wed Oct 04 16:44:53 2023 +0200
+++ b/castle/aigr/events.py Wed Oct 04 22:31:58 2023 +0200
@@ -7,17 +7,17 @@
77 TODO: update the CC2Cpy parts to use this generic AIGR layer
88 """
99
10-import typing as PTH # Python TypeHints
10+import typing as PTH # Python TypeHints
1111 from dataclasses import dataclass, KW_ONLY
1212 from . import AIGR
13-from .types import TypedParameter # Castle/AIGR types
13+from .aid import TypedParameter # Castle/AIGR type
1414
1515 __all__ = ['Event']
1616
1717
1818
1919
20-@dataclass # pragma: no mutate
20+@dataclass # pragma: no mutate
2121 class Event(AIGR):
2222 """An event is like a (remote) function-call
2323
diff -r a1693dfcc1d9 -r fb3240ad99ab castle/aigr/protocols.py
--- a/castle/aigr/protocols.py Wed Oct 04 16:44:53 2023 +0200
+++ b/castle/aigr/protocols.py Wed Oct 04 22:31:58 2023 +0200
@@ -8,13 +8,13 @@
88 """
99
1010 from __future__ import annotations
11-import typing as PTH # Python TypeHints
11+import typing as PTH # Python TypeHints
1212 from enum import Enum
1313 from dataclasses import dataclass, KW_ONLY
1414 from dataclasses import field as dc_field
1515 from . import AIGR
1616 from .events import Event
17-from .types import TypedParameter # Castle/AIGR types
17+from .aid import TypedParameter # Castle/AIGR types
1818
1919 __all__ = ['ProtocolKind', 'Protocol', 'EventProtocol']
2020 # DataProtocol, StreamProtocol are added eventually
@@ -30,14 +30,13 @@
3030 Data = 2
3131 Stream = 3
3232
33-
3433 @dataclass
3534 class Protocol(AIGR):
3635 """ .. note:: Use one of the subclasses -- Only Event is defined yet
3736 .. todo:: Design: What is the `kind` self and the inherited ones are not the same?
3837 overriding ProtocolKind.Unknown is always allowed
3938 """
40- _BASE: PTH.ClassVar=None # pragma: no mutate
39+ _BASE: PTH.ClassVar=None # pragma: no mutate
4140
4241 name: str
4342 kind: ProtocolKind
@@ -45,20 +44,32 @@
4544 typedParameters: PTH.Optional[PTH.Sequence[TypedParameter]]=()
4645
4746
48-@dataclass # pragma: no mutate
47+@dataclass # pragma: no mutate
4948 class _RootProtocol(Protocol):
5049 """This is the base protocol; it exist as we can't instantiate Protocol"""
5150
52-baseProtocol = _RootProtocol("Protocol", kind=ProtocolKind.Unknown, based_on=None) # pragma: no mutate
51+baseProtocol = _RootProtocol("Protocol", kind=ProtocolKind.Unknown, based_on=None) # pragma: no mutate
5352 Protocol._BASE=baseProtocol
5453
55-@dataclass # pragma: no mutate
54+@dataclass # pragma: no mutate
5655 class DataProtocol(Protocol): pass ### XXX ToDo (not exported)
57-@dataclass # pragma: no mutate
56+@dataclass # pragma: no mutate
5857 class StreamProtocol(Protocol): pass ### XXX ToDo (not exported)
5958
59+@dataclass # pragma: no mutate
60+class ProtocolWrapper(Protocol):
61+ name: str=""
62+ kind : ProtocolKind=None
63+ _: KW_ONLY
64+ arguments: PTH.Sequence[Argument]=()
6065
61-@dataclass # pragma: no mutate
66+ def __post_init__(self):
67+ if not self.kind:
68+ self.kind = self.based_on.kind
69+ if self.name == "":
70+ self.name = f"Wrapper for {self.based_on.name}({self.arguments})"
71+
72+@dataclass # pragma: no mutate
6273 class EventProtocol(Protocol):
6374 """An event-based protocol is basically a set of events.
6475
diff -r a1693dfcc1d9 -r fb3240ad99ab castle/aigr/types.py
--- a/castle/aigr/types.py Wed Oct 04 16:44:53 2023 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
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-#not needed/used? TypedParameterTuple: PTH.TypeAlias = PTH.Sequence[TypedParameter]
24-
diff -r a1693dfcc1d9 -r fb3240ad99ab pytst/aigr/test_1_events.py
--- a/pytst/aigr/test_1_events.py Wed Oct 04 16:44:53 2023 +0200
+++ b/pytst/aigr/test_1_events.py Wed Oct 04 22:31:58 2023 +0200
@@ -1,7 +1,7 @@
11 # (C) Albert Mietus, 2023. Part of Castle/CCastle project
22
33 from castle.aigr import Event
4-from castle.aigr.types import TypedParameter
4+from castle.aigr.aid import TypedParameter
55
66
77 def test_0_Event_empty():
diff -r a1693dfcc1d9 -r fb3240ad99ab pytst/aigr/test_2b_protocol.py
--- a/pytst/aigr/test_2b_protocol.py Wed Oct 04 16:44:53 2023 +0200
+++ b/pytst/aigr/test_2b_protocol.py Wed Oct 04 22:31:58 2023 +0200
@@ -5,7 +5,8 @@
55
66 from castle.aigr import Protocol, ProtocolKind
77 from castle.aigr import Event, EventProtocol
8-from castle.aigr.types import TypedParameter
8+from castle.aigr.aid import TypedParameter
9+
910
1011 @pytest.fixture
1112 def emptyProtocol():
@@ -58,6 +59,7 @@
5859 def test_protocol_with_Noparms(emptyProtocol):
5960 assert emptyProtocol.typedParameters == ()
6061
62+
6163 def test_protocol_with_aParm():
6264 e = EventProtocol("With_a_parm", events=[], based_on=None,
6365 typedParameters=[TypedParameter(name='p', type=float)])
@@ -65,6 +67,7 @@
6567 assert e.typedParameters[0].name == 'p'
6668 assert e.typedParameters[0].type == float
6769
70+
6871 def test_protocol_with_4Parms():
6972 e = EventProtocol("With_4_Parms", events=[], based_on=None,
7073 typedParameters=(
@@ -80,6 +83,5 @@
8083 assert (e.typedParameters[3].name, e.typedParameters[3].type) == ('p3', None)
8184
8285
83-@pytest.mark.xfail(reason="Implementation is needed (test & prod)")
84-def test_inherit_base_withParms():
85- assert False
86+#Note: for more complicated cases, see :file:`test_2c_WrappedProtocols.py`
87+
diff -r a1693dfcc1d9 -r fb3240ad99ab pytst/aigr/test_2c_GenericProtocols.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pytst/aigr/test_2c_GenericProtocols.py Wed Oct 04 22:31:58 2023 +0200
@@ -0,0 +1,90 @@
1+# (C) Albert Mietus, 2023. Part of Castle/CCastle project
2+
3+""" With :file:`test_2b_protocol.py` most/all normal cases are verified. This file focus on **GenericProtocols**:
4+ GenericProtocols are protocols that inherited from a protocol with parameters. We focus on EventProtocols, but it
5+ will be entende to all protocols.
6+
7+ .. code-block: Castle:
8+
9+ protocol SlowStart(queue_max:int): EventProtocol ...
10+ protocol SimpleSieve : SlowStart(1) ...
11+
12+ * `SlowStart` is a **Generic Protocol** with a (1) parameter: the (initial) (max) size of the queue.
13+ * `SimpleSieve` used that protocol, and set that parameter to 1.
14+
15+ The parameter to SlowStart, in SimpleSieve, can be seen as a template-specialisation as it is called in C++
16+
17+ .. code-block: C++
18+
19+ // A C++ Template approximation of above: using classes not protocols ...
20+ template <int queue_max>
21+ class SlowStart {...}
22+
23+ class SimpleSieve : SlowStart<1> { ...}
24+
25+ In both examples the value ``1`` is filled in (aka hard-coded) into the implementation of SlowStart: Wherever
26+ `queue_max` is used the value `1` is used -- as if the source always has had a `1`....
27+
28+ .. note:: The Castle syntax uses parentheses, as for normal (formal) arguments, not chevrons <angle brackets> as in C++
29+
30+ It read as-if SlowStart is instantiated (as in Python: calling ``SlowStart()``), but actually it is
31+ **specialised*, by filling in the template/parentheses. The result is a not an instance of SlowStart, but a "new
32+ Protocol, which kind-of inherits from SlowStart -- and SimpleSieve inherits from that one.
33+
34+ This *syntax detail* is handled in the parser!
35+
36+ In the AIGR, the specialised *SlowStart(1)* protocol is modeled by a ProtocolWrapper; which in placed in-between
37+ (the generic) Slowstart and SimpleSieve. """
38+
39+import logging; logger = logging.getLogger(__name__)
40+import pytest
41+
42+from castle.aigr import Protocol, ProtocolKind
43+from castle.aigr import Event, EventProtocol
44+from castle.aigr.aid import TypedParameter, Argument
45+from castle.aigr.protocols import ProtocolWrapper
46+
47+""" There are a few cases
48+///CastleCode
49+ protocol Base(queue_max:int): EventProtocol
50+ protocol Sub_a: Base(queue_max=1) # (a): named argument
51+ protocol Sub_b: Base(1) # (b): positional arg
52+
53+"""
54+
55+
56+@pytest.fixture
57+def base():
58+ return EventProtocol("Base", events=[], typedParameters=[TypedParameter(name='queue_max', type=int)])
59+
60+@pytest.fixture
61+def sub_a(base):
62+ return EventProtocol("Sub_a", events=[], based_on=ProtocolWrapper(based_on=base, arguments=(Argument(name='queue_max', value=1),)))
63+
64+@pytest.fixture
65+def sub_b(base):
66+ return EventProtocol("Sub_b", events=[], based_on=ProtocolWrapper(based_on=base, arguments=(Argument(value=1),)))
67+
68+def assert_GP_kind(base, sub):
69+ assert sub.kind == base.kind
70+ assert sub.based_on.kind == base.kind
71+ assert sub.based_on.based_on is base
72+
73+
74+def test_GenericProtocol_kind_a(base, sub_a):
75+ assert_GP_kind(base, sub_a)
76+
77+def test_GenericProtocol_kind_a(base, sub_b):
78+ assert_GP_kind(base, sub_b)
79+
80+
81+def assert_GP_name(base, sub):
82+ assert "Wrapper" in sub.based_on.name
83+ assert "Base" in sub.based_on.name
84+
85+def test_GenericProtocol_name(base, sub_a):
86+ assert_GP_name(base, sub_a)
87+ assert "queue_max" in sub.based_on.name
88+
89+def test_GenericProtocol_name(base, sub_b):
90+ assert_GP_name(base, sub_b)
diff -r a1693dfcc1d9 -r fb3240ad99ab pytst/writers/RPy/test_1_EventIndexes.py
--- a/pytst/writers/RPy/test_1_EventIndexes.py Wed Oct 04 16:44:53 2023 +0200
+++ b/pytst/writers/RPy/test_1_EventIndexes.py Wed Oct 04 22:31:58 2023 +0200
@@ -3,7 +3,7 @@
33 import logging; logger = logging.getLogger(__name__)
44 import pytest
55
6-from castle.aigr.types import TypedParameter
6+from castle.aigr.aid import TypedParameter
77
88 from castle.aigr import EventProtocol, Event
99 from . import T_EventIndexes
diff -r a1693dfcc1d9 -r fb3240ad99ab pytst/writers/RPy/test_2_ProtocolDataStructures.py
--- a/pytst/writers/RPy/test_2_ProtocolDataStructures.py Wed Oct 04 16:44:53 2023 +0200
+++ b/pytst/writers/RPy/test_2_ProtocolDataStructures.py Wed Oct 04 22:31:58 2023 +0200
@@ -4,7 +4,7 @@
44 import pytest
55
66 from castle.aigr import EventProtocol, Event
7-from castle.aigr.types import TypedParameter
7+from castle.aigr.aid import TypedParameter
88
99 from . import T_ProtocolDataStructures
1010 from . import T_Protocol