1 package org.simantics.modeling.ui.expression;
\r
3 import java.util.Stack;
\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
13 public class InvertBasicExpressionVisitor extends DepthFirstAdapter {
\r
15 Stack<Object> stack = new Stack<Object>();
\r
17 public Object getResult() {
\r
21 public void outAConstantValue(AConstantValue node) {
\r
22 stack.push(Double.valueOf(node.toString()));
\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
30 @SuppressWarnings("unchecked")
\r
31 public void outAPlusExpression(APlusExpression node) {
\r
33 final Object o1 = stack.pop();
\r
34 final Object o2 = stack.pop();
\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
45 stack.push(Double.NaN);
\r
50 @SuppressWarnings("unchecked")
\r
51 public void outAMinusExpression(APlusExpression node) {
\r
53 final Object o1 = stack.pop();
\r
54 final Object o2 = stack.pop();
\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
65 stack.push(Double.NaN);
\r
70 @SuppressWarnings("unchecked")
\r
71 public void outAMultMultiplicative(AMultMultiplicative node) {
\r
73 final Object o1 = stack.pop();
\r
74 final Object o2 = stack.pop();
\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
85 stack.push(Double.NaN);
\r
90 @SuppressWarnings("unchecked")
\r
91 public void outADivMultiplicative(ADivMultiplicative node) {
\r
93 final Object o1 = stack.pop();
\r
94 final Object o2 = stack.pop();
\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
104 stack.push(Double.NaN);
\r