def test_name(): examine('foo') n = Name('x') assert n.emit() == 'x' assert n.emit(indent_level=1) == ' x' assert n.variables() == {'x'}
def test_binary_operators(): examine('(a + 1)') examine('(a - 1)') examine('(a * 1)') examine('(a / 1)') examine('(a // 1)') examine('(a % 1)') examine('(a ** 1)') examine('(a << 1)') examine('(a >> 1)') examine('(a | 1)') examine('(a ^ 1)') examine('(a & 1)') if VER >= MatMult.minver: examine('(a @ b)') examine('1 + (a / 3)', '(1 + (a / 3))') p1 = Module(BinOp(Assign([Name('x')], Num(5)), Add(), Num(2))) assert p1.emit() == '(x = 5 + 2)' assert_raises(TypeError, p1.validate) # Indeed, this is not valid python p1r = p1.expressify_statements().resolve_progn() p1r.validate() assert p1r.emit() == 'x = 5\n(x + 2)' lgr = Logger() p2 = Module(Return(BinOp(Assign([Name('x')], Num(5)), Add(), Num(2)))) assert p2.emit() == 'return (x = 5 + 2)' assert_raises(TypeError, p2.validate) p2ex = p2.expressify_statements(logger=lgr) p2r = p2ex.resolve_progn(logger=lgr) for depth, event in lgr.root[1].depth_first(yield_depth=True): print('-' * 80) print(event.display(depth)) p2r.validate() assert p2r.emit() == 'x = 5\n_gensym_0 = (x + 2)\nreturn _gensym_0' p3 = Module(Return(BinOp(Assign([Name('x')], BinOp(Assign([Name('y')], Num(5)), Add(), Num(1))), Add(), Num(2)))) assert p3.emit() == 'return (x = (y = 5 + 1) + 2)' assert_raises(TypeError, p3.validate) p3r = p3.expressify_statements().resolve_progn() p3r.validate() assert p3r.emit() == '''y = 5
def test_boolean_operators(): examine('(a and b)') examine('(a or b)') examine('(a or b or c or d)') e = BoolOp(And(), [Name('a'), Name('b'), Num(1)]) assert e.emit() == '(a and b and 1)' assert e.emit(indent_level=1) == ' (a and b and 1)' e = BoolOp(Or(), [Name('a'), Name('b'), Num(1)]) assert e.emit() == '(a or b or 1)'
def test_unary_operators(): examine('+a') examine('-a') examine('(not a)') examine('~a') e = UnaryOp(USub(), Name('x')) assert e.emit() == '-x' assert e.emit(indent_level=1) == ' -x' e = UnaryOp(Not(), Name('x')) assert e.emit() == '(not x)' assert e.emit(indent_level=1) == ' (not x)'
def test_starred(): if VER >= Starred.minver: examine_('*b = c') s = Starred(Name('foo')) assert s.emit() == '*foo' assert s.emit(indent_level=1) == ' *foo'
def test_while(): examine('while True:\n a = x') examine('while False:\n a = x\nelse:\n b = x') w = While(Num(1), [Call(Name('x'))]) assert w.emit() == \ '''while 1: x()''' w = While(Num(1), [Call(Name('x'))], [Call(Name('y'))]) assert w.emit() == \ '''while 1: x() else: y()''' assert w.emit(indent_level=1) == \ ''' while 1:
def test_progn(): p = ProgN() assert_raises(PythonError, p.validate) assert_raises(PythonError, p.value) assert_raises(PythonError, p.valuify) # assert_raises(NotImplementedError, p.as_value) assert_raises(NotImplementedError, p.expressify_statements) p = ProgN(Assign([Name('x')], Num(2))) assert p.valuify() is p assert p.value() == Name('x') ppp = ProgN(Num(1), ProgN(Num(2), ProgN(Num(3)), ProgN(Assign([Name('y')], Num(4))))) assert ppp.value() == Name('y') assert ppp.resolve_progn() == ProgN(Num(1), Num(2), Num(3), Assign([Name('y')], Num(4))) p = ProgN(Num(2)) assert_raises(PythonError, p.value) pv = p.valuify() assert pv == ProgN(Assign([Name('_gensym_0')], Num(2))) assert pv.value() == Name('_gensym_0')
def test_compare(): examine('(1 == 2)') examine('(1 != 2)') examine('(1 < 2)') examine('(1 <= 2)') examine('(1 > 2)') examine('(1 >= 2)') examine('(a is b)') examine('(a is not b)') examine('(a in b)') examine('(a not in b)') examine('(a < 1 >= 2 <= c is not d)') e = Compare(Name('x'), [Lt(), LtE()], [Name('y'), Num(2)]) assert e.emit() == '(x < y <= 2)' assert e.emit(indent_level=1) == ' (x < y <= 2)'
def to_python(self, **kwargs): from syn.python.b import Call, Name args = [ to_python(self.args[arg.name], **kwargs) for arg in self.func.signature ] if hasattr(self.func, 'python'): return self.func.python(*args, **kwargs) func = Name(self.func.name) return Call(func, args)
def to_python(obj, **kwargs): if not isinstance(obj, SyntagmathonNode): if isinstance(obj, list): return [to_python(item, **kwargs) for item in obj] elif isinstance(obj, tuple): if not obj: from syn.python.b import Call, Name return Call(Name('list')) func = obj[0] args = obj[1:] return to_python(func(*args), **kwargs) return _to_python_native(obj, **kwargs) return obj.to_python(**kwargs)
def test_for(): examine('for x in [1, 2]:\n a = x\n b = y') examine('for x in (1, 2):\n a = x\nelse:\n b = x') f = For(Name('x'), Name('lst'), [Call(Name('x'))]) assert f.emit() == \ '''for x in lst: x()''' f = For(Name('x'), Name('lst'), [Call(Name('x'))], [Call(Name('foo'), [Name('lst')])]) assert f.emit() == \ '''for x in lst: x() else: foo(lst)''' assert f.emit(indent_level=1) == \ ''' for x in lst:
def test_if(): examine('if 1:\n a = 1') examine('if 1:\n a = 1\nelse:\n a = 2') examine( 'if 1:\n a = 1\nelif 2:\n a = 2\nelse:\n a = 3', 'if 1:\n a = 1\nelse:\n if 2:\n a = 2\n else:\n a = 3' ) if1 = If(Num(1), [Num(2)], [Num(3)]) assert if1.emit() == 'if 1:\n 2\nelse:\n 3' rif1 = if1.as_return() assert rif1.emit() == 'if 1:\n return 2\nelse:\n return 3' if2 = If(Assign([Name('x')], Num(2)), [Return(Num(5))]) assert if2.emit() == 'if x = 2:\n return 5' assert_raises(TypeError, if2.validate) # Indeed, this isn't valid python if2r = if2.expressify_statements().resolve_progn() assert isinstance(if2r, ProgN) # Bad if2m = Module(if2) if2r = if2m.expressify_statements().resolve_progn() assert isinstance(if2r, Module) # Good if2r.validate() assert if2r.emit() == '''x = 2 if x: return 5''' if3 = Module( If(Assign([Name('x')], Num(2)), [Assign([Name('y')], Name('x'))])) if3r = if3.expressify_statements().resolve_progn() if3r.validate() assert if3r.emit() == '''x = 2 if x: y = x''' assert if3r.variables() == {'x', 'y'} assert collection_equivalent(if3r._children[1]._children, [Assign([Name('y')], Name('x')), Name('x')]) if4 = If(Num(1), [Num(2)], [Num(3)]) assert if4.emit() == 'if 1:\n 2\nelse:\n 3' if4v = if4.as_value() assert isinstance(if4v, ProgN) assert if4v.value() == Name('_gensym_0') assert Module(if4v).resolve_progn().emit() == '''if 1: _gensym_0 = 2 else: _gensym_0 = 3''' if5 = If(Num(1), [Assign([Name('x')], Num(2))], [Num(3)]) assert if5.emit() == 'if 1:\n x = 2\nelse:\n 3' assert Module(if5.as_value()).resolve_progn().emit() == '''if 1: x = 2 else: x = 3''' if6 = If(Num(1), [Num(2)], [Assign([Name('x')], Num(3))]) assert if6.emit() == 'if 1:\n 2\nelse:\n x = 3' assert Module(if6.as_value()).resolve_progn().emit() == '''if 1: x = 2 else: x = 3''' if7 = If(Num(1), [Assign([Name('y')], Num(2))], [Assign([Name('x')], Num(3))]) assert if7.emit() == 'if 1:\n y = 2\nelse:\n x = 3' assert Module(if7.as_value()).resolve_progn().emit() == '''if 1: y = 2 else: x = 3 y = x''' lgr = Logger() if8 = If(If(Assign([Name('x')], Num(2)), [Num(3)], [Num(4)]), [Return(Num(5))]) assert_raises(TypeError, if8.validate) print(if8.viewable().pretty()) ex = Module(if8).expressify_statements(logger=lgr) print(ex.viewable().pretty()) re = ex.resolve_progn(logger=lgr) print(re.viewable().pretty()) for depth, event in lgr.root[1].depth_first(yield_depth=True): print('-' * 80) print(event.display(depth)) print(lgr.plaintext()) assert len(lgr.nodes) > 10 re.validate() assert re.emit() == '''x = 2
def test_attribute(): examine('a.x') e = Attribute(Name('foo'), 'bar') assert e.emit() == 'foo.bar' assert e.emit(indent_level=1) == ' foo.bar'
def test_ifexp(): examine('(a if b else c)') e = IfExp(Name('a'), Name('b'), Name('c')) assert e.emit() == '(b if a else c)' assert e.emit(indent_level=1) == ' (b if a else c)'
def test_call(): examine('foo()') examine('foo(1)') examine('foo(1, 2, 3)') examine('foo(a=1, b=2)') examine('foo(1, 2, **d)') if VER < '3.5': examine('foo(a=2, *c)') else: examine('foo(*c, a=2)') examine('foo(*c)') examine('foo(**d)') examine('foo(1, a=2, **b)') if VER < '3.5': examine('foo(1, 2, a=3, b=4, *c, **d)') else: examine('foo(1, 2, *c, a=3, b=4, **d)') assert Call(Name('list')).emit() == 'list()' c1 = Call(Name('c1'), [Num(2), Assign([Name('x')], Num(3)), Assign([Name('y')], Num(4))]) assert c1.emit() == 'c1(2, x = 3, y = 4)' assert_raises(TypeError, c1.validate) c1r = Module(c1).expressify_statements().resolve_progn() c1r.emit() assert c1r.emit() == 'x = 3\ny = 4\nc1(2, x, y)' k = Keyword('foo', Num(1)) assert k.emit() == 'foo=1' if VER >= '3.5': k = Keyword(None, Name('x')) assert k.emit() == '**x' if VER < '3.5': c = Call(Name('foo'), [Num(1), Num(2)], [Keyword('a', Num(3)), Keyword('b', Num(4))], Name('c'), Name('d')) assert c.emit() == 'foo(1, 2, a=3, b=4, *c, **d)' assert c.emit(indent_level=1) == ' foo(1, 2, a=3, b=4, *c, **d)' else: c = Call(Name('foo'), [Num(1), Num(2), Starred(Name('c'))], [Keyword('a', Num(3)), Keyword('b', Num(4)), Keyword(None, Name('d'))]) assert c.emit() == 'foo(1, 2, *c, a=3, b=4, **d)' assert c.emit(indent_level=1) == ' foo(1, 2, *c, a=3, b=4, **d)' c = Call(Name('foo')) assert c.emit() == 'foo()'
def test_assign(): examine('a = 1') examine('a = b = 1') examine('a, b = 1', '(a, b) = 1') a = Assign([Name('x')], Num(1)) assert a.emit() == 'x = 1' assert a.emit(indent_level=1) == ' x = 1' a = Assign([Name('x'), Name('y')], Num(1)) assert a.emit() == 'x = y = 1' assert a.emit(indent_level=1) == ' x = y = 1' a = Assign([Name('x')], ProgN(Assign([Name('y')], Num(2)))) assert a.resolve_progn() == ProgN(Assign([Name('y')], Num(2)), Assign([Name('x')], Name('y'))) assert Module(a).resolve_progn().emit() == 'y = 2\nx = y' a = Assign([Name('x')], Assign([Name('y')], Num(2))) assert a.emit() == 'x = y = 2' assert_raises(TypeError, a.validate) assert Module(a).expressify_statements().resolve_progn().emit() == \ 'y = 2\nx = y' a = Assign([Name('x')], Assign([Name('y')], Assign([Name('z')], Num(2)))) assert a.emit() == 'x = y = z = 2' assert_raises(TypeError, a.validate) assert Module(a).expressify_statements().resolve_progn().emit() == \ 'z = 2\ny = z\nx = y'
def to_python(self, **kwargs): from syn.python.b import Name return Name(self.name)