delorean_lang 0.0.39 → 0.0.40

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,8 @@ module Delorean
26
26
 
27
27
  MAXLIST_SIG = [ 1, 1 ]
28
28
 
29
+ ######################################################################
30
+
29
31
  def MINLIST(_e, arg)
30
32
  raise "argument must be list" unless arg.is_a? Array
31
33
  arg.min
@@ -43,8 +45,19 @@ module Delorean
43
45
 
44
46
  ######################################################################
45
47
 
48
+ def ABS(_e, n)
49
+ raise "#{n} is not a number" unless
50
+ n.is_a?(Float) || n.is_a?(Fixnum) || n.is_a?(BigDecimal)
51
+ n.abs
52
+ end
53
+
54
+ ABS_SIG = [ 1, 1 ]
55
+
56
+ ######################################################################
57
+
46
58
  def NUMBER(_e, s)
47
- return s if s.is_a?(Float) || s.is_a?(Fixnum)
59
+ # FIXME: handle BigDecimal
60
+ return s if s.is_a?(Float) || s.is_a?(Fixnum) || s.is_a?(BigDecimal)
48
61
  raise "Can't convert #{s} to number" unless
49
62
  s =~ /^\d+(\.\d+)?$/
50
63
 
@@ -138,5 +151,45 @@ module Delorean
138
151
 
139
152
  ######################################################################
140
153
 
154
+ RUBY_METHODS = {
155
+ sort: [Array],
156
+ reverse: [Array],
157
+ min: [Array],
158
+ max: [Array],
159
+ uniq: [Array],
160
+ length: [[Array, String]],
161
+ flatten: [Array, [Fixnum, nil]],
162
+ }
163
+
164
+ def RUBY(_e, method, *args)
165
+ raise "method must be a string" unless method.class.name=="String"
166
+ msg = method.to_sym
167
+
168
+ raise "no such method #{method}" unless RUBY_METHODS.member? msg
169
+
170
+ sig = RUBY_METHODS[msg]
171
+ raise "too many args to #{method}" if args.length>sig.length
172
+
173
+ sig.each_with_index { |s, i|
174
+ s = [s] unless s.is_a?(Array)
175
+
176
+ ok = false
177
+ s.each { |sc|
178
+ if (sc.nil? && i>=args.length) || (sc && args[i].class <= sc)
179
+ ok = true
180
+ break
181
+ end
182
+ }
183
+
184
+ raise "bad argument #{args[i]} at position #{i} to method #{method}" unless ok
185
+ }
186
+
187
+ args[0].send(msg, *args[1, args.length])
188
+ end
189
+
190
+ RUBY_SIG = [ 1, Float::INFINITY ]
191
+
192
+ ######################################################################
193
+
141
194
  end
142
195
  end
@@ -351,7 +351,7 @@ eos
351
351
 
352
352
  class HashExpr < SNode
353
353
  def check(context, *)
354
- defined?(args) ? args.check(context) : {}
354
+ defined?(args) ? args.check(context) : []
355
355
  end
356
356
 
357
357
  def rewrite(context)
@@ -1,3 +1,3 @@
1
1
  module Delorean
2
- VERSION = "0.0.39"
2
+ VERSION = "0.0.40"
3
3
  end
data/spec/func_spec.rb CHANGED
@@ -79,6 +79,18 @@ describe "Delorean" do
79
79
  r.should == [12.3456, 12.3456, 12]
80
80
  end
81
81
 
82
+ it "should handle ABS" do
83
+ engine.parse defn("A:",
84
+ " a = ABS(-123)",
85
+ " b = ABS(-1.1)",
86
+ " c = ABS(2.3)",
87
+ " d = ABS(0)",
88
+ )
89
+
90
+ r = engine.evaluate_attrs("A", ["a", "b", "c", "d"])
91
+ r.should == [123, 1.1, 2.3, 0]
92
+ end
93
+
82
94
  it "should handle STRING" do
83
95
  engine.parse defn("A:",
84
96
  " a = STRING('hello')",
@@ -191,6 +203,28 @@ describe "Delorean" do
191
203
  lambda {
192
204
  r = engine.evaluate("A", "b")
193
205
  }.should raise_error("xx, 1, 2, 3")
206
+ end
207
+
208
+ it "should handle RUBY" do
209
+ x = [[1, 2, [-3]], 4, 5, [6], -3, 4, 5, 0]
194
210
 
211
+ engine.parse defn("A:",
212
+ " a = #{x}",
213
+ " b = RUBY('flatten', a)",
214
+ " c = RUBY('flatten', a, 1)",
215
+ " d = b+c",
216
+ " dd = RUBY('flatten', d)",
217
+ " e = RUBY('sort', dd)",
218
+ " f = RUBY('uniq', e)",
219
+ " g = RUBY('length', e)",
220
+ )
221
+
222
+ engine.evaluate("A", "c").should == x.flatten(1)
223
+ d = engine.evaluate("A", "d").should == x.flatten + x.flatten(1)
224
+ dd = engine.evaluate("A", "dd")
225
+ engine.evaluate("A", "e").should == dd.sort
226
+ engine.evaluate("A", "f").should == dd.sort.uniq
227
+ engine.evaluate("A", "g").should == dd.length
195
228
  end
229
+
196
230
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delorean_lang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.39
4
+ version: 0.0.40
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-13 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: treetop