wml_action 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,81 +16,110 @@ require 'wml_action/log'
16
16
  module WMLAction
17
17
  class Parser < Racc::Parser
18
18
 
19
- module_eval(<<'...end parser.y/module_eval...', 'parser.y', 49)
19
+ module_eval(<<'...end parser.y/module_eval...', 'parser.y', 62)
20
20
  include Log
21
21
 
22
22
  ...end parser.y/module_eval...
23
23
  ##### State transition tables begin ###
24
24
 
25
25
  racc_action_table = [
26
- 4, 8, 14, 16, 17, 18, 22, 24, 25, 26,
27
- 27, 29, 19, 4, 18, 21, 4, 7, 5, 30,
28
- 31, 32 ]
26
+ 4, 8, 14, 22, 33, 4, 45, 21, 31, 34,
27
+ 47, 35, 36, 37, 39, 16, 17, 18, 23, 25,
28
+ 26, 28, 29, 18, 19, 33, 41, 42, 43, 44,
29
+ 34, 33, 35, 36, 37, 38, 34, 33, 35, 36,
30
+ 37, 7, 34, 33, 35, 36, 37, 5, 34, 33,
31
+ 35, 36, 37, 4, 34, nil, 35, 36, 37, 41,
32
+ 42, 43, 44, nil, nil, nil, 52, 40, 41, 42,
33
+ 43, 44, 41, 42, 43, 44, 41, 42, 43, 44,
34
+ 41, 42, 43, 44 ]
29
35
 
30
36
  racc_action_check = [
31
- 6, 6, 6, 6, 6, 6, 18, 18, 18, 18,
32
- 18, 19, 6, 15, 19, 15, 0, 5, 1, 23,
33
- 27, 30 ]
37
+ 6, 6, 6, 18, 42, 15, 33, 15, 19, 42,
38
+ 38, 42, 42, 42, 29, 6, 6, 6, 18, 18,
39
+ 18, 18, 18, 19, 6, 34, 48, 48, 48, 48,
40
+ 34, 43, 34, 34, 34, 24, 43, 41, 43, 43,
41
+ 43, 5, 41, 44, 41, 41, 41, 1, 44, 22,
42
+ 44, 44, 44, 0, 22, nil, 22, 22, 22, 46,
43
+ 46, 46, 46, nil, nil, nil, 46, 32, 32, 32,
44
+ 32, 32, 51, 51, 51, 51, 49, 49, 49, 49,
45
+ 50, 50, 50, 50 ]
34
46
 
35
47
  racc_action_pointer = [
36
- 14, 18, nil, nil, nil, 17, -2, nil, nil, nil,
37
- nil, nil, nil, nil, nil, 11, nil, nil, -2, 7,
38
- nil, nil, nil, 6, nil, nil, nil, 9, nil, nil,
39
- 12, nil, nil ]
48
+ 51, 47, nil, nil, nil, 41, -2, nil, nil, nil,
49
+ nil, nil, nil, nil, nil, 3, nil, nil, -2, 4,
50
+ nil, nil, 42, nil, 10, nil, nil, nil, nil, -9,
51
+ nil, nil, 62, -4, 18, nil, nil, nil, -11, nil,
52
+ nil, 30, -3, 24, 36, nil, 53, nil, 20, 70,
53
+ 74, 66, nil ]
40
54
 
41
55
  racc_action_default = [
42
- -1, -26, -2, -3, -5, -26, -26, 33, -4, -6,
43
- -7, -8, -9, -10, -11, -26, -14, -15, -16, -26,
44
- -12, -13, -17, -18, -19, -20, -21, -26, -24, -25,
45
- -26, -22, -23 ]
56
+ -1, -37, -2, -3, -5, -37, -37, 53, -4, -6,
57
+ -7, -8, -9, -10, -11, -37, -24, -25, -26, -37,
58
+ -22, -23, -37, -27, -28, -29, -30, -31, -32, -37,
59
+ -35, -36, -37, -37, -37, -19, -20, -21, -37, -33,
60
+ -12, -37, -37, -37, -37, -17, -37, -34, -13, -14,
61
+ -15, -16, -18 ]
46
62
 
47
63
  racc_goto_table = [
48
- 3, 12, 10, 6, 9, 1, 11, 2, 13, 15,
49
- 23, nil, nil, nil, 28, 20 ]
64
+ 32, 3, 12, 1, 9, 10, 6, 11, 13, 27,
65
+ 2, 15, 46, 24, nil, 30, 20, nil, nil, 48,
66
+ 49, 50, 51 ]
50
67
 
51
68
  racc_goto_check = [
52
- 3, 7, 6, 4, 5, 1, 3, 2, 8, 9,
53
- 10, nil, nil, nil, 7, 3 ]
69
+ 10, 3, 7, 1, 5, 6, 4, 3, 8, 9,
70
+ 2, 11, 10, 12, nil, 7, 3, nil, nil, 10,
71
+ 10, 10, 10 ]
54
72
 
55
73
  racc_goto_pointer = [
56
- nil, 5, 7, 0, -1, -2, -4, -5, 2, 3,
57
- -8 ]
74
+ nil, 3, 10, 1, 2, -2, -1, -4, 2, -9,
75
+ -22, 5, -5 ]
58
76
 
59
77
  racc_goto_default = [
60
78
  nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
61
- nil ]
79
+ nil, nil, nil ]
62
80
 
63
81
  racc_reduce_table = [
64
82
  0, 0, :racc_error,
65
- 0, 16, :_reduce_none,
66
- 1, 16, :_reduce_2,
67
- 1, 17, :_reduce_3,
68
- 3, 18, :_reduce_4,
69
- 0, 19, :_reduce_5,
70
- 2, 19, :_reduce_6,
71
- 1, 20, :_reduce_none,
72
- 1, 20, :_reduce_8,
73
- 1, 20, :_reduce_none,
74
- 1, 20, :_reduce_none,
75
- 1, 20, :_reduce_11,
76
- 2, 21, :_reduce_12,
77
- 2, 21, :_reduce_13,
78
- 1, 24, :_reduce_none,
79
- 1, 24, :_reduce_none,
80
- 1, 22, :_reduce_16,
81
- 2, 22, :_reduce_17,
82
- 2, 22, :_reduce_18,
83
- 2, 22, :_reduce_19,
84
- 2, 22, :_reduce_20,
85
- 1, 25, :_reduce_21,
86
- 2, 25, :_reduce_22,
87
- 3, 25, :_reduce_23,
88
- 2, 23, :_reduce_24,
89
- 2, 23, :_reduce_25 ]
90
-
91
- racc_reduce_n = 26
92
-
93
- racc_shift_n = 33
83
+ 0, 28, :_reduce_none,
84
+ 1, 28, :_reduce_2,
85
+ 1, 29, :_reduce_3,
86
+ 3, 30, :_reduce_4,
87
+ 0, 31, :_reduce_5,
88
+ 2, 31, :_reduce_6,
89
+ 1, 32, :_reduce_none,
90
+ 1, 32, :_reduce_8,
91
+ 1, 32, :_reduce_none,
92
+ 1, 32, :_reduce_none,
93
+ 1, 32, :_reduce_11,
94
+ 3, 36, :_reduce_12,
95
+ 3, 37, :_reduce_13,
96
+ 3, 37, :_reduce_14,
97
+ 3, 37, :_reduce_15,
98
+ 3, 37, :_reduce_16,
99
+ 2, 37, :_reduce_17,
100
+ 3, 37, :_reduce_18,
101
+ 1, 37, :_reduce_19,
102
+ 1, 37, :_reduce_20,
103
+ 1, 37, :_reduce_21,
104
+ 2, 33, :_reduce_22,
105
+ 2, 33, :_reduce_23,
106
+ 1, 38, :_reduce_none,
107
+ 1, 38, :_reduce_none,
108
+ 1, 34, :_reduce_26,
109
+ 2, 34, :_reduce_27,
110
+ 2, 34, :_reduce_28,
111
+ 2, 34, :_reduce_29,
112
+ 2, 34, :_reduce_30,
113
+ 2, 34, :_reduce_31,
114
+ 1, 39, :_reduce_32,
115
+ 2, 39, :_reduce_33,
116
+ 3, 39, :_reduce_34,
117
+ 2, 35, :_reduce_35,
118
+ 2, 35, :_reduce_36 ]
119
+
120
+ racc_reduce_n = 37
121
+
122
+ racc_shift_n = 53
94
123
 
95
124
  racc_token_table = {
96
125
  false => 0,
@@ -98,18 +127,30 @@ racc_token_table = {
98
127
  :OTAG => 2,
99
128
  :CTAG => 3,
100
129
  :MACRO => 4,
101
- "+" => 5,
102
- "-" => 6,
103
- :ATTR => 7,
104
- :APLAIN => 8,
105
- :AMACRO => 9,
106
- :ANUMBER => 10,
107
- :ASTR => 11,
108
- :UNDERSC => 12,
109
- :APLUS => 13,
110
- :SLASH => 14 }
111
-
112
- racc_nt_base = 15
130
+ :BACKQ => 5,
131
+ :EPLUS => 6,
132
+ :EMINUS => 7,
133
+ :EMUL => 8,
134
+ :EDIV => 9,
135
+ :ENUMBER => 10,
136
+ :EUMIN => 11,
137
+ "(" => 12,
138
+ ")" => 13,
139
+ :ESTR => 14,
140
+ :ENUM => 15,
141
+ :EVAR => 16,
142
+ "+" => 17,
143
+ "-" => 18,
144
+ :ATTR => 19,
145
+ :APLAIN => 20,
146
+ :AMACRO => 21,
147
+ :ANUMBER => 22,
148
+ :ASTR => 23,
149
+ :UNDERSC => 24,
150
+ :APLUS => 25,
151
+ :SLASH => 26 }
152
+
153
+ racc_nt_base = 27
113
154
 
114
155
  racc_use_result_var = true
115
156
 
@@ -135,6 +176,18 @@ Racc_token_to_s_table = [
135
176
  "OTAG",
136
177
  "CTAG",
137
178
  "MACRO",
179
+ "BACKQ",
180
+ "EPLUS",
181
+ "EMINUS",
182
+ "EMUL",
183
+ "EDIV",
184
+ "ENUMBER",
185
+ "EUMIN",
186
+ "\"(\"",
187
+ "\")\"",
188
+ "ESTR",
189
+ "ENUM",
190
+ "EVAR",
138
191
  "\"+\"",
139
192
  "\"-\"",
140
193
  "ATTR",
@@ -154,6 +207,8 @@ Racc_token_to_s_table = [
154
207
  "action",
155
208
  "attribute",
156
209
  "filter",
210
+ "expression",
211
+ "expr",
157
212
  "aop",
158
213
  "string_val" ]
159
214
 
@@ -222,87 +277,164 @@ module_eval(<<'.,.,', 'parser.y', 16)
222
277
 
223
278
  module_eval(<<'.,.,', 'parser.y', 18)
224
279
  def _reduce_12(val, _values, result)
225
- log.debug "Found a action tag #{val[0]}:#{val[1]}"; return Tag::Action[val[1],val[0]]
280
+ return val[1]
226
281
  result
227
282
  end
228
283
  .,.,
229
284
 
230
- module_eval(<<'.,.,', 'parser.y', 19)
285
+ module_eval(<<'.,.,', 'parser.y', 20)
231
286
  def _reduce_13(val, _values, result)
232
- log.debug "Found a action mac #{val[0]}:#{val[1]}"; return Tag::Action[Tag::Macro[val[1]],val[0]]
287
+ log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]]
233
288
  result
234
289
  end
235
290
  .,.,
236
291
 
237
- # reduce 14 omitted
292
+ module_eval(<<'.,.,', 'parser.y', 21)
293
+ def _reduce_14(val, _values, result)
294
+ log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]]
295
+ result
296
+ end
297
+ .,.,
238
298
 
239
- # reduce 15 omitted
299
+ module_eval(<<'.,.,', 'parser.y', 22)
300
+ def _reduce_15(val, _values, result)
301
+ log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]]
302
+ result
303
+ end
304
+ .,.,
240
305
 
241
- module_eval(<<'.,.,', 'parser.y', 24)
306
+ module_eval(<<'.,.,', 'parser.y', 23)
242
307
  def _reduce_16(val, _values, result)
243
- log.debug "Found empty attribute: #{val[0]}"; return Tag::Attribute[val[0],'']
308
+ log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]]
244
309
  result
245
310
  end
246
311
  .,.,
247
312
 
248
- module_eval(<<'.,.,', 'parser.y', 25)
313
+ module_eval(<<'.,.,', 'parser.y', 24)
249
314
  def _reduce_17(val, _values, result)
250
- log.debug "Found plain attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
315
+ return Tag:Expr[-val[1]]
251
316
  result
252
317
  end
253
318
  .,.,
254
319
 
255
- module_eval(<<'.,.,', 'parser.y', 26)
320
+ module_eval(<<'.,.,', 'parser.y', 25)
256
321
  def _reduce_18(val, _values, result)
257
- log.debug "Found string attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
322
+ return val[1]
258
323
  result
259
324
  end
260
325
  .,.,
261
326
 
262
- module_eval(<<'.,.,', 'parser.y', 27)
327
+ module_eval(<<'.,.,', 'parser.y', 26)
263
328
  def _reduce_19(val, _values, result)
264
- log.debug "Found macro attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
329
+ log.debug "Found a string #{val[0]}"; return Tag::Expr[val[0]]
265
330
  result
266
331
  end
267
332
  .,.,
268
333
 
269
- module_eval(<<'.,.,', 'parser.y', 28)
334
+ module_eval(<<'.,.,', 'parser.y', 27)
270
335
  def _reduce_20(val, _values, result)
271
- log.debug "Found numeric attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
336
+ log.debug "Found a number #{val[0]}"; return Tag::Expr[val[0]]
272
337
  result
273
338
  end
274
339
  .,.,
275
340
 
276
- module_eval(<<'.,.,', 'parser.y', 30)
341
+ module_eval(<<'.,.,', 'parser.y', 28)
277
342
  def _reduce_21(val, _values, result)
278
- return " #{val[0]}"
343
+ log.debug "Found a variable #{val[0]}"; return Tag::Expr[Tag::Expr::Var[val[0]]]
279
344
  result
280
345
  end
281
346
  .,.,
282
347
 
283
- module_eval(<<'.,.,', 'parser.y', 31)
348
+ module_eval(<<'.,.,', 'parser.y', 30)
284
349
  def _reduce_22(val, _values, result)
285
- return ' '+val[0]+' '+val[1]
350
+ log.debug "Found a action tag #{val[0]}:#{val[1]}"; return Tag::Action[val[1],val[0]]
286
351
  result
287
352
  end
288
353
  .,.,
289
354
 
290
- module_eval(<<'.,.,', 'parser.y', 32)
355
+ module_eval(<<'.,.,', 'parser.y', 31)
291
356
  def _reduce_23(val, _values, result)
357
+ log.debug "Found a action mac #{val[0]}:#{val[1]}"; return Tag::Action[Tag::Macro[val[1]],val[0]]
358
+ result
359
+ end
360
+ .,.,
361
+
362
+ # reduce 24 omitted
363
+
364
+ # reduce 25 omitted
365
+
366
+ module_eval(<<'.,.,', 'parser.y', 36)
367
+ def _reduce_26(val, _values, result)
368
+ log.debug "Found empty attribute: #{val[0]}"; return Tag::Attribute[val[0],'']
369
+ result
370
+ end
371
+ .,.,
372
+
373
+ module_eval(<<'.,.,', 'parser.y', 37)
374
+ def _reduce_27(val, _values, result)
375
+ log.debug "Found plain attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
376
+ result
377
+ end
378
+ .,.,
379
+
380
+ module_eval(<<'.,.,', 'parser.y', 38)
381
+ def _reduce_28(val, _values, result)
382
+ log.debug "Found string attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
383
+ result
384
+ end
385
+ .,.,
386
+
387
+ module_eval(<<'.,.,', 'parser.y', 39)
388
+ def _reduce_29(val, _values, result)
389
+ log.debug "Found macro attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
390
+ result
391
+ end
392
+ .,.,
393
+
394
+ module_eval(<<'.,.,', 'parser.y', 40)
395
+ def _reduce_30(val, _values, result)
396
+ log.debug "Found numeric attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]]
397
+ result
398
+ end
399
+ .,.,
400
+
401
+ module_eval(<<'.,.,', 'parser.y', 41)
402
+ def _reduce_31(val, _values, result)
403
+ log.debug "Found expression attribute: #{val[0]}"; return Tag::Attribute[val[0],val[1]]
404
+ result
405
+ end
406
+ .,.,
407
+
408
+ module_eval(<<'.,.,', 'parser.y', 43)
409
+ def _reduce_32(val, _values, result)
410
+ return " #{val[0]}"
411
+ result
412
+ end
413
+ .,.,
414
+
415
+ module_eval(<<'.,.,', 'parser.y', 44)
416
+ def _reduce_33(val, _values, result)
417
+ return ' '+val[0]+' '+val[1]
418
+ result
419
+ end
420
+ .,.,
421
+
422
+ module_eval(<<'.,.,', 'parser.y', 45)
423
+ def _reduce_34(val, _values, result)
292
424
  return val[0] + '+' + val[2]
293
425
  result
294
426
  end
295
427
  .,.,
296
428
 
297
- module_eval(<<'.,.,', 'parser.y', 34)
298
- def _reduce_24(val, _values, result)
429
+ module_eval(<<'.,.,', 'parser.y', 47)
430
+ def _reduce_35(val, _values, result)
299
431
  log.debug "Found an attribute filter #{val[1]}"; return Tag::Filter[val[1]]
300
432
  result
301
433
  end
302
434
  .,.,
303
435
 
304
- module_eval(<<'.,.,', 'parser.y', 35)
305
- def _reduce_25(val, _values, result)
436
+ module_eval(<<'.,.,', 'parser.y', 48)
437
+ def _reduce_36(val, _values, result)
306
438
  log.debug "Found a macro filter #{val[1]}"; return Tag::Filter[Tag::Macro[val[1]]]
307
439
  result
308
440
  end
@@ -15,6 +15,18 @@ rule
15
15
  | attribute
16
16
  | filter
17
17
  | MACRO { log.debug "Found a macro #{val[0]}"; return Tag::Macro[val[0]] }
18
+
19
+ expression : BACKQ expr BACKQ { return val[1] }
20
+
21
+ expr : expr EPLUS expr { log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]] }
22
+ | expr EMINUS expr { log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]] }
23
+ | expr EMUL expr { log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]] }
24
+ | expr EDIV expr { log.debug "Found a #{val[0]} #{val[1]} #{val[2]} expression"; return val[0] << val[2] << Tag::Expr[Tag::Expr::Op[val[1]]] }
25
+ | EMINUS ENUMBER =EUMIN { return Tag:Expr[-val[1]] }
26
+ | '(' expr ')' { return val[1] }
27
+ | ESTR { log.debug "Found a string #{val[0]}"; return Tag::Expr[val[0]] }
28
+ | ENUM { log.debug "Found a number #{val[0]}"; return Tag::Expr[val[0]] }
29
+ | EVAR { log.debug "Found a variable #{val[0]}"; return Tag::Expr[Tag::Expr::Var[val[0]]] }
18
30
 
19
31
  action : aop tag { log.debug "Found a action tag #{val[0]}:#{val[1]}"; return Tag::Action[val[1],val[0]] }
20
32
  | aop MACRO { log.debug "Found a action mac #{val[0]}:#{val[1]}"; return Tag::Action[Tag::Macro[val[1]],val[0]] }
@@ -27,6 +39,7 @@ rule
27
39
  | ATTR string_val { log.debug "Found string attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]] }
28
40
  | ATTR AMACRO { log.debug "Found macro attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]] }
29
41
  | ATTR ANUMBER { log.debug "Found numeric attribute: #{val[0]}:#{val[1]}"; return Tag::Attribute[val[0],val[1]] }
42
+ | ATTR expression { log.debug "Found expression attribute: #{val[0]}"; return Tag::Attribute[val[0],val[1]] }
30
43
 
31
44
  string_val : ASTR { return " #{val[0]}" }
32
45
  | UNDERSC ASTR { return ' '+val[0]+' '+val[1] }
@@ -76,10 +76,22 @@ module WMLAction
76
76
  return self unless @name == other.name
77
77
  return self unless match?( other.filter )
78
78
  log.info "Merging [#{@name}] section with [#{other.name}] with filter: #{other.filter}"
79
+ delayed_attrs={}
79
80
  other.attrs.each_pair do |key,value|
80
81
  log.debug "Processing key: #{key}=#{value}"
82
+ if value.class==Expr
83
+ delayed_attrs.store(key,value)
84
+ next
85
+ end
81
86
  @attrs.store(key,value)
82
87
  end
88
+ saved_attrs=@attrs.clone unless delayed_attrs.empty?
89
+ delayed_attrs.each_pair do |key,value|
90
+ log.debug "Calculating attr expression: #{key}=#{value}"
91
+ result = value.result(@attrs)
92
+ @attrs.store(key,result)
93
+ end
94
+ saved_attrs=[]
83
95
  other.macros.each do |macro|
84
96
  log.debug "Adding macro: #{macro}"
85
97
  @macros<<(macro)
@@ -139,6 +151,10 @@ module WMLAction
139
151
  @actions.include?(action)
140
152
  end
141
153
 
154
+ def attr(name)
155
+ @attrs[name]
156
+ end
157
+
142
158
  end
143
159
 
144
160
  end
@@ -1,3 +1,3 @@
1
1
  module WMLAction
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/wml_action.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "wml_action/version"
2
2
  require "wml_action/tag"
3
+ require "wml_action/expr"
3
4
  require "wml_action/cli"
4
5
  require "wml_action/document"
5
6
  require "wml_action/parser.tab"
@@ -75,5 +75,12 @@ module WMLAction
75
75
  expect(s.actions[3].object.name).to eq "resists"
76
76
  end
77
77
 
78
+ it 'reads expressions' do
79
+ d = Document.from_file('spec/fixtures/expr.cfg')
80
+ s = d.root
81
+ expect(s.attr('hp')).to eq Tag::Expr[Tag::Expr::Var['hp'],3.0,Tag::Expr::Op['+']]
82
+ expect(s.attr('damage')).to eq Tag::Expr[Tag::Expr::Var['damage'],1.0,Tag::Expr::Op['-'],2.0,Tag::Expr::Op['*']]
83
+ end
84
+
78
85
  end
79
86
  end
data/spec/expr_spec.rb ADDED
@@ -0,0 +1,89 @@
1
+ require 'wml_action'
2
+ require 'debugger'
3
+
4
+ module WMLAction
5
+
6
+ describe Tag::Expr do
7
+
8
+ before(:all) do
9
+ Expr=Tag::Expr unless WMLAction.const_defined? 'Expr'
10
+ Var=Tag::Expr::Var unless WMLAction.const_defined? 'Var'
11
+ Op=Tag::Expr::Op unless WMLAction.const_defined? 'Op'
12
+ end
13
+
14
+ describe '#result' do
15
+
16
+ it 'sums numbers' do
17
+ e=Expr[1,2,Op['+']]
18
+ expect(e.result).to eq 3
19
+ end
20
+
21
+ it 'substract numbers' do
22
+ e=Expr[2,1,Op['-']]
23
+ expect(e.result).to eq 1
24
+ end
25
+
26
+ it 'multiply numbers' do
27
+ e=Expr[2,3,Op['*']]
28
+ expect(e.result).to eq 6
29
+ end
30
+
31
+ it 'multiply numbers' do
32
+ e=Expr[6,3,Op['/']]
33
+ expect(e.result).to eq 2
34
+ end
35
+
36
+ it 'more than one operation' do
37
+ e=Expr[2,3,Op['*'],3,Op['-']]
38
+ expect(e.result).to eq 3
39
+ end
40
+
41
+ it 'results in integers' do
42
+ e=Expr[6,4,Op['/']]
43
+ expect(e.result).to be_integer
44
+ end
45
+
46
+ it 'concatenates strings' do
47
+ e=Expr['Hello',' World!',Op['+']]
48
+ expect(e.result).to eq 'Hello World!'
49
+ end
50
+
51
+ it 'substitute variables' do
52
+ e=Expr[Var[:a],Var[:b],Op['+']]
53
+ expect(e.result({a: 1, b: 3})).to eq 4
54
+ end
55
+
56
+ it 'returns empty string on empty expr' do
57
+ e=Expr[]
58
+ expect(e.result).to eq ''
59
+ end
60
+
61
+ it 'raises syntax error on error with binary op' do
62
+ e=Expr[1,Op['+']]
63
+ expect { e.result }.to raise_error
64
+ end
65
+
66
+ it 'raises syntax error on error with lack of op' do
67
+ e=Expr[1,2]
68
+ expect { e.result }.to raise_error
69
+ end
70
+
71
+ end
72
+
73
+ describe '#dump' do
74
+ it 'dumps' do
75
+ e=Expr[Var['hp'],1,Op['+'],2,Op['*']]
76
+ expect(e.dump).to eq 'hp 1 + 2 *'
77
+ end
78
+ end
79
+
80
+ describe '#to_s' do
81
+ it 'pretty prints for wml file output' do
82
+ pending('Do not think I need this feature')
83
+ e=Expr[Var['hp'],1,Op['+'],2,Op['*']]
84
+ expect(e.to_s).to eq '(hp+1)*2'
85
+ end
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,4 @@
1
+ [unit]
2
+ hp=`hp+3`
3
+ damage=`(damage-1)*2`
4
+ [/unit]