@ 6025dda55b1abdf77b71aa1d3aa49f2f5b7180b5 | history
// Package builtin is the one list of GOVERNMENT's builtins: for each, its
// name, its permanent OID code, its kind and how many operands it takes.
// The OID encoding (package ast) and the evaluator (package eval) both read
// it, so they cannot disagree. eval supplies the implementations and
// refuses to start if any builtin lacks one.
package builtin
import "fmt"
type Kind int
const (
// Structural nodes shape the tree and are never values or names.
Structural Kind = iota
// Constants are values with no symbol spelling: the reader makes them.
Constant
// Special forms see their operands unevaluated.
SpecialForm
// Functions are values bound to their names in every program.
Function
)
func (k Kind) String() string {
return [...]string{"structural", "constant", "special form", "function"}[k]
}
// Names of the builtins that are not symbols. The reader turns "()", "T"
// and "F" into values and handles "." itself, so none of these can collide
// with a user symbol.
const (
ListNode = "(...)"
Dot = "."
True = "T"
False = "F"
Empty = "()"
)
// Spec says everything about one builtin except how it runs.
type Spec struct {
// Code is the builtin's whole OID payload, GOV.0.<id>.<code>. A code is
// permanent once published: changing one would change the meaning of
// every .gov file already compiled, so a new builtin takes a free code
// in its kind's range (structural 1-2, constants 3-5, special forms
// 10-15, functions 20 and up).
Code uint64
Name string
Kind Kind
// Min and Max bound the operands of a special form or the arguments of
// a function. Max < 0 means no upper bound.
Min, Max int
}
// All is every builtin, in code order.
var All = []Spec{
{1, ListNode, Structural, 0, 0},
{2, Dot, Structural, 0, 0},
{3, True, Constant, 0, 0},
{4, False, Constant, 0, 0},
{5, Empty, Constant, 0, 0},
{10, "quote", SpecialForm, 1, 1},
{11, "if", SpecialForm, 2, 3},
{12, "define", SpecialForm, 2, 2},
{13, "lambda", SpecialForm, 2, -1}, // a parameter list and a body of one or more forms
{14, "and", SpecialForm, 0, -1},
{15, "or", SpecialForm, 0, -1},
{20, "not", Function, 1, 1},
{21, "+", Function, 0, -1},
{22, "-", Function, 1, -1},
{23, "*", Function, 0, -1},
{24, "/", Function, 1, -1},
{25, "<", Function, 2, 2},
{26, ">", Function, 2, 2},
{27, "<=", Function, 2, 2},
{28, ">=", Function, 2, 2},
{29, "==", Function, 2, 2},
{30, "!=", Function, 2, 2},
{31, "cons", Function, 2, 2},
{32, "car", Function, 1, 1},
{33, "cdr", Function, 1, 1},
{34, "strlen", Function, 1, 1},
{35, "strcat", Function, 0, -1},
{36, "str?", Function, 1, 1},
}
var (
byName = map[string]Spec{}
byCode = map[uint64]Spec{}
)
func init() {
for _, s := range All {
if _, dup := byName[s.Name]; dup {
panic("builtin: two builtins named " + s.Name)
}
if _, dup := byCode[s.Code]; dup {
panic(fmt.Sprintf("builtin: two builtins with code %d", s.Code))
}
byName[s.Name] = s
byCode[s.Code] = s
}
}
func Lookup(name string) (Spec, bool) {
s, ok := byName[name]
return s, ok
}
func ByCode(code uint64) (Spec, bool) {
s, ok := byCode[code]
return s, ok
}
// IsReserved reports whether name is a special form or function. A source
// symbol with such a name is the builtin, in its own namespace, so a
// program can never define or bind it.
func IsReserved(name string) bool {
s, ok := byName[name]
return ok && (s.Kind == SpecialForm || s.Kind == Function)
}
// CheckCount reports whether n operands (or arguments) are allowed, and if
// not, says how many are, e.g. "if takes 2 or 3 operands, got 4". noun is
// "operand" for a special form and "argument" for a function.
func (s Spec) CheckCount(n int, noun string) error {
if n >= s.Min && (s.Max < 0 || n <= s.Max) {
return nil
}
var takes string
switch {
case s.Max < 0:
takes = "at least " + plural(s.Min, noun)
case s.Min == s.Max:
takes = plural(s.Min, noun)
case s.Max == s.Min+1:
takes = fmt.Sprintf("%d or %d %ss", s.Min, s.Max, noun)
default:
takes = fmt.Sprintf("%d to %d %ss", s.Min, s.Max, noun)
}
return fmt.Errorf("%s takes %s, got %d", s.Name, takes, n)
}
func plural(n int, noun string) string {
if n == 1 {
return "1 " + noun
}
return fmt.Sprintf("%d %ss", n, noun)
}