Documentation
¶
Overview ¶
Package forms provides a shared registry for special form validators.
The package maps Scheme keywords (if, lambda, define, etc.) to their validation functions. This decouples the validate and machine/compilation packages by providing a shared name table that both can reference.
A form's codegen function is registered here too, on FormSpec.Compile. That field is [any]-typed because its concrete type (machine/compilation.CompilerFunc) cannot be named without a forms → machine/compilation import cycle. machine/compilation asserts it back to the typed signature at the dispatch site, and its VerifyCompilers backstops a mis-typed Compile.
Registration ¶
forms.RegisterValidator("if", validateIf)
forms.RegisterCompiler("if", compileIf)
Lookup ¶
spec := forms.Lookup("if")
if spec != nil && spec.Validate != nil {
result := spec.Validate(ctx, env, pair, validationResult)
}
Index ¶
- func Names() []string
- func Register(spec *FormSpec)
- func RegisterCompiler(name string, compile any)
- func RegisterValidator(name string, fn ValidatorFunc)
- func Verify() error
- type FormRegistry
- func (r *FormRegistry) Clone() *FormRegistry
- func (r *FormRegistry) Lookup(name string) *FormSpec
- func (r *FormRegistry) Names() []string
- func (r *FormRegistry) Register(spec *FormSpec)
- func (r *FormRegistry) RegisterCompiler(name string, compile any)
- func (r *FormRegistry) RegisterValidator(name string, fn ValidatorFunc)
- func (r *FormRegistry) Remove(name string)
- func (r *FormRegistry) Verify() error
- type FormSpec
- type ValidatedExpr
- type ValidatorFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
func Register(spec *FormSpec)
Register adds or replaces a FormSpec in the package default.
func RegisterCompiler ¶
RegisterCompiler sets a compiler on the package default.
func RegisterValidator ¶
func RegisterValidator(name string, fn ValidatorFunc)
RegisterValidator sets a validator on the package default.
Types ¶
type FormRegistry ¶
type FormRegistry struct {
// contains filtered or unexported fields
}
FormRegistry is a per-engine set of special-form specs. The package-level default (defaultRegistry) backs the delegator functions and the built-in R7RS forms; a dialect forks a Clone at engine origin (see wile/engine.go).
func DefaultRegistry ¶
func DefaultRegistry() *FormRegistry
DefaultRegistry returns the package default (the R7RS baseline forms).
func NewFormRegistry ¶
func NewFormRegistry() *FormRegistry
NewFormRegistry returns an empty registry.
func RegistryFor ¶
func RegistryFor(env *environment.EnvironmentFrame) *FormRegistry
RegistryFor returns the per-engine FormRegistry reachable from env, or the package default when env is nil, has no Namespace, or has no registry set. This guard absorbs every nil-env / fresh-namespace caller with no signature change at the readers.
func (*FormRegistry) Clone ¶
func (r *FormRegistry) Clone() *FormRegistry
Clone returns a shallow copy. Safe because every registrar goes through update, which is copy-on-write, so an override on a clone assigns a fresh FormSpec rather than mutating the one it shares with the registry it forked from.
func (*FormRegistry) Lookup ¶
func (r *FormRegistry) Lookup(name string) *FormSpec
Lookup returns the FormSpec for a keyword, or nil if not found.
func (*FormRegistry) Names ¶
func (r *FormRegistry) Names() []string
Names returns all registered form names (unordered).
func (*FormRegistry) Register ¶
func (r *FormRegistry) Register(spec *FormSpec)
Register adds or replaces a complete FormSpec.
func (*FormRegistry) RegisterCompiler ¶
func (r *FormRegistry) RegisterCompiler(name string, compile any)
RegisterCompiler sets the compiler for a form, symmetric with RegisterValidator. compile is [any]-typed (a machine/compilation.CompilerFunc) to break the import cycle.
func (*FormRegistry) RegisterValidator ¶
func (r *FormRegistry) RegisterValidator(name string, fn ValidatorFunc)
RegisterValidator sets the validator for a form, preserving every other field already registered so validator and compiler co-locate on one spec.
func (*FormRegistry) Remove ¶
func (r *FormRegistry) Remove(name string)
Remove deletes a form so a dialect can drop it: the validator disappears and the keyword is thereafter validated as a call. No-op if the form is absent.
func (*FormRegistry) Verify ¶
func (r *FormRegistry) Verify() error
Verify checks that every registered form has a validator. Returns an error listing any forms with missing validators, or nil if all are consistent.
Compiler registration consistency is checked separately by machine/compilation.VerifyCompilers, which has access to the typed compiler registry without requiring any type erasure.
type FormSpec ¶
type FormSpec struct {
// Name is the keyword that triggers this form (e.g., "if", "lambda").
Name string
// Validate is called during the validation phase to produce a ValidatedExpr.
// If nil, the form passes through as ValidatedLiteral.
Validate ValidatorFunc
// Compile holds the form's codegen function. It is [any]-typed — the concrete
// type is machine/compilation.CompilerFunc, which forms cannot name without a
// forms → machine/compilation import cycle (mirrors ValidatorFunc's result any).
// Asserted at the dispatch site in machine/compilation (comma-ok); a checked
// assertion in VerifyCompilers backstops a mis-typed Compile at test time.
Compile any
}
FormSpec defines how a special form is validated.
The result parameter in ValidatorFunc uses [any] to break the forms → validate import cycle. Type safety is restored at registration time in validate/register.go.
The Compile field is likewise [any]-typed, for the same reason: its concrete type lives in machine/compilation, which forms cannot import. The comma-ok assertion back to CompilerFunc happens at the dispatch site there.
type ValidatedExpr ¶
type ValidatedExpr interface {
SetFormName(name string)
FormName() string
Source() *syntax.SourceContext
}
ValidatedExpr is the interface for all validated expressions. It lives in the forms package (rather than validate) to break the validate → forms ← machine import cycle while preserving type safety.
type ValidatorFunc ¶
type ValidatorFunc func(ctx context.Context, env *environment.EnvironmentFrame, pair *syntax.SyntaxPair, result any) ValidatedExpr
ValidatorFunc is the signature for validation functions. The result parameter remains [any] (*validate.ValidationResult) because validate imports forms, so forms cannot import validate.