75行目: |
75行目: |
| end | | end |
| </syntaxhighlight> | | </syntaxhighlight> |
| + | |
| + | == parren_killer == |
| + | |
| + | https://github.com/torgtaitai/BCDice/blob/86b8dd03ae00f8ea6a8787f9f514a5d83b2e3d41/src/bcdiceCore.rb#L1550-L1608 |
| + | |
| + | 括弧内前処理、つまり括弧で囲まれた数式部分を還元して整数に変える処理。 |
| + | |
| + | BCDiceのダイス構文は正規文法ではないのに、正規表現だけで処理しようとしているため、無理矢理気味な処理となっている。また、原理上、文法エラーを検出できない場合がある。例えば、 |
| + | |
| + | * <code>(2*/2)d6</code> → <code>(2*/2)d6 : (1D6) > 3</code> |
| + | |
| + | など。 |
| + | |
| + | <syntaxhighlight lang="ruby"> |
| + | def parren_killer(string) |
| + | debug("parren_killer input", string) |
| + | |
| + | while( /^(.*?)\[(\d+[Dd]\d+)\](.*)/ =~ string ) |
| + | str_before = "" |
| + | str_after = "" |
| + | dice_cmd = $2 |
| + | str_before = $1 if($1) |
| + | str_after = $3 if($3) |
| + | rolled, dmy = rollDiceAddingUp(dice_cmd) |
| + | string = "#{str_before}#{rolled}#{str_after}" |
| + | end |
| + | |
| + | string = changeRangeTextToNumberText(string) |
| + | |
| + | while(/^(.*?)(\([\d\/*+-]+?\))(.*)/ =~ string) |
| + | debug("while string", string) |
| + | |
| + | str_a = $3 |
| + | str_a ||= "" |
| + | |
| + | str_b = $1 |
| + | str_b ||= "" |
| + | debug("str_b", str_b) |
| + | |
| + | par_i = $2 |
| + | |
| + | debug("par_i", par_i) |
| + | par_o = paren_k(par_i) |
| + | debug("par_o", par_o) |
| + | |
| + | if(par_o != 0) |
| + | if(par_o < 0) |
| + | if(/(.+?)(\+)$/ =~ str_b) |
| + | str_b = $1 |
| + | elsif(/(.+?)(-)$/ =~ str_b) |
| + | str_b = "#{$1}+" |
| + | par_o = par_o * -1 |
| + | end |
| + | end |
| + | string = "#{str_b}#{par_o}#{str_a}" |
| + | else |
| + | if(/^([DBRUdbru][\d]+)(.*)/ =~ $str_a) |
| + | str_a = $2 |
| + | end |
| + | string = "#{str_b}0#{str_a}" |
| + | end |
| + | end |
| + | |
| + | debug("diceBot.changeText(string) begin", string) |
| + | string = @diceBot.changeText(string) |
| + | debug("diceBot.changeText(string) end", string) |
| + | |
| + | string = string.gsub(/([\d]+[dD])([^\d\w]|$)/) {"#{$1}6#{$2}"} |
| + | |
| + | debug("parren_killer output", string) |
| + | |
| + | return string |
| + | end |
| + | </syntaxhighlight> |
| + | |
| | | |
| {{DEFAULTSORT:BCDice くらす}} | | {{DEFAULTSORT:BCDice くらす}} |
| [[Category:BCDice/内部処理]] | | [[Category:BCDice/内部処理]] |