Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/bcdice/arithmetic/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,38 @@ def divide_and_round(dividend, divisor, _round_type)
end
end

# べき乗(x^n)のノード
class Power
# ノードを初期化する
# @param [Object] base 底のノード
# @param [Object] exp 指数のノード
def initialize(base, exp)
@base = base
@exp = exp
end

# @param round_type [Symbol] 端数処理方法
# @return [Integer] 評価結果
# @raise [ArgumentError] 指数が負の場合
def eval(round_type)
b = @base.eval(round_type)
e = @exp.eval(round_type)
raise ArgumentError, "べき乗の指数に負の値は使用できません: #{e}" if e.negative?

b**e
end

# @return [String] メッセージへの出力
def output
"#{@base.output}^#{@exp.output}"
end

# @return [String] ノードのS式
def s_exp
"(^ #{@base.s_exp} #{@exp.s_exp})"
end
end

class Negative
def initialize(body)
@body = body
Expand Down
6 changes: 5 additions & 1 deletion lib/bcdice/arithmetic/parser.y
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BCDice::Arithmetic::Parser
token NUMBER R U C F PLUS MINUS ASTERISK SLASH PARENL PARENR
token NUMBER R U C F PLUS MINUS ASTERISK SLASH PARENL PARENR CARET

rule
add: add PLUS mul
Expand Down Expand Up @@ -32,6 +32,10 @@ class BCDice::Arithmetic::Parser
{ result = val[1] }
| MINUS unary
{ result = Arithmetic::Node::Negative.new(val[1]) }
| power

power: term CARET power
{ result = Arithmetic::Node::Power.new(val[0], val[2]) }
| term

term: PARENL add PARENR
Expand Down
6 changes: 5 additions & 1 deletion lib/bcdice/common_command/calc/parser.y
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BCDice::CommonCommand::Calc::Parser
token NUMBER R U C F S PLUS MINUS ASTERISK SLASH PARENL PARENR
token NUMBER R U C F S PLUS MINUS ASTERISK SLASH PARENL PARENR CARET

rule
expr: secret C add
Expand Down Expand Up @@ -45,6 +45,10 @@ class BCDice::CommonCommand::Calc::Parser
{ result = val[1] }
| MINUS unary
{ result = Arithmetic::Node::Negative.new(val[1]) }
| power

power: term CARET power
{ result = Arithmetic::Node::Power.new(val[0], val[2]) }
| term

term: PARENL add PARENR
Expand Down
1 change: 1 addition & 0 deletions lib/bcdice/common_command/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Lexer
"]" => :BRACKETR,
"?" => :QUESTION,
"@" => :AT,
"^" => :CARET,
}.freeze

def initialize(source)
Expand Down
26 changes: 26 additions & 0 deletions test/data/Cthulhu.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
[[ test ]]
game_system = "Cthulhu"
input = "c(10^10)"
output = "c(10^10) > 10000000000"
rands = [
]
[[ test ]]
game_system = "Cthulhu"
input = "c((2^3^4)-2^(3^4))"
output = "c((2^3^4)-2^(3^4)) > 0"
rands = [
]
[[ test ]]
game_system = "Cthulhu"
input = "c((2^3^4)-(2^3)^4)"
output = "c((2^3^4)-(2^3)^4) > 2417851639229258349408256"
rands = [
]
[[ test ]]
game_system = "Cthulhu"
input = "c(10*5)"
output = "c(10*5) > 50"
rands = [
]


[[ test ]]
game_system = "Cthulhu"
input = "1D100<=70 ファンブルなし"
Expand Down
Loading