1 package org.simantics.modeling.ui.expression;
3 import java.util.Stack;
5 import org.simantics.basicexpression.analysis.DepthFirstAdapter;
6 import org.simantics.basicexpression.node.AConstantValue;
7 import org.simantics.basicexpression.node.ADivMultiplicative;
8 import org.simantics.basicexpression.node.AMultMultiplicative;
9 import org.simantics.basicexpression.node.APlusExpression;
10 import org.simantics.basicexpression.node.AStringValue;
11 import org.simantics.utils.datastructures.Triple;
13 public class InvertBasicExpressionVisitor extends DepthFirstAdapter {
15 Stack<Object> stack = new Stack<Object>();
17 public Object getResult() {
21 public void outAConstantValue(AConstantValue node) {
22 stack.push(Double.valueOf(node.toString()));
25 public void outAStringValue(AStringValue node) {
26 String value = node.toString();
27 stack.push(Triple.make(1.0, 0.0, value.substring(1, value.length() - 2).trim()));
30 @SuppressWarnings("unchecked")
31 public void outAPlusExpression(APlusExpression node) {
33 final Object o1 = stack.pop();
34 final Object o2 = stack.pop();
36 if(o1 instanceof Double && o2 instanceof Triple) {
37 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;
38 stack.push(Triple.make(p.first, p.second + (Double)o1, p.third));
39 } else if (o2 instanceof Double && o1 instanceof Triple) {
40 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;
41 stack.push(Triple.make(p.first, p.second + (Double)o2, p.third));
42 } else if (o2 instanceof Double && o1 instanceof Double) {
43 stack.push((Double)o1 + (Double)o2);
45 stack.push(Double.NaN);
50 @SuppressWarnings("unchecked")
51 public void outAMinusExpression(APlusExpression node) {
53 final Object o1 = stack.pop();
54 final Object o2 = stack.pop();
56 if(o1 instanceof Double && o2 instanceof Triple) {
57 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;
58 stack.push(Triple.make(-p.first, (Double)o1 - p.second, p.third ));
59 } else if (o2 instanceof Double && o1 instanceof Triple) {
60 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;
61 stack.push(Triple.make(p.first, p.second - (Double)o2, p.third));
62 } else if (o2 instanceof Double && o1 instanceof Double) {
63 stack.push((Double)o1 - (Double)o2);
65 stack.push(Double.NaN);
70 @SuppressWarnings("unchecked")
71 public void outAMultMultiplicative(AMultMultiplicative node) {
73 final Object o1 = stack.pop();
74 final Object o2 = stack.pop();
76 if(o1 instanceof Double && o2 instanceof Triple) {
77 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o2;
78 stack.push(Triple.make(p.first * (Double)o1, p.second * (Double)o1, p.third));
79 } else if (o2 instanceof Double && o1 instanceof Triple) {
80 Triple<Double, Double, String> p = (Triple<Double, Double, String>)o1;
81 stack.push(Triple.make(p.first * (Double)o2, p.second * (Double)o2, p.third));
82 } else if (o2 instanceof Double && o1 instanceof Double) {
83 stack.push((Double)o1 * (Double)o2);
85 stack.push(Double.NaN);
90 @SuppressWarnings("unchecked")
91 public void outADivMultiplicative(ADivMultiplicative node) {
93 final Object o1 = stack.pop();
94 final Object o2 = stack.pop();
96 if(o1 instanceof Double && o2 instanceof Triple) {
97 stack.push(Double.NaN);
98 } else if (o2 instanceof Double && o1 instanceof Triple) {
99 Triple<Double, Double, String> p = (Triple<Double,Double, String>)o1;
100 stack.push(Triple.make(p.first / (Double)o2, p.second / (Double)o2, p.third));
101 } else if (o2 instanceof Double && o1 instanceof Double) {
102 stack.push((Double)o1 / (Double)o2);
104 stack.push(Double.NaN);