diff --git a/de.churl.simple/simpleparser.py b/de.churl.simple/simpleparser.py index 0a4de7f..a413802 100644 --- a/de.churl.simple/simpleparser.py +++ b/de.churl.simple/simpleparser.py @@ -175,10 +175,15 @@ def sugar(expr): if len(expr) == 2: return build_methodcall([op, []], simpleast.PrimitiveMethodCall, expr[0]) # ([name, arg], class, receiver) - + return build_methodcall([op, [expr[2]]], simpleast.PrimitiveMethodCall, expr[0]) # ([name, arg], class, receiver) +@pg.production("expression : OpenBracket expression CloseBracket") +def sugar_parenthesis(expr): + return expr[1] + + @pg.production("msg-chain : methodcall") @pg.production("msg-chain : methodcall msg-chain") def msg_chain(cc): diff --git a/mytest/test_sugar.py b/mytest/test_sugar.py index 0266283..c2b1bf7 100644 --- a/mytest/test_sugar.py +++ b/mytest/test_sugar.py @@ -7,6 +7,7 @@ from interpreter import Interpreter def sugar_test_helper(expr): ast = parse("x = " + expr) + print(ast) interpreter = Interpreter() w_module = interpreter.make_module() interpreter.eval(ast, w_module) @@ -40,6 +41,8 @@ def test_mod(): def test_inc(): assert sugar_test_helper("1 ++") == 2 + assert sugar_test_helper("1 ++ + 1") == 3 # Is this allowed? + assert sugar_test_helper("2 * 2 ++") == 6 def test_mixed_precedence(): @@ -47,3 +50,10 @@ def test_mixed_precedence(): assert sugar_test_helper("1 + 2 + 3 - 5") == 1 assert sugar_test_helper("10 - 2 * 5") == 0 assert sugar_test_helper("1 + 2 * 2 * 3 - 3 * 4") == 1 + + +def test_parenthesis(): + assert sugar_test_helper("2 * (2 + 3)") == 10 + assert sugar_test_helper("(2 + 5)") == 7 + assert sugar_test_helper("(3 + 3) % 2") == 0 + assert sugar_test_helper("(2 * 2) ++") == 5