Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix literal int division #951

Merged
merged 3 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/parser/types/numbers/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ def test_arithmetic(a: int128) -> int128:
assert c.test_uint256(2**256 - 1) is True

assert c.test_arithmetic(5000) == 2**127 - 1 - 5000


def test_reserved_keyword(get_contract, assert_compile_failed):
code = """
@public
def test():
ZERO_ADDRESS: address
"""
assert_compile_failed(lambda: get_contract(code))
23 changes: 23 additions & 0 deletions tests/parser/types/numbers/test_int128.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,26 @@ def num_pow(a: int128, b: int128) -> int128:
assert c.num_pow(-2, 127) == (-2**127)
assert c.num_pow(2, 126) == (2**126)
assert_tx_failed(lambda: c.num_pow(2**126, 2))


def test_literal_int_division(get_contract):
code = """
@public
def foo() -> int128:
z: int128 = 5 / 2
return z
"""

c = get_contract(code)

assert c.foo() == 2


def test_literal_int_division_return(get_contract, assert_compile_failed):
code = """
@public
def test() -> decimal:
return 5 / 2
"""

assert_compile_failed(lambda: get_contract(code))
6 changes: 1 addition & 5 deletions vyper/parser/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,14 @@ def arithmetic(self):
elif isinstance(self.expr.op, ast.Mult):
val = left.value * right.value
elif isinstance(self.expr.op, ast.Div):
val = left.value / right.value
val = left.value // right.value
elif isinstance(self.expr.op, ast.Mod):
val = left.value % right.value
elif isinstance(self.expr.op, ast.Pow):
val = left.value ** right.value
else:
raise ParserException('Unsupported literal operator: %s' % str(type(self.expr.op)), self.expr)

# For scenario were mul and div produce a whole number:
if isinstance(val, float) and val.is_integer():
val = int(val)

num = ast.Num(val)
num.source_code = self.expr.source_code
num.lineno = self.expr.lineno
Expand Down
2 changes: 1 addition & 1 deletion vyper/parser/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def parse_return(self):
elif is_base_type(sub.typ, self.context.return_type.typ) or \
(is_base_type(sub.typ, 'int128') and is_base_type(self.context.return_type, 'int256')):
return LLLnode.from_list(['seq', ['mstore', 0, sub], ['return', 0, 32]], typ=None, pos=getpos(self.stmt))
if sub.typ.is_literal and SizeLimits.in_bounds(self.context.return_type.typ, sub.value):
if sub.typ.is_literal and SizeLimits.in_bounds(self.context.return_type.typ, sub.value) and self.context.return_type.typ == sub.typ:
return LLLnode.from_list(['seq', ['mstore', 0, sub], ['return', 0, 32]], typ=None, pos=getpos(self.stmt))
else:
raise TypeMismatchException("Unsupported type conversion: %r to %r" % (sub.typ, self.context.return_type), self.stmt.value)
Expand Down
19 changes: 11 additions & 8 deletions vyper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,17 @@ def in_bounds(cls, type_str, value):
valid_global_keywords = ['public', 'modifying', 'event'] + valid_units + valid_call_keywords

# Cannot be used for variable or member naming
reserved_words = ['int128', 'int256', 'uint256', 'address', 'bytes32',
'if', 'for', 'while', 'until',
'pass', 'def', 'push', 'dup', 'swap', 'send', 'call',
'selfdestruct', 'assert', 'stop', 'throw',
'raise', 'init', '_init_', '___init___', '____init____',
'true', 'false', 'self', 'this', 'continue', 'ether',
'wei', 'finney', 'szabo', 'shannon', 'lovelace', 'ada',
'babbage', 'gwei', 'kwei', 'mwei', 'twei', 'pwei', 'contract', 'units']
reserved_words = [
'int128', 'int256', 'uint256', 'address', 'bytes32',
'if', 'for', 'while', 'until',
'pass', 'def', 'push', 'dup', 'swap', 'send', 'call',
'selfdestruct', 'assert', 'stop', 'throw',
'raise', 'init', '_init_', '___init___', '____init____',
'true', 'false', 'self', 'this', 'continue',
'ether', 'wei', 'finney', 'szabo', 'shannon', 'lovelace', 'ada', 'babbage', 'gwei', 'kwei', 'mwei', 'twei', 'pwei', 'contract',
'units',
'zero_address', 'max_int128', 'min_int128', 'max_decimal', 'min_decimal', 'max_uint256', # constants
]


# Is a variable or member variable name valid?
Expand Down