Example #1
0
def test_arith1(compile_expr):
    frame = compile_expr('1 - 2')

    assert frame.code == [
        ops.LoadConst(0),
        ops.LoadConst(1),
        ops.BinarySubtract(),
    ]
    assert frame.consts == [
        obj.LNumber(1.0),
        obj.LNumber(2.0),
    ]
Example #2
0
def test_ass1(compile_stmt):
    frame = compile_stmt('a = 1')

    assert frame.code == [
        ops.LoadConst(0),
        ops.LoadConst(1),
        ops.StoreName(),
    ]
    assert frame.consts == [
        obj.LVar('a'),
        obj.LNumber(1.0),
    ]
Example #3
0
def test_call1(compile_stmt):
    frame = compile_stmt('print(1)')

    assert frame.code == [
        ops.LoadConst(0),
        ops.LoadConst(1),
        ops.Call(),
    ]
    assert frame.consts == [
        obj.LVar('print'),
        obj.LNumber(1.0),
    ]
Example #4
0
def test_binop1(compile_expr):
    frame = compile_expr('1 + 2')

    assert frame.code == [
        ops.LoadConst(0),
        ops.LoadConst(1),
        ops.BinaryAdd(),
    ]
    assert frame.consts == [
        obj.LNumber(1.0),
        obj.LNumber(2.0),
    ]
Example #5
0
def test_ass3(compile_stmt):
    frame = compile_stmt('a = 1 - 2')

    assert frame.code == [
        ops.LoadConst(0),
        ops.LoadConst(1),
        ops.BinarySubtract(),
        ops.StoreName(0),
    ]
    assert frame.consts == [
        obj.LNumber(1.0),
        obj.LNumber(2.0),
    ]
    assert frame.vars == [
        obj.LVar('a'),
    ]
Example #6
0
def test_call1(compile_expr):
    frame = compile_expr('print(1)')

    assert frame.code == [ops.LoadConst(0), ops.LoadName(0), ops.Call()]
    assert frame.consts == [
        obj.LNumber(1.0),
    ]
    assert frame.vars == [obj.LVar('print')]
Example #7
0
 def add_operand(self, val):
     if type(val) == obj.LVar:
         i = self.add_var(val)
         self.emit(ops.LoadName(i))
     else:
         i = self.add_const(val)
         self.emit(ops.LoadConst(i))
     return i