from peak.rules.indexing import TypeIndex, to_bits |
from peak.rules.indexing import TypeIndex, to_bits |
from peak.rules.predicates import class_node |
from peak.rules.predicates import class_node |
from peak.rules.criteria import Class, Conjunction |
from peak.rules.criteria import Class, Conjunction |
|
try: |
from types import InstanceType |
from types import InstanceType |
ind = TypeIndex(self, 'expr') |
except ImportError: |
|
class InstanceType(object): pass |
|
class a(InstanceType): pass |
|
class b(InstanceType): pass |
|
else: |
class a: pass |
class a: pass |
class b: pass |
class b: pass |
|
ind = TypeIndex(self, 'expr') |
class c(a,b): pass |
class c(a,b): pass |
class x(a,b,object): pass |
class x(a,b,object): pass |
|
|
ind.add_case(0, Class(InstanceType)) |
ind.add_case(0, Class(InstanceType)) |
ind.add_case(1, Conjunction([Class(a), Class(b), Class(c,False)])) |
ind.add_case(1, Conjunction([Class(a), Class(b), Class(c,False)])) |
ind.add_case(2, Class(object)) |
ind.add_case(2, Class(object)) |
(c, to_bits([0,2,3,4,5])), |
(c, to_bits([0,2,3,4,5])), |
(x, to_bits([1,2,3,4,5])) |
(x, to_bits([1,2,3,4,5])) |
] |
] |
for k, v in data: |
if issubclass(a, InstanceType): |
self.assertEqual(lookup(k), v) |
data.pop() |
|
for k, v in data: self.assertEqual(lookup(k), v) |
self.assertEqual(cache, dict(data)) |
self.assertEqual(cache, dict(data)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DecompilationTests(unittest.TestCase): |
class DecompilationTests(unittest.TestCase): |
|
|
def setUp(self): |
def setUp(self): |
from peak.util.assembler import Pass, Getattr, Local |
from peak.util.assembler import Pass, Getattr, Local |
self.check(Pass, '') |
self.check(Pass, '') |
self.check(Getattr(Local('a'), 'b'), 'a.b') |
self.check(Getattr(Local('a'), 'b'), 'a.b') |
self.roundtrip('not -+~`a`') |
self.roundtrip(no_backquotes('not -+~`a`')) |
self.roundtrip('a+b-c*d/e%f**g//h') |
self.roundtrip('a+b-c*d/e%f**g//h') |
self.roundtrip('a and b or not c') |
self.roundtrip('a and b or not c') |
self.roundtrip('~a|b^c&d<<e>>f') |
self.roundtrip('~a|b^c&d<<e>>f') |
def test_paren_precedence(self): |
def test_paren_precedence(self): |
self.roundtrip('(1).__class__') |
self.roundtrip('(1).__class__') |
self.roundtrip('1.2.__class__') |
self.roundtrip('1.2.__class__') |
self.roundtrip('`(a, b)`') |
self.roundtrip(no_backquotes('`(a, b)`')) |
self.roundtrip('`a,b`', '`(a, b)`') |
self.roundtrip(no_backquotes('`a,b`'), no_backquotes('`(a, b)`')) |
self.roundtrip('(a, [b, c])') |
self.roundtrip('(a, [b, c])') |
self.roundtrip('a,b', '(a, b)') |
self.roundtrip('a,b', '(a, b)') |
self.roundtrip('a+b*c') |
self.roundtrip('a+b*c') |
except NameError: |
except NameError: |
from functools import reduce |
from functools import reduce |
|
|
|
def no_backquotes(s): |
|
if sys.version >= "3": |
|
return s.replace('`','') |
|
return s |
|
|
|
|
|
|