Skip to content

Commit

Permalink
3.7 ifstmt with "and" bool
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Mar 31, 2024
1 parent c2cc6e7 commit d6429e8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions decompyle3/parsers/p37/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ def p_grammar(self, args):
# empty they have to be reasonable, e.g. testexpr has to
# jump to one of the COME_FROMS
ifstmt ::= testexpr stmts _come_froms
ifstmt ::= bool_op stmts _come_froms
ifstmt ::= testexpr ifstmts_jump _come_froms
stmt ::= ifstmt_bool
Expand Down
11 changes: 8 additions & 3 deletions decompyle3/parsers/reduce_check/ifstmt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, 2022-2023 Rocky Bernstein
# Copyright (c) 2020, 2022-2024 Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -27,11 +27,14 @@ def ifstmt(
self, lhs: str, n: int, rule, tree, tokens: list, first: int, last: int
) -> bool:

# print("XXX", first, last, rule)
# print("XXX", tokens[first].offset , tokens[last].offset, rule)
# for t in range(first, last):
# print(tokens[t])
# print("=" * 40)

if rule == ("ifstmt", ("bool_op", "stmts", "\\e__come_froms")):
return False

ltm1_index = last - 1
while tokens[ltm1_index] == "COME_FROM":
ltm1_index -= 1
Expand Down Expand Up @@ -182,7 +185,7 @@ def ifstmt(
pass

# If there is a final COME_FROM and that test jumps to that, this is a strong
# indication that this is ok, s we'll skip jumps jumping too far test.
# indication that this is ok, so we'll skip jumps jumping too far test.
if (
pop_jump_if is not None
and ltm1 == "COME_FROM"
Expand Down Expand Up @@ -221,5 +224,7 @@ def ifstmt(
# been a prior reduction that doesn't include the last COME_FROM.
if ltm1 == "COME_FROM":
return ltm1.attr < first_offset
elif tokens[last] == "COME_FROM":
return tokens[last].attr < first_offset

return False
6 changes: 6 additions & 0 deletions decompyle3/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def build_instructions(self, co):
self.offset2inst_index = {}
for i, inst in enumerate(self.insts):
self.offset2inst_index[inst.offset] = i
offset = inst.offset
inst_size = inst.inst_size
while inst_size > 0:
self.offset2inst_index[offset] = i
offset += 2
inst_size -= 2

return bytecode

Expand Down
4 changes: 3 additions & 1 deletion decompyle3/semantics/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@
# "yield": ( "yield %c", 0),
# "return": ( "%|return %c\n", 0),
"return_if_stmt": ("return %c\n", 0),
"ifstmt": ("%|if %c:\n%+%c%-", (0, "testexpr"), (1, ("ifstmts_jump", "stmts"))),
"ifstmt": (
"%|if %c:\n%+%c%-", (0, ("testexpr", "bool_op")), (1, ("ifstmts_jump", "stmts"))
),
"iflaststmt": ("%|if %c:\n%+%c%-", 0, 1),
"iflaststmtc": ("%|if %c:\n%+%c%-", 0, 1),
"testtrue": ("not %p", (0, PRECEDENCE["unary_not"])),
Expand Down

0 comments on commit d6429e8

Please sign in to comment.