/*
 * $Id$
 *
 * JSDAI(TM), a way to implement STEP, ISO 10303
 * Copyright (C) 1997-2008, LKSoftWare GmbH, Germany
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License
 * version 3 as published by the Free Software Foundation (AGPL v3).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * JSDAI is a registered trademark of LKSoftWare GmbH, Germany
 * This software is also available under commercial licenses.
 * See also http://www.jsdai.net/
 */

// %modified: 1016210367423 %

/* Generated By:JJTree: Do not edit this line. X_CaseStmt.java */
package jsdai.expressCompiler;

import java.util.*;
import jsdai.lang.*;


public class X_CaseStmt extends StatementNode {
  // public class X_CaseStmt extends SimpleNode {
  boolean otherwise_present;
  String selector;
  Vector actions;
  String otherwise;
  int number_of_actions;
  String tmpstr;
  String tmpstr2;
  String if_str;
  boolean first_time;
  Vector statements;

  public X_CaseStmt(int id) {
    super(id);
  }

  public X_CaseStmt(Compiler2 p, int id) {
    super(p, id);
  }

  /** Accept the visitor. **/
  public Object jjtAccept(Compiler2Visitor visitor, Object data)
                   throws SdaiException {
    return visitor.visit(this, data);
  }

  public Object childrenAccept(Compiler2Visitor visitor, Object data)
                        throws SdaiException {
    JavaClass jc = (JavaClass) data;
    boolean first_time_if = true;
    boolean first_time_else = true;

    if (children != null) {
      // jc.java_str4 = "";
      selector = "";
      actions = new Vector();
      statements = new Vector();
      otherwise = "";
      number_of_actions = 0;

      for (int i = 0; i < children.length; ++i) {
        children[i].jjtAccept(visitor, data);

        if (jc != null) {
          if (jc.active) {
            //      System.out.println("<><> if count: " + if_count + ", else count: " + else_count);
            if (children[i] instanceof X_CaseSelector) {
              selector = jc.generated_java;
              jc.generated_java = "";
            } else if (children[i] instanceof X_CaseAction) {
              // here we may make the essential part of IF with || connected multiple parts
              tmpstr = "";
              tmpstr2 = "";

              if (((X_CaseAction) children[i]).number_of_labels == 1) {
                tmpstr2 = (String) ((X_CaseAction) children[i]).labels.elementAt(0);
                tmpstr = "Value.alloc(ExpressTypes.LOGICAL_TYPE).equal(_context, " + selector + "," + 
                         tmpstr2 + ").getLogical() == 2";
                tmpstr2 = "";
              } else if (((X_CaseAction) children[i]).number_of_labels > 1) {
                first_time = true;

                for (int j = 0; j < ((X_CaseAction) children[i]).number_of_labels; j++) {
                  tmpstr2 = (String) ((X_CaseAction) children[i]).labels.elementAt(j);

                  if (first_time) {
                    tmpstr += ("(Value.alloc(ExpressTypes.LOGICAL_TYPE).equal(_context, " + selector + "," + tmpstr2 + ").getLogical() == 2)");
                    first_time = false;
                  } else {
                    tmpstr += (" || (Value.alloc(ExpressTypes.LOGICAL_TYPE).equal(_context, " + selector + "," + tmpstr2 + ").getLogical() == 2)");
                  }

                  tmpstr2 = "";
                }
              } else {
                // an error  - no label at all
              }


              //                     actions.addElement(jc.print_string);
              //                     actions.addElement(jc.generated_java);
              actions.addElement("if (" + tmpstr + ") {");
              statements.addElement(((X_CaseAction) children[i]).statement);
              tmpstr = "";
              jc.print_string = "";
              number_of_actions++;
            } else if (children[i] instanceof X_CaseOtherwise) {
              otherwise = ((SimpleNode) children[i]).forwarded_java;
              otherwise += ((SimpleNode) children[i]).generated_java;

              ((SimpleNode) children[i]).forwarded_java = "";
              ((SimpleNode) children[i]).generated_java = "";
              otherwise_present = true;
              jc.print_string = "";
            } else {
              // internal error, I think
            }
          }
        }
      }
    }

    return data;
  }
}
