Case.java :  » Web-Framework » anvil » anvil » script » statements » Java Open Source

Java Open Source » Web Framework » anvil 
anvil » anvil » script » statements » Case.java
/*
 * $Id: Case.java,v 1.4 2002/09/16 08:05:06 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.statements;

import anvil.ErrorListener;
import anvil.Location;
import anvil.core.Any;
import anvil.script.expression.Expression;
import anvil.script.compiler.ByteCompiler;
import anvil.codec.Source;
import anvil.codec.Target;
import anvil.codec.Code;

public class Case
{

  protected Source _source = null;
  protected Target _target = null;
  protected Expression _expr;
  protected Any _const = null;
  protected BlockStatement _block;

  public Case(Expression expr, BlockStatement block)
  {
    _expr = expr;
    _block = block;
  }


  public boolean isDefault()
  {
    return _expr == null;
  }
  

  public Expression getExpression()
  {
    return _expr;
  }
  

  public BlockStatement getBlock()
  {
    return _block;
  }


  public Any getConstant()
  {
    if (_const != null) {
      return _const;
    }
    if (_expr.isConstant()) {
      return _const = _expr.eval();
    } else {
      return null;
    }
  }
  
  
  public Location getLocation()
  {
    return _expr.getLocation();
  }
  

  public void addChild(Statement statement)
  {
    _block.add(statement);
  }
  

  public void check(ErrorListener listener)
  {
    _block.check(listener);
  }
  

  public void checkExpression(ErrorListener listener)
  {
    if (_expr != null) {
      _expr.check(listener);
    }
  }
  

  public Jumps eliminate(ErrorListener listener)
  {
    return _block.eliminate(listener);
  }  


  public void compile(ByteCompiler context)
  {
    if (_source != null) {
      _source.bind();
      _source = null;
    }
    _target = context.getCode().getTarget();
    _block.compile(context);
  }


  public void go_to(Code code)
  {
    if (_target != null) {
      code.go_to(_target);
    } else if (_source != null) { 
      code.go_to(_source);
    } else {
      _source = code.go_to();
    }
  }

  public void if_ne(Code code)
  {
    if (_target != null) {
      code.if_ne(_target);
    } else if (_source != null) { 
      code.if_ne(_source);
    } else {
      _source = code.if_ne();
    }
  }


}

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.