Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Révision | 67b41b41168be43083fc96d7c3548c1ae6dbd128 (tree) |
---|---|
l'heure | 2024-05-28 06:15:12 |
Auteur | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
TESTDOUBLES::sieveCastle:EH done
@@ -42,7 +42,7 @@ | ||
42 | 42 | aigr.VoidCall( |
43 | 43 | aigr.Call( |
44 | 44 | callable=aigr.Part( |
45 | - base=aigr.Call(callable=ID('super')), attribute=ID('init')), | |
45 | + base=aigr.Call(callable=ID('super')), attribute=ID('init', context=aigr.Ref())), | |
46 | 46 | arguments=())), |
47 | 47 | aigr.Become( |
48 | 48 | targets=(aigr.Part(base=ID('self'), attribute=ID('myPrime', context=aigr.Set())),), |
@@ -66,14 +66,20 @@ | ||
66 | 66 | port=ID('try', context=aigr.Ref()), |
67 | 67 | body=aigr.Body(statements=[ |
68 | 68 | aigr.If( |
69 | - test=aigr.Compare(ops=aigr.operators.NotEqual(), values=( | |
70 | - builders.Modulo( | |
71 | - ID("try", context=aigr.Ref()), | |
72 | - ID("myPrime",context=aigr.Ref())), | |
73 | - aigr.Constant(value=0))), | |
74 | - body=aigr.Body(statements=[ | |
75 | - "XXX" | |
76 | - ]))])) | |
69 | + test=aigr.Compare( | |
70 | + ops=aigr.operators.NotEqual(), | |
71 | + values=( | |
72 | + builders.Modulo( | |
73 | + ID("try", context=aigr.Ref()), | |
74 | + ID("myPrime",context=aigr.Ref())), | |
75 | + aigr.Constant(value=0),)), | |
76 | + body=aigr.Body( | |
77 | + statements=[ | |
78 | + aigr.machinery.sendEvent( | |
79 | + outport=aigr.Part(base=ID('self'), attribute=ID('coprime', context=aigr.Ref())), | |
80 | + event=ID('input',context=aigr.Ref()), | |
81 | + arguments=[aigr.Argument(ID('try', context=aigr.Ref()))]) | |
82 | + ]))])) | |
77 | 83 | |
78 | 84 | |
79 | 85 |
@@ -15,6 +15,14 @@ | ||
15 | 15 | from . import find_name_in_body |
16 | 16 | |
17 | 17 | |
18 | +def verify_ID(id, name, isRef=False, isDef=False, isSet=False): | |
19 | + assert isinstance(id, aigr.ID), f"Expected an ID, found {type(id)} for {id}" | |
20 | + assert id == name, f"wrong ID, expected {name}, got {id}" | |
21 | + if isRef: assert isinstance(id.context, aigr.Ref) | |
22 | + if isDef: assert isinstance(id.context, aigr.Def) | |
23 | + if isSet: assert isinstance(id.context, aigr.Set) | |
24 | + | |
25 | + | |
18 | 26 | @pytest.fixture |
19 | 27 | def comp(): |
20 | 28 | return sieveCastle.Sieve |
@@ -44,22 +52,48 @@ | ||
44 | 52 | def test_0c_noParms(comp): |
45 | 53 | assert comp.parameters == () |
46 | 54 | |
47 | -def test_1_init_has_2lines(comp): | |
55 | +def test_1a_init_has_2lines(comp): | |
48 | 56 | init = find_name_in_body('init', comp.body) |
49 | 57 | assert isinstance(init, aigr.Method), f"Expected an init method, got {init}" |
50 | 58 | assert len(init.body)==2, f"Expected that 'init' has 2 statements, but found: {len(init.body.statements)}" |
51 | 59 | |
52 | 60 | |
53 | -def verify_IDref(id, expected_name): | |
54 | - assert id == expected_name, f"ID does not match, expected {expected_name}, got {id}" | |
55 | - assert isinstance(id, aigr.ID), f"ID ({id}) is not an ID, but type:{type(id)}" | |
56 | - assert isinstance(id.context, aigr.Ref), f"found ID '{id}' has not ref" | |
57 | - # XXX ToDo: check the ref -- for now empty is fine | |
61 | +def test_1b_init_1st_line_superinit(comp): | |
62 | + """ CastleCode: super.init(); """ | |
63 | + init = find_name_in_body('init', comp.body) | |
64 | + line = init.body[0] | |
65 | + | |
66 | + assert isinstance(line, aigr.VoidCall) and isinstance(line.call, aigr.Call) | |
67 | + callable, arguments = line.call.callable, line.call.arguments | |
68 | + | |
69 | + assert isinstance(callable, aigr.Part) | |
70 | + assert isinstance(callable.base, aigr.Call) and callable.base.callable == "super" and callable.base.arguments is () | |
71 | + verify_ID(callable.attribute, "init", isRef=True) | |
72 | + assert callable.index is None | |
73 | + | |
74 | + assert arguments is (), f"Expected no arguments, but found: {arguments}" | |
75 | + | |
76 | + | |
77 | +def test_1c_init_2nd_line_become(comp): | |
78 | + """ CastleCode: .myPrime := onPrime; """ | |
79 | + | |
80 | + init = find_name_in_body('init', comp.body) | |
81 | + line = init.body[1] | |
82 | + | |
83 | + assert isinstance(line, aigr.Become) and len(line.targets)==1 and len(line.values)==1 | |
84 | + myPrime, onPrime = line.targets[0], line.values[0] | |
85 | + | |
86 | + assert isinstance(myPrime, aigr.Part) | |
87 | + verify_ID(myPrime.base, "self") | |
88 | + verify_ID(myPrime.attribute, "myPrime", isSet=True) | |
89 | + | |
90 | + verify_ID(onPrime, "onPrime", isRef=True) | |
91 | + | |
58 | 92 | |
59 | 93 | def test_2_handler_on_try(event_handler): |
60 | - verify_IDref(event_handler.protocol, "SimpleSieve") | |
61 | - verify_IDref(event_handler.event, "input") | |
62 | - verify_IDref(event_handler.port, "try") | |
94 | + verify_ID(event_handler.protocol, "SimpleSieve", isRef=True) | |
95 | + verify_ID(event_handler.event, "input", isRef=True) | |
96 | + verify_ID(event_handler.port, "try", isRef=True) | |
63 | 97 | |
64 | 98 | |
65 | 99 | def test_3a_EH_is_one_statement(event_handler): |
@@ -84,7 +118,7 @@ | ||
84 | 118 | |
85 | 119 | |
86 | 120 | def test_3c_EH_test_exps(event_handler): |
87 | - """ CastleCode: (try % .myPrime) !=0 """ | |
121 | + """ CastleCode: try % .myPrime) !=0 """ | |
88 | 122 | if_statement = event_handler.body[0] |
89 | 123 | test_expr = if_statement.test |
90 | 124 |
@@ -98,8 +132,8 @@ | ||
98 | 132 | assert len(lhs.values) == 2 |
99 | 133 | var_try, myPrime = lhs.values[0], lhs.values[1] |
100 | 134 | |
101 | - assert isinstance(var_try, aigr.ID) and var_try == 'try' and isinstance(var_try.context, aigr.Ref) | |
102 | - assert isinstance(myPrime, aigr.ID) and myPrime == 'myPrime' and isinstance(myPrime.context, aigr.Ref) | |
135 | + verify_ID(var_try, "try", isRef=True) | |
136 | + verify_ID(myPrime, "myPrime", isRef=True) | |
103 | 137 | |
104 | 138 | # The rhs is easy/simple |
105 | 139 | logger.debug("rhs: %s -- ``0``", rhs) |
@@ -108,10 +142,21 @@ | ||
108 | 142 | |
109 | 143 | |
110 | 144 | def test_3d_EH_then_send(event_handler): |
145 | + """ CastleCode: .coprime.input(try); """ | |
111 | 146 | if_statement=event_handler.body[0] |
112 | 147 | then = if_statement.body |
113 | 148 | assert len(then) == 1 # Not a test, only to check. |
114 | 149 | send = then[0] |
115 | 150 | |
116 | - assert False, "ToDo if-then" | |
151 | + assert isinstance(send, aigr.machinery.sendEvent) | |
117 | 152 | |
153 | + assert isinstance(send.outport, aigr.Part) | |
154 | + verify_ID(send.outport.base, "self") | |
155 | + verify_ID(send.outport.attribute, "coprime", isRef=True) | |
156 | + | |
157 | + verify_ID(send.event, "input", isRef=True) | |
158 | + | |
159 | + assert isinstance(send.arguments, (tuple, list)) and len(send.arguments) == 1 | |
160 | + assert isinstance(send.arguments[0], aigr.Argument) | |
161 | + verify_ID(send.arguments[0].value, "try", isRef=True) | |
162 | + |
@@ -13,3 +13,5 @@ | ||
13 | 13 | |
14 | 14 | from .statements import * |
15 | 15 | from .expressions import * |
16 | + | |
17 | +from . import machinery |
@@ -20,7 +20,7 @@ | ||
20 | 20 | @dataclass |
21 | 21 | class send_proto(machinery): |
22 | 22 | _: KW_ONLY |
23 | - outport : Port | |
23 | + outport : AIGR # ID | Parts| ... | |
24 | 24 | |
25 | 25 | @dataclass |
26 | 26 | class sendStream(send_proto, todo.mark_Dataclass): ... |
@@ -29,7 +29,8 @@ | ||
29 | 29 | |
30 | 30 | @dataclass |
31 | 31 | class sendEvent(send_proto): |
32 | - event: Event | |
32 | + _: KW_ONLY | |
33 | + event: AIGR # ID | Parts| ... | |
33 | 34 | arguments: PTH.Sequence[Argument] |
34 | 35 | |
35 | 36 | @dataclass |