trxl 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.1.10
@@ -313,6 +313,35 @@ module Trxl
313
313
 
314
314
  end
315
315
 
316
+ module Builtin
317
+
318
+ def self.avg(values)
319
+ strict = true
320
+ nr_of_vals = 0
321
+ strict_flag = values.first
322
+ if strict_flag.is_a?(TrueClass) || strict_flag.is_a?(FalseClass)
323
+ values.shift
324
+ strict = strict_flag
325
+ end
326
+
327
+ # if all values are nil return nil
328
+ return nil if values.compact.size == 0
329
+
330
+ s = values.inject(0) do |sum, next_val|
331
+ sum + if next_val.is_a?(Array)
332
+ next_val.flatten.inject(0) do |next_sum, val|
333
+ nr_of_vals += 1 if val && (strict || (!strict && val != 0))
334
+ next_sum + (val || 0)
335
+ end
336
+ else
337
+ nr_of_vals += 1 if next_val && (strict || (!strict && next_val != 0))
338
+ next_val || 0
339
+ end
340
+ end
341
+ (s != 0 && nr_of_vals != 0) ? s.to_f / nr_of_vals : 0
342
+ end
343
+ end
344
+
316
345
  module StdLib
317
346
 
318
347
  FOREACH_IN = <<-PROGRAM
@@ -4861,20 +4861,25 @@ module Trxl
4861
4861
  if r17
4862
4862
  r0 = r17
4863
4863
  else
4864
- r18 = _nt_is_empty_function
4864
+ r18 = _nt_compact_avg_function
4865
4865
  if r18
4866
4866
  r0 = r18
4867
4867
  else
4868
- r19 = _nt_matching_ids_function
4868
+ r19 = _nt_is_empty_function
4869
4869
  if r19
4870
4870
  r0 = r19
4871
4871
  else
4872
- r20 = _nt_values_of_type_function
4872
+ r20 = _nt_matching_ids_function
4873
4873
  if r20
4874
4874
  r0 = r20
4875
4875
  else
4876
- @index = i0
4877
- r0 = nil
4876
+ r21 = _nt_values_of_type_function
4877
+ if r21
4878
+ r0 = r21
4879
+ else
4880
+ @index = i0
4881
+ r0 = nil
4882
+ end
4878
4883
  end
4879
4884
  end
4880
4885
  end
@@ -4934,7 +4939,7 @@ module Trxl
4934
4939
  help << " SUM, MULT, AVG, PRINT, PRINT_LINE\n"
4935
4940
  help << " TO_INT, TO_FLOAT, TO_ARRAY, AVG_SUM\n"
4936
4941
  help << " MATCHING_IDS, VALUES_OF_TYPE, COMPACT\n"
4937
- help << " IS_EMPTY\n"
4942
+ help << " IS_EMPTY, COMPACT_AVG\n"
4938
4943
  help << "-----------------------------------------\n"
4939
4944
  help << "7) Standard library functions:\n"
4940
4945
  help << " foreach_in, inject, map, select\n"
@@ -7082,36 +7087,16 @@ module Trxl
7082
7087
 
7083
7088
  module AvgFunction2
7084
7089
  def eval(env = Environment.new)
7085
- strict = true
7086
- nr_of_vals = 0
7087
- values = expressions
7088
- strict_flag = values[0].eval(env)
7089
- if strict_flag.is_a?(TrueClass) || strict_flag.is_a?(FalseClass)
7090
- values.shift
7091
- strict = strict_flag
7092
- end
7093
-
7094
- # if all values are nil return nil
7095
- values = values.map { |v| v.eval(env) }
7096
- return nil if values.compact.size == 0
7097
-
7098
- s = values.inject(0) do |sum, next_val|
7099
- sum + if next_val.is_a?(Array)
7100
- next_val.flatten.inject(0) do |next_sum, val|
7101
- nr_of_vals += 1 if val && (strict || (!strict && val != 0))
7102
- next_sum + (val || 0)
7103
- end
7104
- else
7105
- nr_of_vals += 1 if next_val && (strict || (!strict && next_val != 0))
7106
- next_val || 0
7107
- end
7108
- end
7109
- (s != 0 && nr_of_vals != 0) ? s.to_f / nr_of_vals : 0
7090
+ Trxl::Builtin.avg(values(env))
7110
7091
  end
7111
7092
 
7112
7093
  def expressions
7113
7094
  [ expression ] + more_expressions.elements.map { |e| e.expression }
7114
7095
  end
7096
+
7097
+ def values(env)
7098
+ expressions.map { |e| e.eval(env) }
7099
+ end
7115
7100
  end
7116
7101
 
7117
7102
  module AvgFunction3
@@ -7297,6 +7282,179 @@ module Trxl
7297
7282
  r0
7298
7283
  end
7299
7284
 
7285
+ module CompactAvgFunction0
7286
+ def space1
7287
+ elements[1]
7288
+ end
7289
+
7290
+ def space2
7291
+ elements[3]
7292
+ end
7293
+
7294
+ def expression
7295
+ elements[4]
7296
+ end
7297
+
7298
+ def space3
7299
+ elements[5]
7300
+ end
7301
+
7302
+ end
7303
+
7304
+ module CompactAvgFunction1
7305
+ def eval(env = Environment.new)
7306
+ values = values(env)
7307
+ values.any? ? Trxl::Builtin.avg(values) : nil
7308
+ end
7309
+
7310
+ def values(env)
7311
+ expression.eval(env).compact
7312
+ end
7313
+ end
7314
+
7315
+ module CompactAvgFunction2
7316
+ def space1
7317
+ elements[1]
7318
+ end
7319
+
7320
+ def space2
7321
+ elements[3]
7322
+ end
7323
+
7324
+ end
7325
+
7326
+ module CompactAvgFunction3
7327
+ def eval(env = Environment.new)
7328
+ nil
7329
+ end
7330
+ end
7331
+
7332
+ def _nt_compact_avg_function
7333
+ start_index = index
7334
+ if node_cache[:compact_avg_function].has_key?(index)
7335
+ cached = node_cache[:compact_avg_function][index]
7336
+ if cached
7337
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
7338
+ @index = cached.interval.end
7339
+ end
7340
+ return cached
7341
+ end
7342
+
7343
+ i0 = index
7344
+ i1, s1 = index, []
7345
+ if has_terminal?('COMPACT_AVG', false, index)
7346
+ r2 = instantiate_node(SyntaxNode,input, index...(index + 11))
7347
+ @index += 11
7348
+ else
7349
+ terminal_parse_failure('COMPACT_AVG')
7350
+ r2 = nil
7351
+ end
7352
+ s1 << r2
7353
+ if r2
7354
+ r3 = _nt_space
7355
+ s1 << r3
7356
+ if r3
7357
+ if has_terminal?('(', false, index)
7358
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
7359
+ @index += 1
7360
+ else
7361
+ terminal_parse_failure('(')
7362
+ r4 = nil
7363
+ end
7364
+ s1 << r4
7365
+ if r4
7366
+ r5 = _nt_space
7367
+ s1 << r5
7368
+ if r5
7369
+ r6 = _nt_expression
7370
+ s1 << r6
7371
+ if r6
7372
+ r7 = _nt_space
7373
+ s1 << r7
7374
+ if r7
7375
+ if has_terminal?(')', false, index)
7376
+ r8 = instantiate_node(SyntaxNode,input, index...(index + 1))
7377
+ @index += 1
7378
+ else
7379
+ terminal_parse_failure(')')
7380
+ r8 = nil
7381
+ end
7382
+ s1 << r8
7383
+ end
7384
+ end
7385
+ end
7386
+ end
7387
+ end
7388
+ end
7389
+ if s1.last
7390
+ r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
7391
+ r1.extend(CompactAvgFunction0)
7392
+ r1.extend(CompactAvgFunction1)
7393
+ else
7394
+ @index = i1
7395
+ r1 = nil
7396
+ end
7397
+ if r1
7398
+ r0 = r1
7399
+ else
7400
+ i9, s9 = index, []
7401
+ if has_terminal?('COMPACT_AVG', false, index)
7402
+ r10 = instantiate_node(SyntaxNode,input, index...(index + 11))
7403
+ @index += 11
7404
+ else
7405
+ terminal_parse_failure('COMPACT_AVG')
7406
+ r10 = nil
7407
+ end
7408
+ s9 << r10
7409
+ if r10
7410
+ r11 = _nt_space
7411
+ s9 << r11
7412
+ if r11
7413
+ if has_terminal?('(', false, index)
7414
+ r12 = instantiate_node(SyntaxNode,input, index...(index + 1))
7415
+ @index += 1
7416
+ else
7417
+ terminal_parse_failure('(')
7418
+ r12 = nil
7419
+ end
7420
+ s9 << r12
7421
+ if r12
7422
+ r13 = _nt_space
7423
+ s9 << r13
7424
+ if r13
7425
+ if has_terminal?(')', false, index)
7426
+ r14 = instantiate_node(SyntaxNode,input, index...(index + 1))
7427
+ @index += 1
7428
+ else
7429
+ terminal_parse_failure(')')
7430
+ r14 = nil
7431
+ end
7432
+ s9 << r14
7433
+ end
7434
+ end
7435
+ end
7436
+ end
7437
+ if s9.last
7438
+ r9 = instantiate_node(SyntaxNode,input, i9...index, s9)
7439
+ r9.extend(CompactAvgFunction2)
7440
+ r9.extend(CompactAvgFunction3)
7441
+ else
7442
+ @index = i9
7443
+ r9 = nil
7444
+ end
7445
+ if r9
7446
+ r0 = r9
7447
+ else
7448
+ @index = i0
7449
+ r0 = nil
7450
+ end
7451
+ end
7452
+
7453
+ node_cache[:compact_avg_function][start_index] = r0
7454
+
7455
+ r0
7456
+ end
7457
+
7300
7458
  module AvgSumFunction0
7301
7459
  def space1
7302
7460
  elements[0]
@@ -827,6 +827,8 @@ grammar Trxl
827
827
  /
828
828
  compact_function
829
829
  /
830
+ compact_avg_function
831
+ /
830
832
  is_empty_function
831
833
  /
832
834
  matching_ids_function
@@ -868,7 +870,7 @@ grammar Trxl
868
870
  help << " SUM, MULT, AVG, PRINT, PRINT_LINE\n"
869
871
  help << " TO_INT, TO_FLOAT, TO_ARRAY, AVG_SUM\n"
870
872
  help << " MATCHING_IDS, VALUES_OF_TYPE, COMPACT\n"
871
- help << " IS_EMPTY\n"
873
+ help << " IS_EMPTY, COMPACT_AVG\n"
872
874
  help << "-----------------------------------------\n"
873
875
  help << "7) Standard library functions:\n"
874
876
  help << " foreach_in, inject, map, select\n"
@@ -1211,36 +1213,16 @@ grammar Trxl
1211
1213
  rule avg_function
1212
1214
  'AVG' space '(' space expression more_expressions:( space ',' space expression)* space ')' {
1213
1215
  def eval(env = Environment.new)
1214
- strict = true
1215
- nr_of_vals = 0
1216
- values = expressions
1217
- strict_flag = values[0].eval(env)
1218
- if strict_flag.is_a?(TrueClass) || strict_flag.is_a?(FalseClass)
1219
- values.shift
1220
- strict = strict_flag
1221
- end
1222
-
1223
- # if all values are nil return nil
1224
- values = values.map { |v| v.eval(env) }
1225
- return nil if values.compact.size == 0
1226
-
1227
- s = values.inject(0) do |sum, next_val|
1228
- sum + if next_val.is_a?(Array)
1229
- next_val.flatten.inject(0) do |next_sum, val|
1230
- nr_of_vals += 1 if val && (strict || (!strict && val != 0))
1231
- next_sum + (val || 0)
1232
- end
1233
- else
1234
- nr_of_vals += 1 if next_val && (strict || (!strict && next_val != 0))
1235
- next_val || 0
1236
- end
1237
- end
1238
- (s != 0 && nr_of_vals != 0) ? s.to_f / nr_of_vals : 0
1216
+ Trxl::Builtin.avg(values(env))
1239
1217
  end
1240
1218
 
1241
1219
  def expressions
1242
1220
  [ expression ] + more_expressions.elements.map { |e| e.expression }
1243
1221
  end
1222
+
1223
+ def values(env)
1224
+ expressions.map { |e| e.eval(env) }
1225
+ end
1244
1226
  }
1245
1227
  /
1246
1228
  'AVG' space '(' space ')' {
@@ -1250,6 +1232,25 @@ grammar Trxl
1250
1232
  }
1251
1233
  end
1252
1234
 
1235
+ rule compact_avg_function
1236
+ 'COMPACT_AVG' space '(' space expression space ')' {
1237
+ def eval(env = Environment.new)
1238
+ values = values(env)
1239
+ values.any? ? Trxl::Builtin.avg(values) : nil
1240
+ end
1241
+
1242
+ def values(env)
1243
+ expression.eval(env).compact
1244
+ end
1245
+ }
1246
+ /
1247
+ 'COMPACT_AVG' space '(' space ')' {
1248
+ def eval(env = Environment.new)
1249
+ nil
1250
+ end
1251
+ }
1252
+ end
1253
+
1253
1254
  rule avg_sum_function
1254
1255
  'AVG_SUM' space '(' space expression more_expressions:( space ',' space expression)* space ')' {
1255
1256
  def eval(env = Environment.new)
@@ -189,6 +189,17 @@ describe "The language" do
189
189
  eval(program).should == 4
190
190
  end
191
191
 
192
+ it "should provide COMPACT_AVG in order to compact an array before calculating its AVG value" do
193
+ program = "COMPACT_AVG([1,2,3])"
194
+ eval(program).should == 2
195
+ program = "COMPACT_AVG([1, NULL])"
196
+ eval(program).should == 1
197
+ program = "COMPACT_AVG([NULL])"
198
+ eval(program).should == nil
199
+ program = "COMPACT_AVG([NULL, NULL])"
200
+ eval(program).should == nil
201
+ end
202
+
192
203
  it "should be able to calculate AVG_SUM for an arbitrary number of argument expressions" do
193
204
  program = "AVG_SUM([1,2,3],[4,4,4],[6,6,6])"
194
205
  eval(program).should == 12
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{trxl}
8
- s.version = "0.1.9"
8
+ s.version = "0.1.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Martin Gamsjaeger (snusnu)}, %q{Michael Aufreiter}]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trxl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 9
10
- version: 0.1.9
9
+ - 10
10
+ version: 0.1.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Martin Gamsjaeger (snusnu)