From 4dc980df6b54d8945b5d9592f0087f620012aeb1 Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Fri, 13 Jul 2018 15:13:41 +0200 Subject: [PATCH 1/3] Add constants to reserved keywords. --- tests/parser/types/numbers/test_constants.py | 9 +++++++++ vyper/utils.py | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/parser/types/numbers/test_constants.py b/tests/parser/types/numbers/test_constants.py index 91802fcb32..13e8ee7f3a 100644 --- a/tests/parser/types/numbers/test_constants.py +++ b/tests/parser/types/numbers/test_constants.py @@ -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)) diff --git a/vyper/utils.py b/vyper/utils.py index 28e628a879..7197f83ae2 100644 --- a/vyper/utils.py +++ b/vyper/utils.py @@ -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? From d6a56b48f7be79f8f6acd513c2b415182d380ba4 Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Fri, 13 Jul 2018 15:40:04 +0200 Subject: [PATCH 2/3] Make use of integer division for two literal values. --- tests/parser/types/numbers/test_int128.py | 13 +++++++++++++ vyper/parser/expr.py | 6 +----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/parser/types/numbers/test_int128.py b/tests/parser/types/numbers/test_int128.py index d458a5e33c..6572ffc272 100644 --- a/tests/parser/types/numbers/test_int128.py +++ b/tests/parser/types/numbers/test_int128.py @@ -209,3 +209,16 @@ 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, assert_tx_failed): + code = """ +@public +def foo() -> int128: + z: int128 = 5 / 2 + return z + """ + + c = get_contract(code) + + assert c.foo() == 2 diff --git a/vyper/parser/expr.py b/vyper/parser/expr.py index 704a30e9e5..7f3ce10b76 100644 --- a/vyper/parser/expr.py +++ b/vyper/parser/expr.py @@ -271,7 +271,7 @@ 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): @@ -279,10 +279,6 @@ def arithmetic(self): 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 From 881d217c596bfd8ce522d124aa5975b56b1e4fe0 Mon Sep 17 00:00:00 2001 From: Jacques Wagener Date: Fri, 13 Jul 2018 16:16:55 +0200 Subject: [PATCH 3/3] Fixes #927, proper type check on literal return. --- tests/parser/types/numbers/test_int128.py | 12 +++++++++++- vyper/parser/stmt.py | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/parser/types/numbers/test_int128.py b/tests/parser/types/numbers/test_int128.py index 6572ffc272..d8f0af4707 100644 --- a/tests/parser/types/numbers/test_int128.py +++ b/tests/parser/types/numbers/test_int128.py @@ -211,7 +211,7 @@ def num_pow(a: int128, b: int128) -> int128: assert_tx_failed(lambda: c.num_pow(2**126, 2)) -def test_literal_int_division(get_contract, assert_tx_failed): +def test_literal_int_division(get_contract): code = """ @public def foo() -> int128: @@ -222,3 +222,13 @@ def foo() -> int128: 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)) diff --git a/vyper/parser/stmt.py b/vyper/parser/stmt.py index fe0c8eb96c..2e654fded1 100644 --- a/vyper/parser/stmt.py +++ b/vyper/parser/stmt.py @@ -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)