Skip to content

Commit 72e0dfb

Browse files
Merge branch 'dev'
2 parents cd410cf + d85f652 commit 72e0dfb

52 files changed

Lines changed: 1807 additions & 148 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<img align="left" width="64" height="64" src="./assets/dbuilder-icon.png">
22

33
# dBuilder.py
4-
[![PyPI version](https://img.shields.io/badge/dbuilder-0.1.0-informational?style=flat-square&color=FFFF91)](https://pypi.org/project/dbuilder/)
4+
[![PyPI version](https://img.shields.io/badge/dbuilder-0.2.0-informational?style=flat-square&color=FFFF91)](https://pypi.org/project/dbuilder/)
55
[![Telegram](https://img.shields.io/badge/Telegram-@d__builder-informational?style=flat-square&color=0088cc)](https://t.me/d_builder)
66

77
dBuilder.py is smart contract development framework in Python for [TON (The Open Network)](https://ton.org). Its purpose is to make the development, testing, and deployment procedures much easier!

dbuilder/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from dbuilder.ast import *
2-
from dbuilder.func import *
32
from dbuilder.core import *
3+
from dbuilder.func import *
4+
from dbuilder.types import *

dbuilder/ast/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1+
from dbuilder.ast.ast import AST
2+
from dbuilder.ast.ast_patcher import patch
3+
from dbuilder.ast.calls import CallStacks
4+
from dbuilder.ast.compiled_contract import CompiledContract
15
from dbuilder.ast.printer import Printer
2-
from dbuilder.ast.node import Node
3-
from dbuilder.ast.statement import Statement
4-
from dbuilder.ast.expr import Expr
5-
from dbuilder.ast.method import Method
6-
from dbuilder.ast.contract import Contract
7-
from dbuilder.ast.control_flow import ControlFlow
8-
from dbuilder.ast.condition import IfFlow

dbuilder/ast/ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dbuilder.ast import Node
1+
from dbuilder.ast.types import Node
22

33

44
class AST:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ def visit_Assign(self, node):
115115
else:
116116
target = node.targets[0].id
117117
nodes = [node]
118+
if isinstance(node.value, ast.Constant):
119+
if isinstance(node.value.value, int):
120+
node.value = ast.Call(
121+
func=ast.Attribute(
122+
value=ast.Name(id="self", ctx=ast.Load()),
123+
attr="factory_",
124+
ctx=ast.Load(),
125+
),
126+
args=[ast.Constant("int"), node.value],
127+
keywords=[],
128+
)
129+
118130
c_expr = ast.Call(
119131
func=ast.Attribute(
120132
value=ast.Name(id=target, ctx=ast.Load()),
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
from dbuilder.ast import Contract, IfFlow, Method, Node, Statement
2-
from dbuilder.ast.loop import WhileLoop
1+
from dbuilder.ast.types import (
2+
AsmMethod,
3+
Contract,
4+
IfFlow,
5+
Method,
6+
Node,
7+
Statement,
8+
WhileLoop,
9+
)
310

411

512
class CallStacks(object):
@@ -35,6 +42,15 @@ def declare_method(name, args, annotations):
3542
m = Method(name, args, annotations)
3643
CallStacks.current_contract.add_method(m)
3744

45+
@staticmethod
46+
def declare_asm(name, args, annotations, asm_annoations):
47+
m = AsmMethod(name, args, annotations, asm_annoations)
48+
CallStacks.current_contract.add_method(m)
49+
50+
@staticmethod
51+
def add_raw_statement(raw):
52+
CallStacks.current_contract.add_statement(raw)
53+
3854
@staticmethod
3955
def add_statement(type, *args):
4056
s = Statement(type, args)
@@ -44,6 +60,7 @@ def add_statement(type, *args):
4460
@staticmethod
4561
def return_(entity):
4662
CallStacks.add_statement(Statement.RETURN, entity)
63+
return entity
4764

4865
@staticmethod
4966
def end_method(method):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dbuilder import Printer
1+
from dbuilder.ast.printer import Printer
22

33

44
class CompiledContract(object):

dbuilder/ast/types/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from dbuilder.ast.types.asm_method import AsmMethod
2+
from dbuilder.ast.types.condition import IfFlow
3+
from dbuilder.ast.types.contract import Contract
4+
from dbuilder.ast.types.control_flow import ControlFlow
5+
from dbuilder.ast.types.expr import Expr
6+
from dbuilder.ast.types.loop import WhileLoop
7+
from dbuilder.ast.types.method import Method
8+
from dbuilder.ast.types.node import Node
9+
from dbuilder.ast.types.statement import Statement

dbuilder/ast/types/asm_method.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from dbuilder.ast.printer import Printer
2+
from dbuilder.ast.types.node import Node
3+
from dbuilder.ast.types.statement import Statement
4+
from dbuilder.ast.utils import _type_name
5+
6+
7+
class AsmMethod(Node):
8+
statement: Statement
9+
10+
def __init__(self, name, args, annotations, asm_annotations):
11+
super().__init__()
12+
self.name = name
13+
self.args = args
14+
self.annotations = annotations
15+
self.asm_annotations = asm_annotations
16+
self.statement = None
17+
18+
def add_statement(self, statement):
19+
if statement.type != Statement.RETURN:
20+
raise RuntimeError("Unexpected statement on asm method")
21+
self.statement = statement
22+
23+
def _get_specs(self):
24+
sd = self.annotations.get("_method")
25+
if not sd:
26+
return ""
27+
res = []
28+
if sd["impure"]:
29+
res.append("impure")
30+
if sd["inline"]:
31+
res.append("inline")
32+
elif sd["inline_ref"]:
33+
res.append("inline_ref")
34+
elif sd["method_id"]:
35+
if sd["method_id_v"]:
36+
res.append("method_id(%d)" % sd["method_id_v"])
37+
else:
38+
res.append("method_id")
39+
if len(res) == 0:
40+
return ""
41+
return " ".join(res) + " "
42+
43+
def print_func(self, printer: Printer):
44+
type_namer = lambda x: "{type} {name}".format(
45+
type=_type_name(x[0]),
46+
name=x[1],
47+
)
48+
tupler = lambda x: (self.annotations[x], x)
49+
arg_defs = list(map(type_namer, map(tupler, self.args)))
50+
51+
name = self.name
52+
fname = self.annotations["_fname"]
53+
name = name if fname is None else fname
54+
55+
entity = self.statement.args[0]
56+
entity = entity if isinstance(entity, tuple) else (entity,)
57+
rearrange = []
58+
input_order = self.asm_annotations["input_order"]
59+
out_order = self.asm_annotations["out_order"]
60+
# TODO: Check Args (Better on annotation side)
61+
if input_order:
62+
input_arrange = " ".join(input_order)
63+
rearrange.append(input_arrange)
64+
if out_order:
65+
out_arrange = "-> " + " ".join(str(i) for i in out_order)
66+
rearrange.append(out_arrange)
67+
rearrange = " ".join(rearrange)
68+
if rearrange != "":
69+
rearrange = f"({rearrange})"
70+
71+
printer.print(
72+
"{output} {name}({args}) {specs}asm{rearrange} {entity};",
73+
output=_type_name(self.annotations["return"]),
74+
name=name,
75+
args=", ".join(arg_defs),
76+
specs=self._get_specs(),
77+
o="{",
78+
rearrange=rearrange,
79+
entity=" ".join(f'"{x}"' for x in entity),
80+
)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from dbuilder.ast import ControlFlow, Printer, Statement
1+
from dbuilder.ast.printer import Printer
2+
from dbuilder.ast.types.control_flow import ControlFlow
3+
from dbuilder.ast.types.statement import Statement
24

35

46
class IfFlow(ControlFlow):

0 commit comments

Comments
 (0)