BOAST 0.5 → 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/BOAST.gemspec +1 -1
- data/lib/BOAST/Algorithm.rb +31 -25
- metadata +1 -1
data/BOAST.gemspec
CHANGED
data/lib/BOAST/Algorithm.rb
CHANGED
@@ -250,19 +250,39 @@ module BOAST
|
|
250
250
|
return Expression::new("-",nil,self)
|
251
251
|
end
|
252
252
|
|
253
|
+
def Expression.to_str_base(op1, op2, oper)
|
254
|
+
s = ""
|
255
|
+
if op1 then
|
256
|
+
s += "(" if (oper == "*" or oper == "/")
|
257
|
+
s += op1.to_s
|
258
|
+
s += ")" if (oper == "*" or oper == "/")
|
259
|
+
end
|
260
|
+
s += " " unless oper == "++" or oper == "."
|
261
|
+
s += oper
|
262
|
+
s += " " unless oper == "." or oper == "&" or ( oper == "*" and op1.nil? )
|
263
|
+
if op2 then
|
264
|
+
s += "(" if (oper == "*" or oper == "/" or oper == "-")
|
265
|
+
s += op2.to_s
|
266
|
+
s += ")" if (oper == "*" or oper == "/" or oper == "-")
|
267
|
+
end
|
268
|
+
return s
|
269
|
+
end
|
270
|
+
|
253
271
|
def to_var
|
254
272
|
op1 = nil
|
255
|
-
op1 = @operand1.to_var if @operand1
|
273
|
+
op1 = @operand1.to_var if @operand1.respond_to?(:to_var)
|
256
274
|
op2 = nil
|
257
|
-
op2 = @operand2.to_var if @operand2
|
258
|
-
res_exp = Expression::new(@operator, op1.nil? ? @operand1 : op1, op2.nil? ? @operand2 : op2)
|
275
|
+
op2 = @operand2.to_var if @operand2.respond_to?(:to_var)
|
259
276
|
if op1 and op2 then
|
260
277
|
r_t, oper = BOAST::transition(op1, op2, @operator)
|
261
|
-
|
262
|
-
|
263
|
-
return op1.copy("#{res_exp}", :const => nil, :constant => nil)
|
278
|
+
res_exp = BOAST::Expression::to_str_base(op1, op2, oper)
|
279
|
+
return r_t.copy(res_exp, :const => nil, :constant => nil)
|
264
280
|
elsif op2
|
265
|
-
|
281
|
+
res_exp = BOAST::Expression::to_str_base(@operand1, op2, @operator)
|
282
|
+
return op2.copy(res_exp, :const => nil, :constant => nil)
|
283
|
+
elsif op1
|
284
|
+
res_exp = BOAST::Expression::to_str_base(op1, @operand2, @operator)
|
285
|
+
return op1.copy(res_exp, :const => nil, :constant => nil)
|
266
286
|
else
|
267
287
|
STDERR.puts "#{@operand1} #{@operand2}"
|
268
288
|
raise "Expression on no operand!"
|
@@ -271,9 +291,9 @@ module BOAST
|
|
271
291
|
|
272
292
|
def to_str
|
273
293
|
op1 = nil
|
274
|
-
op1 = @operand1.to_var if @operand1
|
294
|
+
op1 = @operand1.to_var if @operand1.respond_to?(:to_var)
|
275
295
|
op2 = nil
|
276
|
-
op2 = @operand2.to_var if @operand2
|
296
|
+
op2 = @operand2.to_var if @operand2.respond_to?(:to_var)
|
277
297
|
if op1 and op2 then
|
278
298
|
r_t, oper = BOAST::transition(op1, op2, @operator)
|
279
299
|
else
|
@@ -282,22 +302,8 @@ module BOAST
|
|
282
302
|
|
283
303
|
op1 = @operand1 if not op1
|
284
304
|
op2 = @operand2 if not op2
|
285
|
-
|
286
|
-
|
287
|
-
if op1 then
|
288
|
-
s += "(" if (oper == "*" or oper == "/")
|
289
|
-
s += op1.to_s
|
290
|
-
s += ")" if (oper == "*" or oper == "/")
|
291
|
-
end
|
292
|
-
s += " " unless oper == "++" or oper == "."
|
293
|
-
s += oper
|
294
|
-
s += " " unless oper == "." or oper == "&" or ( oper == "*" and op1.nil? )
|
295
|
-
if op2 then
|
296
|
-
s += "(" if (oper == "*" or oper == "/" or oper == "-")
|
297
|
-
s += op2.to_s
|
298
|
-
s += ")" if (oper == "*" or oper == "/" or oper == "-")
|
299
|
-
end
|
300
|
-
return s
|
305
|
+
|
306
|
+
return BOAST::Expression::to_str_base(op1, op2, oper)
|
301
307
|
end
|
302
308
|
|
303
309
|
def print(final=true)
|