Skip to content

Commit

Permalink
fix nested unary exprs, fix #27
Browse files Browse the repository at this point in the history
  • Loading branch information
boolangery committed Feb 11, 2023
1 parent fd397bb commit b78f169
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
12 changes: 8 additions & 4 deletions luaparser/astnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ def __init__(self, s: str, is_multi_line: bool = False, **kwargs):
self.is_multi_line: bool = is_multi_line


class Statement(Node):
"""Base class for Lua statement."""


class Expression(Node):
"""Define a Lua expression."""


class Statement(Expression):
"""Base class for Lua statement."""


class Block(Node):
"""Define a Lua Block."""

Expand All @@ -164,6 +164,10 @@ def __init__(self, body: Block, **kwargs):
super(Chunk, self).__init__("Chunk", **kwargs)
self.body = body

def __repr__(self):
from luaparser.ast import to_pretty_str
return to_pretty_str(self)


"""
Left Hand Side expression.
Expand Down
5 changes: 3 additions & 2 deletions luaparser/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ def parse_unary_expr(self) -> Expression or bool:
self.failure_save()
if self.next_is_rc(Tokens.LENGTH):
t: Token = self._LT
expr = self.parse_pow_expr()
expr = self.parse_expr()
if expr:
self.success()
return ULengthOP(expr, first_token=t, last_token=t)
Expand Down Expand Up @@ -1374,7 +1374,7 @@ def parse_pow_expr(self) -> Expression or bool:
while True:
self.save()
if self.next_is_rc(Tokens.POW):
right = self.parse_atom()
right = self.parse_expr()
if right:
self.success()
left = ExpoOp(left, right)
Expand Down Expand Up @@ -1402,6 +1402,7 @@ def parse_atom(self) -> Expression or bool:
if self.next_is(Tokens.VARARGS) and self.next_is_rc(Tokens.VARARGS):
return Varargs()


if self.next_is(Tokens.NUMBER) and self.next_is_rc(Tokens.NUMBER):
# TODO: optimize
# using python number eval to parse lua number
Expand Down
33 changes: 33 additions & 0 deletions luaparser/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@


class ExpressionsTestCase(tests.TestCase):
maxDiff = None

"""
3.4.1 – Arithmetic Operators
"""
Expand Down Expand Up @@ -117,6 +119,34 @@ def test_exponentiation(self):
)
self.assertEqual(exp, tree)

def test_exponentiation_minus(self):
tree = ast.parse(r"a = 1^-2")
exp = Chunk(
Block(
[
Assign(
targets=[Name("a")],
values=[ExpoOp(left=Number(1), right=UMinusOp(Number(2)))],
)
]
)
)
self.assertEqual(exp, tree)

def test_exponentiation_3(self):
tree = ast.parse(r"a = 10^-foo()")
exp = Chunk(
Block(
[
Assign(
targets=[Name("a")],
values=[ExpoOp(left=Number(10), right=UMinusOp(Call(Name("foo"), [])))],
)
]
)
)
self.assertEqual(exp, tree)

"""
3.4.2 – Bitwise Operators
"""
Expand Down Expand Up @@ -361,6 +391,9 @@ def test_length_op(self):
)
self.assertEqual(exp, tree)

def test_length_op_2(self):
ast.parse(r"""len = #{"a","b","c"}""")

""" ----------------------------------------------------------------------- """
""" 3.4.9 – Table Constructors """
""" ----------------------------------------------------------------------- """
Expand Down

0 comments on commit b78f169

Please sign in to comment.