def time_apply_cont(initial, env, cont, vals): from pycket.interpreter import return_multi_vals final = time.clock() ms = values.W_Fixnum(int((final - initial) * 1000)) vals_w = vals.get_all_values() results = values.Values.make( [values.to_list(vals_w), ms, ms, values.W_Fixnum(0)]) return return_multi_vals(results, env, cont)
def vector_copy_cont_get(src, src_start, src_end, dest, dest_start, i, env, cont, _vals): from pycket.interpreter import check_one_val val = check_one_val(_vals) idx = values.W_Fixnum(i.value + dest_start) next = values.W_Fixnum(i.value + 1) return dest.vector_set( idx, val, env, goto_vector_copy_loop(src, src_start, src_end, dest, dest_start, next, env, cont))
def arity_to_value(arity, env, cont): from pycket.interpreter import return_value if arity.at_least != -1: val = [values.W_Fixnum(arity.at_least)] constructor = arity_at_least.constructor return constructor.call(val, env, proc_arity_cont(arity, env, cont)) if len(arity.arity_list) == 1: item = values.W_Fixnum(arity.arity_list[0]) return return_value(item, env, cont) result = make_arity_list(arity) return return_value(result, env, cont)
def do_procedure_arity(proc, env, cont): from pycket.interpreter import return_value result = [] arity = proc.get_arity() for item in arity.arity_list: result.append(values.W_Fixnum(item)) if arity.at_least != -1: val = [values.W_Fixnum(arity.at_least)] return arity_at_least.constr.call(val, env, proc_arity_cont(result, env, cont)) if len(result) == 1: return return_value(result[0], env, cont) return return_value(values.to_list(result[:]), env, cont)
def do_read_one(w_port, as_bytes, peek, env, cont): from pycket.interpreter import return_value if peek: c = w_port.peek() else: c = w_port.read(1) if len(c) == 0: return return_value(values.eof_object, env, cont) i = ord(c[0]) if as_bytes: return return_value(values.W_Fixnum(i), env, cont) else: # hmpf, poking around in internals needed = runicode.utf8_code_length[i] if peek: old = w_port.tell() c = w_port.read(needed) w_port.seek(old) elif needed > 1: c += w_port.read(needed - 1) c = c.decode("utf-8") assert len(c) == 1 return return_value(values.W_Character(c[0]), env, cont)
def equal_vec_done_cont(a, b, idx, info, env, cont, _vals): from pycket.interpreter import check_one_val, return_value eq = check_one_val(_vals) if eq is values.w_false: return return_value(values.w_false, env, cont) inc = values.W_Fixnum(idx.value + 1) return equal_vec_func(a, b, inc, info, env, cont)
def arith_sub_same(self, other): assert isinstance(other, values.W_Fixnum) try: res = rarithmetic.ovfcheck(self.value - other.value) except OverflowError: return values.W_Bignum(rbigint.fromint(self.value)).arith_sub(other) return values.W_Fixnum(res)
def arith_pow_same(self, other): assert isinstance(other, values.W_Fixnum) # XXX nonsense try: res = rarithmetic.ovfcheck_float_to_int(math.pow(self.value, other.value)) except OverflowError: return self.arith_pow(values.W_Bignum(rbigint.fromint(other.value))) return values.W_Fixnum(res)
def make_arity_list(arity, extra=None): jit.promote(arity) acc = values.w_null if extra is not None: acc = values.W_Cons.make(extra, acc) for item in reversed(arity.arity_list): i = values.W_Fixnum(item) acc = values.W_Cons.make(i, acc) return acc
def arith_mod_same(self, other): assert isinstance(other, values.W_Fixnum) if other.value == 0: raise Exception("zero_divisor") try: res = rarithmetic.ovfcheck(self.value % other.value) except OverflowError: return self.arith_mod(values.W_Bignum(rbigint.fromint(other.value))) return values.W_Fixnum(res)
def arith_mul_same(self, other): assert isinstance(other, values.W_Fixnum) if not self.value: return self if not other.value: return other try: res = rarithmetic.ovfcheck(self.value * other.value) except OverflowError: return self.arith_mul(values.W_Bignum(rbigint.fromint(other.value))) return values.W_Fixnum(res)
def default_uncaught_exception_handler(exn, env, cont): # racket/src/cs/rumble/error.ss #FIXME : handle Breaks offset = exn.struct_type().get_offset(exn.struct_type()) original_field_num = 0 # this is for the message field in exceptions message_field_index = values.W_Fixnum(original_field_num - offset) return exn.struct_type().accessor.call([exn, message_field_index], env, display_escape_cont(exn, env, cont))
def vector_copy_loop(src, src_start, src_end, dest, dest_start, i, env, cont): from pycket.interpreter import return_value src_idx = i.value + src_start if src_idx >= src_end: return return_value(values.w_void, env, cont) idx = values.W_Fixnum(src_idx) return src.vector_ref( idx, env, vector_copy_cont_get(src, src_start, src_end, dest, dest_start, i, env, cont))
def length(a): n = 0 while True: if a is values.w_null: return values.W_Fixnum(n) if isinstance(a, values.W_Cons): a = a.cdr() n = n + 1 else: raise SchemeException("length: not a list")
def random(args): if not args: # random flonum return values.W_Flonum(rng.random()) a1 = args[0] if isinstance(a1, values.W_Fixnum): upper = a1.value return values.W_Fixnum(int(rng.random() * upper)) if isinstance(a1, values.W_PseudoRandomGenerator): return values.W_Flonum(rng.random()) raise SchemeException("random: invalid arguments")
def _str2num(s, radix): from rpython.rlib import rarithmetic, rfloat, rbigint from rpython.rlib.rstring import ParseStringError, ParseStringOverflowError from rpython.rlib.rsre import rsre_re as re import math try: if ((radix == 16 and re.match("^[0-9A-Fa-f]+$", s)) or (radix == 8 and re.match("^[0-7]+$", s)) or (radix == 10 and re.match("^[0-9]+$", s))): try: return values.W_Fixnum(rarithmetic.string_to_int(s, base=radix)) except ParseStringOverflowError: return values.W_Bignum(rbigint.rbigint.fromstr(s, base=radix)) if re.match("[+-]?([\d]+)?.?\d+[tT]\d", s): # it's an extflonum return values.W_ExtFlonum(s) if re.match("[+-]?([\d]+)?.?\d+[sf]\d", s): if "f" in s: f_parts = s.split("f") elif "s" in s: f_parts = s.split("s") else: raise ParseStringError("invalid floating point number : %s" % s) if len(f_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: numb = float(f_parts[0]) prec = int(f_parts[1]) p = math.pow(10, prec) except ValueError, e: return values.w_false return values.W_Flonum.make(numb * p, True) if re.match("[+-]?([\d]+)?.?\d+e\d", s): e_parts = s.split("e") if len(e_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: num = float(e_parts[0]) exp = int(e_parts[1]) p = math.pow(10, exp) except ValueError, e: return values.w_false return values.W_Flonum(num * p)
def define_struct(name, super=values.w_null, fields=[]): immutables = [] for i in range(len(fields)): immutables.append(values.W_Fixnum(i)) struct_type, struct_constr, struct_pred, struct_acc, struct_mut = \ values_struct.W_StructType.make_simple(values.W_Symbol.make(name), super, values.W_Fixnum(len(fields)), values.W_Fixnum(0), values.w_false, values.w_null, values.w_false, values.w_false, values.to_list(immutables)).make_struct_tuple() expose_val("struct:" + name, struct_type) expose_val(name, struct_constr) # this is almost always also provided expose_val("make-" + name, struct_constr) expose_val(name + "?", struct_pred) for field, field_name in enumerate(fields): w_num = values.W_Fixnum(field) w_name = values.W_Symbol.make(field_name) acc = values_struct.W_StructFieldAccessor(struct_acc, w_num, w_name) expose_val(name + "-" + field_name, acc) return struct_type