Skip to content

Commit

Permalink
Add more tests, add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tvorogme committed Jul 11, 2023
1 parent 319eda4 commit 3feda0d
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 10 deletions.
9 changes: 6 additions & 3 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ What is supported?
- `CellSlice`_
- `CellBuilder`_
- `VmDict`_
- `TLB`_

- TVM
- Raw TVM
- Transaction Emulator

- TLB
- Python codegen
- `Python codegen`_


.. _Cell: https://tonpy.dton.io/tonpy.types.html#module-tonpy.types.cell
.. _CellSlice: https://tonpy.dton.io/tonpy.types.html#module-tonpy.types.cellslice
.. _CellBuilder: https://tonpy.dton.io/tonpy.types.html#module-tonpy.types.cellbuilder
.. _VmDict: https://tonpy.dton.io/tonpy.types.html#module-tonpy.types.vmdict

.. _TLB:
.. _Python codegen:
|
.. _check tests examples out: https://github.com/disintar/tonpy/tree/main/src/tonpy/tests
Expand All @@ -49,7 +51,8 @@ What is supported?
:caption: Contents:

installation
tonpy
tonpy.types
tonpy.tlb_gen


.. role:: raw-html(raw)
Expand Down
13 changes: 13 additions & 0 deletions docs/source/tonpy.tlb_gen.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tonpy.tlb_gen package
===================


tonpy.tlb_gen.py module
-----------------------

.. automodule:: tonpy.tlb_gen.py
:members:
:undoc-members:
:show-inheritance:


10 changes: 4 additions & 6 deletions docs/source/tonpy.types.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
tonpy.types package
===================

Submodules
----------

tonpy.types.cell module
-----------------------
Expand All @@ -29,17 +27,17 @@ tonpy.types.cellslice module
:show-inheritance:

tonpy.types.vmdict module
----------------------------
-------------------------

.. automodule:: tonpy.types.vmdict
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------
tonpy.types.tlb module
----------------------

.. automodule:: tonpy.types
.. automodule:: tonpy.types.tlb
:members:
:undoc-members:
:show-inheritance:
130 changes: 130 additions & 0 deletions src/tonpy/tests/test_tlb.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,133 @@ def test_tag_multi_int_large_zfilled():
exec(f"assert A_record.get_tag(instance) == A_record.Tag.a{i}", globals(), locals())


def test_tag_with_aug():
# language=tl-b
tlb_text = """
test$001 {x:#} a:(## x) = A x;
test1$000 a:# = A 1;
test2$010 a:# = A 2;
test3$110 a:# = A 3;
test4$111 a:# = A 4;
test5$100 a:# = A 5;
test$001 {x:#} {y:#} a:(## x) b:(## y) = B x y;
test1$000 {y:#} a:# b:(## y) = B 1 y;
test2$010 {x:#} a:# b:(## x) = B x 2;
test3$110 a:# = B 1 1;
test4$111 a:# = B 2 2;
test5$100 a:# = B 3 3;
test$001 {x:#} {y:#} {z:#} a:(## x) b:(## y) c:(## z) = C x y z;
test1$000 {y:#} {z:#} a:# b:(## y) c:(## z) = C 1 y z;
test2$010 {x:#} {z:#} a:# b:(## x) c:(## z)= C x 2 z;
test3$110 {z:#} a:# b:# c:(## z) = C 1 1 z;
test4$111 a:# = C 2 2 2;
test5$100 a:# = C 3 3 3;
test$001 {x:#} {y:#} {z:#} {h:#} a:(## x) b:(## y) c:(## z) d:(## h) = D x y z h;
test$001 {x:#} {y:#} {z:#} {h:#} {i:#} a:(## x) b:(## y) c:(## z) d:(## h) e:(## i) = E x y z h i;
test$001 {x:#} a:(## x) = F ~a x;
test1$011 a:(## 32) = F ~a 0;
"""
add_tlb(tlb_text, globals())

A_record = A(0) # noqa
B_record = B(0, 0) # noqa
C_record = C(0, 0, 0) # noqa
D_record = D(0, 0, 0, 0) # noqa
E_record = E(0, 0, 0, 0, 0) # noqa
F_record = F(0) # noqa

assert A_record.get_tag(CellBuilder().store_bitstring("001").begin_parse()) == A_record.Tag.test
assert A_record.get_tag(CellBuilder().store_bitstring("000").begin_parse()) == A_record.Tag.test1
assert A_record.get_tag(CellBuilder().store_bitstring("010").begin_parse()) == A_record.Tag.test2
assert A_record.get_tag(CellBuilder().store_bitstring("110").begin_parse()) == A_record.Tag.test3
assert A_record.get_tag(CellBuilder().store_bitstring("111").begin_parse()) == A_record.Tag.test4
assert A_record.get_tag(CellBuilder().store_bitstring("100").begin_parse()) == A_record.Tag.test5

assert B_record.get_tag(CellBuilder().store_bitstring("001").begin_parse()) == B_record.Tag.test
assert B_record.get_tag(CellBuilder().store_bitstring("000").begin_parse()) == B_record.Tag.test1
assert B_record.get_tag(CellBuilder().store_bitstring("010").begin_parse()) == B_record.Tag.test2
assert B_record.get_tag(CellBuilder().store_bitstring("110").begin_parse()) == B_record.Tag.test3
assert B_record.get_tag(CellBuilder().store_bitstring("111").begin_parse()) == B_record.Tag.test4
assert B_record.get_tag(CellBuilder().store_bitstring("100").begin_parse()) == B_record.Tag.test5

assert C_record.get_tag(CellBuilder().store_bitstring("001").begin_parse()) == C_record.Tag.test
assert C_record.get_tag(CellBuilder().store_bitstring("000").begin_parse()) == C_record.Tag.test1
assert C_record.get_tag(CellBuilder().store_bitstring("010").begin_parse()) == C_record.Tag.test2
assert C_record.get_tag(CellBuilder().store_bitstring("110").begin_parse()) == C_record.Tag.test3
assert C_record.get_tag(CellBuilder().store_bitstring("111").begin_parse()) == C_record.Tag.test4
assert C_record.get_tag(CellBuilder().store_bitstring("100").begin_parse()) == C_record.Tag.test5

assert D_record.get_tag(CellBuilder().store_bitstring("001").begin_parse()) == D_record.Tag.test
assert E_record.get_tag(CellBuilder().store_bitstring("001").begin_parse()) == E_record.Tag.test

assert F_record.get_tag(CellBuilder().store_bitstring("001").begin_parse()) == F_record.Tag.test
assert F_record.get_tag(CellBuilder().store_bitstring("011").begin_parse()) == F_record.Tag.test1


def test_enum():
# language=tl-b
tlb_text = """
test$_ = A; // simple Enum
test$01 = B;
a$0 = C;
b$1 = C;
a$0 = D;
c$101 = D;
b$11 = D;
"""
add_tlb(tlb_text, globals())

A_record = A()
B_record = B()
C_record = C()
D_record = D()

assert A_record.fetch_enum(CellBuilder().begin_parse()) == 0

cb = CellBuilder()
A_record.store_enum_from(cb) # While enum is $_ it'll do nothing
assert A_record.fetch_enum(cb.begin_parse()) == cb.bits == 0

assert B_record.fetch_enum(CellBuilder().store_bitstring('01').begin_parse()) == int('01', 2)

cb = CellBuilder()
B_record.store_enum_from(cb) # While enum is const it'll always save const
assert B_record.fetch_enum(cb.begin_parse()) == int('01', 2)

assert C_record.fetch_enum(CellBuilder().store_bitstring('0').begin_parse()) == int('0', 2)

cb = CellBuilder()
C_record.store_enum_from(cb, 0)
assert C_record.fetch_enum(cb.begin_parse()) == int('0', 2)

assert C_record.fetch_enum(CellBuilder().store_bitstring('1').begin_parse()) == int('1', 2)

cb = CellBuilder()
C_record.store_enum_from(cb, 1)
assert C_record.fetch_enum(cb.begin_parse()) == int('1', 2)

assert D_record.fetch_enum(CellBuilder().store_bitstring('0').begin_parse()) == int('0', 2)

cb = CellBuilder()
D_record.store_enum_from(cb, 0)
assert D_record.fetch_enum(cb.begin_parse()) == D_record.cons_tag[0] == int('0', 2)

assert D_record.fetch_enum(CellBuilder().store_bitstring('11').begin_parse()) == int('11', 2)

cb = CellBuilder()
D_record.store_enum_from(cb, 2)
assert D_record.fetch_enum(cb.begin_parse()) == D_record.cons_tag[2] == int('11', 2)

assert D_record.fetch_enum(CellBuilder().store_bitstring('101').begin_parse()) == int('101', 2)

cb = CellBuilder()
D_record.store_enum_from(cb, 1)
assert D_record.fetch_enum(cb.begin_parse()) == D_record.cons_tag[1] == int('101', 2)
63 changes: 62 additions & 1 deletion src/tonpy/types/tlb.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,69 @@
from collections import defaultdict
from enum import Enum
from typing import Optional

from tonpy.types import CellSlice, CellBuilder


class TLB(object):
pass
class Tag(Enum):
"""
Contractor tags enums stored as lexicographic order
.. code-block::
a$0 = A;
b$10 = A;
c$1 = A;
Means:
.. code-block:: Python
class Tag(Enum):
a = 0
b = 1
c = 2
cons_len = [1, 2, 1]
cons_tag = [0, 2, 1]
"""

raise NotImplemented

cons_len = None
cons_tag = None

def get_tag(self, cs: CellSlice) -> Optional["TLB.Tag"]:
"""
Fetch tag from CellSlice ``cs`` and return ``TLB.Tag`` enum
:param cs: CellSlice to fetch tag from
:return: ``TLB.Tag`` enum
"""
raise NotImplemented

def fetch_enum(self, cs: CellSlice) -> int:
"""
Fetch enum tag value from ``CellSlice`` of type ``TLB``
:param cs: CellSlice to fetch enum tag value from
:return: Enum tag value of type ``TLB`` store in ``cs: CellSlice``
"""

raise NotImplementedError

def store_enum_from(self, cb: CellBuilder, value: int = None) -> bool:
"""
Store enum from ``self.cons_tag`` on position ``value`` (if ``TLB.Tag`` not constant) to `cb: CellBuilder`
If ``self.const_tag`` is exact (tags are matched positions in lexicographic order) then will store ``value``
If ``value is None`` and ``TLB.Tag is constant`` will store constant ``TLB.Tag`` else will raise an error
:param cb: CellBuilder to store enum to
:param value: Value or enum position to store enum from
:return: True
"""
raise NotImplementedError


class TLBComplex(object):
Expand Down

0 comments on commit 3feda0d

Please sign in to comment.