def test_W_IntObject(): w_a = tla.W_IntObject(0) w_b = tla.W_IntObject(10) w_c = tla.W_IntObject(32) assert not w_a.is_true() assert w_b.is_true() assert w_c.is_true() assert w_b.add(w_c).intvalue == 42 assert w_b.getrepr() == '10'
def test_jump_if(): code = [ tla.JUMP_IF, 5, # jump to target tla.CONST_INT, 123, tla.RETURN, tla.CONST_INT, 234, # target tla.RETURN ] res = interp(code, tla.W_IntObject(0)) assert res.intvalue == 123 res = interp(code, tla.W_IntObject(1)) assert res.intvalue == 234
def test_sub(): code = [ tla.CONST_INT, 20, tla.SUB, tla.RETURN ] res = interp(code, tla.W_IntObject(22)) assert res.intvalue == 2
def test_add(): code = [ tla.CONST_INT, 20, tla.ADD, tla.RETURN ] res = interp(code, tla.W_IntObject(22)) assert res.intvalue == 42
def test_dup(): code = [ tla.DUP, tla.ADD, tla.RETURN ] res = interp(code, tla.W_IntObject(41)) assert res.intvalue == 2 * 41
def test_pop(): code = [ tla.CONST_INT, 99, tla.POP, tla.RETURN ] res = interp(code, tla.W_IntObject(42)) assert res.intvalue == 42
def test_newstr(): code = [ tla.POP, tla.NEWSTR, ord('x'), tla.RETURN ] res = interp(code, tla.W_IntObject(0)) assert isinstance(res, tla.W_StringObject) assert res.strvalue == 'x'
def test_mul(): py.test.skip('exercise!') code = [ tla.CONST_INT, 2, tla.MUL, tla.RETURN ] res = interp(code, tla.W_IntObject(21)) assert res.intvalue == 42
def test_div_float(): py.test.skip('exercise!') code = [ tla.CONST_INT, 2, tla.DIV, tla.RETURN ] res = interp(code, tla.W_IntObject(5)) assert isinstance(res, tla.W_FloatObject) assert res.floatval == 2.5
def entry_point(args): """Main entry point of the stand-alone executable: takes a list of strings and returns the exit code. """ if len(args) < 3: print "Usage: %s filename x" % (args[0], ) return 2 filename = args[1] x = int(args[2]) w_x = tla.W_IntObject(x) bytecode = load_bytecode(filename) w_res = tla.run(bytecode, w_x) print w_res.getrepr() return 0
def test_interp(): code = [ tla.RETURN ] res = interp(code, tla.W_IntObject(42)) assert res.intvalue == 42
def interp_w(intvalue): w_result = interp(code, tla.W_IntObject(intvalue)) assert isinstance(w_result, tla.W_IntObject) return w_result.intvalue