government

commit 0d159351c7131f98bbe83fa0f45b743e0d28b3f9

tree

parent:
0fff601606926403bf0266dd4ab33726a7814f2d

nmyk <nick@nmyk.io>

2026-11-14T13:10:53-05:00

add short-circuiting and and or

diff --git a/eval/eval.go b/eval/eval.go
index 242d5c9a99742cf77287d0473a617b94b59e59a0..4b9c3f3e21fbbd9e6feff1217c8ed39acfafb7f7 100644
--- a/eval/eval.go
+++ b/eval/eval.go
@@ -16,6 +16,8 @@ 		"quote":  evalQuote,
 		"if":     evalIf,
 		"lambda": evalLambda,
 		"define": evalMisplacedDefine,
+		"and":    evalAnd,
+		"or":     evalOr,
 	}
 }
 
diff --git a/eval/logic.go b/eval/logic.go
new file mode 100644
index 0000000000000000000000000000000000000000..9b0c47815c1b8e6b0cdcdbe84340be83402b59b1
--- /dev/null
+++ b/eval/logic.go
@@ -0,0 +1,38 @@
+package eval
+
+import "government/sexpr"
+
+// and and or are special forms rather than functions because they must be
+// able to skip evaluating their remaining operands. As in Scheme they
+// return the deciding value, not just T or F, so (or x default) is the
+// idiom for "x, or default if x is F".
+
+// (and x...) returns F at the first F operand, else the last value.
+func evalAnd(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
+	var result sexpr.Value = sexpr.T
+	for _, operand := range operands {
+		v, err := Eval(operand, env)
+		if err != nil {
+			return nil, err
+		}
+		if !isTrue(v) {
+			return sexpr.F, nil
+		}
+		result = v
+	}
+	return result, nil
+}
+
+// (or x...) returns the first operand that is not F, else F.
+func evalOr(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
+	for _, operand := range operands {
+		v, err := Eval(operand, env)
+		if err != nil {
+			return nil, err
+		}
+		if isTrue(v) {
+			return v, nil
+		}
+	}
+	return sexpr.F, nil
+}
diff --git a/eval/logic_test.go b/eval/logic_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..5436d587f915c2a0e571054bcbb585e4e3608f14
--- /dev/null
+++ b/eval/logic_test.go
@@ -0,0 +1,55 @@
+package eval
+
+import "testing"
+
+func TestAndOr(t *testing.T) {
+	runEvalCases(t, []evalCase{
+		{"(and)", "T"},
+		{"(and T)", "T"},
+		{"(and F)", "F"},
+		{"(and 1 2)", "2"},
+		{`(and T "yes")`, `"yes"`},
+		{"(and 1 F 3)", "F"},
+		{"(and 0 () \"\")", `""`},
+		{"(or)", "F"},
+		{"(or F)", "F"},
+		{"(or 7)", "7"},
+		{"(or F 7)", "7"},
+		{"(or F F)", "F"},
+		{"(or F () 3)", "()"},
+		{"(or 1 2)", "1"},
+
+		// Operands after the deciding one are never evaluated.
+		{"(and F (car '()))", "F"},
+		{"(and F boom)", "F"},
+		{"(or 1 (car '()))", "1"},
+		{"(or T boom)", "T"},
+
+		{"(and (or F 1) (or F 2))", "2"},
+		{"(or (and 1 F) (and 2 3))", "3"},
+		{"(not (and T F))", "T"},
+		{"(if (and (< 1 2) (< 2 3)) 'ordered 'not)", "ordered"},
+
+		// The "value or default" idiom.
+		{"(define lookup (lambda (k) (if (== k 'a) 1 F))) (or (lookup 'a) 0)", "1"},
+		{"(define lookup (lambda (k) (if (== k 'a) 1 F))) (or (lookup 'b) 0)", "0"},
+
+		{`(define member? (lambda (x l) (and (!= l ()) (or (== x (car l)) (member? x (cdr l))))))
+		  (cons (member? 2 '(1 2 3)) (member? 4 '(1 2 3)))`, "(T . F)"},
+	})
+}
+
+func TestAndOrErrors(t *testing.T) {
+	runErrorCases(t, []errorCase{
+		// Operands are evaluated left to right until one decides.
+		{"(and T boom)", ErrUnbound, "no statute for symbol boom"},
+		{"(or F boom)", ErrUnbound, "no statute for symbol boom"},
+		{"(and (car '()) boom)", ErrType, "car needs a pair, got ()"},
+		{"(or (car '()) boom)", ErrType, "car needs a pair, got ()"},
+
+		{"and", ErrSpecialForm, "and is a special form, not a value"},
+		{"(cons or ())", ErrSpecialForm, "or is a special form, not a value"},
+		{"(lambda (and) 1)", ErrSyntax, "lambda parameter and is a reserved name"},
+		{"(define or 1)", ErrSyntax, "cannot define reserved name or"},
+	})
+}