puff

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package puff is a small lisp for whiff's services: immutable values, and Results and Options instead of nil and exceptions.

Index

Constants

This section is empty.

Variables

View Source
var Done = Ok(Keyword("done"))

Done is the value of an effect with nothing else to return.

Functions

func Command added in v0.8.1

func Command(ctx context.Context, environ []string, argv []string) *exec.Cmd

Command is exec.CommandContext for argv in environ: argv[0] found on its PATH and the process given it, or the process's own if environ is nil. A whiff test's daemon has an environment of its own this way, so tests with fake programs on their PATH run side by side.

func Equal

func Equal(a, b Value) bool

Equal reports whether a and b are the same value. Values of different types are never equal, so (= 1 1.0) is #f.

func Format added in v0.4.0

func Format(file, src string) (string, error)

Format lays src, a puff file, out in one way, so that who wrote it and how doesn't show in a diff. It keeps the line breaks and every token as written: strings, """ strings, numbers, map keys in their order and comments. What it decides is the rest:

  • A line inside ( ) is indented 2 past the (; one inside [ ] or { } lines up with the first element, 1 past the bracket. The rule doesn't look at what the form is, so renaming a function doesn't move its arguments.
  • Closing brackets on a line of their own join the line before, unless that ends in a comment.
  • One space between tokens, none inside brackets; a comment after code keeps the space before it.
  • At most one blank line in a row, none at the start or end, and one newline at the end of the file.

A """ string's lines move with the line it starts on, which keeps its value. A file that doesn't read is an error at file:line:col.

func Getenv added in v0.8.1

func Getenv(environ []string, name string) (string, bool)

Getenv is the variable name in environ, KEY=value pairs as os.Environ gives them, or the process's environment if environ is nil.

func Home added in v0.8.1

func Home(environ []string) (string, error)

Home is the home directory in environ: its HOME, or the process's.

func LookPath added in v0.8.1

func LookPath(environ []string, file string) (string, error)

LookPath is file as exec.LookPath finds it, on environ's PATH; on the process's, by exec.LookPath, if environ is nil.

func Print

func Print(v Value) string

Print returns v's readable form: reading it back gives a value Equal to v, and Equal values print the same.

func PrintPretty added in v0.7.1

func PrintPretty(v Value, col, width int) string

PrintPretty is v as Print has it, broken over lines to fit in width columns where it can, for a person to read: a vector, map or tag that doesn't fit on what is left of its line has an item, entry or argument a line (a vector of atoms, as many as fit), indented as puff -f indents, 1 past a [ or { and 2 past a (. A value that fits stays on one line, and so does an atom too long for width. col is the column v starts at, for the lines after the first.

func ReadOnly added in v0.4.0

func ReadOnly(ctx context.Context) (string, bool)

ReadOnly returns what runs under :read in ctx, if anything does.

func ReadOnlyCommand added in v0.4.0

func ReadOnlyCommand(cmd Value) bool

ReadOnlyCommand says whether cmd, a vector of the program and its arguments as proc/run takes it, is in the table of commands that only read.

func ReadOnlyCommands added in v0.4.0

func ReadOnlyCommands() []string

ReadOnlyCommands names the commands in the table, "git status" and so on.

func WithMark added in v0.4.0

func WithMark(ctx context.Context, what string, m Mark) context.Context

WithMark returns ctx for running what, a function whose mark is m. Under :read, calling a :write builtin faults: the host's backstop for a mark it worked out. It replaces what an outer WithMark said.

Types

type Bool

type Bool bool

type Builtin

type Builtin struct {
	Name string
	// Args names the arguments for help and sets the arity: required
	// names, then optional ones in brackets, then ... for any number more
	// of the last: "s sep", "a [b]", "a b ...".
	Args string
	Mark Mark
	// MarkByArgs is for a :write builtin whose mark depends on its
	// arguments, as a host's call does on its target's: under WithMark
	// :read, Fn checks for itself instead of faulting.
	MarkByArgs bool
	Doc        string
	// Fn returns the call's value. A failure for the caller to handle is a
	// value, (err e) from Errorf; a returned error is a fault: a wrong
	// argument, an overflow.
	Fn func(c *Call, args []Value) (Value, error)
	// contains filtered or unexported fields
}

Builtin is a function implemented in Go: one entry of a table.

func Base added in v0.3.0

func Base() []*Builtin

Base returns puff's own table, which every Env starts with, in the order of its groups: for documentation.

func (*Builtin) Signature added in v0.3.0

func (b *Builtin) Signature() string

Signature is how help shows a call of b: (string/split s sep).

type Call

type Call struct {
	Ctx context.Context
	Env *Env
	// contains filtered or unexported fields
}

Call is what a builtin gets besides its arguments.

func (*Call) Apply

func (c *Call) Apply(f Value, args ...Value) (Value, error)

Apply calls the function f with args, for builtins that take functions. A fault in f stops the builtin as it is.

type Duration

type Duration time.Duration

type Entry

type Entry struct{ Key, Val Value }

func Key added in v0.7.0

func Key(k string, v Value) Entry

Key is the map entry :k v: how Go code builds a map with keyword keys.

type Env

type Env struct {
	// Stdout is where io/println and help write.
	Stdout io.Writer
	// Dir is where relative paths, os/cwd and proc/run start; "" is the
	// process's working directory.
	Dir string
	// Environ is the environment env/get, os/home and proc/run see, as
	// os.Environ gives it; nil is the process's.
	Environ []string
	// Hints are words for an unknown name's error, by name: why it isn't
	// here, for a function another host has, as whiff's are in puff.
	Hints map[string]string
	// contains filtered or unexported fields
}

Env is a global scope: the builtins and the top-level defs of everything loaded into it. It is not safe for concurrent use.

func NewEnv

func NewEnv() *Env

NewEnv returns an Env with puff's base table, writing to os.Stdout.

func (*Env) Call

func (e *Env) Call(ctx context.Context, pos Pos, f Value, args ...Value) (Value, error)

Call calls the function f with args, as a host does: puff calling main, whiff calling a handler. pos is where a fault in the call itself points.

func (*Env) Load

func (e *Env) Load(ctx context.Context, file, src string) (Value, error)

Load reads src and evaluates its forms, as LoadForms does.

func (*Env) LoadFile added in v0.6.0

func (e *Env) LoadFile(ctx context.Context, file, src string) (Value, error)

LoadFile is Load for src, read from file: its uses start from file's directory, not e.Dir.

func (*Env) LoadForms

func (e *Env) LoadForms(ctx context.Context, forms []*Form) (Value, error)

LoadForms checks and evaluates forms, and returns the value of the last: nil if that is a def or there are no forms. A (use "path") brings in that file's forms, as ExpandUses does, from e.Dir. Names are checked before anything runs, so an unknown name fails the whole load with an *Error. Evaluation stops at a *Fault, keeping the defs that ran before it. A def of an existing name replaces it for everything that uses it.

func (*Env) Lookup

func (e *Env) Lookup(name string) (Value, bool)

Lookup returns the value of a global: a builtin, or a def that has run.

func (*Env) Register

func (e *Env) Register(bs ...*Builtin) error

Register adds builtins, such as a host's own on top of the base table. A name that is already taken is an error.

func (*Env) RunTest added in v0.4.0

func (e *Env) RunTest(ctx context.Context, t *Test) string

RunTest runs t in e: "" if it passed, else why it failed.

func (*Env) Tests added in v0.4.0

func (e *Env) Tests() []*Test

Tests are the tests of everything loaded into e, in order.

type Error

type Error struct {
	Pos Pos
	Msg string
	// Unclosed is set when the source ends inside a form or string, so a
	// REPL can read another line instead.
	Unclosed bool
}

Error is an error at a position in source.

func (*Error) Error

func (e *Error) Error() string

type Fault

type Fault struct {
	Pos Pos
	Msg string
	// Trace is the calls it came back through, innermost first: where
	// the function it happened in was called, and where that one was.
	Trace []Pos
}

Fault stops an evaluation: a wrong call, a condition that isn't a bool, an overflow. Nothing in puff catches it; the host reports it.

func (*Fault) Error

func (f *Fault) Error() string

type Float

type Float float64

type Form

type Form struct {
	Kind  FormKind
	Pos   Pos
	Val   Value   // FormLiteral: a bool, int, float, string, keyword, duration or time
	Name  string  // FormSymbol
	Items []*Form // FormList, FormVector, FormMap (keys and values alternating)
}

Form is a piece of read source with its position. The reader doesn't tell code from data: Value turns a form into data, the evaluator reads it as code.

func ExpandUses added in v0.6.0

func ExpandUses(forms []*Form, file, dir string) (out []*Form, files []string, err error)

ExpandUses returns forms with each (use "path") among them replaced by the forms of the file it names, whose own uses are expanded in turn. A used file holds only defs, tests and uses. A relative path starts from the directory of file, the file forms came from, or from dir if file is "" ("" is the working directory); in a used file, from that file's. A file that is a symlink resolves from its target's directory. Each file comes in once, however many use it; one that uses itself, by any way round, is an error at the use that closes the cycle. files are the files it read or tried to, even when it fails, so a host can watch them for changes.

func Read

func Read(file, src string) ([]*Form, error)

Read reads every form in src. file names src in positions.

func (*Form) String

func (f *Form) String() string

String prints f in normalized source form.

func (*Form) Value

func (f *Form) Value() (Value, error)

Value converts a form read as data: literals, vectors, maps, and lists as tags named by their first symbol. Other symbols are not data.

type FormKind

type FormKind int
const (
	FormLiteral FormKind = iota
	FormSymbol
	FormList
	FormVector
	FormMap
)

type Func

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

Func is a function defined in puff.

func (*Func) Arity

func (f *Func) Arity() int

Arity is the number of parameters f takes.

type Int

type Int int64

type Keyword

type Keyword string // the name, without the leading colon

type Map

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

Map is an immutable map with bool, int, string or keyword keys, kept in print order: {:a 1 :b 2}.

func NewMap

func NewMap(entries ...Entry) (Map, error)

NewMap returns a map of entries, in any order. It fails on a key that isn't a bool, int, string or keyword, and on a duplicate key.

func (Map) Entry

func (m Map) Entry(i int) Entry

Entry returns the i-th entry in print order.

func (Map) Get

func (m Map) Get(key Value) (Value, bool)

Get returns the value under key.

func (Map) Len

func (m Map) Len() int

func (Map) With

func (m Map) With(key, val Value) (Map, error)

With returns m with key set to val.

type Mark

type Mark int

Mark says whether a function only reads, or changes something that is yours: a file, a remote, a service's state.

const (
	MarkRead Mark = iota
	MarkWrite
)

func MarkOf

func MarkOf(v Value) Mark

MarkOf returns v's mark. A builtin's is in its table entry. A puff function is :write if anything it refers to is: the globals in its body, followed through other functions, and the values it closed over. Data is :write if it holds a :write function. Marks are worked out each time, so redefining a helper changes the marks of its users.

func (Mark) String

func (m Mark) String() string

type Pos

type Pos struct {
	File      string
	Line, Col int
}

Pos is a position in source. Line and column count from 1; the column is in bytes.

func (Pos) String

func (p Pos) String() string

type String

type String string

type Tag

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

Tag is a named tuple: (ok 1), (none), (circle 2.0). Parens in data are always tags.

func NewTag

func NewTag(name string, args ...Value) Tag

NewTag returns the tag name with a copy of args.

func (Tag) At

func (t Tag) At(i int) Value

func (Tag) Len

func (t Tag) Len() int

func (Tag) Name

func (t Tag) Name() string

type Test added in v0.4.0

type Test struct {
	Name string
	Pos  Pos
	// contains filtered or unexported fields
}

Test is a (test "name" body...) that a load found. Loading a file compiles its tests but doesn't run them; RunTest does. A test passes if its body's value is #t, or, for a body that ends in (= got want), if got equals want, which a failure then shows.

type Time

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

Time is a point in time, as durations are spans: 2026-10-27T10:00:00Z. It is kept as UTC nanoseconds since 1970, so equal times are equal values; that covers the years 1678 to 2262.

func NewTime

func NewTime(t time.Time) Time

NewTime returns t as a puff time; t must be within timeFits's range.

func (Time) Go

func (v Time) Go() time.Time

Go returns v as a Go time, in UTC.

type Value

type Value interface {
	// contains filtered or unexported methods
}

Value is a puff value. Values are immutable and compare with Equal.

func ErrValue added in v0.7.0

func ErrValue(r Value) Value

ErrValue is e of r, an (err e); nil for anything else.

func Errorf

func Errorf(kind, format string, args ...any) Value

Errorf returns (err {:kind kind :message msg}): how a builtin reports a failure for its caller to handle.

func Field added in v0.7.0

func Field(v Value, key string) Value

Field is v's value for the keyword key, if v is a map that has it; else nil.

func None

func None() Value

func Ok

func Ok(v Value) Value

Ok, Some and None build Results and Options.

func OkValue added in v0.7.0

func OkValue(r Value) Value

OkValue is v of r, an (ok v); nil for anything else.

func ParseJSON added in v0.7.0

func ParseJSON(s string) (Value, error)

ParseJSON reads s, one JSON value, as json/parse does: objects are maps, with keyword keys where the key is a name; null is (null); whole numbers that fit stay ints.

func ReadValue

func ReadValue(src string) (Value, error)

ReadValue reads src as exactly one value, such as a message or a printed value.

func Some

func Some(v Value) Value

type Vector

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

Vector is an immutable sequence: [1 2 3].

func NewVector

func NewVector(items ...Value) Vector

NewVector returns a vector of a copy of items.

func (Vector) At

func (v Vector) At(i int) Value

func (Vector) Len

func (v Vector) Len() int

Jump to

Keyboard shortcuts

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