-
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1382 from jacqueswww/1363_vyper_ast
1363 vyper ast
- Loading branch information
Showing
32 changed files
with
767 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from vyper.ast_utils import ( | ||
parse_to_ast, | ||
) | ||
|
||
|
||
def test_ast_equal(): | ||
code = """ | ||
@public | ||
def test() -> int128: | ||
a: uint256 = 100 | ||
return 123 | ||
""" | ||
|
||
ast1 = parse_to_ast(code) | ||
ast2 = parse_to_ast("\n \n" + code + "\n\n") | ||
|
||
assert ast1 == ast2 | ||
|
||
|
||
def test_ast_unequal(): | ||
code1 = """ | ||
@public | ||
def test() -> int128: | ||
a: uint256 = 100 | ||
return 123 | ||
""" | ||
code2 = """ | ||
@public | ||
def test() -> int128: | ||
a: uint256 = 100 | ||
return 121 | ||
""" | ||
|
||
ast1 = parse_to_ast(code1) | ||
ast2 = parse_to_ast(code2) | ||
|
||
assert ast1 != ast2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from vyper import ( | ||
compiler, | ||
) | ||
from vyper.ast_utils import ( | ||
ast_to_dict, | ||
dict_to_ast, | ||
parse_to_ast, | ||
) | ||
|
||
|
||
def get_node_ids(ast_struct, ids=None): | ||
if ids is None: | ||
ids = [] | ||
|
||
for k, v in ast_struct.items(): | ||
if isinstance(v, dict): | ||
ids = get_node_ids(v, ids) | ||
elif isinstance(v, list): | ||
for x in v: | ||
ids = get_node_ids(x, ids) | ||
elif k == 'node_id': | ||
ids.append(v) | ||
elif v is None or isinstance(v, (str, int)): | ||
continue | ||
else: | ||
raise Exception('Unknown ast_struct provided.') | ||
return ids | ||
|
||
|
||
def test_ast_to_dict_node_id(): | ||
code = """ | ||
@public | ||
def test() -> int128: | ||
a: uint256 = 100 | ||
return 123 | ||
""" | ||
dict_out = compiler.compile_code(code, ['ast_dict']) | ||
node_ids = get_node_ids(dict_out) | ||
|
||
assert len(node_ids) == len(set(node_ids)) | ||
|
||
|
||
def test_basic_ast(): | ||
code = """ | ||
a: int128 | ||
""" | ||
dict_out = compiler.compile_code(code, ['ast_dict']) | ||
assert dict_out['ast_dict']['ast'][0] == { | ||
'annotation': { | ||
'ast_type': 'Name', | ||
'col_offset': 3, | ||
'id': 'int128', | ||
'lineno': 2, | ||
'node_id': 4 | ||
}, | ||
'ast_type': 'AnnAssign', | ||
'col_offset': 0, | ||
'lineno': 2, | ||
'node_id': 1, | ||
'simple': 1, | ||
'target': { | ||
'ast_type': 'Name', | ||
'col_offset': 0, | ||
'id': 'a', | ||
'lineno': 2, | ||
'node_id': 2 | ||
}, | ||
'value': None | ||
} | ||
|
||
|
||
def test_dict_to_ast(): | ||
code = """ | ||
@public | ||
def test() -> int128: | ||
a: uint256 = 100 | ||
return 123 | ||
""" | ||
|
||
original_ast = parse_to_ast(code) | ||
out_dict = ast_to_dict(original_ast) | ||
new_ast = dict_to_ast(out_dict) | ||
|
||
assert new_ast == original_ast |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from vyper.ast_utils import ( | ||
ast_to_string, | ||
parse_to_ast, | ||
) | ||
|
||
|
||
def test_ast_to_string(): | ||
code = """ | ||
@public | ||
def testme(a: int128) -> int128: | ||
return a | ||
""" | ||
vyper_ast = parse_to_ast(code) | ||
assert ast_to_string(vyper_ast) == ( | ||
"Module(body=[FunctionDef(name='testme', args=arguments(args=[arg(arg='a'," | ||
" annotation=Name(id='int128'))], defaults=[]), body=[Return(value=Name(id='a'))]," | ||
" decorator_list=[Name(id='public')], returns=Name(id='int128'))])" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
from pytest import ( | ||
raises, | ||
) | ||
|
||
from vyper import ( | ||
compiler, | ||
) | ||
from vyper.exceptions import ( | ||
SyntaxException, | ||
) | ||
|
||
fail_list = [ | ||
""" | ||
x: bytes[1:3] | ||
""", | ||
""" | ||
b: int128[int128: address] | ||
""", | ||
""" | ||
x: int128[5] | ||
@public | ||
def foo(): | ||
self.x[2:4] = 3 | ||
""", | ||
""" | ||
@public | ||
def foo(): | ||
x: address = ~self | ||
""", | ||
""" | ||
x: int128[5] | ||
@public | ||
def foo(): | ||
z = self.x[2:4] | ||
""", | ||
""" | ||
@public | ||
def foo(): | ||
x: int128[5] | ||
z = x[2:4] | ||
""", | ||
""" | ||
x: int128(wei >> 3) | ||
""", | ||
""" | ||
Transfer: event({_&rom: indexed(address)}) | ||
""", | ||
] | ||
|
||
|
||
@pytest.mark.parametrize('bad_code', fail_list) | ||
def test_syntax_exception(bad_code): | ||
with raises(SyntaxException): | ||
compiler.compile_code(bad_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.