Documentation
¶
Overview ¶
Package expr parses and evaluates a small, deterministic expression language with named variables and game functions, for data-driven conditions, formulas, cheat codes and console commands.
Index ¶
Examples ¶
Constants ¶
const MaxDepth = 128
MaxDepth is the deepest nesting one expression may hold, bounding recursion in Parse and Eval.
const MaxNodes = 4096
MaxNodes is the most syntax nodes one expression may hold, so a mod's formula cannot make the game parse or evaluate an unbounded tree.
Variables ¶
var ( // ErrUnknownVariable reports a name the Env.Vars does not return. ErrUnknownVariable = errors.New("expr: unknown variable") // ErrUnknownFunction reports a function the Env.Funcs lacks. ErrUnknownFunction = errors.New("expr: unknown function") // ErrDivisionByZero reports a division or remainder by zero. ErrDivisionByZero = errors.New("expr: division by zero") // ErrCostLimit reports an expression longer than MaxNodes or nested deeper than MaxDepth. ErrCostLimit = errors.New("expr: expression too large") )
Functions ¶
func CheckSyntax ¶
CheckSyntax validates src's grammar and enforces MaxNodes and MaxDepth without resolving variable or function names or checking types. Callers whose variable types or custom functions are bound later (such as dialogue scripts parsed before a runner is created) use CheckSyntax at load time and ParseVars at evaluation time.
Types ¶
type Env ¶
type Env struct {
// Vars returns a variable's value. A missing variable makes the evaluation fail with ErrUnknownVariable.
Vars func(name string) (Value, bool)
// Funcs are the callable functions; a function the parser knew but this Env lacks fails with ErrUnknownFunction.
Funcs map[string]Func
}
Env is what an expression evaluates against: the variables it may read and the functions it may call.
type Expr ¶
type Expr struct {
// contains filtered or unexported fields
}
Expr is one parsed and type-checked expression, safe to evaluate concurrently against different Envs.
func Parse ¶
Parse parses and type-checks src. names are the variables the expression may use, all numbers; funcs are the functions it may call beside the builtins floor, round, abs, min, max and clamp. A syntax or type error is a *ParseError with its column, and an expression over MaxNodes or MaxDepth is ErrCostLimit.
Example ¶
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/expr"
)
func main() {
e, err := expr.Parse(`hp < 20 ? "wounded" : "ready"`, []string{"hp"}, nil)
if err != nil {
fmt.Println(err)
return
}
text, err := e.String(&expr.Env{Vars: func(string) (expr.Value, bool) { return expr.Number(12), true }})
fmt.Println(text, err)
}
Output: wounded <nil>
func ParseVars ¶
ParseVars parses and type-checks src with explicitly typed variables. Each entry in vars maps a variable name (which may include dots, such as "player.hp") to its Kind; funcs are the functions it may call beside the builtins floor, round, abs, min, max and clamp. A syntax or type error is a *ParseError with its column, and an expression over MaxNodes or MaxDepth is ErrCostLimit.
func (*Expr) Eval ¶
Eval evaluates the expression against env. A missing variable or function, a division by zero and an error from a called function are returned; a type error cannot happen because Parse checked the types.
type Func ¶
type Func struct {
// Params are the parameter kinds, in order.
Params []Kind
// Result is the kind Call returns.
Result Kind
// Call runs the function; an error stops the evaluation and is returned as is.
Call func(args []Value) (Value, error)
}
Func is a function an expression may call. The parser checks its arity and parameter kinds, so Call can assume them.
type Kind ¶
type Kind uint8
Kind is the type of a Value, and of a variable, function parameter or result.
type ParseError ¶
type ParseError struct {
// Pos is the 1-based column of the offending token.
Pos int
// Msg says what is wrong.
Msg string
}
ParseError is a syntax or type error with its position in the source.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error returns the error with its position.
type Value ¶
type Value struct {
Kind Kind
// contains filtered or unexported fields
}
Value is one typed value the language evaluates to.