def test_atomic_decrement(self): self.assert_generates_ast(''' k-- --l ''', [ AtomicDecrement(_('k'), True), AtomicDecrement(_('l'), False) ])
def test_read_member(self): self.assert_generates_ast(''' foo->attr blah.attr2 ''', [ ObjectMember(_('foo'), _('attr'), do_unary=True), ObjectMember(_('blah'), _('attr2'), do_unary=False) ])
def test_atomic_increment(self): self.assert_generates_ast(''' i++ ++j ''', [ AtomicIncrement(_('i'), True), AtomicIncrement(_('j'), False) ])
def test_basic(self): self.assert_generates_ast(''' while a < b: do_something() ''', [ WhileLoop( Condition([_('a'), _op('<'), _('b')]), Block([ Call(_('do_something'), []) ]) ) ])
def test_simple(self): self.assert_generates_ast(''' for i as Int from 0 to 10: printf("Hi! %d\\n", i) ''', [ ForLoop( Declaration(_('i'), _t('Int')), Integer(0), Integer(10), AtomicIncrement( Declaration(_('i'), _t('Int')), return_new_value=False ) ) ])
def test_with_vars_and_calls(self): self.assert_generates_ast(''' a as Int = 5 ** 0.7 b as Float = 0.2 * sqrt(2) * a / blah(a*sqrt(2)) ''', [ Definition( Declaration(_('a'), _t('Int')), MathExpression([Integer(5), _op('**'), Float(0.7)]) ), Definition( Declaration(_('b'), _t('Float')), MathExpression([ Float(0.2), _op('*'), Call(_('sqrt'), [Integer(2)]), _op('*'), _('a'), _op('/'), Call(_('blah'), [MathExpression([ _('a'), _op('*'), Call(_('sqrt'), [Integer(2)]) ])] ) ]) ) ])
def test_declaration(self): self.assert_generates_ast(''' Foo <- Object: a as Int b as Float foo as Foo ''', [ ObjectTypeDeclaration( _t('Foo'), _t('Object'), DeclarationBlock([ Declaration(_('a'), _t('Int')), Declaration(_('b'), _t('Float')), Declaration(_('foo'), _t('Foo')) ]) ) ])
def test_malloc(self): self.assert_generates_ast(''' Blah <- Object: a as Int fizz as Blah* = malloc(sizeof(Blah)) ''', [ ObjectTypeDeclaration( _t('Blah'), _t('Object'), DeclarationBlock([ Declaration(_('a'), _t('Int')) ]) ), Definition( Declaration(_('fizz'), _t('Blah', True)), Call(_('malloc'), [Call(_('sizeof'), [_('Blah')])]) ) ])
def test_inheritance(self): ast = self.assert_generates_ast(''' Parent <- Object: a as Int b as Parent* Child1 <- Parent: c as Parent* d as Float Child2 <- Parent: e as Int Child1_1 <- Child1: f as Child2 ''', [ ObjectTypeDeclaration( _t('Parent'), _t('Object'), DeclarationBlock([ Declaration(_('a'), _t('Int')), Declaration(_('b'), _t('Parent', True)) ]) ), ObjectTypeDeclaration( _t('Child1'), _t('Parent'), DeclarationBlock([ Declaration(_('c'), _t('Parent', True)), Declaration(_('d'), _t('Float')) ]) ), ObjectTypeDeclaration( _t('Child2'), _t('Parent'), DeclarationBlock([ Declaration(_('e'), _t('Int')) ]) ), ObjectTypeDeclaration( _t('Child1_1'), _t('Child1'), DeclarationBlock([ Declaration(_('f'), _t('Child2')) ]) ) ]) Parent, Child1, Child2, Child1_1 = ast self.assert_equal(Parent.children['parent_type'], None) self.assert_equal(Child1.children['parent_type'], _t('Parent')) self.assert_equal(Child2.children['parent_type'], _t('Parent')) self.assert_equal(Child1_1.children['parent_type'], _t('Child1'))
def test_set_member(self): self.assert_generates_ast(''' foo->attr = 42 blah.attr = foo->attr ''', [ Assignment( ObjectMember(_('foo'), _('attr'), do_unary=True), Integer(42) ), Assignment( ObjectMember(_('blah'), _('attr'), do_unary=False), ObjectMember(_('foo'), _('attr'), do_unary=True) ) ])
def test_recursion(self): self.assert_generates_ast(''' bear as Function(emma as Heifer*, bertram as Bull*) -> Calf*: return malloc(sizeof(Calf)) bear_n as Function(n as Int, emma as Heifer*, bertram as Bull*): if not n: return else: bear(eve, adam) return bear_n(n-1) ''', [ Function( _('bear'), FunctionHeader( _t('Calf', True), [ Declaration(_('emma'), _t('Heifer', True)), Declaration(_('bertram'), _t('Bull', True)) ], ), Block([Return(Call(_('malloc'), [Call(_('sizeof'), [_('Calf')])]))]) ), Function( _('bear_n'), FunctionHeader( None, [ Declaration(_('n'), _t('Int')), Declaration(_('emma'), _t('Heifer', True)), Declaration(_('bertram'), _t('Bull', True)) ], ), Block([ If( Condition([_op('not'), _('n')]), Block([Return(None)]), Block([ Call(_('bear'), [_('eve'), _('adam')]), Return( Call( _('bear_n'), [MathExpression([_('n'), _op('-'), Integer(1)])] ) ) ]) ) ]) ) ])
def test_definition(self): self.assert_generates_ast(''' foo as Function(a as Int, b as Float, c as Bool, d as Bool, e as Bool, f as Bool) -> String: if a: return a if c and e and (d or e) or (f): return a else: return b foo2 as Function(): return 0 ''', [ Function( _('foo'), FunctionHeader( _t('String'), [ Declaration(_('a'), _t('Int')), Declaration(_('b'), _t('Float')), Declaration(_('c'), _t('Bool')), Declaration(_('d'), _t('Bool')), Declaration(_('e'), _t('Bool')), Declaration(_('f'), _t('Bool')) ] ), Block([ If( Condition([_('a')]), Block([Return(_('a'))]) ), If( Condition([ _('c'), _op('and'), _('e'), _op('and'), ExpressionContainer([Condition([_('d'), _op('or'), _('e')])]), _op('or'), ExpressionContainer([_('f')]) ]), Block([Return(_('a'))]), Block([Return(_('b'))]) ) ]) ), Function( _('foo2'), FunctionHeader(None, []), Block([Return(Integer(0))]) ) ])
def test_multiple(self): self.assert_generates_ast(''' a as Int = 5 * 8 b as Float = 10 * 3.14 b = 10 / 5.678 b = 5 * 5 + 1 b = 5 * 3 + (2 * 7) 7 + (3 ^ (2 + 1) / 5 ** 7 * (8/9)) - 9 * (5 - 3) ''', [ Definition( Declaration(_('a'), _t('Int')), MathExpression([Integer(5), _op('*'), Integer(8)]), ), Definition( Declaration(_('b'), _t('Float')), MathExpression([Integer(10), _op('*'), Float(3.14)]) ), Assignment(_('b'), MathExpression([Integer(10), _op('/'), Float(5.678)]) ), Assignment(_('b'), MathExpression([Integer(5), _op('*'), Integer(5), _op('+'), Integer(1)]) ), Assignment(_('b'), MathExpression([ Integer(5), _op('*'), Integer(3), _op('+'), ExpressionContainer([ MathExpression([ Integer(2), _op('*'), Integer(7) ]) ]) ]) ), MathExpression([ Integer(7), _op('+'), ExpressionContainer([ MathExpression([ Integer(3), _op('**'), ExpressionContainer([ MathExpression([Integer(2), _op('+'), Integer(1)]) ]), _op('/'), Integer(5), _op('^'), Integer(7), _op('*'), ExpressionContainer([ MathExpression([Integer(8), _op('/'), Integer(9)]) ]), ]), ]), _op('-'), Integer(9), _op('*'), ExpressionContainer([ MathExpression([Integer(5), _op('-'), Integer(3)]) ]) ]) ])