program

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
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

func (*DefaultRandSource) Get

func (r *DefaultRandSource) Get(low int, high int) int

Get implementation for RandomSource.

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

func NewExpression(varOrder []string, vars map[string]Evallable, expr Evallable) Evallable

NewExpression creates a new expression Evallable.

func NewFunction

func NewFunction(name string, params []Evallable) (Evallable, error)

NewFunction creates a function Evallable, erroring if it can on the format of the parameters.

func NewListExpression

func NewListExpression(items []Evallable) Evallable

NewListExpression creates a new list of epxressions for a row.

func NewNumber

func NewNumber(value int) Evallable

NewNumber creates a new numeric Evallable.

func NewString

func NewString(value string, isLabel bool) Evallable

NewString creates a new string evallable.

func NewTableCall

func NewTableCall(
	packageKey string,
	packageName string,
	tableName string,
	params []Evallable,
) (Evallable, error)

NewTableCall creates a new table call Evallable.

func NewVariable

func NewVariable(name string) Evallable

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

func NewProgram(packs TableMap) *Program

NewProgram creates a new program from a keyed set of tablepacks.

func (*Program) Copy

func (p *Program) Copy() *Program

Copy returns a deep copy of the Program

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) PackCount

func (p *Program) PackCount() int

PackCount returns the number of packs (files) loaded into this program.

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.

func NewRange

func NewRange(low int, high int) *Range

NewRange creates a new range value.

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 NewRoll

func NewRoll(count int, sides int) *Roll

NewRoll creates a new roll value.

func (*Roll) Eval

func (r *Roll) Eval() ExpressionEval

Eval implementation for Evallable interface.

func (*Roll) WithAggr

func (r *Roll) WithAggr(aggrFn string) *Roll

WithAggr configures this roll with an aggregation function.

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) WithPrint

func (r *Roll) WithPrint(print bool) *Roll

WithPrint configures this roll to print if set to true.

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.

func (*String) IsLabel

func (s *String) IsLabel() bool

IsLabel returns whether the string was considered a label at parse time.

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 NewTable

func NewTable(name string, tags map[string]string, rows []*TableRow) *Table

NewTable creates a new table object.

func (*Table) Copy

func (t *Table) Copy() *Table

Copy deep copies a Table

func (*Table) DeckDraw

func (t *Table) DeckDraw() (Evallable, error)

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

func (t *Table) Default() (Evallable, error)

Default returns the default row for the table or an error if ther isn't one.

func (*Table) IndexRoll

func (t *Table) IndexRoll(key int) (Evallable, error)

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

func (t *Table) LabelRoll(key string) (Evallable, error)

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) Name

func (t *Table) Name() string

Name returns the defined name of the table.

func (*Table) Roll

func (t *Table) Roll() Evallable

Roll randomly on the table treating each row with equal weight.

func (*Table) RowCount

func (t *Table) RowCount() int

RowCount returns the number of rows in the table.

func (*Table) Shuffle

func (t *Table) Shuffle()

Shuffle resets all counts for DeckDraw calls.

func (*Table) Tag

func (t *Table) Tag(tagName string) (string, bool)

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

func (t *Table) TotalCount() int

TotalCount returns the total number of "cards" for a DeckDraw.

func (*Table) TotalWeight

func (t *Table) TotalWeight() int

TotalWeight returns the total weight of all tale rows.

func (*Table) WeightedRoll

func (t *Table) WeightedRoll() Evallable

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 TableMap

type TableMap map[string]*TablePack

TableMap is a type alias for mapping file hash keys to table definitions.

type TablePack

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

TablePack represents a single executable tableman source file.

func NewTablePack

func NewTablePack(key string, name string, tables map[string]*Table) *TablePack

NewTablePack creates a new TablePack with the given tables.

func (*TablePack) Copy

func (t *TablePack) Copy() *TablePack

Copy deep copies a TablePack

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) Copy

func (r *TableRow) Copy() *TableRow

Copy deep copies a TableRow

func (*TableRow) Count

func (r *TableRow) Count() int

Count returns the current DeckDraw count for the row.

func (*TableRow) Default

func (r *TableRow) Default() bool

Default returns whether the row is a default value.

func (*TableRow) Label

func (r *TableRow) Label() string

Label returns the label for the row, or an empty string if one isn't defined.

func (*TableRow) Ranges

func (r *TableRow) Ranges() []*Range

Ranges returns the list of index ranges defined for this row.

func (*TableRow) Value

func (r *TableRow) Value() Evallable

Value returns the Evalable avlue for this row.

func (*TableRow) Weight

func (r *TableRow) Weight() int

Weight returns the weight of the row.

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.

func (*TestingRandSource) Get

func (r *TestingRandSource) Get(low int, high int) int

Get implementation for RandomSource.

type Variable

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

Variable is an evallable for a variable access.

func (*Variable) Eval

func (v *Variable) Eval() ExpressionEval

Eval implementation for Evallable interface.

Jump to

Keyboard shortcuts

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