]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/expression/InvertBasicExpressionVisitor.java
Allow client-logic in component type interface property name editing
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / expression / InvertBasicExpressionVisitor.java
1 package org.simantics.modeling.ui.expression;
2
3 import java.util.Stack;
4
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;
12
13 public class InvertBasicExpressionVisitor extends DepthFirstAdapter {
14
15         Stack<Object> stack = new Stack<Object>();
16
17         public Object getResult() {
18                 return stack.pop();
19         }
20
21         public void outAConstantValue(AConstantValue node) {
22                 stack.push(Double.valueOf(node.toString()));
23         }
24
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()));
28         }
29
30         @SuppressWarnings("unchecked")
31     public void outAPlusExpression(APlusExpression node) {
32                 
33                 final Object o1 = stack.pop();
34                 final Object o2 = stack.pop();
35                 
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);
44                 } else {
45                         stack.push(Double.NaN);
46                 }
47                 
48         }
49
50         @SuppressWarnings("unchecked")
51     public void outAMinusExpression(APlusExpression node) {
52                 
53                 final Object o1 = stack.pop();
54                 final Object o2 = stack.pop();
55                 
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);
64                 } else {
65                         stack.push(Double.NaN);
66                 }
67                 
68         }
69
70         @SuppressWarnings("unchecked")
71     public void outAMultMultiplicative(AMultMultiplicative node) {
72                 
73                 final Object o1 = stack.pop();
74                 final Object o2 = stack.pop();
75                 
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);
84                 } else {
85                         stack.push(Double.NaN);
86                 }
87                 
88         }
89
90         @SuppressWarnings("unchecked")
91     public void outADivMultiplicative(ADivMultiplicative node) {
92                 
93                 final Object o1 = stack.pop();
94                 final Object o2 = stack.pop();
95                 
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);
103                 } else {
104                         stack.push(Double.NaN);
105                 }
106                 
107         }
108         
109 }