{"modulePath":"github.com/nsgo-dev/game","version":"v0.4.0-rc.3","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package expr\n\nPackage 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.\n\n## Constants\n\n```go\nconst MaxDepth = 128\n```\nMaxDepth is the deepest nesting one expression may hold, bounding recursion in Parse and Eval.\n\n```go\nconst MaxNodes = 4096\n```\nMaxNodes is the most syntax nodes one expression may hold, so a mod's formula cannot make the game parse or evaluate an unbounded tree.\n\n## Variables\n\n```go\nvar (\n\t// ErrUnknownVariable reports a name the Env.Vars does not return.\n\tErrUnknownVariable = errors.New(\"expr: unknown variable\")\n\t// ErrUnknownFunction reports a function the Env.Funcs lacks.\n\tErrUnknownFunction = errors.New(\"expr: unknown function\")\n\t// ErrDivisionByZero reports a division or remainder by zero.\n\tErrDivisionByZero = errors.New(\"expr: division by zero\")\n\t// ErrCostLimit reports an expression longer than MaxNodes or nested deeper than MaxDepth.\n\tErrCostLimit = errors.New(\"expr: expression too large\")\n)\n```\n\n## Functions\n\n```go\nfunc CheckSyntax(src string) error\n```\nCheckSyntax 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.\n\n## Types\n\n```go\ntype Env struct {\n\t// Vars returns a variable's value. A missing variable makes the evaluation fail with ErrUnknownVariable.\n\tVars func(name string) (Value, bool)\n\t// Funcs are the callable functions; a function the parser knew but this Env lacks fails with ErrUnknownFunction.\n\tFuncs map[string]Func\n}\n```\nEnv is what an expression evaluates against: the variables it may read and the functions it may call.\n\n```go\ntype Expr struct {\n\t// contains filtered or unexported fields\n}\n```\nExpr is one parsed and type-checked expression, safe to evaluate concurrently against different Envs.\n\n```go\nfunc Parse(src string, names []string, funcs map[string]Func) (*Expr, error)\n```\nParse 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.\n\n```go\nfunc ParseVars(src string, vars map[string]Kind, funcs map[string]Func) (*Expr, error)\n```\nParseVars 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.\n\n```go\nfunc (e *Expr) Bool(env *Env) (bool, error)\n```\nBool evaluates the expression and requires a bool.\n\n```go\nfunc (e *Expr) Eval(env *Env) (Value, error)\n```\nEval 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.\n\n```go\nfunc (e *Expr) Kind() Kind\n```\nKind returns the type the expression evaluates to.\n\n```go\nfunc (e *Expr) NodeCount() int\n```\nNodeCount returns how many syntax nodes the expression holds.\n\n```go\nfunc (e *Expr) Number(env *Env) (float64, error)\n```\nNumber evaluates the expression and requires a number.\n\n```go\nfunc (e *Expr) Source() string\n```\nSource returns the expression source text passed to Parse or ParseVars.\n\n```go\nfunc (e *Expr) String(env *Env) (string, error)\n```\nString evaluates the expression and requires a string.\n\n```go\ntype Func struct {\n\t// Params are the parameter kinds, in order.\n\tParams []Kind\n\t// Result is the kind Call returns.\n\tResult Kind\n\t// Call runs the function; an error stops the evaluation and is returned as is.\n\tCall func(args []Value) (Value, error)\n}\n```\nFunc is a function an expression may call. The parser checks its arity and parameter kinds, so Call can assume them.\n\n```go\ntype Kind uint8\n```\nKind is the type of a Value, and of a variable, function parameter or result.\n\n```go\nconst (\n\t// KindNumber is a float64.\n\tKindNumber Kind = iota\n\t// KindBool is true or false.\n\tKindBool\n\t// KindString is text.\n\tKindString\n)\n```\n\n```go\nfunc (k Kind) String() string\n```\nString returns the kind's name.\n\n```go\ntype ParseError struct {\n\t// Pos is the 1-based column of the offending token.\n\tPos int\n\t// Msg says what is wrong.\n\tMsg string\n}\n```\nParseError is a syntax or type error with its position in the source.\n\n```go\nfunc (e *ParseError) Error() string\n```\nError returns the error with its position.\n\n```go\ntype Value struct {\n\tKind Kind\n\t// contains filtered or unexported fields\n}\n```\nValue is one typed value the language evaluates to.\n\n```go\nfunc Bool(v bool) Value\n```\nBool returns a boolean value.\n\n```go\nfunc Number(v float64) Value\n```\nNumber returns a numeric value.\n\n```go\nfunc String(v string) Value\n```\nString returns a string value.\n\n```go\nfunc (v Value) AsBool() (bool, bool)\n```\nAsBool returns the boolean v holds, and false for any other kind.\n\n```go\nfunc (v Value) AsNumber() (float64, bool)\n```\nAsNumber returns the number v holds, and false for any other kind.\n\n```go\nfunc (v Value) AsString() (string, bool)\n```\nAsString returns the text v holds, and false for any other kind.\n\n","path":"github.com/nsgo-dev/game/pkg/expr","name":"expr","synopsis":"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.","isRedistributable":true}
