government / eval / closure_test.go

@ bf971275d54b7235006309f4e84fd06723f4f70e | history


package eval

import "testing"

func TestLambda(t *testing.T) {
	runEvalCases(t, []evalCase{
		{"(lambda (x) x)", "#<lambda>"},
		{"((lambda (x) (* x x)) 7)", "49"},
		{"((lambda () 42))", "42"},
		{"((lambda (a b c) (- a b c)) 10 3 2)", "5"},
		{"((lambda (x) 1 2 x) 3)", "3"},
		{"((lambda (x) 'x) 1)", "x"},

		// Arguments are evaluated before the call; parameters bind values.
		{"((lambda (x) x) (+ 1 2))", "3"},
		{"((lambda (x y) (cons y x)) 1 2)", "(2 . 1)"},

		// Functions are values: passed in, returned, stored in lists.
		{"((lambda (f) (f 1 2)) +)", "3"},
		{"((lambda (f) (f 1 2)) (lambda (a b) (- a b)))", "-1"},
		{"((car (cons (lambda (x) (+ x 1)) ())) 41)", "42"},
		{"(((lambda (n) (lambda (x) (+ x n))) 3) 4)", "7"},

		// A closure keeps the environment it was created in. Each call to
		// the outer lambda creates a new n, captured separately.
		{"((lambda (make-adder) (+ ((make-adder 1) 10) ((make-adder 2) 10))) (lambda (n) (lambda (x) (+ x n))))", "23"},

		// Parameters shadow outer bindings, including the test env's x = 1.
		{"((lambda (x) x) 99)", "99"},
		{"x", "1"},
		{"((lambda (x) ((lambda (x) x) 2)) 1)", "2"},
		{"((lambda (y) ((lambda (x) (+ x y)) 2)) 1)", "3"},

		// The design's scoping test, with lambdas standing in for define
		// until step 1.10. f is created where x is 1; g calls f from a
		// scope where x is 2. Lexical scope gives 1; dynamic scope would
		// give 2.
		{`((lambda (x)
		     ((lambda (f)
		        ((lambda (g) (g 2))
		         (lambda (x) (f))))
		      (lambda () x)))
		   1)`, "1"},

		// Recursion without define: the function receives itself as an
		// argument (self-application). Step 1.10 makes this unnecessary.
		{`((lambda (fact) (fact fact 5))
		   (lambda (self n) (if (== n 0) 1 (* n (self self (- n 1))))))`, "120"},

		// Unbound names inside a body are only an error when evaluated.
		{"((lambda (x) (if x 1 boom)) T)", "1"},
		{"(lambda () boom)", "#<lambda>"},
	})
}

func TestLambdaErrors(t *testing.T) {
	runErrorCases(t, []errorCase{
		{"((lambda (x) x))", ErrArity, "lambda takes 1 argument, got 0"},
		{"((lambda (x) x) 1 2)", ErrArity, "lambda takes 1 argument, got 2"},
		{"((lambda () 1) 1)", ErrArity, "lambda takes 0 arguments, got 1"},
		{"((lambda (a b) a) 1)", ErrArity, "lambda takes 2 arguments, got 1"},

		{"(lambda)", ErrSyntax, "lambda takes at least 2 operands, got 0"},
		{"(lambda (x))", ErrSyntax, "lambda takes at least 2 operands, got 1"},
		{"(lambda x x)", ErrSyntax, "lambda parameters must be a list of symbols, got x"},
		{"(lambda (x . y) x)", ErrSyntax, "lambda parameters must be a list of symbols, got (x . y)"},
		{"(lambda (1) 1)", ErrSyntax, "lambda parameter 1 is not a symbol"},
		{`(lambda ("s") 1)`, ErrSyntax, `lambda parameter "s" is not a symbol`},
		{"(lambda ((x)) 1)", ErrSyntax, "lambda parameter (x) is not a symbol"},
		{"(lambda (T) 1)", ErrSyntax, "lambda parameter T is not a symbol"},
		{"(lambda (x y x) x)", ErrSyntax, "duplicate lambda parameter x"},

		{"(lambda (+) 1)", ErrSyntax, "lambda parameter + is a reserved name"},
		{"(lambda (car) 1)", ErrSyntax, "lambda parameter car is a reserved name"},
		{"(lambda (if) 1)", ErrSyntax, "lambda parameter if is a reserved name"},
		{"(lambda (lambda) 1)", ErrSyntax, "lambda parameter lambda is a reserved name"},
		{"lambda", ErrSpecialForm, "lambda is a special form, not a value"},

		{"((lambda (x) (car x)) 1)", ErrType, "car needs a pair, got 1"},
		{"((lambda (x) boom x) 1)", ErrUnbound, "no statute for symbol boom"},
		{"(== (lambda () 1) (lambda () 1))", ErrType, "cannot compare functions"},
	})
}