from pixie.vm.interpreter import ShallowContinuation from rpython.rlib.objectmodel import we_are_translated defprotocol("pixie.stdlib", "ISeq", ["-first", "-next"]) defprotocol("pixie.stdlib", "ISeqable", ["-seq"]) defprotocol("pixie.stdlib", "ICounted", ["-count"]) defprotocol("pixie.stdlib", "IIndexed", ["-nth", "-nth-not-found"]) defprotocol("pixie.stdlib", "IPersistentCollection", ["-conj", "-disj"]) defprotocol("pixie.stdlib", "IEmpty", ["-empty"]) defprotocol("pixie.stdlib", "IObject", ["-hash", "-eq", "-str", "-repr"]) _eq.set_default_fn(wrap_fn(lambda a, b: false)) defprotocol("pixie.stdlib", "IReduce", ["-reduce"]) defprotocol("pixie.stdlib", "IDeref", ["-deref"]) defprotocol("pixie.stdlib", "IReset", ["-reset!"]) defprotocol("pixie.stdlib", "INamed", ["-namespace", "-name"]) defprotocol("pixie.stdlib", "IAssociative", ["-assoc", "-contains-key", "-dissoc"]) defprotocol("pixie.stdlib", "ILookup", ["-val-at"]) defprotocol("pixie.stdlib", "IMapEntry", ["-key", "-val"])
DYNAMIC_KW = keyword(u"dynamic") gensym_id = Atom(numbers.zero_int) def gensym1(): return gensym2(rt.wrap(u"gensym_")) def gensym2(prefix): rt.reset_BANG_(gensym_id, rt._add(rt.deref(gensym_id), rt.wrap(1))) i = rt.deref(gensym_id) return rt.symbol(rt.str(prefix, i)) gensym = code.intern_var(u"pixie.stdlib", u"gensym") gensym.set_root(code.MultiArityFn(u"gensym", {0: code.wrap_fn(gensym1), 1: code.wrap_fn(gensym2)})) class with_ns(object): def __init__(self, nm, include_stdlib=False): assert isinstance(nm, unicode) self._ns = nm self._include_stdlib=include_stdlib def __enter__(self): code._dynamic_vars.push_binding_frame() NS_VAR.set_value(code._ns_registry.find_or_make(self._ns)) if self._include_stdlib: NS_VAR.deref().include_stdlib() def __exit__(self, exc_type, exc_val, exc_tb): code._dynamic_vars.pop_binding_frame()
if start >= 0: return rt.wrap(rt.name(a).find(rt.name(sep), start)) else: runtime_error(u"Third argument must be a non-negative integer") def index_of4(a, sep, start, end): affirm(isinstance(start, Integer) and isinstance(end, Integer), u"Third and fourth argument must be integers") start = start.int_val() end = end.int_val() if start >= 0 and end >= 0: return rt.wrap(rt.name(a).find(rt.name(sep), start, end)) else: runtime_error(u"Third and fourth argument must be non-negative integers") index_of = intern_var(u"pixie.string.internal", u"index-of") index_of.set_root(MultiArityFn(u"index-of", {2: wrap_fn(index_of2), 3: wrap_fn(index_of3), 4: wrap_fn(index_of4)}, required_arity = 2)) def substring2(a, start): return substring3(a, start, rt._count(a)) def substring3(a, start, end): affirm(isinstance(a, String), u"First argument must be a string") affirm(isinstance(start, Integer) and isinstance(end, Integer), u"Second and third argument must be integers") start = start.int_val() end = end.int_val() if start >= 0 and end >= 0: return rt.wrap(rt.name(a)[start:end]) else: runtime_error(u"Second and third argument must be non-negative integers")
def gensym1(): return gensym2(rt.wrap(u"gensym_")) def gensym2(prefix): rt.reset_BANG_(gensym_id, rt._add(rt.deref(gensym_id), rt.wrap(1))) i = rt.deref(gensym_id) return rt.symbol(rt.str(prefix, i)) gensym = code.intern_var(u"pixie.stdlib", u"gensym") gensym.set_root( code.MultiArityFn(u"gensym", { 0: code.wrap_fn(gensym1), 1: code.wrap_fn(gensym2) })) class with_ns(object): def __init__(self, nm, include_stdlib=False): assert isinstance(nm, unicode) self._ns = nm self._include_stdlib = include_stdlib def __enter__(self): code._dynamic_vars.push_binding_frame() NS_VAR.set_value(code._ns_registry.find_or_make(self._ns)) if self._include_stdlib: NS_VAR.deref().include_stdlib()
def type(self): return Ratio._type IMath = as_var("IMath")(Protocol(u"IMath")) _add = as_var("-add")(DoublePolymorphicFn(u"-add", IMath)) _sub = as_var("-sub")(DoublePolymorphicFn(u"-sub", IMath)) _mul = as_var("-mul")(DoublePolymorphicFn(u"-mul", IMath)) _div = as_var("-div")(DoublePolymorphicFn(u"-div", IMath)) _quot = as_var("-quot")(DoublePolymorphicFn(u"-quot", IMath)) _rem = as_var("-rem")(DoublePolymorphicFn(u"-rem", IMath)) _lt = as_var("-lt")(DoublePolymorphicFn(u"-lt", IMath)) _gt = as_var("-gt")(DoublePolymorphicFn(u"-gt", IMath)) _lte = as_var("-lte")(DoublePolymorphicFn(u"-lte", IMath)) _gte = as_var("-gte")(DoublePolymorphicFn(u"-gte", IMath)) _num_eq = as_var("-num-eq")(DoublePolymorphicFn(u"-num-eq", IMath)) _num_eq.set_default_fn(wrap_fn(lambda a, b: false)) as_var("MAX-NUMBER")(Integer(100000)) # TODO: set this to a real max number num_op_template = """@extend({pfn}, {ty1}._type, {ty2}._type) def {pfn}_{ty1}_{ty2}(a, b): assert isinstance(a, {ty1}) and isinstance(b, {ty2}) return {wrap_start}a.{conv1}() {op} b.{conv2}(){wrap_end} """ def extend_num_op(pfn, ty1, ty2, conv1, op, conv2, wrap_start = "rt.wrap(", wrap_end = ")"): tp = num_op_template.format(pfn=pfn, ty1=ty1.__name__, ty2=ty2.__name__, conv1=conv1, op=op, conv2=conv2, wrap_start=wrap_start, wrap_end=wrap_end) exec tp
affirm( isinstance(start, Integer) and isinstance(end, Integer), u"Third and fourth argument must be integers") start = start.int_val() end = end.int_val() if start >= 0 and end >= 0: return rt.wrap(rt.name(a).find(rt.name(sep), start, end)) else: runtime_error( u"Third and fourth argument must be non-negative integers") index_of = intern_var(u"pixie.string.internal", u"index-of") index_of.set_root( MultiArityFn(u"index-of", { 2: wrap_fn(index_of2), 3: wrap_fn(index_of3), 4: wrap_fn(index_of4) }, required_arity=2)) def substring2(a, start): return substring3(a, start, rt._count(a)) def substring3(a, start, end): affirm(isinstance(a, String), u"First argument must be a string") affirm( isinstance(start, Integer) and isinstance(end, Integer), u"Second and third argument must be integers")
add_marshall_handlers(Ratio._type, ratio_write, ratio_read) IMath = as_var("IMath")(Protocol(u"IMath")) _add = as_var("-add")(DoublePolymorphicFn(u"-add", IMath)) _sub = as_var("-sub")(DoublePolymorphicFn(u"-sub", IMath)) _mul = as_var("-mul")(DoublePolymorphicFn(u"-mul", IMath)) _div = as_var("-div")(DoublePolymorphicFn(u"-div", IMath)) _quot = as_var("-quot")(DoublePolymorphicFn(u"-quot", IMath)) _rem = as_var("-rem")(DoublePolymorphicFn(u"-rem", IMath)) _lt = as_var("-lt")(DoublePolymorphicFn(u"-lt", IMath)) _gt = as_var("-gt")(DoublePolymorphicFn(u"-gt", IMath)) _lte = as_var("-lte")(DoublePolymorphicFn(u"-lte", IMath)) _gte = as_var("-gte")(DoublePolymorphicFn(u"-gte", IMath)) _num_eq = as_var("-num-eq")(DoublePolymorphicFn(u"-num-eq", IMath)) _num_eq.set_default_fn(wrap_fn(lambda a, b: false)) IUncheckedMath = as_var("IUncheckedMath")(Protocol(u"IUncheckedMath")) _unchecked_add = as_var("-unchecked-add")(DoublePolymorphicFn( u"-unchecked-add", IUncheckedMath)) _unchecked_subtract = as_var("-unchecked-subtract")(DoublePolymorphicFn( u"-unchecked-subtract", IUncheckedMath)) _unchecked_multiply = as_var("-unchecked-multiply")(DoublePolymorphicFn( u"-unchecked-multiply", IUncheckedMath)) as_var("MAX-NUMBER")(Integer(100000)) # TODO: set this to a real max number num_op_template = """@extend({pfn}, {ty1}._type, {ty2}._type) def {pfn}_{ty1}_{ty2}(a, b): assert isinstance(a, {ty1}) and isinstance(b, {ty2}) return {wrap_start}a.{conv1}() {op} b.{conv2}(){wrap_end}
if start > 0: return rt.wrap(rt.name(a).find(rt.name(sep), start)) else: runtime_error(u"Third argument must be a non-negative integer") def index_of4(a, sep, start, end): affirm(isinstance(start, Integer) and isinstance(end, Integer), u"Third and fourth argument must be integers") start = start.int_val() end = end.int_val() if start > 0 and end > 0: return rt.wrap(rt.name(a).find(rt.name(sep), start, end)) else: runtime_error(u"Third and fourth argument must non-negative integers") index_of = intern_var(u"pixie.string", u"index-of") index_of.set_root(MultiArityFn({2: wrap_fn(index_of2), 3: wrap_fn(index_of3), 4: wrap_fn(index_of4)}, required_arity = 2)) @as_var("pixie.string", "upper-case") def upper_case(a): a = rt.name(a) res = "" for ch in a: res += chr(unicodedb.toupper(ord(ch))) return rt.wrap(res) @as_var("pixie.string", "lower-case") def lower_case(a): a = rt.name(a) res = "" for ch in a:
import rpython.rlib.jit as jit import rpython.rlib.rstacklet as rstacklet from rpython.rlib.rarithmetic import r_uint defprotocol("pixie.stdlib", "ISeq", ["-first", "-next"]) defprotocol("pixie.stdlib", "ISeqable", ["-seq"]) defprotocol("pixie.stdlib", "ICounted", ["-count"]) defprotocol("pixie.stdlib", "IIndexed", ["-nth"]) defprotocol("pixie.stdlib", "IPersistentCollection", ["-conj"]) defprotocol("pixie.stdlib", "IObject", ["-hash", "-eq", "-str", "-repr"]) _eq.set_default_fn(wrap_fn(lambda a, b: false)) defprotocol("pixie.stdlib", "IReduce", ["-reduce"]) defprotocol("pixie.stdlib", "IDeref", ["-deref"]) defprotocol("pixie.stdlib", "IReset", ["-reset!"]) defprotocol("pixie.stdlib", "INamed", ["-namespace", "-name"]) defprotocol("pixie.stdlib", "IAssociative", ["-assoc"]) defprotocol("pixie.stdlib", "ILookup", ["-val-at"]) defprotocol("pixie.stdlib", "IMapEntry", ["-key", "-val"])