government / ast / builtin.go

@ f98307e213f9f3c11b00c8edc3c367c315b9819a | history


package ast

// Names for the builtins that have no source spelling as a symbol. 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    = "()"
)

// builtinCodes is the table from DESIGN.md. A code is permanent once
// published: changing one would change the meaning of every .gov file
// already compiled, so new builtins only ever take a free code.
var builtinCodes = []struct {
	code uint64
	name string
}{
	{1, ListNode},
	{2, Dot},
	{3, True},
	{4, False},
	{5, Empty},

	{10, "quote"},
	{11, "if"},
	{12, "define"},
	{13, "lambda"},
	{14, "and"},
	{15, "or"},

	{20, "not"},
	{21, "+"},
	{22, "-"},
	{23, "*"},
	{24, "/"},
	{25, "<"},
	{26, ">"},
	{27, "<="},
	{28, ">="},
	{29, "=="},
	{30, "!="},
	{31, "cons"},
	{32, "car"},
	{33, "cdr"},
	{34, "strlen"},
	{35, "strcat"},
	{36, "str?"},
}

var (
	codeByName = map[string]uint64{}
	nameByCode = map[uint64]string{}
)

func init() {
	for _, b := range builtinCodes {
		codeByName[b.name] = b.code
		nameByCode[b.code] = b.name
	}
}

// IsBuiltin reports whether name is in namespace 0. A symbol with this name
// is always encoded as the builtin, which is why programs cannot rebind one.
func IsBuiltin(name string) bool {
	_, ok := codeByName[name]
	return ok
}