OSDN Git Service

add inverse flag to show expression
authorTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 26 Feb 2010 02:47:07 +0000 (11:47 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 26 Feb 2010 02:47:07 +0000 (11:47 +0900)
lib/mint/builder/expression_tree.rb

index 14b6dd6..bf7a29d 100644 (file)
@@ -10,17 +10,17 @@ module Mint
       @right    = right
       @operator = :unknown
     end
-    def to_s
-      show to_s_local
+    def to_s(inverse = false)
+      show to_s_local(inverse)
     end
-    def to_latex
-      show to_latex_local
+    def to_latex(inverse = false)
+      show to_latex_local(inverse)
     end
     private
-    def to_s_local
+    def to_s_local(inverse)
       "#{left} #{operator} #{right}"
     end
-    def to_latex_local
+    def to_latex_local(inverse)
       "#{left.to_latex} #{operator} #{right.to_latex}"
     end
     def show(string)
@@ -42,6 +42,10 @@ module Mint
       super
       @operator = :-
     end
+    def to_s_local(inverse)
+      _right = right.__send__(:to_s, (right.minus ? true : false))
+      "#{left} #{operator} #{_right}"
+    end
   end
 
   class MultipleNode < ExpressionNode
@@ -49,7 +53,7 @@ module Mint
       super
       @operator = :*
     end
-    def to_latex_local
+    def to_latex_local(inverse)
       # FIXME: too dirty
       if right.instance_of?(LiteralNode) ||
         right.instance_of?(RootNode)
@@ -66,7 +70,7 @@ module Mint
       super
       @operator = :div
     end
-    def to_latex_local
+    def to_latex_local(inverse)
       "#{left.to_latex} \\div #{right.to_latex}"
     end
   end
@@ -76,7 +80,7 @@ module Mint
       super
       @operator = :/
     end
-    def to_latex
+    def to_latex_local(inverse)
       "\\frac{#{left.to_latex}}{#{right.to_latex}}"
     end
   end
@@ -86,17 +90,17 @@ module Mint
     def initialize(value)
       @value = value
     end
-    def to_s
-      show to_s_local
+    def to_s(inverse = false)
+      show to_s_local(inverse)
     end
-    def to_latex
-      show to_latex_local
+    def to_latex(inverse = false)
+      show to_latex_local(inverse)
     end
     private
-    def to_s_local
+    def to_s_local(inverse)
       @value.to_s
     end
-    def to_latex_local
+    def to_latex_local(inverse)
       @value.to_s
     end
     def show(string)
@@ -112,7 +116,7 @@ module Mint
     def initialize(value)
       @value = value
     end
-    def to_s_local
+    def to_s_local(inverse)
       round_n(value.to_f, 3).to_s
     end
 
@@ -129,22 +133,22 @@ module Mint
       super value
       @power = power
     end
-    def to_s
+    def to_s_local(inverse)
       "#{value}^#{power}"
     end
-    def to_latex_local
+    def to_latex_local(inverse)
       return to_s if value.instance_of?(String)
-      "#{value.to_latex}^#{power}"
+      "#{value.to_latex}^#{power.to_latex}"
     end
   end
 
   class RootNode < LiteralNode
     attr_accessor :minus
     attr_reader :value
-    def to_s_local
+    def to_s_local(inverse)
       "sqrt(#{value})"
     end
-    def to_latex_local
+    def to_latex_local(inverse)
       "\\sqrt{#{value}}"
     end
   end