def compile_map_literal(form, ctx): ctx.push_const(code.intern_var(u"pixie.stdlib", u"hashmap")) rt.reduce(CompileMapRf(ctx), nil, form) size = rt.count(form).int_val() * 2 ctx.bytecode.append(code.INVOKE) ctx.bytecode.append(r_uint(size) + 1)
def read_var(rdr): ns = read_raw_string(rdr) nm = read_raw_string(rdr) is_dynamic = read_tag(rdr) var = intern_var(ns, nm) if is_dynamic is TRUE: var.set_dynamic() return var
def inner_invoke(self, args): import pixie.vm.rt as rt from pixie.vm.code import intern_var rt.load_ns(rt.wrap(u"pixie/repl.pxi")) repl = intern_var(u"pixie.repl", u"repl") with with_ns(u"user"): repl.invoke([])
def compile_form(form, ctx): if form is nil: ctx.push_const(nil) return if rt.instance_QMARK_(rt.ISeq.deref(), form) and form is not nil: return compile_cons(form, ctx) if isinstance(form, numbers.Integer): ctx.push_const(form) return if isinstance(form, symbol.Symbol): name = form._str loc = resolve_local(ctx, name) if loc is None: var = resolve_var(ctx, form) if var is None: var = NS_VAR.deref().intern_or_make(name) ctx.push_const(var) ctx.bytecode.append(code.DEREF_VAR) return loc.emit(ctx) return if isinstance(form, Bool) or form is nil: ctx.push_const(form) return if isinstance(form, Keyword): ctx.push_const(form) return if isinstance(form, PersistentVector): vector_var = rt.vector() size = rt.count(form).int_val() #assert rt.count(form).int_val() == 0 ctx.push_const(code.intern_var(u"pixie.stdlib", u"vector")) for x in range(size): compile_form(rt.nth(form, numbers.Integer(x)), ctx) ctx.bytecode.append(code.INVOKE) ctx.bytecode.append(r_uint(size + 1)) ctx.sub_sp(size) return if rt.instance_QMARK_(rt.IMap.deref(), form): compile_map_literal(form, ctx) return if isinstance(form, String): ctx.push_const(form) return raise Exception("Can't compile ")
def compile_meta(meta, ctx): ctx.push_const(code.intern_var(u"pixie.stdlib", u'with-meta')) ctx.bytecode.append(code.DUP_NTH) ctx.bytecode.append(r_uint(1)) ctx.add_sp(1) ctx.push_const(meta) ctx.bytecode.append(code.INVOKE) ctx.bytecode.append(r_uint(3)) ctx.sub_sp(2) ctx.bytecode.append(code.POP_UP_N) ctx.bytecode.append(1) ctx.sub_sp(1)
def _defprotocol(name, methods): from pixie.vm.compiler import NS_VAR from pixie.vm.persistent_vector import PersistentVector from pixie.vm.symbol import Symbol affirm(isinstance(name, Symbol), u"protocol name must be a symbol") affirm(isinstance(methods, PersistentVector), u"protocol methods must be a vector of symbols") method_list = [] for i in range(0, rt.count(methods)): method_sym = rt.nth(methods, rt.wrap(i)) affirm(isinstance(method_sym, Symbol), u"protocol methods must be a vector of symbols") method_list.append(rt.name(method_sym)) proto = Protocol(rt.name(name)) ns = rt.name(NS_VAR.deref()) intern_var(ns, rt.name(name)).set_root(proto) for method in method_list: method = unicode(method) poly = PolymorphicFn(method, proto) intern_var(ns, method).set_root(poly) return name
def compile_map_literal(form, ctx): ctx.push_const(code.intern_var(u"pixie.stdlib", u"hashmap")) rt.reduce(CompileMapRf(ctx), nil, form) size = rt.count(form) * 2 ctx.bytecode.append(code.INVOKE) ctx.bytecode.append(r_uint(size) + 1) if size > 0: ctx.sub_sp(size) compile_meta(rt.meta(form), ctx)
from pixie.vm.object import affirm from pixie.vm.primitives import nil, true, Bool from pixie.vm.persistent_vector import EMPTY, PersistentVector from pixie.vm.persistent_hash_set import PersistentHashSet import pixie.vm.numbers as numbers import pixie.vm.symbol as symbol import pixie.vm.code as code from pixie.vm.keyword import Keyword, keyword from pixie.vm.string import Character, String from pixie.vm.atom import Atom from rpython.rlib.rarithmetic import r_uint, intmask from pixie.vm.persistent_list import EmptyList import pixie.vm.rt as rt NS_VAR = code.intern_var(u"pixie.stdlib", u"*ns*") NS_VAR.set_dynamic() FN_NAME = code.intern_var(u"pixie.stdlib", u"*fn-name*") FN_NAME.set_dynamic() 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)
class DebugIFace(JitHookInterface): def on_abort(self, reason, jitdriver, greenkey, greenkey_repr, logops, operations): print "Aborted Trace, reason: ", Counters.counter_names[reason], logops, greenkey_repr import sys, pdb class Policy(JitPolicy, AnnotatorPolicy): def __init__(self): JitPolicy.__init__(self, DebugIFace()) def jitpolicy(driver): return JitPolicy(jithookiface=DebugIFace()) PROGRAM_ARGUMENTS = intern_var(u"pixie.stdlib", u"program-arguments") PROGRAM_ARGUMENTS.set_root(nil) class ReplFn(NativeFn): def __init__(self, args): self._argv = args def inner_invoke(self, args): from pixie.vm.keyword import keyword import pixie.vm.rt as rt from pixie.vm.string import String import pixie.vm.persistent_vector as vector print "Pixie 0.1 - Interactive REPL" print "(" + platform.name + ", " + platform.cc + ")"
from pixie.vm.libs.readline import _readline from pixie.vm.string import Character, String from pixie.vm.code import wrap_fn, extend from pixie.vm.persistent_hash_map import EMPTY as EMPTY_MAP from pixie.vm.persistent_hash_set import EMPTY as EMPTY_SET import pixie.vm.stdlib as proto import pixie.vm.compiler as compiler from rpython.rlib.rsre import rsre_re as re LINE_NUMBER_KW = keyword(u"line-number") COLUMN_NUMBER_KW = keyword(u"column-number") LINE_KW = keyword(u"line") FILE_KW = keyword(u"file") GEN_SYM_ENV = code.intern_var(u"pixie.stdlib.reader", u"*gen-sym-env*") GEN_SYM_ENV.set_dynamic() GEN_SYM_ENV.set_value(EMPTY_MAP) class PlatformReader(object.Object): _type = object.Type(u"PlatformReader") def read(self): assert False def unread(self, ch): pass def reset_line(self): return self
pass import sys, pdb class Policy(JitPolicy, AnnotatorPolicy): def __init__(self): JitPolicy.__init__(self, DebugIFace()) def jitpolicy(driver): return JitPolicy(jithookiface=DebugIFace()) PROGRAM_ARGUMENTS = intern_var(u"pixie.stdlib", u"program-arguments") PROGRAM_ARGUMENTS.set_root(nil) LOAD_PATHS = intern_var(u"pixie.stdlib", u"load-paths") LOAD_PATHS.set_root(nil) load_path = Var(u"pixie.stdlib", u"internal-load-path") class ReplFn(NativeFn): def __init__(self, args): self._argv = args def inner_invoke(self, args): import pixie.vm.rt as rt from pixie.vm.code import intern_var rt.load_ns(rt.wrap(u"pixie/repl.pxi"))
start = start.int_val() 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")
reason], logops, greenkey_repr import sys, pdb class Policy(JitPolicy, AnnotatorPolicy): def __init__(self): JitPolicy.__init__(self, DebugIFace()) def jitpolicy(driver): return JitPolicy(jithookiface=DebugIFace()) PROGRAM_ARGUMENTS = intern_var(u"pixie.stdlib", u"program-arguments") PROGRAM_ARGUMENTS.set_root(nil) LOAD_PATHS = intern_var(u"pixie.stdlib", u"load-paths") LOAD_PATHS.set_root(nil) load_path = Var(u"pixie.stdlib", u"internal-load-path") STAR_1 = intern_var(u"pixie.stdlib", u"*1") STAR_1.set_root(nil) STAR_2 = intern_var(u"pixie.stdlib", u"*2") STAR_2.set_root(nil) STAR_3 = intern_var(u"pixie.stdlib", u"*3") STAR_3.set_root(nil) STAR_E = intern_var(u"pixie.stdlib", u"*e") STAR_E.set_root(nil)
from pixie.vm.persistent_vector import EMPTY, PersistentVector from pixie.vm.persistent_hash_set import PersistentHashSet import pixie.vm.numbers as numbers import pixie.vm.symbol as symbol import pixie.vm.code as code from pixie.vm.keyword import Keyword, keyword from pixie.vm.string import Character, String from pixie.vm.atom import Atom from rpython.rlib.rarithmetic import r_uint, intmask from pixie.vm.persistent_list import EmptyList from pixie.vm.cons import cons from pixie.vm.persistent_list import create_from_list import pixie.vm.rt as rt NS_VAR = code.intern_var(u"pixie.stdlib", u"*ns*") NS_VAR.set_dynamic() FN_NAME = code.intern_var(u"pixie.stdlib", u"*fn-name*") FN_NAME.set_dynamic() DYNAMIC_KW = keyword(u"dynamic") gensym_id = Atom(numbers.zero_int) def gensym1(): return gensym2(rt.wrap(u"gensym_")) def gensym2(prefix):
class DebugIFace(JitHookInterface): def on_abort(self, reason, jitdriver, greenkey, greenkey_repr, logops, operations): print "Aborted Trace, reason: ", Counters.counter_names[reason], logops, greenkey_repr import sys, pdb class Policy(JitPolicy, AnnotatorPolicy): def __init__(self): JitPolicy.__init__(self, DebugIFace()) def jitpolicy(driver): return JitPolicy(jithookiface=DebugIFace()) PROGRAM_ARGUMENTS = intern_var(u"pixie.stdlib", u"program-arguments") PROGRAM_ARGUMENTS.set_root(nil) LOAD_PATHS = intern_var(u"pixie.stdlib", u"load-paths") LOAD_PATHS.set_root(nil) load_path = Var(u"pixie.stdlib", u"internal-load-path") STAR_1 = intern_var(u"pixie.stdlib", u"*1") STAR_1.set_root(nil) STAR_2 = intern_var(u"pixie.stdlib", u"*2") STAR_2.set_root(nil) STAR_3 = intern_var(u"pixie.stdlib", u"*3") STAR_3.set_root(nil) STAR_E = intern_var(u"pixie.stdlib", u"*e") STAR_E.set_root(nil)
u"Contents of load-paths must be strings") full_path = path.join(str(rt.name(path_x)), str(filename_str)) if path.isfile(full_path): f = full_path break if f is None: affirm( False, u"File '" + rt.name(filename) + u"' does not exist in any directory found in load-paths") else: rt.load_file(rt.wrap(f)) return nil PXIC_WRITER = intern_var(u"pixie.stdlib", u"*pxic-writer*") PXIC_WRITER.set_root(nil) PXIC_WRITER.set_dynamic() @as_var("load-file") def load_file(filename): return _load_file(filename, False) @as_var("compile-file") def compile_file(filename): return _load_file(filename, True) def _load_file(filename, compile=False):
class DebugIFace(JitHookInterface): def on_abort(self, reason, jitdriver, greenkey, greenkey_repr, logops, operations): # print "Aborted Trace, reason: ", Counters.counter_names[reason], logops, greenkey_repr pass import sys, pdb class Policy(JitPolicy, AnnotatorPolicy): def __init__(self): JitPolicy.__init__(self, DebugIFace()) def jitpolicy(driver): return JitPolicy(jithookiface=DebugIFace()) PROGRAM_ARGUMENTS = intern_var(u"pixie.stdlib", u"program-arguments") PROGRAM_ARGUMENTS.set_root(nil) LOAD_PATHS = intern_var(u"pixie.stdlib", u"load-paths") LOAD_PATHS.set_root(nil) load_path = intern_var(u"pixie.stdlib", u"internal-load-path") class ReplFn(NativeFn): def __init__(self, args): self._argv = args def inner_invoke(self, args): import pixie.vm.rt as rt from pixie.vm.code import intern_var rt.load_ns(rt.wrap(u"pixie/repl.pxi"))
py_object = object import pixie.vm.object as object from pixie.vm.primitives import nil, true, false import pixie.vm.persistent_hash_map as persistent_hash_map import pixie.vm.stdlib as proto from pixie.vm.code import extend, as_var, intern_var import pixie.vm.rt as rt VAR_KEY = intern_var(u"pixie.stdlib", u"key") class PersistentHashSet(object.Object): _type = object.Type(u"pixie.stdlib.PersistentHashSet") def type(self): return PersistentHashSet._type def __init__(self, meta, m): self._meta = meta self._map = m def conj(self, v): return PersistentHashSet(self._meta, self._map.assoc(v, v)) def disj(self, k): return PersistentHashSet(self._meta, self._map.without(k)) def meta(self): return self._meta def with_meta(self, meta):
def compile_form(form, ctx): if form is nil: ctx.push_const(nil) return if rt._satisfies_QMARK_(rt.ISeq.deref(), form) and form is not nil: form = macroexpand(form) return compile_cons(form, ctx) if isinstance(form, numbers.Integer): ctx.push_const(form) return if isinstance(form, numbers.BigInteger): ctx.push_const(form) return if isinstance(form, numbers.Float): ctx.push_const(form) return if isinstance(form, numbers.Ratio): ctx.push_const(form) return if isinstance(form, symbol.Symbol): name = rt.name(form) ns = rt.namespace(form) loc = resolve_local(ctx, name) var = resolve_var(ctx, form) if var is None and loc: loc.emit(ctx) return if var and loc and ns is None: loc.emit(ctx) return if var is None: name = rt.name(form) var = NS_VAR.deref().intern_or_make(name) ctx.push_const(var) meta = rt.meta(form) if meta is not nil: ctx.debug_points[len(ctx.bytecode)] = rt.interpreter_code_info(meta) ctx.bytecode.append(code.DEREF_VAR) return if isinstance(form, Bool) or form is nil: ctx.push_const(form) return if isinstance(form, Keyword): ctx.push_const(form) return if isinstance(form, PersistentVector): vector_var = rt.vector() size = rt.count(form) #assert rt.count(form).int_val() == 0 ctx.push_const(code.intern_var(u"pixie.stdlib", u"vector")) for x in range(size): compile_form(rt.nth(form, rt.wrap(x)), ctx) ctx.bytecode.append(code.INVOKE) ctx.bytecode.append(r_uint(size + 1)) ctx.sub_sp(size) compile_meta(rt.meta(form), ctx) return if isinstance(form, PersistentHashSet): compile_set_literal(form, ctx) return if rt._satisfies_QMARK_(rt.IMap.deref(), form): compile_map_literal(form, ctx) return if isinstance(form, String): ctx.push_const(form) return if isinstance(form, Character): ctx.push_const(form) return raise Exception("Can't compile ")
py_object = object import pixie.vm.object as object from pixie.vm.object import affirm from pixie.vm.primitives import nil, true, false from pixie.vm.numbers import Integer import pixie.vm.persistent_hash_map as persistent_hash_map import pixie.vm.stdlib as proto from pixie.vm.code import extend, as_var, intern_var, wrap_fn from rpython.rlib.rarithmetic import r_uint, intmask import rpython.rlib.jit as jit import pixie.vm.rt as rt from pixie.vm.iterator import MapIterator from pixie.vm.libs.pxic.util import add_marshall_handlers VAR_KEY = intern_var(u"pixie.stdlib", u"key") class PersistentHashSet(object.Object): _type = object.Type(u"pixie.stdlib.PersistentHashSet") def type(self): return PersistentHashSet._type def __init__(self, meta, m): self._meta = meta self._map = m def conj(self, v): return PersistentHashSet(self._meta, self._map.assoc(v, v)) def disj(self, k):
f = None for x in range(rt.count(paths)): path_x = rt.nth(paths, rt.wrap(x)) affirm(isinstance(path_x, string.String), u"Contents of load-paths must be strings") full_path = path.join(str(rt.name(path_x)), str(filename_str)) if path.isfile(full_path): f = full_path break if f is None: affirm(False, u"File '" + rt.name(filename) + u"' does not exist in any directory found in load-paths") else: rt.load_file(rt.wrap(f)) return nil PXIC_WRITER = intern_var(u"pixie.stdlib", u"*pxic-writer*") PXIC_WRITER.set_root(nil) PXIC_WRITER.set_dynamic() @as_var("load-file") def load_file(filename): return _load_file(filename, False) @as_var("compile-file") def compile_file(filename): return _load_file(filename, True) def _load_file(filename, compile=False): from pixie.vm.string import String from pixie.vm.util import unicode_from_utf8 import pixie.vm.reader as reader
from pixie.vm.symbol import symbol, Symbol from pixie.vm.keyword import keyword, Keyword import pixie.vm.rt as rt from pixie.vm.persistent_vector import EMPTY as EMPTY_VECTOR from pixie.vm.libs.libedit import _readline from pixie.vm.string import Character from pixie.vm.code import wrap_fn from pixie.vm.persistent_hash_map import EMPTY as EMPTY_MAP from pixie.vm.persistent_hash_set import EMPTY as EMPTY_SET from pixie.vm.persistent_list import EmptyList import pixie.vm.compiler as compiler from rpython.rlib.rbigint import rbigint from rpython.rlib.rsre import rsre_re as re READING_FORM_VAR = code.intern_var(u"pixie.stdlib", u"*reading-form*") READING_FORM_VAR.set_dynamic() READING_FORM_VAR.set_root(false) LINE_NUMBER_KW = keyword(u"line-number") COLUMN_NUMBER_KW = keyword(u"column-number") LINE_KW = keyword(u"line") FILE_KW = keyword(u"file") GEN_SYM_ENV = code.intern_var(u"pixie.stdlib.reader", u"*gen-sym-env*") GEN_SYM_ENV.set_dynamic() GEN_SYM_ENV.set_root(EMPTY_MAP) ARG_AMP = symbol(u"&") ARG_MAX = keyword(u"max-arg") ARG_ENV = code.intern_var(u"pixie.stdlib.reader", u"*arg-env*")
from pixie.vm.code import wrap_fn, extend from pixie.vm.persistent_hash_map import EMPTY as EMPTY_MAP from pixie.vm.persistent_hash_set import EMPTY as EMPTY_SET from pixie.vm.persistent_list import EmptyList import pixie.vm.stdlib as proto import pixie.vm.compiler as compiler from rpython.rlib.rbigint import rbigint from rpython.rlib.rsre import rsre_re as re LINE_NUMBER_KW = keyword(u"line-number") COLUMN_NUMBER_KW = keyword(u"column-number") LINE_KW = keyword(u"line") FILE_KW = keyword(u"file") GEN_SYM_ENV = code.intern_var(u"pixie.stdlib.reader", u"*gen-sym-env*") GEN_SYM_ENV.set_dynamic() GEN_SYM_ENV.set_value(EMPTY_MAP) ARG_AMP = symbol(u"&") ARG_MAX = keyword(u"max-arg") ARG_ENV = code.intern_var(u"pixie.stdlib.reader", u"*arg-env*") ARG_ENV.set_dynamic() ARG_ENV.set_value(nil) class PlatformReader(object.Object): _type = object.Type(u"PlatformReader") def read(self): assert False
def init(): import pixie.vm.code as code from pixie.vm.object import affirm, _type_registry from rpython.rlib.rarithmetic import r_uint from rpython.rlib.rbigint import rbigint from pixie.vm.primitives import nil, true, false from pixie.vm.string import String from pixie.vm.object import Object _type_registry.set_registry(code._ns_registry) def unwrap(fn): if isinstance(fn, code.Var) and fn.is_defined() and hasattr(fn.deref(), "_returns"): tp = fn.deref()._returns if tp is bool: def wrapper(*args): ret = fn.invoke(py_list(args)) if ret is nil or ret is false: return False return True return wrapper elif tp is r_uint: return lambda *args: fn.invoke(py_list(args)).r_uint_val() elif tp is unicode: def wrapper(*args): ret = fn.invoke(py_list(args)) if ret is nil: return None if not isinstance(ret, String): from pixie.vm.object import runtime_error runtime_error(u"Invalid return value, expected String") return ret._str return wrapper else: assert False, "Don't know how to convert" + str(tp) return lambda *args: fn.invoke(py_list(args)) if "__inited__" in globals(): return import sys #sys.setrecursionlimit(10000) # Yeah we blow the stack sometimes, we promise it's not a bug import pixie.vm.numbers as numbers import pixie.vm.bits import pixie.vm.interpreter import pixie.vm.atom import pixie.vm.reduced import pixie.vm.util import pixie.vm.array import pixie.vm.lazy_seq import pixie.vm.persistent_list import pixie.vm.persistent_hash_map import pixie.vm.persistent_hash_set import pixie.vm.custom_types import pixie.vm.map_entry import pixie.vm.libs.platform import pixie.vm.libs.ffi import pixie.vm.symbol import pixie.vm.libs.path import pixie.vm.libs.string import pixie.vm.threads import pixie.vm.string_builder numbers.init() @specialize.argtype(0) def wrap(x): if isinstance(x, bool): return true if x else false if isinstance(x, int): return numbers.Integer(x) if isinstance(x, rbigint): return numbers.BigInteger(x) if isinstance(x, float): return numbers.Float(x) if isinstance(x, unicode): return String(x) if isinstance(x, py_str): return String(unicode(x)) if isinstance(x, Object): return x if x is None: return nil if not we_are_translated(): print x, type(x) affirm(False, u"Bad wrap") globals()["wrap"] = wrap def int_val(x): affirm(isinstance(x, numbers.Number), u"Expected number") return x.int_val() globals()["int_val"] = int_val from pixie.vm.code import _ns_registry, BaseCode, munge for name, var in _ns_registry._registry[u"pixie.stdlib"]._registry.iteritems(): name = munge(name) if var.is_defined() and isinstance(var.deref(), BaseCode): globals()[name] = unwrap(var) else: globals()[name] = var import pixie.vm.bootstrap def reinit(): for name, var in _ns_registry._registry[u"pixie.stdlib"]._registry.iteritems(): name = munge(name) if name in globals(): continue if var.is_defined() and isinstance(var.deref(), BaseCode): globals()[name] = unwrap(var) else: globals()[name] = var #f = open("pixie/stdlib.pxi") #data = f.read() #f.close() #rdr = reader.MetaDataReader(reader.StringReader(unicode(data)), u"pixie/stdlib.pixie") #result = nil # # @wrap_fn # def run_load_stdlib(): # with compiler.with_ns(u"pixie.stdlib"): # while True: # form = reader.read(rdr, False) # if form is reader.eof: # return result # result = compiler.compile(form).invoke([]) # reinit() # # stacklet.with_stacklets(run_load_stdlib) init_fns = [u"reduce", u"get", u"reset!", u"assoc", u"key", u"val", u"keys", u"vals", u"vec", u"load-file", u"compile-file", u"load-ns"] for x in init_fns: globals()[py_str(code.munge(x))] = unwrap(code.intern_var(u"pixie.stdlib", x)) init_vars = [u"load-paths"] for x in init_vars: globals()[py_str(code.munge(x))] = code.intern_var(u"pixie.stdlib", x) globals()["__inited__"] = True
from pixie.vm.object import Object, _type_registry, affirm from pixie.vm.primitives import nil, true, false, Bool from pixie.vm.persistent_vector import EMPTY, PersistentVector import pixie.vm.numbers as numbers from pixie.vm.cons import cons, Cons import pixie.vm.symbol as symbol import pixie.vm.code as code from pixie.vm.keyword import Keyword from pixie.vm.string import String import pixie.vm.protocols as proto from rpython.rlib.rarithmetic import r_uint import pixie.vm.rt as rt from pixie.vm.util import * NS_VAR = code.intern_var(u"pixie.stdlib", u"*ns*") NS_VAR.set_dynamic() class with_ns(object): def __init__(self, nm): assert isinstance(nm, unicode) self._ns = nm def __enter__(self): code._dynamic_vars.push_binding_frame() NS_VAR.set_value(code._ns_registry.find_or_make(self._ns)) def __exit__(self, exc_type, exc_val, exc_tb): code._dynamic_vars.pop_binding_frame() def clone(lst):
def test_intern(): assert intern_var(u"foo", u"bar") is intern_var(u"foo", u"bar") assert intern_var(u"foo2", u"bar") is not intern_var(u"foo", u"bar") assert get_var_if_defined(u"foo", u"bar") assert get_var_if_defined(u"foo2", u"bar")
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")
def init(): import pixie.vm.code as code from pixie.vm.object import affirm, _type_registry from rpython.rlib.rarithmetic import r_uint from rpython.rlib.rbigint import rbigint from pixie.vm.primitives import nil, true, false from pixie.vm.string import String from pixie.vm.object import Object from pixie.vm.compiler import NS_VAR _type_registry.set_registry(code._ns_registry) def unwrap(fn): if isinstance(fn, code.Var) and fn.is_defined() and hasattr( fn.deref(), "_returns"): tp = fn.deref()._returns if tp is bool: def wrapper(*args): ret = fn.invoke(py_list(args)) if ret is nil or ret is false: return False return True return wrapper elif tp is r_uint: return lambda *args: fn.invoke(py_list(args)).r_uint_val() elif tp is unicode: def wrapper(*args): ret = fn.invoke(py_list(args)) if ret is nil: return None if not isinstance(ret, String): from pixie.vm.object import runtime_error runtime_error(u"Invalid return value, expected String") return ret._str return wrapper else: assert False, "Don't know how to convert" + str(tp) return lambda *args: fn.invoke(py_list(args)) if "__inited__" in globals(): return import sys #sys.setrecursionlimit(10000) # Yeah we blow the stack sometimes, we promise it's not a bug import pixie.vm.code as code import pixie.vm.numbers as numbers import pixie.vm.bits import pixie.vm.interpreter import pixie.vm.atom import pixie.vm.reduced import pixie.vm.util import pixie.vm.array import pixie.vm.lazy_seq import pixie.vm.persistent_list import pixie.vm.persistent_hash_map import pixie.vm.persistent_hash_set import pixie.vm.custom_types import pixie.vm.map_entry import pixie.vm.libs.platform import pixie.vm.libs.ffi import pixie.vm.symbol import pixie.vm.libs.path import pixie.vm.libs.string import pixie.vm.threads import pixie.vm.string_builder import pixie.vm.stacklet @specialize.argtype(0) def wrap(x): if isinstance(x, bool): return true if x else false if isinstance(x, int): return numbers.Integer(x) if isinstance(x, rbigint): return numbers.BigInteger(x) if isinstance(x, float): return numbers.Float(x) if isinstance(x, unicode): return String(x) if isinstance(x, py_str): return String(unicode(x)) if isinstance(x, Object): return x if x is None: return nil if not we_are_translated(): print x, type(x) affirm(False, u"Bad wrap") globals()["wrap"] = wrap def int_val(x): affirm(isinstance(x, numbers.Number), u"Expected number") return x.int_val() globals()["int_val"] = int_val from pixie.vm.code import _ns_registry, BaseCode, munge for name, var in _ns_registry._registry[ u"pixie.stdlib"]._registry.iteritems(): name = munge(name) if var.is_defined() and isinstance(var.deref(), BaseCode): globals()[name] = unwrap(var) else: globals()[name] = var import pixie.vm.bootstrap def reinit(): for name, var in _ns_registry._registry[ u"pixie.stdlib"]._registry.iteritems(): name = munge(name) if name in globals(): continue if var.is_defined() and isinstance(var.deref(), BaseCode): globals()[name] = unwrap(var) else: globals()[name] = var #f = open("pixie/stdlib.pxi") #data = f.read() #f.close() #rdr = reader.MetaDataReader(reader.StringReader(unicode(data)), u"pixie/stdlib.pixie") #result = nil # # @wrap_fn # def run_load_stdlib(): # with compiler.with_ns(u"pixie.stdlib"): # while True: # form = reader.read(rdr, False) # if form is reader.eof: # return result # result = compiler.compile(form).invoke([]) # reinit() # # stacklet.with_stacklets(run_load_stdlib) init_fns = [ u"reduce", u"get", u"reset!", u"assoc", u"key", u"val", u"keys", u"vals", u"vec", u"load-file", u"compile-file", u"load-ns", u"hashmap", u"cons", u"-assoc", u"-val-at" ] for x in init_fns: globals()[py_str(code.munge(x))] = unwrap( code.intern_var(u"pixie.stdlib", x)) init_vars = [u"load-paths"] for x in init_vars: globals()[py_str(code.munge(x))] = code.intern_var(u"pixie.stdlib", x) globals()[py_str(code.munge(u"ns"))] = NS_VAR globals()["__inited__"] = True globals( )["is_true"] = lambda x: False if x is false or x is nil or x is None else True numbers.init() code.init()
start = start.int_val() 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 = ""
def compile_form(form, ctx): if form is nil: ctx.push_const(nil) return if rt._satisfies_QMARK_(rt.ISeq.deref(), form) and form is not nil: form = maybe_oop_invoke(form) return compile_cons(form, ctx) if isinstance(form, numbers.Integer): ctx.push_const(form) return if isinstance(form, numbers.BigInteger): ctx.push_const(form) return if isinstance(form, numbers.Float): ctx.push_const(form) return if isinstance(form, numbers.Ratio): ctx.push_const(form) return if isinstance(form, symbol.Symbol): name = rt.name(form) ns = rt.namespace(form) loc = resolve_local(ctx, name) var = resolve_var(form) if var is None and loc: loc.emit(ctx) return if var and loc and ns is None: loc.emit(ctx) return if var is None: name = rt.name(form) var = NS_VAR.deref().intern_or_make(name) ctx.push_const(var) meta = rt.meta(form) if meta is not nil: ctx.debug_points[len( ctx.bytecode)] = rt.interpreter_code_info(meta) ctx.bytecode.append(code.DEREF_VAR) return if isinstance(form, Bool) or form is nil: ctx.push_const(form) return if isinstance(form, Keyword): ctx.push_const(form) return if isinstance(form, PersistentVector): size = rt.count(form) #assert rt.count(form).int_val() == 0 ctx.push_const(code.intern_var(u"pixie.stdlib", u"vector")) for x in range(size): compile_form(rt.nth(form, rt.wrap(x)), ctx) ctx.bytecode.append(code.INVOKE) ctx.bytecode.append(r_uint(size + 1)) ctx.sub_sp(size) compile_meta(rt.meta(form), ctx) return if isinstance(form, PersistentHashSet): compile_set_literal(form, ctx) return if rt._satisfies_QMARK_(rt.IMap.deref(), form): compile_map_literal(form, ctx) return if isinstance(form, String): ctx.push_const(form) return if isinstance(form, Character): ctx.push_const(form) return raise Exception("Can't compile ")
reason], logops, greenkey_repr import sys, pdb class Policy(JitPolicy, AnnotatorPolicy): def __init__(self): JitPolicy.__init__(self, DebugIFace()) def jitpolicy(driver): return JitPolicy(jithookiface=DebugIFace()) PROGRAM_ARGUMENTS = intern_var(u"pixie.stdlib", u"program-arguments") PROGRAM_ARGUMENTS.set_root(nil) LOAD_PATHS = intern_var(u"pixie.stdlib", u"load-paths") LOAD_PATHS.set_root(nil) load_path = Var(u"", u"internal-load-path") STAR_1 = intern_var(u"pixie.stdlib", u"*1") STAR_1.set_root(nil) STAR_2 = intern_var(u"pixie.stdlib", u"*2") STAR_2.set_root(nil) STAR_3 = intern_var(u"pixie.stdlib", u"*3") STAR_3.set_root(nil) STAR_E = intern_var(u"pixie.stdlib", u"*e") STAR_E.set_root(nil)