예제 #1
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
예제 #2
0
파일: lx.py 프로젝트: kr/sodium
    def make_each(ms):
      def find_next_arity(this, arities):
          arities = [x for x in arities if x > this]
          if not arities: return MEC
          return max(MEC, min(arities))

      if ms is nil: return nil
      meth = ms.car()
      rest = make_each(ms.cdr())

      names = None
      if fixed_arityp(meth):
        names = (meth_name(meth, meth_arity(meth)),)
      else:
        names = nil
        base = meth_base_name(meth)
        this_arity = meth_arity(meth)
        next_arity = find_next_arity(this_arity, variable_arities[base])
        for i in range(this_arity, next_arity):
          if i not in fixed_arities.get(base, ()):
            names = cons(meth_name(meth, i), names)
        if this_arity == max(variable_arities[base]):
          names = cons(meth_name(meth, '+'), names)

      return cons((meth, names), rest)
예제 #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
예제 #4
0
파일: lx.py 프로젝트: kr/sodium
def collect_elifs(seq):
  if seq.nullp(): return false_s, seq
  next = seq.car()
  if tagged_list(next, else_s):
    return cons(do_s, next.cdr()), seq.cdr()
  if not tagged_list(next, elif_s): return false_s, seq
  alt, tail = collect_elifs(seq.cdr())
  return plist(do_s, cons(if_s, next.cdr()), plist(else_s, alt)), tail
예제 #5
0
파일: lx.py 프로젝트: kr/sodium
def expand_for(exp):
  param = exp.cadr()
  in_word = exp.caddr()
  seq_exp = exp.cadddr()
  body_seq = exp.cddddr()
  if in_word is not in_s:
    raise CompileError(exp, 'should be "for <expr> in <expr>"')
  f = cons(fn_s, cons(plist(param), body_seq))
  return plist(map_s, f, seq_exp)
예제 #6
0
파일: lx.py 프로젝트: kr/sodium
def set_merge(l1, l2):
  if l1.nullp(): return l2
  if l2.nullp(): return l1
  l1a = l1.car()
  l2a = l2.car()
  if l1a is l2a: return cons(l1a, set_merge(l1.cdr(), l2.cdr()))
  if l1a < l2a: return cons(l1a, set_merge(l1.cdr(), l2))
  if l1a > l2a: return cons(l2a, set_merge(l1, l2.cdr()))
  raise RuntimeError("can't happen")
예제 #7
0
def expandQuasiquotation(exp, depth=1):
    """Takes a quasiquoted expression and constructs a new quoted
    expression.

    FIXME: this function is SO evil.  *grin*  Must clean this up.
    """
    if not pair.isList(exp):
        return makeQuoted(exp)
    if isUnquoted(exp):
        if depth == 1:
            return textOfUnquotation(exp)
        else:
            return pair.list(
                Symbol("list"),
                makeQuoted(Symbol("unquote")),
                expandQuasiquotation(textOfUnquotation(exp), depth - 1))
    if isQuasiquoted(exp):
        return pair.list(
            Symbol("list"),
            makeQuoted(Symbol("quasiquote")),
            expandQuasiquotation(textOfUnquotation(exp), depth + 1))
    else:
        return pair.cons(
            Symbol("list"),
            pair.listMap(lambda subexp:
                         expandQuasiquotation(subexp, depth),
                         exp))
예제 #8
0
파일: lx.py 프로젝트: kr/sodium
def scan_out_defines(body, cenv):
    def scan(body):
        if body.nullp(): return nil, nil, nil
        exp = body.car()
        vars, exps, voids = scan(body.cdr())
        if tagged_list(exp, def_s):
            var = definition_variable(exp)
            val = definition_value(exp)
            set = plist(set__s, var, val)
            return cons(var, vars), cons(set, exps), cons(q_unassigned_s, voids)
        if tagged_list2(exp, assign_s):
            var = exp.car()
            addr, tail = find_variable(var, cenv)
            if addr is not_found_s:
              exp = assign2prefix(exp, def_s)
              val = definition_value(exp)
              set = plist(set__s, var, val)
              return cons(var, vars), cons(set, exps), cons(q_unassigned_s, voids)
            else:
              set = assign2prefix(exp, set__s)
              return vars, cons(set, exps), voids
        else:
            return vars, cons(exp, exps), voids
    vars, exps, voids = scan(body)
    if vars.nullp(): return exps
    binding = make_fn(vars, exps)
    return plist(cons(binding, voids)) # apply the fn to the void values
예제 #9
0
파일: lx.py 프로젝트: kr/sodium
def expand_if(seq):
  def help(stmt, alt):
    test = stmt.cadr()
    consequent = cons(do_s, stmt.cddr())
    return plist(qmark_s, test, consequent, alt)
  if seq.nullp(): return seq
  alternative, tail = collect_elifs(seq.cdr())
  return cons(help(seq.car(), alternative), tail)
예제 #10
0
 def testSetCarCdr(self):
     self.pe("(define x '(hello world))")
     self.pe("(define y x)")
     self.pe("(define z (cons 'hi 'earth))")
     self.pe("(set-car! x 'hi)")
     self.pe("(set-cdr! y 'earth)")
     self.assertEquals(pair.cons(Symbol("hi"), Symbol("earth")),
                       self.pe("x"))
     self.assert_(self.pe("(eq? x y)"))
     self.assert_(self.pe("(not (eq? x z))"))
     self.assert_(self.pe("(equal? x z)"))
예제 #11
0
 def testSetCarCdr(self):
     self.pe("(define x '(hello world))")
     self.pe("(define y x)")
     self.pe("(define z (cons 'hi 'earth))")
     self.pe("(set-car! x 'hi)")
     self.pe("(set-cdr! y 'earth)")
     self.assertEquals(pair.cons(Symbol("hi"), Symbol("earth")),
                       self.pe("x"))
     self.assert_(self.pe("(eq? x y)"))
     self.assert_(self.pe("(not (eq? x z))"))
     self.assert_(self.pe("(equal? x z)"))
예제 #12
0
파일: lx.py 프로젝트: kr/sodium
 def expand_import(stmt):
   def old_name(term):
     if symbolp(term): return term
     return term.car()
   def new_name(term):
     if symbolp(term): return term
     return term.caddr()
   def import_term2def(term):
     return make_def(new_name(term),
                     make_call(new_name(stmt.cadr()), old_name(term)))
   def module2def(name):
     return make_def(new_name(name), make_load_module(old_name(name)))
   terms = stmt.cddr()
   return cons(module2def(stmt.cadr()), terms.map(import_term2def))
예제 #13
0
파일: lx.py 프로젝트: kr/sodium
def compile_meth_body(meth, meth_entry, cenv):
    pop_all_symbol = make_label('pop-all')
    if is_inline_meth(meth):
        return compile_inline_meth_body(meth, meth_entry, cenv)
    if is_asm_meth(meth): return compile_asm_meth_body(meth, meth_entry, cenv)
    formals = meth_params(meth)
    body = meth_body(meth)
    cenv = cons(formals, cenv)
    return append_ir_seqs(
        make_ir_seq((proc_r,), (env_r,),
            backptr(),
            meth_entry,
            closure_env(env_r, proc_r)),
        compile_literal(formals, tmp_r, next_s, cenv, pop_all_symbol),
        make_ir_seq((env_r, tmp_r, argl_r), (env_r,),
            extend_environment(env_r, env_r, argl_r, tmp_r)),
        compile_sequence(body, val_r, return_s, cenv, pop_all_symbol))
예제 #14
0
파일: lx.py 프로젝트: kr/sodium
 def scan(body):
     if body.nullp(): return nil, nil, nil
     exp = body.car()
     vars, exps, voids = scan(body.cdr())
     if tagged_list(exp, def_s):
         var = definition_variable(exp)
         val = definition_value(exp)
         set = plist(set__s, var, val)
         return cons(var, vars), cons(set, exps), cons(q_unassigned_s, voids)
     if tagged_list2(exp, assign_s):
         var = exp.car()
         addr, tail = find_variable(var, cenv)
         if addr is not_found_s:
           exp = assign2prefix(exp, def_s)
           val = definition_value(exp)
           set = plist(set__s, var, val)
           return cons(var, vars), cons(set, exps), cons(q_unassigned_s, voids)
         else:
           set = assign2prefix(exp, set__s)
           return vars, cons(set, exps), voids
     else:
         return vars, cons(exp, exps), voids
def expandQuasiquotation(exp, depth=1):
    """Takes a quasiquoted expression and constructs a new quoted
    expression.

    FIXME: this function is SO evil.  *grin*  Must clean this up.
    """
    if not pair.isList(exp):
        return makeQuoted(exp)
    if isUnquoted(exp):
        if depth == 1:
            return textOfUnquotation(exp)
        else:
            return pair.list(
                Symbol("list"), makeQuoted(Symbol("unquote")),
                expandQuasiquotation(textOfUnquotation(exp), depth - 1))
    if isQuasiquoted(exp):
        return pair.list(
            Symbol("list"), makeQuoted(Symbol("quasiquote")),
            expandQuasiquotation(textOfUnquotation(exp), depth + 1))
    else:
        return pair.cons(
            Symbol("list"),
            pair.listMap(lambda subexp: expandQuasiquotation(subexp, depth),
                         exp))
예제 #16
0
def makeAnd(clauses):
    return pair.cons(symbol.Symbol("AND"), clauses)    
예제 #17
0
파일: lx.py 프로젝트: kr/sodium
def make_fn(parameters, body):
    return cons(fn_s, cons(parameters, body))
예제 #18
0
파일: lx.py 프로젝트: kr/sodium
def make_obj(tos):
    return cons(obj_s, tos)
def letBindingValues(bindings):
    if pair.isNull(bindings): return pair.list()
    return pair.cons(pair.cadr(pair.car(bindings)),
                     letBindingValues(pair.cdr(bindings)))
예제 #20
0
def makeApplication(operators, operands):
    return pair.cons(operators, operands)
예제 #21
0
파일: lx.py 프로젝트: kr/sodium
 def help(stmt, alt):
   test = stmt.cadr()
   consequent = cons(do_s, stmt.cddr())
   return plist(qmark_s, test, consequent, alt)
예제 #22
0
 def c_rest_eaten(restVal):
     return pogo.bounce(cont, pair.cons(firstVal, restVal))
예제 #23
0
파일: evaluator.py 프로젝트: EvelynHf/basil
 def c2(tail_val):
     return pogo.bounce(cont, pair.cons(head_val, tail_val))
예제 #24
0
def makeBegin(seq):
    return pair.cons(Symbol("begin"), seq)
예제 #25
0
파일: lx.py 프로젝트: kr/sodium
def set_minus1(l, x):
  if l.nullp(): return nil
  a = l.car()
  d = set_minus1(l.cdr(), x)
  return d if a is x else cons(a, d)
def makeBegin(seq):
    return pair.cons(Symbol("begin"), seq)
def letToApplication(exp):
    bindings = letBindings(exp)
    return pair.cons(makeLambda(letBindingVariables(bindings), letBody(exp)),
                     letBindingValues(bindings))
예제 #28
0
파일: lx.py 프로젝트: kr/sodium
 def export_term2to(term):
   if symbolp(term): return cons(plist(term), plist(term))
   return cons(plist(term.caddr()), plist(term.car()))
예제 #29
0
파일: analyzer.py 프로젝트: EvelynHf/basil
 def eval_rest_cont(tailVal):
     return pogo.bounce(cont, pair.cons(headVal, tailVal))
예제 #30
0
파일: lx.py 프로젝트: kr/sodium
def if_macro_alternative(seq):
  if seq.nullp(): return false_s
  next = seq.car()
  if tagged_list(next, else_s): return cons(do_s, next.cdr())
  return false_s
예제 #31
0
def makeOr(clauses):
    return pair.cons(symbol.Symbol("OR"), clauses)
예제 #32
0
 def testRecursiveExpressionsDontKillUs(self):
     ## Makes sure that circular structures do not kill us.
     loopyList = pair.cons(1, 1)
     pair.setCdrBang(loopyList, loopyList)
     self.assertEquals("(1 . [...])", toString(loopyList))
예제 #33
0
파일: lx.py 프로젝트: kr/sodium
def exp_operands(exp):
    if not message_send(exp): return exp.cdr()
    if call_app(exp): return exp.cddr()
    msg = plist(quote_s, extract_message(exp.cadr()))
    argl = cons(S('list'), exp.cddr())
    if send_app(exp): return plist(exp.car(), msg, argl)
예제 #34
0
 def testDottedPairs(self):
     self.assertEquals("(1 . 2)", toString(pair.cons(1, 2)))
예제 #35
0
파일: lx.py 프로젝트: kr/sodium
def fn2obj(exp):
    sig = cons(run_s, fn_params(exp))
    body = fn_body(exp)
    return make_obj(plist(cons(sig, body)))
def makeApplication(operators, operands):
    return pair.cons(operators, operands)
예제 #37
0
파일: reader.py 프로젝트: kr/sodium
 def match_loop(self, parse, *sentinels):
     if self.peek in sentinels: return nil
     first = parse()
     return cons(first, self.match_loop(parse, *sentinels))
 def testDottedPairs(self):
     self.assertEquals("(1 . 2)", toString(pair.cons(1, 2)))
 def testRecursiveExpressionsDontKillUs(self):
     ## Makes sure that circular structures do not kill us.
     loopyList = pair.cons(1, 1)
     pair.setCdrBang(loopyList, loopyList)
     self.assertEquals("(1 . [...])", toString(loopyList))
예제 #40
0
파일: parser.py 프로젝트: EvelynHf/basil
 def c_rest_eaten(restVal):
     return pogo.bounce(cont, pair.cons(firstVal, restVal))
예제 #41
0
 def c2(tail_val):
     return pogo.bounce(cont, pair.cons(head_val, tail_val))