fbe 0.0.19 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d25c37b43cfba051943236b5a62e33f24e64569c638cabe65aff2a9bca5823dd
4
- data.tar.gz: 99870b5a82826ea29fee426e357c6cf51533ad480c14a7b46a8a408a238b4a91
3
+ metadata.gz: a57f0e5f562ad6ac7400355614f0af933b2130470759a877d34ea35878a4e52c
4
+ data.tar.gz: 0c93b9b7d1de5cf6f2c5bac41f71b663e19d7c1af667f5e6a41c53a361392afc
5
5
  SHA512:
6
- metadata.gz: 64c1b1d1be331458fe3c34a99a80c81f5bf2e77a6e7b378d1269d5c421024e049b61cdf5651ccd435b7a1233eb8d315e8e39f8d04091cbcf813468e1417b94e0
7
- data.tar.gz: b2feba50e12a9b157b09cbea421f87c97d50201afc0da2619f144f1b6963937718ffdf4fc050995d8c8b7c38798955989bb7b4dcf6d14f0c525c451c8002e738
6
+ metadata.gz: 1ec9cb12e7388c4260e9740255af73d8993e5b05abb0ee613bde3ee87e140a9b4b2bb1177c3dd14025e2a83c3eb3f87e8f5b84293f2b52256491860dd67b0807
7
+ data.tar.gz: e11c60c5bedc1abe4eb68702285c8a76f08c03e3b3e76a3dbf01f8ab514a1c024e1af0f1b38408cff5b5e7d370e011e5da1fee7d9c11e0be50d8e0ccd641de89
data/lib/fbe/award.rb CHANGED
@@ -67,6 +67,12 @@ class Fbe::Award
67
67
  rescue StandardError => e
68
68
  raise "Failure in #{o}: #{e.message}"
69
69
  end
70
+ when :aka
71
+ @operands[0..-2].each do |o|
72
+ o.bill_to(bill)
73
+ rescue StandardError => e
74
+ raise "Failure in #{o}: #{e.message}"
75
+ end
70
76
  when :let, :set
71
77
  bill.set(@operands[0], to_val(@operands[1], bill))
72
78
  when :give
@@ -145,31 +151,31 @@ class Fbe::Award
145
151
  when :total
146
152
  'total'
147
153
  when :if
148
- "if #{to_str(@operands[0])} then #{to_str(@operands[1])} else #{to_str(@operands[2])}"
154
+ "if #{to_p(@operands[0])} then #{to_p(@operands[1])} else #{to_p(@operands[2])}"
149
155
  when :eq
150
- "#{to_str(@operands[0])} == #{to_str(@operands[1])}"
156
+ "#{to_p(@operands[0])} = #{to_p(@operands[1])}"
151
157
  when :lt
152
- "#{to_str(@operands[0])} < #{to_str(@operands[1])}"
158
+ "#{to_p(@operands[0])} < #{to_p(@operands[1])}"
153
159
  when :lte
154
- "#{to_str(@operands[0])} ≤ #{to_str(@operands[1])}"
160
+ "#{to_p(@operands[0])} ≤ #{to_p(@operands[1])}"
155
161
  when :gt
156
- "#{to_str(@operands[0])} > #{to_str(@operands[1])}"
162
+ "#{to_p(@operands[0])} > #{to_p(@operands[1])}"
157
163
  when :gte
158
- "#{to_str(@operands[0])} ≥ #{to_str(@operands[1])}"
164
+ "#{to_p(@operands[0])} ≥ #{to_p(@operands[1])}"
159
165
  when :div
160
- "#{to_str(@operands[0])} ÷ #{to_str(@operands[1])}"
166
+ "#{to_p(@operands[0])} ÷ #{to_p(@operands[1])}"
161
167
  when :times
162
- "#{to_str(@operands[0])} × #{to_str(@operands[1])}"
168
+ "#{to_p(@operands[0])} × #{to_p(@operands[1])}"
163
169
  when :plus
164
- "#{to_str(@operands[0])} + #{to_str(@operands[1])}"
170
+ "#{to_p(@operands[0])} + #{to_p(@operands[1])}"
165
171
  when :minus
166
- "#{to_str(@operands[0])} - #{to_str(@operands[1])}"
172
+ "#{to_p(@operands[0])} - #{to_p(@operands[1])}"
167
173
  when :max
168
- "maximum of #{to_str(@operands[0])} and #{to_str(@operands[1])}"
174
+ "maximum of #{to_p(@operands[0])} and #{to_p(@operands[1])}"
169
175
  when :min
170
- "minimum of #{to_str(@operands[0])} and #{to_str(@operands[1])}"
176
+ "minimum of #{to_p(@operands[0])} and #{to_p(@operands[1])}"
171
177
  when :between
172
- "at least #{to_str(@operands[0])} and at most #{to_str(@operands[1])}"
178
+ "at least #{to_p(@operands[0])} and at most #{to_p(@operands[1])}"
173
179
  else
174
180
  raise "Unknown term '#{@op}'"
175
181
  end
@@ -183,26 +189,52 @@ class Fbe::Award
183
189
  rescue StandardError => e
184
190
  raise "Failure in #{o}: #{e.message}"
185
191
  end
192
+ when :aka
193
+ @operands[0..-2].each do |o|
194
+ o.publish_to(policy)
195
+ rescue StandardError => e
196
+ raise "Failure in #{o}: #{e.message}"
197
+ end
198
+ policy.revert(@operands.size - 1)
199
+ policy.line(to_p(@operands[@operands.size - 1]))
186
200
  when :explain
187
- policy.intro(to_str(@operands[0]))
201
+ policy.intro(to_p(@operands[0]))
188
202
  when :in
189
- policy.line("assume that #{to_str(@operands[0])} is #{to_str(@operands[1])}")
203
+ policy.line("assume that #{to_p(@operands[0])} is #{to_p(@operands[1])}")
190
204
  when :let
191
- policy.line("let #{to_str(@operands[0])} be equal to #{to_str(@operands[1])}")
205
+ policy.line("let #{to_p(@operands[0])} be equal to #{to_p(@operands[1])}")
206
+ policy.let(@operands[0], @operands[1])
192
207
  when :set
193
- policy.line("set #{to_str(@operands[0])} to #{to_str(@operands[1])}")
208
+ policy.line("set #{to_p(@operands[0])} to #{to_p(@operands[1])}")
194
209
  when :give
195
- policy.line("award #{to_str(@operands[0])}")
210
+ policy.line("award #{to_p(@operands[0])}")
196
211
  else
197
212
  raise "Unknown term '#{@op}'"
198
213
  end
199
214
  end
200
215
 
201
- def to_str(any)
202
- if any.is_a?(PTerm)
216
+ def to_p(any)
217
+ case any
218
+ when PTerm
203
219
  any.to_s
204
- elsif any.is_a?(Symbol)
205
- "_#{any}_"
220
+ when Symbol
221
+ s = any.to_s
222
+ subs = {
223
+ 0 => '₀',
224
+ 1 => '₁',
225
+ 2 => '₂',
226
+ 3 => '₃',
227
+ 4 => '₄',
228
+ 5 => '₅',
229
+ 6 => '₆',
230
+ 7 => '₇',
231
+ 8 => '₈',
232
+ 9 => '₉'
233
+ }
234
+ s.gsub!(/([a-z]+)([0-9])/) { |_| "#{Regexp.last_match[1]}#{subs[Regexp.last_match[2].to_i]}" }
235
+ "_#{s.gsub('_', '-')}_"
236
+ when Integer, Float
237
+ "**#{any}**"
206
238
  else
207
239
  any
208
240
  end
@@ -245,6 +277,11 @@ class Fbe::Award
245
277
  def initialize
246
278
  @lines = []
247
279
  @intro = ''
280
+ @lets = {}
281
+ end
282
+
283
+ def revert(num)
284
+ @lines.slice!(-num, num)
248
285
  end
249
286
 
250
287
  def intro(text)
@@ -252,11 +289,24 @@ class Fbe::Award
252
289
  end
253
290
 
254
291
  def line(line)
292
+ line = line.gsub(/\$\{([a-z_0-9]+)\}/) { |_x| @lets[Regexp.last_match[1].to_sym] }
255
293
  @lines << line
256
294
  end
257
295
 
296
+ def let(key, value)
297
+ @lets[key] = value
298
+ end
299
+
258
300
  def markdown
259
- "#{@intro}. Here it how it's calculated: First, #{@lines.join('. Then, ')}."
301
+ pars = []
302
+ pars << "#{@intro}." unless @intro.empty?
303
+ pars << 'Here is how it\'s calculated:'
304
+ if @lines.size == 1
305
+ pars << "Just #{@lines.first}."
306
+ else
307
+ pars += @lines.each_with_index.map { |t, i| "#{i.zero? ? 'First' : 'Then'}, #{t}." }
308
+ end
309
+ pars.join(' ').gsub('. Then, award ', ', and award ')
260
310
  end
261
311
  end
262
312
  end
data/lib/fbe.rb CHANGED
@@ -27,5 +27,5 @@
27
27
  # License:: MIT
28
28
  module Fbe
29
29
  # Current version of the gem (changed by .rultor.yml on every release)
30
- VERSION = '0.0.19'
30
+ VERSION = '0.0.21'
31
31
  end
@@ -40,8 +40,11 @@ class TestAward < Minitest::Test
40
40
  (let max 36)
41
41
  (let basis 30)
42
42
  (give basis "as a basis")
43
- (set b1 (if (lt hours max) 10 0))
44
- (give b1 "for resolving the bug in ${hours} (<${max}) hours")
43
+ (let fee 10)
44
+ (aka
45
+ (set b1 (if (lt hours max) fee 0))
46
+ (give b1 "for resolving the bug in ${hours} (<${max}) hours")
47
+ "add ${+fee} if it was resolved in less than ${max} hours")
45
48
  (set days (div hours 24))
46
49
  (set b2 (times days -1))
47
50
  (let worst -20)
@@ -67,7 +70,7 @@ class TestAward < Minitest::Test
67
70
  md = a.policy.markdown
68
71
  [
69
72
  'First, assume that _hours_ is hours',
70
- 'Then, award _b2_'
73
+ ', and award _b₂_'
71
74
  ].each { |t| assert(md.include?(t), md) }
72
75
  end
73
76
 
@@ -83,4 +86,16 @@ class TestAward < Minitest::Test
83
86
  assert_equal(v, a.bill.points, q)
84
87
  end
85
88
  end
89
+
90
+ def test_some_policies
91
+ {
92
+ '(award (let x_a 25) (set z (plus x_a 1)) (give z "..."))' =>
93
+ 'First, let _x-a_ be equal to **25**. Then, set _z_ to _x-a_ + **1**, and award _z_.',
94
+ '(award (aka (let x 17) (give x "hey") "add ${x} when necessary"))' =>
95
+ 'Just add 17 when necessary'
96
+ }.each do |q, t|
97
+ md = Fbe::Award.new(q).policy.markdown
98
+ assert(md.include?(t), md)
99
+ end
100
+ end
86
101
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fbe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-17 00:00:00.000000000 Z
11
+ date: 2024-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace