def test_integration(rt, form): try: if types.is_list(form) and types.is_symbol(form.car) and form.car.value == "is": result = rt.execute(rt.ns, form) assert types.true == result, "\nIn file %s:\nIn form:\n %s\n\n%s" % (rt.__test_filename__, repr(form), pprint_assert(rt, form)) else: rt.execute(rt.ns, form) except LispException as e: pytest.fail("\nIn file %s:\nIn form:\n %s\n\nException: %s" % (rt.__test_filename__, repr(form), e))
def eval(self, scope, exp): """ If exp is a list, execute it as a function call. If exp is a symbol, resolve it. Else, return it as is. """ if types.is_list(exp) and not types.is_nil(exp): return self.execute(scope, exp) elif types.is_symbol(exp): return self.lookup(scope, exp) else: return exp
def _unquote(self, scope, sexp): if types.is_list(sexp) and not types.is_nil(sexp): if types.is_symbol(sexp.car) and sexp.car.value == "unquote": return self.rt.eval(scope, sexp.cdr.car) out = types.nil for el in sexp: if is_splice(el): for splice in self.rt.eval(scope, el.cdr.car): out = types.conj(out, splice) else: out = types.conj(out, self._unquote(scope, el)) return out else: return sexp
def cond(self, scope, args): for i in xrange(len(args)): sexp = args[i] if not types.is_list(sexp): raise LispException("argument %d of cond must be list, is %s" % (i + 1, types.type_name(sexp))) if types.is_nil(sexp) or types.is_nil(sexp.cdr): raise LispException("argument %d of cond must have a length of >= 2" % (i + 1)) for rule in args: test = self.rt.eval(scope, rule.car) if test != types.true and test != types.false: raise LispException("expr %s does not evaluate to a boolean" % repr(rule.car)) if test == types.true: for sexp in rule.cdr: rv = self.rt.eval(scope, sexp) return rv return types.nil
def is_splice(sexp): return (not types.is_nil(sexp)) and types.is_list(sexp) and types.is_symbol(sexp.car) and sexp.car.value == "unquote-splice"