Package anvil.script.expression

Source Code of anvil.script.expression.ChainedComparisonNode

/*
* $Id: ChainedComparisonNode.java,v 1.5 2002/09/16 08:05:04 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.expression;

import anvil.core.Any;
import anvil.core.Any.Op;
import anvil.codec.Code;
import anvil.codec.Source;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.parser.ParserBaseConstants;
import java.io.IOException;

/**
* class ChainedComparisonNode
*
* @author: Jani Lehtim�ki
*/
public class ChainedComparisonNode extends MultiParent
  implements ParserBaseConstants
{


  private int[] _op;

  public ChainedComparisonNode(Node[] child, int[] op)
  {
    super(child);
    _op = op;
  }
 

  public int typeOf()
  {
    return Node.EXPR_COMPARISON_CHAIN;
  }


  public String getOp(int op)
  {
    switch(op) {
    case LESS:
      return "<";
    case LESS_OR_EQUAL:
      return "<=";
    case GREATER_OR_EQUAL:
      return ">=";
    case GREATER:
      return ">";
    default:
      return "";
    }
  }


  public Any eval()
  {
    Any a = getChild(0).eval();
    Any b;
    int n = childs();
    for(int i = 1; i < n; i++) {
      b = getChild(i).eval();
      switch(_op[i-1]) {
      case LESS:
        if (Op.test(a,b) >= 0) {
          return Any.FALSE;
        }
        break;
      case LESS_OR_EQUAL:
        if (Op.test(a,b) > 0) {
          return Any.FALSE;
        }
        break;
      case GREATER_OR_EQUAL:
        if (Op.test(a,b) < 0) {
          return Any.FALSE;
        }
        break;
      case GREATER:
        if (Op.test(a,b) <= 0) {
          return Any.FALSE;
        }
        break;
      }
      a = b;
    }
    return Any.TRUE;
  }



  public void compile(ByteCompiler context, int operation)
  {
    Code code = context.getCode();
    int n = childs();
    int testmethod = code.getPool().addMethodRef(
      context.TYPE_ANY_OP, "test", "(Lanvil/core/Any;Lanvil/core/Any;)I");
    Source isfalse = code.getSource();
    for(int i=0; i<n; i++) {
      if (i==0) {
        getChild(i++).compile(context, GET);
      }
      getChild(i).compile(context, GET);
      code.dup_x1();
      code.invokestatic(testmethod);
      switch(_op[i-1]) {
      case LESS:
        code.if_ge(isfalse);
        break;       
      case LESS_OR_EQUAL:
        code.if_gt(isfalse);
        break;       
      case GREATER_OR_EQUAL:
        code.if_lt(isfalse);
        break;       
      case GREATER:
        code.if_le(isfalse);
        break;       
      default:
        code.go_to(isfalse);
      }
    }
    code.pop();
    code.iconst(true);
    Source toend = code.go_to();
    isfalse.bind();
    code.pop();
    code.iconst(false);
    toend.bind();
    if (operation != GET_BOOLEAN) {
      context.boolean2any();
    }
  }

}
TOP

Related Classes of anvil.script.expression.ChainedComparisonNode

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.