forms

package
v1.9.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package forms provides a unified registry for special form handlers.

The package maps Scheme keywords (if, lambda, define, etc.) to their validation and compilation functions. This decouples the validate and machine packages by providing a shared dispatch table.

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)
}

The registry uses [any] for function parameter types to break circular import cycles. Type safety is enforced at call sites via runtime assertions.

Package forms provides a unified registry for special form handlers. It maps keywords to their validation and compilation functions, allowing both the validate and machine packages to share the same dispatch table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Names

func Names() []string

Names returns all registered form names.

func Register

func Register(spec *FormSpec)

Register adds a FormSpec to the registry. If a spec with the same name exists, it is replaced.

func RegisterCompiler

func RegisterCompiler(name string, fn CompilerFunc)

RegisterCompiler sets the compiler for an existing form or creates a new entry.

func RegisterValidator

func RegisterValidator(name string, fn ValidatorFunc)

RegisterValidator sets the validator for an existing form or creates a new entry.

func Verify added in v1.7.0

func Verify() error

Verify checks that every registered form has both a validator and a compiler, except for forms that are handled entirely during expansion. Returns an error listing any forms with missing handlers, or nil if all pairings are consistent.

Types

type CompilerFunc

type CompilerFunc func(ctc any, ctctx any, expr ValidatedExpr) error

CompilerFunc is the signature for compilation functions. The ctc (*machine.CompileTimeContinuation) and ctctx (machine.CompileTimeCallContext) parameters remain [any] because machine imports forms.

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 is called during compilation to emit bytecode.
	// If nil, the form cannot be compiled (error).
	Compile CompilerFunc
}

FormSpec defines how a special form is validated and compiled.

Parameters that cross the validate/machine boundary use [any] to break import cycles. Type safety is restored at registration time: validate/register.go wraps typed validators into ValidatorFunc, and machine/register.go wraps typed compilers into CompilerFunc.

func Lookup

func Lookup(name string) *FormSpec

Lookup returns the FormSpec for a keyword, or nil if not found.

type ValidatedExpr added in v1.6.0

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL