You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
Small changes
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
from and_check import *
|
from uncompyle6.parsers.reducecheck.and_check import *
|
||||||
from ifelsestmt import *
|
from uncompyle6.parsers.reducecheck.ifelsestmt import *
|
||||||
from iflaststmt import *
|
from uncompyle6.parsers.reducecheck.iflaststmt import *
|
||||||
|
@@ -1,3 +1,6 @@
|
|||||||
|
# Copyright (c) 2020 Rocky Bernstein
|
||||||
|
|
||||||
|
|
||||||
def and_check(self, lhs, n, rule, ast, tokens, first, last):
|
def and_check(self, lhs, n, rule, ast, tokens, first, last):
|
||||||
jmp = ast[1]
|
jmp = ast[1]
|
||||||
if jmp.kind.startswith("jmp_"):
|
if jmp.kind.startswith("jmp_"):
|
||||||
|
@@ -1,121 +1,119 @@
|
|||||||
# Copyright (c) 2020 Rocky Bernstein
|
# Copyright (c) 2020 Rocky Bernstein
|
||||||
|
|
||||||
|
from uncompyle6.scanners.tok import Token
|
||||||
|
|
||||||
|
|
||||||
def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
|
def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
|
||||||
if (last + 1) < n and tokens[last + 1] == "COME_FROM_LOOP":
|
if (last + 1) < n and tokens[last + 1] == "COME_FROM_LOOP":
|
||||||
# ifelsestmt jumped outside of loop. No good.
|
# ifelsestmt jumped outside of loop. No good.
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if rule not in (
|
if rule not in (
|
||||||
|
(
|
||||||
|
"ifelsestmt",
|
||||||
(
|
(
|
||||||
"ifelsestmt",
|
"testexpr",
|
||||||
(
|
"c_stmts_opt",
|
||||||
"testexpr",
|
"jump_forward_else",
|
||||||
"c_stmts_opt",
|
"else_suite",
|
||||||
"jump_forward_else",
|
"_come_froms",
|
||||||
"else_suite",
|
|
||||||
"_come_froms",
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ifelsestmt",
|
||||||
(
|
(
|
||||||
"ifelsestmt",
|
"testexpr",
|
||||||
(
|
"c_stmts_opt",
|
||||||
"testexpr",
|
"jump_forward_else",
|
||||||
"c_stmts_opt",
|
"else_suite",
|
||||||
"jump_forward_else",
|
"\\e__come_froms",
|
||||||
"else_suite",
|
|
||||||
'\\e__come_froms'
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ifelsestmt",
|
||||||
(
|
(
|
||||||
"ifelsestmt",
|
"testexpr",
|
||||||
(
|
"c_stmts_opt",
|
||||||
"testexpr",
|
"jf_cfs",
|
||||||
"c_stmts_opt",
|
"else_suite",
|
||||||
"jf_cfs",
|
"\\e_opt_come_from_except",
|
||||||
"else_suite",
|
|
||||||
'\\e_opt_come_from_except',
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ifelsestmt",
|
||||||
|
("testexpr", "c_stmts_opt", "come_froms", "else_suite", "come_froms",),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ifelsestmt",
|
||||||
(
|
(
|
||||||
"ifelsestmt",
|
"testexpr",
|
||||||
(
|
"c_stmts_opt",
|
||||||
"testexpr",
|
"jf_cfs",
|
||||||
"c_stmts_opt",
|
"else_suite",
|
||||||
"come_froms",
|
"opt_come_from_except",
|
||||||
"else_suite",
|
|
||||||
'come_froms',
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
(
|
),
|
||||||
"ifelsestmt",
|
):
|
||||||
(
|
|
||||||
"testexpr",
|
|
||||||
"c_stmts_opt",
|
|
||||||
"jf_cfs",
|
|
||||||
"else_suite",
|
|
||||||
"opt_come_from_except",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Make sure all of the "come froms" offset at the
|
|
||||||
# end of the "if" come from somewhere inside the "if".
|
|
||||||
# Since the come_froms are ordered so that lowest
|
|
||||||
# offset COME_FROM is last, it is sufficient to test
|
|
||||||
# just the last one.
|
|
||||||
come_froms = ast[-1]
|
|
||||||
if come_froms == "opt_come_from_except" and len(come_froms) > 0:
|
|
||||||
come_froms = come_froms[0]
|
|
||||||
if not isinstance(come_froms, Token):
|
|
||||||
if len(come_froms):
|
|
||||||
return tokens[first].offset > come_froms[-1].attr
|
|
||||||
elif tokens[first].offset > come_froms.attr:
|
|
||||||
return True
|
|
||||||
|
|
||||||
# For mysterious reasons a COME_FROM in tokens[last+1] might be part of the grammar rule
|
|
||||||
# even though it is not found in come_froms.
|
|
||||||
# Work around this.
|
|
||||||
if (
|
|
||||||
last < n
|
|
||||||
and tokens[last] == "COME_FROM"
|
|
||||||
and tokens[first].offset > tokens[last].attr
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
|
|
||||||
testexpr = ast[0]
|
|
||||||
|
|
||||||
# Check that the condition portion of the "if"
|
|
||||||
# jumps to the "else" part.
|
|
||||||
if testexpr[0] in ("testtrue", "testfalse"):
|
|
||||||
test = testexpr[0]
|
|
||||||
|
|
||||||
else_suite = ast[3]
|
|
||||||
assert else_suite == "else_suite"
|
|
||||||
|
|
||||||
if len(test) > 1 and test[1].kind.startswith("jmp_"):
|
|
||||||
if last == n:
|
|
||||||
last -= 1
|
|
||||||
jmp = test[1]
|
|
||||||
jmp_target = jmp[0].attr
|
|
||||||
|
|
||||||
# FIXME: the jump inside "else" check below should be added.
|
|
||||||
#
|
|
||||||
# add this until we can find out what's wrong with
|
|
||||||
# not being able to parse:
|
|
||||||
# if a and b or c:
|
|
||||||
# x = 1
|
|
||||||
# else:
|
|
||||||
# x = 2
|
|
||||||
|
|
||||||
# FIXME: add this
|
|
||||||
# if jmp_target < else_suite.first_child().off2int():
|
|
||||||
# return True
|
|
||||||
|
|
||||||
if tokens[first].off2int() > jmp_target:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return (jmp_target > tokens[last].off2int()) and tokens[
|
|
||||||
last
|
|
||||||
] != "JUMP_FORWARD"
|
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Make sure all of the "come froms" offset at the
|
||||||
|
# end of the "if" come from somewhere inside the "if".
|
||||||
|
# Since the come_froms are ordered so that lowest
|
||||||
|
# offset COME_FROM is last, it is sufficient to test
|
||||||
|
# just the last one.
|
||||||
|
come_froms = ast[-1]
|
||||||
|
if come_froms == "opt_come_from_except" and len(come_froms) > 0:
|
||||||
|
come_froms = come_froms[0]
|
||||||
|
if not isinstance(come_froms, Token):
|
||||||
|
if len(come_froms):
|
||||||
|
return tokens[first].offset > come_froms[-1].attr
|
||||||
|
elif tokens[first].offset > come_froms.attr:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# For mysterious reasons a COME_FROM in tokens[last+1] might be part of the grammar rule
|
||||||
|
# even though it is not found in come_froms.
|
||||||
|
# Work around this.
|
||||||
|
if (
|
||||||
|
last < n
|
||||||
|
and tokens[last] == "COME_FROM"
|
||||||
|
and tokens[first].offset > tokens[last].attr
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
|
||||||
|
testexpr = ast[0]
|
||||||
|
|
||||||
|
# Check that the condition portion of the "if"
|
||||||
|
# jumps to the "else" part.
|
||||||
|
if testexpr[0] in ("testtrue", "testfalse"):
|
||||||
|
test = testexpr[0]
|
||||||
|
|
||||||
|
else_suite = ast[3]
|
||||||
|
assert else_suite == "else_suite"
|
||||||
|
|
||||||
|
if len(test) > 1 and test[1].kind.startswith("jmp_"):
|
||||||
|
if last == n:
|
||||||
|
last -= 1
|
||||||
|
jmp = test[1]
|
||||||
|
jmp_target = jmp[0].attr
|
||||||
|
|
||||||
|
# FIXME: the jump inside "else" check below should be added.
|
||||||
|
#
|
||||||
|
# add this until we can find out what's wrong with
|
||||||
|
# not being able to parse:
|
||||||
|
# if a and b or c:
|
||||||
|
# x = 1
|
||||||
|
# else:
|
||||||
|
# x = 2
|
||||||
|
|
||||||
|
# FIXME: add this
|
||||||
|
# if jmp_target < else_suite.first_child().off2int():
|
||||||
|
# return True
|
||||||
|
|
||||||
|
if tokens[first].off2int() > jmp_target:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return (jmp_target > tokens[last].off2int()) and tokens[
|
||||||
|
last
|
||||||
|
] != "JUMP_FORWARD"
|
||||||
|
|
||||||
|
return False
|
||||||
|
@@ -1,45 +1,47 @@
|
|||||||
|
# Copyright (c) 2020 Rocky Bernstein
|
||||||
|
|
||||||
|
|
||||||
def iflaststmt(self, lhs, n, rule, ast, tokens, first, last):
|
def iflaststmt(self, lhs, n, rule, ast, tokens, first, last):
|
||||||
# FIXME: put in a routine somewhere
|
testexpr = ast[0]
|
||||||
testexpr = ast[0]
|
|
||||||
|
|
||||||
if testexpr[0] in ("testtrue", "testfalse"):
|
if testexpr[0] in ("testtrue", "testfalse"):
|
||||||
|
|
||||||
test = testexpr[0]
|
test = testexpr[0]
|
||||||
if len(test) > 1 and test[1].kind.startswith("jmp_"):
|
if len(test) > 1 and test[1].kind.startswith("jmp_"):
|
||||||
if last == n:
|
if last == n:
|
||||||
last -= 1
|
last -= 1
|
||||||
jmp_target = test[1][0].attr
|
jmp_target = test[1][0].attr
|
||||||
if tokens[first].off2int() <= jmp_target < tokens[last].off2int():
|
if tokens[first].off2int() <= jmp_target < tokens[last].off2int():
|
||||||
return True
|
return True
|
||||||
# jmp_target less than tokens[first] is okay - is to a loop
|
# jmp_target less than tokens[first] is okay - is to a loop
|
||||||
# jmp_target equal tokens[last] is also okay: normal non-optimized non-loop jump
|
# jmp_target equal tokens[last] is also okay: normal non-optimized non-loop jump
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(last + 1) < n
|
(last + 1) < n
|
||||||
and tokens[last - 1] != "JUMP_BACK"
|
and tokens[last - 1] != "JUMP_BACK"
|
||||||
and tokens[last + 1] == "COME_FROM_LOOP"
|
and tokens[last + 1] == "COME_FROM_LOOP"
|
||||||
):
|
):
|
||||||
# iflastsmtl is not at the end of a loop, but jumped outside of loop. No good.
|
# iflastsmtl is not at the end of a loop, but jumped outside of loop. No good.
|
||||||
# FIXME: check that tokens[last] == "POP_BLOCK"? Or allow for it not to appear?
|
# FIXME: check that tokens[last] == "POP_BLOCK"? Or allow for it not to appear?
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# If the instruction before "first" is a "POP_JUMP_IF_FALSE" which goes
|
# If the instruction before "first" is a "POP_JUMP_IF_FALSE" which goes
|
||||||
# to the same target as jmp_target, then this not nested "if .. if .."
|
# to the same target as jmp_target, then this not nested "if .. if .."
|
||||||
# but rather "if ... and ..."
|
# but rather "if ... and ..."
|
||||||
if first > 0 and tokens[first - 1] == "POP_JUMP_IF_FALSE":
|
if first > 0 and tokens[first - 1] == "POP_JUMP_IF_FALSE":
|
||||||
return tokens[first - 1].attr == jmp_target
|
return tokens[first - 1].attr == jmp_target
|
||||||
|
|
||||||
if jmp_target > tokens[last].off2int():
|
if jmp_target > tokens[last].off2int():
|
||||||
# One more weird case to look out for
|
# One more weird case to look out for
|
||||||
# if c1:
|
# if c1:
|
||||||
# if c2: # Jumps around the *outer* "else"
|
# if c2: # Jumps around the *outer* "else"
|
||||||
# ...
|
# ...
|
||||||
# else:
|
# else:
|
||||||
if jmp_target == tokens[last - 1].attr:
|
if jmp_target == tokens[last - 1].attr:
|
||||||
return False
|
return False
|
||||||
if last < n and tokens[last].kind.startswith("JUMP"):
|
if last < n and tokens[last].kind.startswith("JUMP"):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
pass
|
pass
|
||||||
return False
|
return False
|
||||||
|
Reference in New Issue
Block a user