Documentation ¶
Index ¶
- Constants
- type DefaultRandSource
- type Evallable
- func NewExpression(varOrder []string, vars map[string]Evallable, expr Evallable) Evallable
- func NewFunction(name string, params []Evallable) (Evallable, error)
- func NewListExpression(items []Evallable) Evallable
- func NewNumber(value int) Evallable
- func NewString(value string, isLabel bool) Evallable
- func NewTableCall(packageKey string, packageName string, tableName string, params []Evallable) (Evallable, error)
- func NewVariable(name string) Evallable
- type ExecutionContext
- func (ctx *ExecutionContext) Child() *ExecutionContext
- func (ctx *ExecutionContext) Rand(low int, high int) int
- func (ctx *ExecutionContext) Resolve(key string) (*ExpressionResult, error)
- func (ctx *ExecutionContext) Set(key string, val *ExpressionResult)
- func (ctx *ExecutionContext) SetHistory(h *RollHistory) *ExecutionContext
- func (ctx *ExecutionContext) SetPacks(packs TableMap)
- func (ctx *ExecutionContext) SetRandom(r RandomSource) *ExecutionContext
- type Expression
- type ExpressionEval
- type ExpressionResult
- func (e *ExpressionResult) BoolVal() bool
- func (e *ExpressionResult) Equal(other *ExpressionResult) bool
- func (e *ExpressionResult) IntVal() int
- func (e *ExpressionResult) MatchType(types ...ResultType) bool
- func (e *ExpressionResult) SameType(other *ExpressionResult) bool
- func (e *ExpressionResult) StringVal() string
- type FunctionDef
- type GenericFunction
- type ListExpression
- type Number
- type Program
- type RandomSource
- type Range
- type ResultType
- type Roll
- type RollCountAggr
- type RollHistory
- type RollSelect
- type String
- type Table
- func (t *Table) Copy() *Table
- func (t *Table) DeckDraw() (Evallable, error)
- func (t *Table) Default() (Evallable, error)
- func (t *Table) IndexRoll(key int) (Evallable, error)
- func (t *Table) LabelRoll(key string) (Evallable, error)
- func (t *Table) Name() string
- func (t *Table) Roll() Evallable
- func (t *Table) RowCount() int
- func (t *Table) Shuffle()
- func (t *Table) Tag(tagName string) (string, bool)
- func (t *Table) TotalCount() int
- func (t *Table) TotalWeight() int
- func (t *Table) WeightedRoll() Evallable
- type TableCall
- type TableMap
- type TablePack
- type TableRow
- type TestingRandSource
- type Variable
Constants ¶
const (
// RootPack is the default key for table calls.
RootPack = "_ROOT"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultRandSource ¶
type DefaultRandSource struct { }
DefaultRandSource is a RandomSource that uses rand.Intn
type Evallable ¶
type Evallable interface { // Eval returns a stateful executable version of this expression. Eval() ExpressionEval }
Evallable is an interface for a loaded program unit to provide an executable ExpressionEval during program execution.
The returned ExpressionEval should always be semantically the same, but each value returned should have unique state (excepting deck draw counts).
The root Evallable is should be considered a stateless generator for executable expressions.
Example: An Evallable for the expression `if(eq(@foo, 3), "high", "low")` should always return an ExpressionEval that acts the same for a given setting of @foo. But each value returned may be provided a different value for @foo, and those different settings should not interact or interfere with eachothers evaluation.
func NewExpression ¶
NewExpression creates a new expression Evallable.
func NewFunction ¶
NewFunction creates a function Evallable, erroring if it can on the format of the parameters.
func NewListExpression ¶
NewListExpression creates a new list of epxressions for a row.
func NewTableCall ¶
func NewTableCall( packageKey string, packageName string, tableName string, params []Evallable, ) (Evallable, error)
NewTableCall creates a new table call Evallable.
func NewVariable ¶
NewVariable creates a new variable access Evallable.
type ExecutionContext ¶
type ExecutionContext struct { *RollHistory // contains filtered or unexported fields }
ExecutionContext is a runtime context for scoping variable values, keepina consistent random number generator and referencing other tables.
func NewRootExecutionContext ¶
func NewRootExecutionContext() *ExecutionContext
NewRootExecutionContext creates an empty ExecutionContext that is ready to be used, with empty history and the default random generator.
func (*ExecutionContext) Child ¶
func (ctx *ExecutionContext) Child() *ExecutionContext
Child creates a child execution context that can read-access the parent's variable values but writing to the same variables will not write those values to the parent definitions. This allows for recursion-based iteration.
All other internal fields are direct copied to limit stack overflow issues and lightly optimise for fast access over traversing to the root node each evaluation.
func (*ExecutionContext) Rand ¶
func (ctx *ExecutionContext) Rand(low int, high int) int
Rand is a convenience method to get a random number from the context.
func (*ExecutionContext) Resolve ¶
func (ctx *ExecutionContext) Resolve(key string) (*ExpressionResult, error)
Resolve fetches key from the given context or the closest parent that has it defined. Nil is returned if the
func (*ExecutionContext) Set ¶
func (ctx *ExecutionContext) Set(key string, val *ExpressionResult)
Set will set a variable named `key` to the value given.
func (*ExecutionContext) SetHistory ¶
func (ctx *ExecutionContext) SetHistory(h *RollHistory) *ExecutionContext
SetHistory assignes a specific roll history object.
func (*ExecutionContext) SetPacks ¶
func (ctx *ExecutionContext) SetPacks(packs TableMap)
SetPacks assigns the table packs for this context.
func (*ExecutionContext) SetRandom ¶
func (ctx *ExecutionContext) SetRandom(r RandomSource) *ExecutionContext
SetRandom sets the RandomSource for this context.
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
An Expression is an Evallable with for an expression.
func (*Expression) Eval ¶
func (e *Expression) Eval() ExpressionEval
Eval implementation for Evallable interface.
type ExpressionEval ¶
type ExpressionEval interface { // SetContext returns this ExpressionEval after SetContext(ctx *ExecutionContext) ExpressionEval // HasNext returns whether there is another sub-expression to evaluate. HasNext() bool // Next returns the next sub-expression to be evaluated. // Calling next multiple times should return the same semantic value to evaluate, // but is not required to be the same pointer/reference/memory value. Next() (ExpressionEval, error) // Provide sets the result for the sub-expresion currently returned by `Next()`. Provide(res *ExpressionResult) error // Resolve should be called to compute and return the final value of this // expression when `HasNext()` returns false. Resolve() (*ExpressionResult, error) }
ExpressionEval is an interface for evaluating any program node. It acts much lit an iterator over sub-expressions and allows for program nodes to use internal logic for sub-expression evaluation.
This is assumed to be a stateful object.
The main evaluation loop for a single ExpressionEval is similar to:
process(e) -> result: while(e.HasNext): e.Provide(process(e.Next)) return e.Resolve
With extra care being taken around runtime errors generated.
Implementations are allowed to skip providing sub-expressions or execute sub-expressions in any order, but it is recommended to use a right-to-left evaluation scheme as convention. For example: the `if(Cond, T, F)` flow control function will only evaluate T or F (depending on Cond's truthiness), but will always evaluate Cond first.
Type are not enforced until evaluation so a given ExpressionEval can Resolve to any type necessary based on internal logic.
type ExpressionResult ¶
type ExpressionResult struct {
// contains filtered or unexported fields
}
ExpressionResult is a final result for an evaluated expression.
func EvaluateExpression ¶
func EvaluateExpression(e Evallable, ctx *ExecutionContext) (*ExpressionResult, error)
EvaluateExpression Evaluates an expression with the given execution context outside of a Program. Mostly useful externally for compiler testing.
This method contains the main evaluation loop that uses a slice for a program stack to prevent failing from deep call stacks.
func NewIntResult ¶
func NewIntResult(val int) *ExpressionResult
NewIntResult creates a new int-valued result.
func NewStringResult ¶
func NewStringResult(val string) *ExpressionResult
NewStringResult creates a new string-valued result.
func (*ExpressionResult) BoolVal ¶
func (e *ExpressionResult) BoolVal() bool
BoolVal returns an integer-based boolean value for the result. 0 for false, any other integer for true. If the result is a string, false is always returned.
func (*ExpressionResult) Equal ¶
func (e *ExpressionResult) Equal(other *ExpressionResult) bool
Equal compares 2 expression results for deep equality.
func (*ExpressionResult) IntVal ¶
func (e *ExpressionResult) IntVal() int
IntVal returns the integer value, if it was set. Default 0.
func (*ExpressionResult) MatchType ¶
func (e *ExpressionResult) MatchType(types ...ResultType) bool
MatchType verifies that the type of this result is in the passed list. Setup for possible future types beyond int/string.
func (*ExpressionResult) SameType ¶
func (e *ExpressionResult) SameType(other *ExpressionResult) bool
SameType does a simple type comparison between two results.
func (*ExpressionResult) StringVal ¶
func (e *ExpressionResult) StringVal() string
StringVal returns the string value, if it was set. Default "".
type FunctionDef ¶
type FunctionDef struct {
// contains filtered or unexported fields
}
FunctionDef is a way to define a function so it can be used in a modular way.
type GenericFunction ¶
type GenericFunction struct {
// contains filtered or unexported fields
}
GenericFunction allows a simple FunctionDef to be wrapped for simpler definitions. An Evallable.
func (*GenericFunction) Eval ¶
func (g *GenericFunction) Eval() ExpressionEval
Eval implementation for Evallable interface.
type ListExpression ¶
type ListExpression struct {
// contains filtered or unexported fields
}
ListExpression is an Evallable that wraps all the items for a table row.
func (*ListExpression) Eval ¶
func (l *ListExpression) Eval() ExpressionEval
Eval implementation for Evallable interface.
type Number ¶
type Number struct {
// contains filtered or unexported fields
}
Number is an Evallable for a numeric constant.
func (*Number) Eval ¶
func (n *Number) Eval() ExpressionEval
Eval implementation for Evallable interface.
type Program ¶
type Program struct {
// contains filtered or unexported fields
}
Program is a set of TablePacks that can evaluate expressions as programs.
func NewProgram ¶
NewProgram creates a new program from a keyed set of tablepacks.
func (*Program) Eval ¶
func (p *Program) Eval(expr Evallable) (*ExpressionResult, error)
Eval Evaluates a given Evallable against this program's state (tables+context).
func (*Program) SetHistory ¶
func (p *Program) SetHistory(h *RollHistory)
SetHistory uses the given RollHistory for rolls.
type RandomSource ¶
type RandomSource interface { // Get should return a number in the interval [low, high) Get(low int, high int) int }
RandomSource is a customizable randoom source for replacing for tests or seeding.
type Range ¶
type Range struct {
// contains filtered or unexported fields
}
Range is a low-high number range.
The range can be a single number if low and high are the same.
type ResultType ¶
type ResultType int
ResultType is an alias for allowed return types from an expression.
const ( // AnyTypeResult can be used in type matching to take any result value. AnyTypeResult ResultType = 0 // StringResult can be used to define or match to string expression results. StringResult ResultType = 1 // IntResult can be used to define or matche to number/integer results. IntResult ResultType = 2 )
type Roll ¶
type Roll struct {
// contains filtered or unexported fields
}
Roll is an Evallable roll expression value.
func (*Roll) Eval ¶
func (r *Roll) Eval() ExpressionEval
Eval implementation for Evallable interface.
func (*Roll) WithCountAggr ¶
func (r *Roll) WithCountAggr(countAggrs []*RollCountAggr) *Roll
WithCountAggr configures this roll with a list of count aggrs. Replaces any previous count aggregar setting.
func (*Roll) WithSelector ¶
func (r *Roll) WithSelector(selector *RollSelect) *Roll
WithSelector configures this roll with a high/low selector.
type RollCountAggr ¶
type RollCountAggr struct {
// contains filtered or unexported fields
}
RollCountAggr is an aggregator for counting how many of a given number is rolled.
func NewRollCountAggr ¶
func NewRollCountAggr(number int, multiplier int) *RollCountAggr
NewRollCountAggr creates a new roll count aggregator.
type RollHistory ¶
type RollHistory struct {
// contains filtered or unexported fields
}
RollHistory is a list of all roll results in string format.
Mostly thread safe.
func NewRollHistory ¶
func NewRollHistory() *RollHistory
NewRollHistory creates a new RollHistory object.
func (*RollHistory) AddRollToHistory ¶
func (h *RollHistory) AddRollToHistory(roll string)
AddRollToHistory adds the give roll string result to the history list.
func (*RollHistory) ClearRolls ¶
func (h *RollHistory) ClearRolls()
ClearRolls clears the current roll history.
func (*RollHistory) GetRollHistory ¶
func (h *RollHistory) GetRollHistory() []string
GetRollHistory returns a slice copy of the complete roll history.
func (*RollHistory) LatestRoll ¶
func (h *RollHistory) LatestRoll() string
LatestRoll returns the value of the last stored roll.
type RollSelect ¶
type RollSelect struct {
// contains filtered or unexported fields
}
RollSelect is a roll selector for the highest or lowest N dice.
func NewRollSelect ¶
func NewRollSelect(isHigh bool, count int) *RollSelect
NewRollSelect creates a new high/low roll selector.
type String ¶
type String struct {
// contains filtered or unexported fields
}
String is an Evallable for a string value.
func (*String) Eval ¶
func (s *String) Eval() ExpressionEval
Eval implementation for the Evallable inerface.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a program unit that can randomly and deterministically return row Evallable objects. The core of tableman.
func (*Table) DeckDraw ¶
DeckDraw will treat the table as a deck of cards using the count value (default 1) to choose a row from the deck.
This is the only stateful draw from the table, once all counts have been exhausted an error will be returned if DeckDraw is called. Reset the counts by calling `Shuffle()`.
func (*Table) Default ¶
Default returns the default row for the table or an error if ther isn't one.
func (*Table) IndexRoll ¶
IndexRoll returns the row defined for the given index. If no index machtes, the default row will be used. If no default was defined, an error will be returned.
func (*Table) LabelRoll ¶
LabelRoll fetches a row directly from the table using the passed label. If no label was defined on the table, the default row will be returned. If no default row was specified, an error will be returned.
func (*Table) Tag ¶
Tag returns the tage valuve for the given tag name and a boolean for whether the tag was defined (the same way a map can).
func (*Table) TotalCount ¶
TotalCount returns the total number of "cards" for a DeckDraw.
func (*Table) TotalWeight ¶
TotalWeight returns the total weight of all tale rows.
func (*Table) WeightedRoll ¶
WeightedRoll randomly rolls on the table using the defined row weights to decide which rows to return. Rows without set weights are treated as w=1.
type TableCall ¶
type TableCall struct {
// contains filtered or unexported fields
}
TableCall is an Evallable for calls to a table.
func (*TableCall) Eval ¶
func (c *TableCall) Eval() ExpressionEval
Eval implementation for Evallable interface.
type TablePack ¶
type TablePack struct {
// contains filtered or unexported fields
}
TablePack represents a single executable tableman source file.
func NewTablePack ¶
NewTablePack creates a new TablePack with the given tables.
type TableRow ¶
type TableRow struct {
// contains filtered or unexported fields
}
TableRow is an Evallable row for a tableman table.
func NewTableRow ¶
func NewTableRow(label string, rangeVal []*Range, weight int, count int, isDefault bool, value Evallable) *TableRow
NewTableRow creates a new TableRow object.
func (*TableRow) Label ¶
Label returns the label for the row, or an empty string if one isn't defined.
type TestingRandSource ¶
type TestingRandSource struct {
// contains filtered or unexported fields
}
TestingRandSource is an implementation of RandomSource that uses a predefined list of values.
func NewTestRandSource ¶
func NewTestRandSource(val ...int) *TestingRandSource
NewTestRandSource creates a new random source for testing pre-populated with the passsed values.
func (*TestingRandSource) AddMore ¶
func (r *TestingRandSource) AddMore(vals ...int)
AddMore appends more random values to the internal list in order.