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), ]
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), ]
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'), ]
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')]
def run(self): while self.pc < len(self.code): op = self.code[self.pc] if type(op) == ops.BinaryAdd: x = self.stack.pop() y = self.stack.pop() v = obj.LNumber(y.value + x.value) self.stack.append(v) elif type(op) == ops.BinarySubtract: x = self.stack.pop() y = self.stack.pop() v = obj.LNumber(y.value - x.value) self.stack.append(v) elif type(op) == ops.Call: func = self.stack.pop() arg = self.stack.pop() if type(arg) == obj.LVar: arg = self.env[arg] func(arg) elif type(op) == ops.LoadConst: self.stack.append(self.consts[op.index]) elif type(op) == ops.LoadName: lvar = self.vars[op.index] value = self.env.get(lvar) if value is None: value = getattr(builtin, 'lua_' + lvar.value) self.stack.append(value) elif type(op) == ops.StoreName: var = self.vars[op.index] val = self.stack.pop() if type(val) == obj.LVar: val = self.env[val] self.env[var] = val self.pc += 1 return self
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), ]
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), ]
def visit_number(self, node, vc): return obj.LNumber(float(node.value))
def test_ass1(interp_stmt): frame = interp_stmt('a = 1') assert frame.env == { obj.LVar('a'): obj.LNumber(1.0), }