forked from NiSLa-NiSlimeTRPG/BCDice_N
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.rb
66 lines (55 loc) · 1.5 KB
/
preprocessor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
module BCDice
# 入力文字列に対して前処理を行う
#
# @example
# Preprocessor.process(
# "1d6+4D+(3*4) 切り取られる部分",
# game_system
# ) #=> "1d6+4D6+7"
class Preprocessor
# @param (see #initialize)
# @return [String]
def self.process(text, game_system)
Preprocessor.new(text, game_system).process()
end
# @param text [String]
# @param game_system [Base]
def initialize(text, game_system)
@text = text
@game_system = game_system
end
# @return [String]
def process
trim_after_whitespace()
replace_parentheses()
@text = @game_system.change_text(@text)
replace_implicit_d()
return @text
end
private
# 空白より前だけを取る
def trim_after_whitespace()
@text = @text.strip.split(/\s/, 2).first
end
# カッコ書きの数式を事前計算する
def replace_parentheses
loop do
prev = @text
@text = @text.gsub(%r{\([\d/+*\-CURF]+\)}) do |expr|
Arithmetic.eval(expr, @game_system.round_type) || expr
end
break if prev == @text
end
end
# nDをゲームシステムに応じて置き換える
def replace_implicit_d
@text = @text.gsub(/(\d+)D([^\w]|$)/i) do
times = Regexp.last_match(1)
sides = @game_system.sides_implicit_d
trailer = Regexp.last_match(2)
"#{times}D#{sides}#{trailer}"
end
end
end
end