evaluator

package
v0.0.0-...-3319774 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

BCDiceコマンドの評価処理のパッケージ。

主な処理は、以下の3つ。

1. ダイスロールなどの可変ノードの引数を評価し、整数に変換する。

2. 可変ノードの値を決定する。

3. 抽象構文木を評価し、値のオブジェクトに変換する。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Environment

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

コマンド評価の環境を表す構造体。

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment は新しいコマンド評価環境を返す。

func (*Environment) AppendRolledDice

func (e *Environment) AppendRolledDice(dice []dice.Die)

AppendRolledDice は振られたダイスの列を記録に追加する。

func (*Environment) ClearRolledDice

func (e *Environment) ClearRolledDice()

ClearRolledDice は記録されたダイスロール結果をクリアする。

func (*Environment) PushRolledDie

func (e *Environment) PushRolledDie(d dice.Die)

PushRolledDie は振られたダイスを記録に追加する。

func (*Environment) RolledDice

func (e *Environment) RolledDice() []dice.Die

RolledDice は記録されたダイスロール結果を返す。

type Evaluator

type Evaluator struct {

	// 個数振り足しロールにおける最大振り足し数
	// TODO: 外部から変更するためのインターフェースを作る
	MaxRerolls int
	// contains filtered or unexported fields
}

評価器の構造体。

func NewEvaluator

func NewEvaluator(diceRoller *roller.DiceRoller, env *Environment) *Evaluator

NewEvaluator は新しい評価器を返す。

diceRoller: ダイスローラー, env: 評価環境

func (*Evaluator) CheckRRollThreshold

func (e *Evaluator) CheckRRollThreshold(node *ast.RRollList) error

CheckRRollThreshold は個数振り足しロールの振り足しの閾値をチェックする。

func (*Evaluator) CheckURollThreshold

func (e *Evaluator) CheckURollThreshold(node *ast.RRollList) error

CheckURollThreshold は上方無限ロールの振り足しの閾値をチェックする。

func (*Evaluator) DetermineValues

func (e *Evaluator) DetermineValues(node ast.Node) error

DetermneValuesは、可変ノードの値を決定する

Example

可変ノードの値を決定する例。

r, parseErr := parser.Parse("ExampleEvaluator_DetermineValues", []byte("2d6*3-1d6+1"))
if parseErr != nil {
	return
}

node := r.(ast.Node)

fmt.Println("構文解析直後の抽象構文木: " + node.SExp())

// 可変ノードの値を決定する
dieFeeder := feeder.NewQueue([]dice.Die{{6, 6}, {2, 6}, {3, 6}})
evaluator := NewEvaluator(roller.New(dieFeeder), NewEnvironment())

err := evaluator.DetermineValues(node)
if err != nil {
	return
}

fmt.Println("ダイスロール結果: " + dice.FormatDice(evaluator.RolledDice()))
fmt.Println("値決定後の抽象構文木: " + node.SExp())
Output:

構文解析直後の抽象構文木: (DRollExpr (+ (- (* (DRoll 2 6) 3) (DRoll 1 6)) 1))
ダイスロール結果: 6/6, 2/6, 3/6
値決定後の抽象構文木: (DRollExpr (+ (- (* (SumRollResult (Die 6 6) (Die 2 6)) 3) (SumRollResult (Die 3 6))) 1))

func (*Evaluator) Eval

func (e *Evaluator) Eval(node ast.Node) (object.Object, error)

Eval はnodeを評価してObjectに変換し、返す。

Example

抽象構文木を評価し、値のオブジェクトに変換する例。

// 構文解析する
r, parseErr := parser.Parse("ExampleEvaluator_Eval", []byte("(2*3-4)d6-1d4+1"))
if parseErr != nil {
	return
}

node := r.(ast.Node)

fmt.Println("抽象構文木: " + node.SExp())

// ノードを評価する
dieFeeder := feeder.NewQueue([]dice.Die{{6, 6}, {2, 6}, {3, 4}})
evaluator := NewEvaluator(roller.New(dieFeeder), NewEnvironment())

obj, evalErr := evaluator.Eval(node)
if evalErr != nil {
	return
}

fmt.Println("ダイスロール結果: " + dice.FormatDice(evaluator.RolledDice()))
fmt.Println("評価結果: " + obj.Inspect())
Output:

抽象構文木: (DRollExpr (+ (- (DRoll (- (* 2 3) 4) 6) (DRoll 1 4)) 1))
ダイスロール結果: 6/6, 2/6, 3/4
評価結果: 6

func (*Evaluator) EvalCompareLeft

func (e *Evaluator) EvalCompareLeft(node *ast.BasicInfixExpression) (object.Object, error)

EvalCompareLeft は比較式の左辺を評価する。

func (*Evaluator) EvalVarArgs

func (e *Evaluator) EvalVarArgs(node ast.Node) error

EvalVarArgs は可変ノードの引数を評価して整数に変換する。

Example

可変ノードの引数を評価して整数に変換する例。

// 構文解析する
r, parseErr := parser.Parse("ExampleEvaluator_EvalVarArgs", []byte("(2*3-4)d6-1d4+1"))
if parseErr != nil {
	return
}

node := r.(ast.Node)

fmt.Println("構文解析直後の抽象構文木: " + node.SExp())

// 可変ノードの引数を評価して整数に変換する
dieFeeder := feeder.NewMT19937WithSeedFromTime()
evaluator := NewEvaluator(roller.New(dieFeeder), NewEnvironment())

evalErr := evaluator.EvalVarArgs(node)
if evalErr != nil {
	return
}

fmt.Println("引数評価後の抽象構文木: " + node.SExp())
Output:

構文解析直後の抽象構文木: (DRollExpr (+ (- (DRoll (- (* 2 3) 4) 6) (DRoll 1 4)) 1))
引数評価後の抽象構文木: (DRollExpr (+ (- (DRoll 2 6) (DRoll 1 4)) 1))

func (*Evaluator) RollDice

func (e *Evaluator) RollDice(num int, sides int) ([]dice.Die, error)

RollDice は、sides個の面を持つダイスをnum個振り、その結果を返す。 また、ダイスロールの結果を記録する。

func (*Evaluator) RolledDice

func (e *Evaluator) RolledDice() []dice.Die

RolledDice はダイスロール結果を返す。

func (*Evaluator) SetRerollThreshold

func (e *Evaluator) SetRerollThreshold(node *ast.Command) error

SetRerollThreshold は振り足しの閾値を設定する。

Jump to

Keyboard shortcuts

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