]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/expression/InvertBasicExpressionVisitor.java
Merge "InputStream returns -1 on EOF instead of throwing IOException"
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / expression / InvertBasicExpressionVisitor.java
1 package org.simantics.modeling.ui.expression;\r
2 \r
3 import java.util.Stack;\r
4 \r
5 import org.simantics.basicexpression.analysis.DepthFirstAdapter;\r
6 import org.simantics.basicexpression.node.AConstantValue;\r
7 import org.simantics.basicexpression.node.ADivMultiplicative;\r
8 import org.simantics.basicexpression.node.AMultMultiplicative;\r
9 import org.simantics.basicexpression.node.APlusExpression;\r
10 import org.simantics.basicexpression.node.AStringValue;\r
11 import org.simantics.utils.datastructures.Triple;\r
12 \r
13 public class InvertBasicExpressionVisitor extends DepthFirstAdapter {\r
14 \r
15         Stack<Object> stack = new Stack<Object>();\r
16 \r
17         public Object getResult() {\r
18                 return stack.pop();\r
19         }\r
20 \r
21         public void outAConstantValue(AConstantValue node) {\r
22                 stack.push(Double.valueOf(node.toString()));\r
23         }\r
24 \r
25         public void outAStringValue(AStringValue node) {\r
26                 String value = node.toString();\r
27                 stack.push(Triple.make(1.0, 0.0, value.substring(1, value.length() - 2).trim()));\r
28         }\r
29 \r
30         @SuppressWarnings("unchecked")\r
31     public void outAPlusExpression(APlusExpression node) {\r
32                 \r
33                 final Object o1 = stack.pop();\r
34                 final Object o2 = stack.pop();\r
35                 \r
36                 if(o1 instanceof Double && o2 instanceof Triple) {\r
37                         Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;\r
38                         stack.push(Triple.make(p.first, p.second + (Double)o1, p.third));\r
39                 } else if (o2 instanceof Double && o1 instanceof Triple) {\r
40                         Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;\r
41                         stack.push(Triple.make(p.first, p.second + (Double)o2, p.third));\r
42                 } else if (o2 instanceof Double && o1 instanceof Double) {\r
43                         stack.push((Double)o1 + (Double)o2);\r
44                 } else {\r
45                         stack.push(Double.NaN);\r
46                 }\r
47                 \r
48         }\r
49 \r
50         @SuppressWarnings("unchecked")\r
51     public void outAMinusExpression(APlusExpression node) {\r
52                 \r
53                 final Object o1 = stack.pop();\r
54                 final Object o2 = stack.pop();\r
55                 \r
56                 if(o1 instanceof Double && o2 instanceof Triple) {\r
57                         Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;\r
58                         stack.push(Triple.make(-p.first, (Double)o1 - p.second, p.third ));\r
59                 } else if (o2 instanceof Double && o1 instanceof Triple) {\r
60                         Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;\r
61                         stack.push(Triple.make(p.first, p.second - (Double)o2, p.third));\r
62                 } else if (o2 instanceof Double && o1 instanceof Double) {\r
63                         stack.push((Double)o1 - (Double)o2);\r
64                 } else {\r
65                         stack.push(Double.NaN);\r
66                 }\r
67                 \r
68         }\r
69 \r
70         @SuppressWarnings("unchecked")\r
71     public void outAMultMultiplicative(AMultMultiplicative node) {\r
72                 \r
73                 final Object o1 = stack.pop();\r
74                 final Object o2 = stack.pop();\r
75                 \r
76                 if(o1 instanceof Double && o2 instanceof Triple) {\r
77                         Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;\r
78                         stack.push(Triple.make(p.first * (Double)o1, p.second * (Double)o1, p.third));\r
79                 } else if (o2 instanceof Double && o1 instanceof Triple) {\r
80                         Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;\r
81                         stack.push(Triple.make(p.first * (Double)o2, p.second * (Double)o2, p.third));\r
82                 } else if (o2 instanceof Double && o1 instanceof Double) {\r
83                         stack.push((Double)o1 * (Double)o2);\r
84                 } else {\r
85                         stack.push(Double.NaN);\r
86                 }\r
87                 \r
88         }\r
89 \r
90         @SuppressWarnings("unchecked")\r
91     public void outADivMultiplicative(ADivMultiplicative node) {\r
92                 \r
93                 final Object o1 = stack.pop();\r
94                 final Object o2 = stack.pop();\r
95                 \r
96                 if(o1 instanceof Double && o2 instanceof Triple) {\r
97                         stack.push(Double.NaN);\r
98                 } else if (o2 instanceof Double && o1 instanceof Triple) {\r
99                         Triple<Double, Double, String> p = (Triple<Double,Double, String>)o1;\r
100                         stack.push(Triple.make(p.first / (Double)o2, p.second / (Double)o2, p.third));\r
101                 } else if (o2 instanceof Double && o1 instanceof Double) {\r
102                         stack.push((Double)o1 / (Double)o2);\r
103                 } else {\r
104                         stack.push(Double.NaN);\r
105                 }\r
106                 \r
107         }\r
108         \r
109 }\r