implement parenthesis for expressions
This commit is contained in:
@ -175,10 +175,15 @@ def sugar(expr):
|
|||||||
|
|
||||||
if len(expr) == 2:
|
if len(expr) == 2:
|
||||||
return build_methodcall([op, []], simpleast.PrimitiveMethodCall, expr[0]) # ([name, arg], class, receiver)
|
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)
|
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")
|
||||||
@pg.production("msg-chain : methodcall msg-chain")
|
@pg.production("msg-chain : methodcall msg-chain")
|
||||||
def msg_chain(cc):
|
def msg_chain(cc):
|
||||||
|
@ -7,6 +7,7 @@ from interpreter import Interpreter
|
|||||||
|
|
||||||
def sugar_test_helper(expr):
|
def sugar_test_helper(expr):
|
||||||
ast = parse("x = " + expr)
|
ast = parse("x = " + expr)
|
||||||
|
print(ast)
|
||||||
interpreter = Interpreter()
|
interpreter = Interpreter()
|
||||||
w_module = interpreter.make_module()
|
w_module = interpreter.make_module()
|
||||||
interpreter.eval(ast, w_module)
|
interpreter.eval(ast, w_module)
|
||||||
@ -40,6 +41,8 @@ def test_mod():
|
|||||||
|
|
||||||
def test_inc():
|
def test_inc():
|
||||||
assert sugar_test_helper("1 ++") == 2
|
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():
|
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("1 + 2 + 3 - 5") == 1
|
||||||
assert sugar_test_helper("10 - 2 * 5") == 0
|
assert sugar_test_helper("10 - 2 * 5") == 0
|
||||||
assert sugar_test_helper("1 + 2 * 2 * 3 - 3 * 4") == 1
|
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
|
||||||
|
Reference in New Issue
Block a user