Exemplo n.º 1
0
def installPythonFunction(name, function, env, wrapDefaults=True):
    """Installs a new Python function as a primitive into the given
    environment 'env'"""
    if wrapDefaults:
        wrappedProcedure = expressions.makePrimitiveProcedure(
            wrapPrimitiveWithDefaults(function))
    else:
        wrappedProcedure = expressions.makePrimitiveProcedure(function)
    environment.defineVariable(Symbol(name), wrappedProcedure,
                               env)
Exemplo n.º 2
0
def installPythonFunction(name, function, env, wrapDefaults=True):
    """Installs a new Python function as a primitive into the given
    environment 'env'"""
    if wrapDefaults:
        wrappedProcedure = expressions.makePrimitiveProcedure(
            wrapPrimitiveWithDefaults(function))
    else:
        wrappedProcedure = expressions.makePrimitiveProcedure(function)
    environment.defineVariable(Symbol(name), wrappedProcedure,
                               env)
Exemplo n.º 3
0
 def toscheme(self, val, shallow=False):
     "Convert a Python value to a Scheme value."
     if callable(val):
         val = self._wrap_python_callable(val, shallow)
         return expressions.makePrimitiveProcedure(val)
     if val is True:
         return symbol.true
     if val is False:
         return symbol.false
     if type(val) is schemepy.types.Symbol:
         return symbol.Symbol(val.name)
     if type(val) is schemepy.types.Cons:
         car = val.car
         cdr = val.cdr
         if not shallow:
             car = self.toscheme(car)
             cdr = self.toscheme(cdr)
         return pair.cons(car, cdr)
     if isinstance(val, list):
         lst = pair.NIL
         for el in reversed(val):
             if not shallow:
                 el = self.toscheme(el)
             lst = pair.cons(el, lst)
         return lst
     if isinstance(val, dict):
         lst = pair.NIL
         for key, value in val.items():
             key = self.toscheme(key)
             if not shallow:
                 value = self.toscheme(value)
             lst = pair.cons(pair.cons(key, value), lst)
         return lst
     return val
Exemplo n.º 4
0
 def toscheme(self, val, shallow=False):
     "Convert a Python value to a Scheme value."
     if callable(val):
         val = self._wrap_python_callable(val, shallow)
         return expressions.makePrimitiveProcedure(val)
     if val is True:
         return symbol.true
     if val is False:
         return symbol.false
     if type(val) is schemepy.types.Symbol:
         return symbol.Symbol(val.name)
     if type(val) is schemepy.types.Cons:
         car = val.car
         cdr = val.cdr
         if not shallow:
             car = self.toscheme(car)
             cdr = self.toscheme(cdr)
         return pair.cons(car, cdr)
     if isinstance(val, list):
         lst = pair.NIL
         for el in reversed(val):
             if not shallow:
                 el = self.toscheme(el)
             lst = pair.cons(el, lst)
         return lst
     if isinstance(val, dict):
         lst = pair.NIL
         for key, value in val.items():
             key = self.toscheme(key)
             if not shallow:
                 value = self.toscheme(value)
             lst = pair.cons(pair.cons(key, value), lst)
         return lst
     return val