Documentation
¶
Overview ¶
Package puff is a small lisp for whiff's services: immutable values, and Results and Options instead of nil and exceptions.
Index ¶
- Variables
- func Command(ctx context.Context, environ []string, argv []string) *exec.Cmd
- func Equal(a, b Value) bool
- func Format(file, src string) (string, error)
- func Getenv(environ []string, name string) (string, bool)
- func Home(environ []string) (string, error)
- func LookPath(environ []string, file string) (string, error)
- func Print(v Value) string
- func PrintPretty(v Value, col, width int) string
- func ReadOnly(ctx context.Context) (string, bool)
- func ReadOnlyCommand(cmd Value) bool
- func ReadOnlyCommands() []string
- func WithMark(ctx context.Context, what string, m Mark) context.Context
- type Bool
- type Builtin
- type Call
- type Duration
- type Entry
- type Env
- func (e *Env) Call(ctx context.Context, pos Pos, f Value, args ...Value) (Value, error)
- func (e *Env) Load(ctx context.Context, file, src string) (Value, error)
- func (e *Env) LoadFile(ctx context.Context, file, src string) (Value, error)
- func (e *Env) LoadForms(ctx context.Context, forms []*Form) (Value, error)
- func (e *Env) Lookup(name string) (Value, bool)
- func (e *Env) Register(bs ...*Builtin) error
- func (e *Env) RunTest(ctx context.Context, t *Test) string
- func (e *Env) Tests() []*Test
- type Error
- type Fault
- type Float
- type Form
- type FormKind
- type Func
- type Int
- type Keyword
- type Map
- type Mark
- type Pos
- type String
- type Tag
- type Test
- type Time
- type Value
- type Vector
Constants ¶
This section is empty.
Variables ¶
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
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 ¶
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
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
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 LookPath ¶ added in v0.8.1
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 ¶
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
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 ReadOnlyCommand ¶ added in v0.4.0
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.
Types ¶
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.
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 ¶
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) LoadFile ¶ added in v0.6.0
LoadFile is Load for src, read from file: its uses start from file's directory, not e.Dir.
func (*Env) LoadForms ¶
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) Register ¶
Register adds builtins, such as a host's own on top of the base table. A name that is already taken is an error.
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.
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.
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
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.
type Func ¶
type Func struct {
// contains filtered or unexported fields
}
Func is a function defined in puff.
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}.
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.
func MarkOf ¶
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.
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.
type Test ¶ added in v0.4.0
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.
type Value ¶
type Value interface {
// contains filtered or unexported methods
}
Value is a puff value. Values are immutable and compare with Equal.
func Errorf ¶
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
Field is v's value for the keyword key, if v is a map that has it; else nil.
func ParseJSON ¶ added in v0.7.0
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.