government / eval / define_test.go

@ 21ba553f2ac9aed7435e6a7744c2a33c37c9dec8 | history


package eval

import "testing"

func TestDefine(t *testing.T) {
	runEvalCases(t, []evalCase{
		{"(define y 5)", "y"},
		{"(define y 5) y", "5"},
		{"(define y 5) (define z (+ y 1)) (* y z)", "30"},
		{"(define add +) (add 2 3)", "5"},
		{"(define sq (lambda (n) (* n n))) (sq 7)", "49"},

		// Self-recursion: the name is visible inside its own definition.
		{`(define fact (lambda (n) (if (== n 0) 1 (* n (fact (- n 1))))))
		  (fact 10)`, "3628800"},
		{`(define fib (lambda (n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))
		  (fib 15)`, "610"},
		{`(define len (lambda (l) (if (== l ()) 0 (+ 1 (len (cdr l))))))
		  (len '(a b c d))`, "4"},

		// A top-level define may shadow a name from an enclosing
		// environment: the test env binds x outside the program's scope.
		{"(define x 2) x", "2"},

		// The design's scoping test, now with define.
		{`(define x 1)
		  (define f (lambda () x))
		  (define g (lambda (x) (f)))
		  (g 2)`, "1"},

		// Defines at the start of a lambda body are sequential.
		{"((lambda (x) (define y (+ x 1)) (define z (+ y 1)) z) 1)", "3"},
		{"((lambda () (define a 1) (define b (+ a 1)) (cons a (cons b ()))))", "(1 2)"},

		// Body defines are a scope inside the parameters', so they may
		// shadow a parameter. Earlier defines still see the parameter.
		{"((lambda (x) (define x 2) x) 1)", "2"},
		{"((lambda (x) (define y x) (define x 3) (+ x y)) 1)", "4"},

		// An inner define shadows an outer one without changing it.
		{"(define y 1) ((lambda () (define y 2) y))", "2"},
		{"(define y 1) ((lambda () (define y 2) y)) y", "1"},

		// Each call gets a fresh scope, so the same body can run twice.
		{`(define twice (lambda (n) (define m (* n 2)) m))
		  (+ (twice 1) (twice 10))`, "22"},

		// Closures remember the name they were first defined under.
		{"(define sq (lambda (n) (* n n))) sq", "#<lambda sq>"},
		{"(define sq (lambda (n) (* n n))) (define square sq) square", "#<lambda sq>"},
		{"(define make (lambda () (lambda () 1))) (define g (make)) g", "#<lambda>"},
		{"(define make (lambda () (lambda () 1))) make", "#<lambda make>"},
		{"(lambda () 1)", "#<lambda>"},
		{"((lambda () (define inner (lambda () 1)) inner))", "#<lambda inner>"},
	})
}

func TestDefineErrors(t *testing.T) {
	runErrorCases(t, []errorCase{
		{"(define y 1) (define y 2)", ErrImmutable, "y is already defined in this scope; bindings are immutable"},
		{"((lambda () (define a 1) (define a 2) a))", ErrImmutable, "a is already defined in this scope; bindings are immutable"},

		{"(define y (+ y 1))", ErrUnbound, "y is used before its definition is complete"},
		{"(define x (+ x 1))", ErrUnbound, "x is used before its definition is complete"},
		{"(define y (car (cons y 1)))", ErrUnbound, "y is used before its definition is complete"},
		// The new x shadows the parameter from the moment the define starts.
		{"((lambda (x) (define x (+ x 1)) x) 1)", ErrUnbound, "x is used before its definition is complete"},

		// Mutual recursion is excluded: a's closure was created before b
		// existed, so b is not in its environment.
		{"((lambda () (define a (lambda () (b))) (define b (lambda () 1)) (a)))", ErrUnbound, "no statute for symbol b"},
		{"(define even? (lambda (n) (if (== n 0) T (odd? (- n 1))))) (define odd? (lambda (n) (if (== n 0) F (even? (- n 1))))) (even? 4)", ErrUnbound, "no statute for symbol odd?"},

		{"(define)", ErrSyntax, "define takes a name and a value, got 0 operands"},
		{"(define y)", ErrSyntax, "define takes a name and a value, got 1 operand"},
		{"(define y 1 2)", ErrSyntax, "define takes a name and a value, got 3 operands"},
		{"(define 1 2)", ErrSyntax, "define needs a symbol name, got 1"},
		{"(define (f x) x)", ErrSyntax, "define needs a symbol name, got (f x)"},
		{"(define + 1)", ErrSyntax, "cannot define reserved name +"},
		{"(define if 1)", ErrSyntax, "cannot define reserved name if"},
		{"(define define 1)", ErrSyntax, "cannot define reserved name define"},

		{"(if T (define y 1))", ErrSyntax, "define is only allowed at top level or at the start of a lambda body"},
		{"(+ 1 (define y 1))", ErrSyntax, "define is only allowed at top level or at the start of a lambda body"},
		{"((lambda () ((lambda () (if T (define z 1) 0))) 1))", ErrSyntax, "define is only allowed at top level or at the start of a lambda body"},
		{"(lambda () 1 (define y 2) y)", ErrSyntax, "define must come before any expression in a lambda body"},
		{"(lambda () (define y 2))", ErrSyntax, "lambda body needs an expression after its definitions"},
		{"define", ErrSpecialForm, "define is a special form, not a value"},
		{"(lambda (define) 1)", ErrSyntax, "lambda parameter define is a reserved name"},

		// Errors name the function when it has one.
		{"(define sq (lambda (n) (* n n))) (sq)", ErrArity, "sq takes 1 argument, got 0"},
		{"(define sq (lambda (n) (* n n))) (define square sq) (square 1 2)", ErrArity, "sq takes 1 argument, got 2"},
		{"((lambda (n) n))", ErrArity, "lambda takes 1 argument, got 0"},
	})
}