def stdenv_defmacro_test(): env = make_stdenv() called = 0 def f(e): nonlocal called called += 1 return SNode('num', 5) env.define('f', SNode('function', f)) seval('(~defmacro twice (a) `(+ ,a ,a))', env) eq_(seval_strip('(~twice (f))', env), 10) eq_(called, 2)
def fuzz_test_generator(): program = sparse(""" ((~lambda () (~define x 10) (~define y 20) (~define z '(1 2 3)) (~define li '(~ if (nil? '(2)) (add 4 5) (set! x 20))) (~defun add (x y) (+ x y)) (add (~if (< x y) x y) x))) """) env = make_stdenv() env.define('a', sparse('10')) env.define('b', sparse('(4 5 6)')) env.define('c', sparse('(~define t 10)')) env.define('d', sparse('(+ 20 20)')) env.define('e', seval('(~lambda (x) (* x 2))', env)) for i in range(FUZZ_TIMES): yield do_fuzz, random_modify(env, program), env
def stdenv_append_test(): eq_(seval_strip('(~append (1 2) (3 4) (5))', make_stdenv()), (1, 2, 3, 4, 5))
def stdenv_defun_test(): env = make_stdenv() seval('(~defun add (a b) (+ a b))', env) eq_(seval_strip('(add 1 2)', env), 3)
def stdenv_apply_test(): eq_(seval_strip('(apply + `(2 3))', make_stdenv()), 5)
def stdenv_restargs_test(): eq_(seval_strip('((~lambda (x . y) (apply + y)) 1 2 3)', make_stdenv()), 5)
def stdenv_eval_test(): eq_(seval_strip('(eval `(+ 1 1) (this-env))', make_stdenv()), 2)
def seval_funcall_test(): eq_(seval_strip('(+ 3 2)', make_stdenv()), 5)
def stdenv_tail_test(): eq_(seval_strip("(tail '(1 2 3))", make_stdenv()), (2, 3))
def stdenv_head_test(): eq_(seval_strip("(head '(1 2 3))", make_stdenv()), 1)
def stdenv_cons_nil_test(): eq_(seval_strip('(cons 1 (cons 2 nil))', make_stdenv()), (1, 2))
def stdenv_quote_quote_test(): eq_(seval_strip('(~quote (~quote (1 2 3)))', make_stdenv()), ('~', 'quote', (1, 2, 3)))
def stdenv_lambda_with_args_test(): eq_(seval_strip('((~lambda (x) x) 5)', make_stdenv()), 5)
def stdenv_lambda_test(): eq_(seval_strip('((~lambda () 5))', make_stdenv()), 5)
def seval_unquote_splice_test(): eq_(seval_strip("`(1 ,@'(2))", make_stdenv()), (1, 2))
def stdenv_quasiquote_test(): eq_(seval_strip('`(1 2)', make_stdenv()), (1, 2))
def stdenv_if_test(): eq_(seval_strip('(~if (= 1 1) 1 0)', make_stdenv()), 1)
def stdenv_unquote_test(): eq_(seval_strip('`(1 ,(+ 1 1))', make_stdenv()), (1, 2))
def stdenv_define_test(): env = make_stdenv() seval_strip('(~define x 5)', env) eq_(env.lookup('x'), SNode('num', 5))
def __init__(self): """Initialise the environment.""" self._env = make_stdenv()