lib/qlang/iq.rb in qlang-0.0.14142135 vs lib/qlang/iq.rb in qlang-0.0.27000000
- old
+ new
@@ -5,13 +5,26 @@
def to_q
str = to_s.gsub(/\*\*/, '^').rm(' * ')
str.equalize!
end
end
- def execute(code)
- ruby_obj = eval Q.to_ruby.compile(code)
- output = case ruby_obj
+
+ def self.execute(code)
+ ruby_code = Q.to_ruby.compile(code)
+ ruby_obj = eval(ruby_code)
+
+ optimize_output(ruby_obj)
+ rescue SyntaxError
+ # TODO: emergency
+ case ruby_code
+ when /(\d)+(\w)/
+ execute("#{$1} * #{$2}")
+ end
+ end
+
+ def self.optimize_output(ruby_obj)
+ case ruby_obj
when Matrix, Vector, Dydx::Algebra::Formula
ruby_obj.to_q
when Float::INFINITY
'oo'
when - Float::INFINITY
@@ -19,8 +32,8 @@
else
str = ruby_obj.to_s
str.equalize!
end
end
- module_function :execute
+
end
end