/* Generated By:JavaCC: Do not edit this line. EC.java */
package gnu.jel.generated;

import gnu.jel.Optimizer;
import gnu.jel.Library;
import gnu.jel.ExpressionImage;
import gnu.jel.CompilationException;
import java.io.StringReader;
import gnu.jel.debug.Debug;

public class EC implements ECConstants {

  final public void expression(Optimizer optimizer) throws ParseException, CompilationException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case MINUS:
    case INTEGER_LITERAL:
    case FLOATING_POINT_LITERAL:
    case CHARACTER_LITERAL:
    case ID:
    case 20:
      logical(optimizer);
      break;
    default:
      jj_la1[0] = jj_gen;
      ;
    }
    jj_consume_token(0);
      optimizer.finish();
  }

  final public void logical(Optimizer optimizer) throws ParseException, CompilationException {
 Token x;
    sum(optimizer);
    label_1:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case AND:
      case OR:
      case XOR:
        ;
        break;
      default:
        jj_la1[1] = jj_gen;
        break label_1;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case AND:
        x = jj_consume_token(AND);
        break;
      case OR:
        x = jj_consume_token(OR);
        break;
      case XOR:
        x = jj_consume_token(XOR);
        break;
      default:
        jj_la1[2] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
                                     optimizer.binaryOP_param();
      sum(optimizer);
       try {
         int op=999;
         switch (x.kind) {
         case AND: op=ExpressionImage.BI_AN; break;
         case OR:  op=ExpressionImage.BI_OR; break;
         case XOR: op=ExpressionImage.BI_XO; break;
         default:
       };
         optimizer.binaryOP(op);
       } catch (IllegalStateException e) {
         {if (true) throw new CompilationException(x,e.getMessage());}
       };
    }
  }

  final public void sum(Optimizer optimizer) throws ParseException, CompilationException {
 Token x;
    term(optimizer);
    label_2:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case PLUS:
      case MINUS:
        ;
        break;
      default:
        jj_la1[3] = jj_gen;
        break label_2;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case PLUS:
        x = jj_consume_token(PLUS);
        break;
      case MINUS:
        x = jj_consume_token(MINUS);
        break;
      default:
        jj_la1[4] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
                             optimizer.binaryOP_param();
      term(optimizer);
       try {
         int op=ExpressionImage.BI_MI;
         if (x.kind==PLUS) op=ExpressionImage.BI_PL;
         optimizer.binaryOP(op);
       } catch (IllegalStateException e) {
         {if (true) throw new CompilationException(x,e.getMessage());}
       };
    }
  }

  final public void term(Optimizer optimizer) throws ParseException, CompilationException {
 Token x;
    unary(optimizer);
    label_3:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case MULTIPLY:
      case DIVIDE:
        ;
        break;
      default:
        jj_la1[5] = jj_gen;
        break label_3;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case MULTIPLY:
        x = jj_consume_token(MULTIPLY);
        break;
      case DIVIDE:
        x = jj_consume_token(DIVIDE);
        break;
      default:
        jj_la1[6] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
                                optimizer.binaryOP_param();
      unary(optimizer);
      try {
        int op=ExpressionImage.BI_MU;
        if (x.kind==DIVIDE) op=ExpressionImage.BI_DI;
        optimizer.binaryOP(op);
      } catch (IllegalStateException e) {
        {if (true) throw new CompilationException(x,e.getMessage());}
      };
    }
  }

  final public void unary(Optimizer optimizer) throws ParseException, CompilationException {
 Token x;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case MINUS:
      x = jj_consume_token(MINUS);
      element(optimizer);
      try {
        optimizer.unary(ExpressionImage.UN_NE);
      } catch (IllegalStateException e) {
        {if (true) throw new CompilationException(x,e.getMessage());}
      };
      break;
    case INTEGER_LITERAL:
    case FLOATING_POINT_LITERAL:
    case CHARACTER_LITERAL:
    case ID:
    case 20:
      element(optimizer);
      break;
    default:
      jj_la1[7] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  }

  final public void element(Optimizer optimizer) throws ParseException, CompilationException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER_LITERAL:
    case FLOATING_POINT_LITERAL:
    case CHARACTER_LITERAL:
      literal(optimizer);
      break;
    case ID:
      function(optimizer);
      break;
    case 20:
      jj_consume_token(20);
      logical(optimizer);
      jj_consume_token(21);
      break;
    default:
      jj_la1[8] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  }

  final public void function(Optimizer optimizer) throws ParseException, CompilationException {
  Token id;
    id = jj_consume_token(ID);
             optimizer.function_start();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 20:
      jj_consume_token(20);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case MINUS:
      case INTEGER_LITERAL:
      case FLOATING_POINT_LITERAL:
      case CHARACTER_LITERAL:
      case ID:
      case 20:
        logical(optimizer);
                          optimizer.function_param();
        label_4:
        while (true) {
          switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
          case 22:
            ;
            break;
          default:
            jj_la1[9] = jj_gen;
            break label_4;
          }
          jj_consume_token(22);
          logical(optimizer);
                                       optimizer.function_param();
        }
        break;
      default:
        jj_la1[10] = jj_gen;
        ;
      }
      jj_consume_token(21);
      break;
    default:
      jj_la1[11] = jj_gen;
      ;
    }
       try {
         optimizer.function_call(id.image);
       } catch (IllegalStateException e) {
         {if (true) throw new CompilationException(id,e.getMessage());}
       };
  }

  final public void literal(Optimizer optimizer) throws ParseException, CompilationException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case INTEGER_LITERAL:
      jj_consume_token(INTEGER_LITERAL);
                     {
    String svalue=token.image.toUpperCase();
    long value=0;
    boolean makelong=svalue.endsWith("L");
    if (makelong) svalue=svalue.substring(0,svalue.length()-1);

    try {
      if ( svalue.startsWith("0x") || svalue.startsWith("0X") ) {
        // Hexadecimal number
        svalue=svalue.substring(2);
        value=Long.parseLong(svalue,16);
      } else if (token.image.startsWith("0")) {
        // Octal number
        value=Long.parseLong(svalue,8);
      } else {
        // Decimal number
        value=Long.parseLong(svalue,10);
      };
    } catch (NumberFormatException e) {
      {if (true) throw new CompilationException(token,"Number \""+token.image+
                                     "\" is too large, it does not fit even "+
                                     "into 64 bit long.");} // Overflow ?
    };

    if (!makelong) { // Check ranges
      if (value<=127) optimizer.load((byte)value);
      else if (value<=32767) optimizer.load((short)value);
      else if (value<=2147483647) optimizer.load((int)value);
      else
        {if (true) throw
          new CompilationException(token,"Integer number \""+token.image+
                                   "\" is too large for type 'int'. Be sure"+
                                   " to add 'L' suffix to use 'long' type.");}
    } else {
      optimizer.load(value);
    };

  }
      break;
    case FLOATING_POINT_LITERAL:
      jj_consume_token(FLOATING_POINT_LITERAL);
                            {
  char lc=Character.toUpperCase(token.image.charAt(token.image.length()-1));
  boolean makeFloat = (lc=='F');
  String svalue=token.image;
  if ((lc=='D') || (lc=='F')) {
    svalue=svalue.substring(0,svalue.length()-1);
  };
  Double value=null;
  try {
    value=new Double(svalue);
  } catch (NumberFormatException e) {
    // Overflow ? Will produce infinity.
  };
  if (makeFloat)
    optimizer.load(value.floatValue());
  else
    optimizer.load(value.doubleValue());
  }
      break;
    case CHARACTER_LITERAL:
      jj_consume_token(CHARACTER_LITERAL);
                       {
  String sval=token.image.substring(1,token.image.length()-1);
  if (sval.length()==1)
    optimizer.load(sval.charAt(0));
  else{ // escape or number
    char ec=sval.charAt(1);
    try {
      switch (ec) {
      case 'n': ec='\n'; break;
      case 't': ec='\t'; break;
      case 'b': ec='\b'; break;
      case 'r': ec='\r'; break;
      case 'f': ec='\f'; break;
      case '\\': ec='\\'; break;
      case '\'': ec='\''; break;
      case '\"': ec='"'; break;
      default:
        ec=(char) Integer.parseInt(sval.substring(1),8);
      };
    } catch (NumberFormatException e) {/* Can't be */};
    optimizer.load(ec);
  };
  }
      break;
    default:
      jj_la1[12] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  }

  public ECTokenManager token_source;
  ASCII_CharStream jj_input_stream;
  public Token token, jj_nt;
  private int jj_ntk;
  private int jj_gen;
  final private int[] jj_la1 = new int[13];
  final private int[] jj_la1_0 = {0x168820,0x700,0x700,0x30,0x30,0xc0,0xc0,0x168820,0x168800,0x400000,0x168820,0x100000,0x28800,};

  public EC(java.io.InputStream stream) {
    jj_input_stream = new ASCII_CharStream(stream, 1, 1);
    token_source = new ECTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  public void ReInit(java.io.InputStream stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  public EC(java.io.Reader stream) {
    jj_input_stream = new ASCII_CharStream(stream, 1, 1);
    token_source = new ECTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  public void ReInit(java.io.Reader stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  public EC(ECTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  public void ReInit(ECTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 13; i++) jj_la1[i] = -1;
  }

  final private Token jj_consume_token(int kind) throws ParseException {
    Token oldToken;
    if ((oldToken = token).next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }

  final public Token getNextToken() {
    if (token.next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  }

  final public Token getToken(int index) {
    Token t = token;
    for (int i = 0; i < index; i++) {
      if (t.next != null) t = t.next;
      else t = t.next = token_source.getNextToken();
    }
    return t;
  }

  final private int jj_ntk() {
    if ((jj_nt=token.next) == null)
      return (jj_ntk = (token.next=token_source.getNextToken()).kind);
    else
      return (jj_ntk = jj_nt.kind);
  }

  private java.util.Vector jj_expentries = new java.util.Vector();
  private int[] jj_expentry;
  private int jj_kind = -1;

  final public ParseException generateParseException() {
    jj_expentries.removeAllElements();
    boolean[] la1tokens = new boolean[23];
    for (int i = 0; i < 23; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 13; i++) {
      if (jj_la1[i] == jj_gen) {
        for (int j = 0; j < 32; j++) {
          if ((jj_la1_0[i] & (1<<j)) != 0) {
            la1tokens[j] = true;
          }
        }
      }
    }
    for (int i = 0; i < 23; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.addElement(jj_expentry);
      }
    }
    int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++) {
      exptokseq[i] = (int[])jj_expentries.elementAt(i);
    }
    return new ParseException(token, exptokseq, tokenImage);
  }

  final public void enable_tracing() {
  }

  final public void disable_tracing() {
  }

                  /* EC stands for ExpressionCompiler */
};
