@ 69e3ad85cc0f4df2aca759df28acafb505578821 | history
package sexpr
// Value is any GOVERNMENT value. The set of value kinds is closed: only
// this package defines them, except functions (see FunctionBase).
type Value interface {
isValue()
}
// Int overflow is an error raised by the evaluator, never wraparound.
type Int int64
// Float is always finite: operations that would produce NaN or an infinity
// are errors.
type Float float64
// String holds UTF-8; character counts (strlen) are in runes, not bytes.
type String string
// Bool is T or F. For truthiness only F is false; every other value,
// including 0 and (), is true.
type Bool bool
// Symbol equality is name equality, so a string type is already interned.
type Symbol string
// Pair is a cons cell. A list is a chain of pairs whose last cdr is Nil;
// any other final cdr makes an improper (dotted) list.
//
// Pairs are immutable, so no pair can reach itself: every list is finite
// and acyclic, and the printer and equality never need cycle detection.
type Pair struct {
Car, Cdr Value
}
type Empty struct{}
// Nil is the empty list, ().
var Nil = Empty{}
// Function is a callable value: a closure or a builtin, both defined in
// package eval.
type Function interface {
Value
Builtin() bool
// Name is a builtin's name, the name a closure was first defined
// under, or "" for an anonymous closure.
Name() string
}
// FunctionBase is embedded by eval's closures and builtins to make them
// Values.
type FunctionBase struct{}
func (FunctionBase) isValue() {}
func (Int) isValue() {}
func (Float) isValue() {}
func (String) isValue() {}
func (Bool) isValue() {}
func (Symbol) isValue() {}
func (*Pair) isValue() {}
func (Empty) isValue() {}
const (
T = Bool(true)
F = Bool(false)
)
func Cons(car, cdr Value) *Pair {
return &Pair{Car: car, Cdr: cdr}
}
func List(items ...Value) Value {
return ListWithTail(Nil, items...)
}
// ListWithTail(Int(3), Int(1), Int(2)) is (1 2 . 3).
func ListWithTail(tail Value, items ...Value) Value {
result := tail
for i := len(items) - 1; i >= 0; i-- {
result = Cons(items[i], result)
}
return result
}