VariableNode.java :  » Web-Framework » anvil » anvil » script » expression » Java Open Source

Java Open Source » Web Framework » anvil 
anvil » anvil » script » expression » VariableNode.java
/*
 * $Id: VariableNode.java,v 1.22 2002/09/16 08:05:05 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.ErrorListener;
import anvil.codec.Code;
import anvil.codec.ConstantPool;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.Type;
import anvil.script.statements.LocalVariableStatement;
import java.io.IOException;

/**
 * class VariableNode
 *
 * @author: Jani Lehtimki
 */
public class VariableNode extends Node
{

  private LocalVariableStatement _local;


  public VariableNode(LocalVariableStatement local) 
  {
    super();
    _local = local;
  }
  
  
  public int typeOf() 
  {
    return Node.EXPR_VARIABLE;
  }


  public LocalVariableStatement getVariable()
  {
    return _local;
  }


  public boolean isConstant() 
  {
    return false;
  }

  
  public Node optimize() 
  {
    return this;
  }


  public void compile(ByteCompiler context, Node child)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    if (_local.isEscaped()) {
      code.aload(_local.getFrameIndex());
      code.iconst(_local.getSlot());
      child.compile(context, GET);
      code.invokevirtual(code.getPool().addMethodRef("anvil/script/StackFrame", 
        "setLocal", "(ILanvil/core/Any;)Lanvil/core/Any;"));
    } else {
      child.compile(context, GET);
      code.dup();
      code.astore(_local.getIndex());
    }
  }
  

  public void compile(ByteCompiler context, int operation)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    if (_local.isEscaped()) {
      switch(operation) {
      case GET:
      case CHECK:
        code.aload(_local.getFrameIndex());
        code.iconst(_local.getSlot());
        code.invokevirtual(code.getPool().addMethodRef("anvil/script/StackFrame", "getLocal", "(I)Lanvil/core/Any;"));
        break;

      case GET_REF:
        int refclass = pool.addClass("anvil/core/AnyLocalRef");
        code.anew(refclass);
        code.dup();
        code.aload(_local.getFrameIndex());
        code.iconst(_local.getSlot());
        code.invokespecial(pool.addMethodRef(refclass, "<init>", "(Lanvil/script/StackFrame;I)V"));
        break;

      case GET_BOOLEAN:
        code.aload(_local.getFrameIndex());
        code.iconst(_local.getSlot());
        code.invokevirtual(code.getPool().addMethodRef("anvil/script/StackFrame", "getLocal", "(I)Lanvil/core/Any;"));
        context.any2boolean();
        break;

      case DELETE:
        code.iconst(false);
        break;
      }
      
    } else {
      switch(operation) {
      case GET:
        code.aload(_local.getIndex());
        break;

      case GET_BOOLEAN:
        code.aload(_local.getIndex());
        context.any2boolean();
        break;

      case CHECK:
        code.aload(_local.getIndex());
        break;

      case DELETE:
        code.getstatic(code.getPool().addFieldRef("anvil/core/Any", "UNDEFINED", "Lanvil/core/Any;"));
        break;
      }
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.