notation

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: 5 Imported by: 0

Documentation

Overview

抽象構文木の中置表記生成処理のパッケージ。

抽象構文木の中置表記は、BCDiceコマンドの実行結果のメッセージに含まれる。 中置表記を表示することで、利用者が入力したコマンドが構文解析器に正しく解釈されていることを示すことができる。 この中置表記は、日常的に数式で使われている、最小限の括弧を含むものとする。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatModifier

func FormatModifier(modifier int) string

FormatModifier は修正値を符号に応じて整形する

func InfixNotation

func InfixNotation(node ast.Node, walkingToLeft bool) (string, error)

InfixNotation は構文解析木の中置表記を返す。

node: 構文解析木のルートノード, walkingToLeft: 左側への探索を続けているか。

walkingToLeft は、単項マイナスを括弧で囲むかどうかの決定に使われる。 括弧で囲まれた範囲のルートノードから連続して左側を調べていき、 最左端が単項マイナスであれば括弧で囲まないように、そうでなければ 括弧で囲むようにしている。 この最左端かどうかの判定にwalkingToLeftを使っている。 ルートノードに対してこの関数を呼び出すときはwalkingToLeftをtrueに 設定し、右側の中置表記を求める際にはfalseを設定する。

Example (Associativity)

演算子の結合性を考慮した中置表記の生成例。 "C(1+(2-(3-4)))" を入力した場合: "+" は右結合性であるため、"(2-(3-4))" の部分全体には括弧は不要。 "-" は右結合性でないため、"-(3-4)" では括弧が必要。

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

node := r.(ast.Node)

// 中置表記を生成する
infixNotation, notationErr := InfixNotation(node, true)
if notationErr != nil {
	return
}

// 中置表記を出力する
fmt.Println(infixNotation)
Output:

C(1+2-(3-4))
Example (OperatorPrecedence)

演算子の優先順位を考慮した中置表記の生成例。 "C((1-(2*3))/4)" を入力した場合: 演算子の優先順位が "-" < "*" であるため、"2*3" には括弧は不要。 演算子の優先順位が "-" < "/" であるため、"/" の左側には括弧が必要。

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

node := r.(ast.Node)

// 中置表記を生成する
infixNotation, notationErr := InfixNotation(node, true)
if notationErr != nil {
	return
}

// 中置表記を出力する
fmt.Println(infixNotation)
Output:

C((1-2*3)/4)
Example (SumRoll)

加算ロール式の中置表記の例。

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

node := r.(ast.Node)

// 中置表記を生成する
infixNotation, notationErr := InfixNotation(node, true)
if notationErr != nil {
	return
}

// 中置表記を出力する
fmt.Println(infixNotation)
Output:

(2*3-4)D6-1D4+1

func Parenthesize

func Parenthesize(s string) string

Parenthesize は文字列を括弧で囲む。

Example
parenthesized := Parenthesize("1+2")
fmt.Println(parenthesized)
Output:

(1+2)

Types

This section is empty.

Jump to

Keyboard shortcuts

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