env

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package env provides namespace and binding management for the Ale runtime. It implements lexical scoping, symbol resolution, and environment chaining that supports nested scopes, closures, and public/private visibility. This is the foundation for global storage and lookup throughout the Ale interpreter.

Index

Constants

View Source
const (
	// ErrNameAlreadyBound is raised when an attempt is made to bind a
	// Namespace entry that has already been bound
	ErrNameAlreadyBound = "name is already bound in namespace: %s"

	// ErrNameNotBound is raised when an attempt is made to retrieve a value
	// from a Namespace that hasn't been bound
	ErrNameNotBound = "name is not bound in namespace: %s"
)
View Source
const (
	ErrNamespaceNotFound = "namespace not found: %s"
	ErrNamespaceExists   = "namespace already exists: %s"
)
View Source
const (
	// ErrNameAlreadyDeclared is raised when an attempt to declare a name is
	// performed that has already been declared with different privacy
	ErrNameAlreadyDeclared = "name already declared in namespace: %s"

	// ErrNameNotDeclared is raised when an attempt to forcefully resolve an
	// undeclared name in the Namespace fails
	ErrNameNotDeclared = "name not declared in namespace: %s"
)

Variables

This section is empty.

Functions

func BindPrivate added in v0.3.0

func BindPrivate(ns Namespace, n data.Local, v ale.Value) error

func BindPublic added in v0.3.0

func BindPublic(ns Namespace, n data.Local, v ale.Value) error

func MustResolveValue

func MustResolveValue(ns Namespace, s data.Symbol) ale.Value

MustResolveValue attempts to resolve a value or explodes violently

func ResolveSymbol

func ResolveSymbol(ns Namespace, s data.Symbol) (*Entry, Namespace, error)

ResolveSymbol attempts to resolve a symbol. If it's a qualified symbol, it will be retrieved directly from the identified namespace. Otherwise, it will be searched in the current namespace

func ResolveValue

func ResolveValue(ns Namespace, s data.Symbol) (ale.Value, error)

ResolveValue attempts to resolve a symbol to a bound value

func RootSymbol

func RootSymbol(name data.Local) data.Symbol

RootSymbol returns a symbol qualified by the root domain

Types

type Binder added in v0.3.0

type Binder func(ns Namespace, n data.Local, v ale.Value) error

type Entries added in v0.3.0

type Entries map[data.Local]*Entry

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry represents a namespace entry

func (*Entry) Bind

func (e *Entry) Bind(v ale.Value) error

func (*Entry) IsBound

func (e *Entry) IsBound() bool

func (*Entry) IsPrivate

func (e *Entry) IsPrivate() bool

func (*Entry) Name

func (e *Entry) Name() data.Local

func (*Entry) Value

func (e *Entry) Value() (ale.Value, error)

type Environment

type Environment struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Environment maintains a mapping of domain names to namespaces

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment creates a new synchronous namespace map

func (*Environment) Domains

func (e *Environment) Domains() data.Locals

func (*Environment) GetAnonymous

func (e *Environment) GetAnonymous() Namespace

GetAnonymous returns an anonymous (non-resolvable) namespace

func (*Environment) GetQualified

func (e *Environment) GetQualified(n data.Local) (Namespace, error)

GetQualified returns the namespace for the specified domain.

func (*Environment) GetRoot

func (e *Environment) GetRoot() Namespace

GetRoot returns the root namespace, where built-ins go

func (*Environment) NewQualified added in v0.3.0

func (e *Environment) NewQualified(n data.Local) (Namespace, error)

NewQualified creates a new namespace for the specified domain if it doesn't already exist.

func (*Environment) Snapshot

func (e *Environment) Snapshot() *Environment

type Namespace

type Namespace interface {
	// Environment returns the environment associated with this namespace
	Environment() *Environment

	// Domain returns the domain name of this namespace
	Domain() data.Local

	// Declared returns all declared symbols in this namespace
	Declared() data.Locals

	// Public declares a public symbol in this namespace
	Public(data.Local) (*Entry, error)

	// Private declares a private symbol in this namespace
	Private(data.Local) (*Entry, error)

	// Resolve attempts to resolve a symbol in this namespace or its parents
	Resolve(data.Local) (*Entry, Namespace, error)

	// Snapshot creates a snapshot of this namespace for another environment
	Snapshot(*Environment) Namespace

	// Import atomically adds entries from another namespace to this one
	Import(Entries) error
}

Namespace represents a namespace

func MustGetQualified added in v0.3.0

func MustGetQualified(e *Environment, n data.Local) Namespace

MustGetQualified attempts to retrieve a namespace for the specified domain. If the namespace does not exist, it will attempt to create it. If creation fails, it will panic.

Jump to

Keyboard shortcuts

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