diff --git a/py2wasm/python.py b/py2wasm/python.py index 8e2740a..90f97af 100644 --- a/py2wasm/python.py +++ b/py2wasm/python.py @@ -219,13 +219,19 @@ class Visitor: """ Visits a UnaryOp node as (part of) an expression """ - del module - del wlocals + if isinstance(node.op, ast.UAdd): + return self.visit_expr(module, wlocals, exp_type, node.operand) - if not isinstance(node.operand, ast.Constant): - raise NotImplementedError + if isinstance(node.op, ast.USub): + if not isinstance(node.operand, ast.Constant): + raise NotImplementedError(node.operand) - return self.visit_Constant(exp_type, node.operand) + if not isinstance(node.operand.value, int): + raise NotImplementedError(node.operand.value) + + return self.visit_Constant(exp_type, ast.Constant(-node.operand.value)) + + raise NotImplementedError(node.op) def visit_BinOp( self, diff --git a/tests/integration/test_simple.py b/tests/integration/test_simple.py index fdcab75..17b2b86 100644 --- a/tests/integration/test_simple.py +++ b/tests/integration/test_simple.py @@ -28,6 +28,32 @@ def testEntry(a: i32) -> i32: assert 125 == result.returned_value assert [] == result.log_int32_list +@pytest.mark.integration_test +def test_uadd(): + code_py = """ +@exported +def testEntry() -> i32: + return +523 +""" + + result = Suite(code_py, 'test_addition').run_code() + + assert 523 == result.returned_value + assert [] == result.log_int32_list + +@pytest.mark.integration_test +def test_usub(): + code_py = """ +@exported +def testEntry() -> i32: + return -19 +""" + + result = Suite(code_py, 'test_addition').run_code() + + assert -19 == result.returned_value + assert [] == result.log_int32_list + @pytest.mark.integration_test def test_addition(): code_py = """