def lib_append(*args): """(append '(1 2) '(a b)) or (append '(1 2) 'x) """ if len(args) == 0: return NIL import copy lsts = [copy.deepcopy(x) for x in args] res = lsts[-1] for lst in reversed(lsts[:-1]): try: if lst.isnil: continue p = lst while not cdr(p).isnil: p = cdr(p) p[1] = res res = lst except SchemeError: raise SchemeError('expects proper list, given %s' % (to_str(p))) if not isinstance(res, Pair): return res if lib_islist(lsts[-1]): res.islist = True else: res.islist = False if res.islist: res.length = sum([x.length for x in args]) else: res.length = 0 return res
def testDeepParsing(self): """S-expressions that require deep recursion. """ import sys sys.setrecursionlimit(50) cases = [("'"*1000 + "x", "(quote "*1000 + "x" + ")"*1000), \ ("(x . "*1000 + "()" + ")"*1000, "(" + "x " * 999 + "x)")] for case in cases: sexp = self.parser.parse(self.tokenizer.tokenize(case[0] + '\n'))[0] self.assertEqual(to_str(sexp), case[1])
def lib_list_tail(p, start): """(list-tail '(a b c) 1) """ orig = p try: now = 0 while now != start: p = cdr(p) now += 1 return p except SchemeError: raise SchemeError('index %s is too large for %s' % (start, to_str(orig)))
def testStringRepr(self): """Test the string representation of the parsed S-expressions. """ cases = [("'x", "(quote x)"), \ ("''x", "(quote (quote x))"), \ ("'(quote 'x)", "(quote (quote (quote x)))"), \ ("'(quote 'x 'y)", "(quote (quote (quote x) (quote y)))"), \ ("(a b c)", "(a b c)"), \ ("'(a b c)", "(quote (a b c))"), \ ("(a b . c)", "(a b . c)"), \ ("(a . (b . (c . ())))", "(a b c)"), \ ("(a . (b . (c . d)))", "(a b c . d)")] for case in cases: sexp = self.parser.parse(self.tokenizer.tokenize(case[0] + '\n'))[0] self.assertEqual(to_str(sexp), case[1])
def g(p): try: return func(p) except Exception: raise SchemeError('expect %sable pairs, given %s' % (cxr, to_str(p)))
def lib_length(p): """(length '(1 2 3)) """ if not p.islist: raise SchemeError('expects proper list, given %s' % (to_str(p))) return p.length