Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
修订版 | 054f0dcf5959469d781367d262b1bdf57f8df916 (tree) |
---|---|
时间 | 2024-05-28 01:00:16 |
作者 | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
AIGR: added literals/Constant
@@ -16,14 +16,15 @@ | ||
16 | 16 | pytst/expressions/test_LRmore.py \ |
17 | 17 | pytst/expressions/test_RL.py \ |
18 | 18 | pytst/statements/test_body.py \ |
19 | -# | |
20 | -CURRENT = \ | |
21 | 19 | pytst/extra/test_mangle.py \ |
22 | 20 | pytst/statements/test_method.py \ |
23 | 21 | pytst/statements/test_components.py \ |
24 | 22 | pytst/statements/test_0_dataclass.py \ |
25 | 23 | pytst/statements/test_0_kids.py \ |
26 | 24 | # |
25 | +CURRENT = \ | |
26 | + pytst/expressions/test_literals.py \ | |
27 | +# | |
27 | 28 | TODO = \ |
28 | 29 | pytst/test_3_namespaces.py \ |
29 | 30 | pytst/machinery/test_9_todo.py \ |
@@ -8,3 +8,4 @@ | ||
8 | 8 | |
9 | 9 | from .operator_expressions import * |
10 | 10 | from .calls import * |
11 | +from .literals import * |
@@ -0,0 +1,26 @@ | ||
1 | +# (C) Albert Mietus, 2023-2024. Part of Castle/CCastle project | |
2 | + | |
3 | +from __future__ import annotations # Postponed evaluation of annotations | |
4 | +import logging; logger = logging.getLogger(__name__) | |
5 | + | |
6 | +import typing as PTH # Python TypeHints | |
7 | +from dataclasses import dataclass, KW_ONLY | |
8 | +from dataclasses import field as dc_field | |
9 | + | |
10 | +from .. import AIGR | |
11 | +from . import _expression | |
12 | + | |
13 | +class _literal(_expression):pass # _kids = _expression._kids | |
14 | + | |
15 | +@dataclass | |
16 | +class Constant(_literal): | |
17 | + """A (literal) Constant is a value that is given in code-text, like 0 (an int), 3.14 (a float) or "Hoi" (a string)""" | |
18 | + | |
19 | + _kids = _literal._kids + ('value', 'type') | |
20 | + | |
21 | + _: KW_ONLY | |
22 | + value : AIGR | |
23 | + type : PTH.Optional[type] = None | |
24 | + | |
25 | + | |
26 | + |
@@ -0,0 +1,23 @@ | ||
1 | +# (C) Albert Mietus, 2024. Part of Castle/CCastle project | |
2 | + | |
3 | +import logging; logger = logging.getLogger(__name__) | |
4 | + | |
5 | +import pytest | |
6 | + | |
7 | +from castle.aigr.expressions import literals | |
8 | + | |
9 | +def test_1_aConstant(): | |
10 | + e = literals.Constant(value=-1) | |
11 | + assert e.value == -1 | |
12 | + assert e.type == None | |
13 | + | |
14 | +def test_2_ConstantInt(): | |
15 | + e = literals.Constant(value=42, type=int) # XXX Really? **python-types?** | |
16 | + assert e.value == 42 | |
17 | + assert e.type == int | |
18 | + | |
19 | +def test_3_ConstantStr(): | |
20 | + e = literals.Constant(value="42", type=str) # XXX Really? **python-types?** | |
21 | + assert e.value == "42" | |
22 | + assert e.type == str | |
23 | + |