summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/AoI/plugins/CSGEvaluator/src/org/cheffo/jeplite/optimizer/ExpressionOptimizer.java
blob: 1c8f777c800a920b8562599d780d2ab598863365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package org.cheffo.jeplite.optimizer;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */
import org.cheffo.jeplite.*;
import org.cheffo.jeplite.util.*;
import org.cheffo.jeplite.function.*;

import java.util.*;
public class ExpressionOptimizer implements ParserVisitor {

  SimpleNode node;
  final HashMap constTab = new HashMap();
  public ExpressionOptimizer(SimpleNode node) {
    this.node = node;
  }

  /**
   * Marks a variable name to be a constant name.
   */
  public void addConst(String constName) {
    constTab.put(constName, constName);
  }

  /**
   * Unmarks a variable name to be a constant name.
   */
  public void removeConst(String constName) {
    constTab.remove(constName);
  }

  public void clearConstants() {
    constTab.clear();
  }

  public SimpleNode optimize() {
    return (SimpleNode)node.jjtAccept(this, null);
  }

  public Object visit(ASTFunNode node, Object data) {
    SimpleNode res = node;
    try{
      boolean allConstNode = true;
      int numChildren = node.jjtGetNumChildren();
      SimpleNode[] nodes = new SimpleNode[numChildren];
      for(int i=0; i<numChildren; i++) {
        nodes[i] = (SimpleNode)node.jjtGetChild(i).jjtAccept(this, data);
        allConstNode &= (nodes[i] instanceof ASTConstant);
        // well, let wind the whole loop
      }

      if(res.getName().equals("+")||res.getName().equals("*"))
	      res = (ASTFunNode)visitAdditive(node, data);

      if(allConstNode) {
        ASTConstant constRes = new ASTConstant(-1);
        constRes.jjtSetParent(node.jjtGetParent());
        constRes.setValue(node.getValue());
        res = constRes;
      }

    } catch(Exception ex) {ex.printStackTrace();}
    return res;
  }

  /**
   * Now we are sure that we had visited/preevaluated all possible children.
   */
  private Object visitAdditive(ASTFunNode node, Object data) {
    ArrayList nodes = new ArrayList();
    int numChildren = node.jjtGetNumChildren();
    String nodeName = node.getName();
    boolean toOptimize = false;
    for(int i=0; i<numChildren; i++) {
      SimpleNode curChild = node.jjtGetChild(i);
      String curChildName = curChild.getName();
      if(curChildName!=null&&curChildName.equals(nodeName)) {
	toOptimize = true;
	int grandChildrenNum = curChild.jjtGetNumChildren();
	for(int j=0; j<grandChildrenNum; j++)
	  nodes.add(curChild.jjtGetChild(j));
      } else {
	nodes.add(curChild);
      }
    }
    ASTFunNode res = node;
    if(toOptimize) {
      res = new ASTFunNode(ParserTreeConstants.JJTFUNNODE);
      res.setName(node.getName());
      res.jjtSetParent(node.jjtGetParent());
      int pos = nodes.size()-1;
      for(Iterator i=nodes.iterator(); i.hasNext();)
	node.jjtAddChild((SimpleNode)i.next(), pos--);
      if(nodeName.equals("+"))
        res.setFunction("+", new Madd(nodes.size()));
      else
        res.setFunction("*", new Mmul(nodes.size()));
    }
    return res;
  }

  /**
   * If a var node is defined in the const table, make it to be a real constant.
   */
  public Object visit(ASTVarNode node, Object data) {
    boolean isConst = (constTab.get(node.getName())!=null);
    SimpleNode res = node;
    if(isConst) {
      try {
	ASTConstant res1 = new ASTConstant(ParserTreeConstants.JJTCONSTANT);
	res1.setValue(res.getValue());
	res1.jjtSetParent(res.jjtGetParent());
	res = res1;
      } catch(ParseException ex) {ex.printStackTrace();}
    }
    return res;
  }

  public Object visit(ASTConstant node, Object data) {
    return node;
  }

  public Object visit(SimpleNode node, Object data) {
    return node;
  }

  static class Madd extends PostfixMathCommand {
    Madd(int numberOfParameters) {
      this.numberOfParameters = numberOfParameters;
    }

    public double operation(double[] params) {return 0;};

    public void run(DoubleStack stack) {
      double res = 0;
      for(int i=0; i<numberOfParameters; i++)
	res += stack.pop();
      stack.push(res);
    }
  }

  static class Mmul extends PostfixMathCommand {
    Mmul(int numberOfParameters) {
      this.numberOfParameters = numberOfParameters;
    }

    public double operation(double[] params) {return 0;};

    public void run(DoubleStack stack) {
      double res = 1;
      for(int i=0; i<numberOfParameters; i++)
	res *= stack.pop();
      stack.push(res);
    }
  }
}