Package anvil.script.statements

Source Code of anvil.script.statements.WhileStatement

/*
* $Id: WhileStatement.java,v 1.14 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.core.Any;
import anvil.Location;
import anvil.codec.Code;
import anvil.codec.Source;
import anvil.codec.Target;
import anvil.parser.Tag;
import anvil.ErrorListener;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.expression.Expression;
import anvil.script.parser.TemplateParser;
import anvil.script.Grammar;
import java.io.IOException;

/**
* class WhileStatement
*
* @author: Jani Lehtim�ki
*/
public class WhileStatement extends ScopedStatement implements Labeled
{

  private Expression _condition;
  private Statement  _statement = EMPTY;
  private String     _label;
  private Source     _startscope;
  private Source     _endscope;


  public WhileStatement(Statement parent, Location location)
  {
    super(parent, location);
  }


  public WhileStatement(Statement parent, Location location, Expression condition, String label)
  {
    super(parent, location);
    _condition = condition;
    _label = label;
  }


  public int typeOf()
  {
    return Statement.ST_WHILE;
  }


  public String name()
  {
    return "while";
  }


  public String getLabel()
  {
    return _label;
  }
 

  public void parse(TemplateParser parser, Tag tag)
  {
    boolean negate = false;
    String s = tag.getValue("expr");
    if (s == null) {
      s = tag.getValue("true");
      if (s == null) {
        s = tag.getValue("false");
        if (s != null) {
          negate = true;
        }
      }
    }
    _condition = Grammar.parseStandaloneExpression(s, getLocation(), parser);
    if (negate) {
      _condition.createNegation();
    }
    _label = parseLabel(parser, tag);
  }


  public boolean onTag(TemplateParser parser, int type, Tag tag)
  {
    switch(type) {
    case ST_ENDWHILE:
      parser.pop();
      break;

    default:
      return super.onTag(parser, type, tag);
    }
   
    return true;
  }


  public Statement getChildStatement()
  {
    return _statement;
  }  


  public void setChildStatement(Statement statement)
  {
    _statement = statement;
  }


  public void check(ErrorListener context)
  {
    _condition.check(context);
    _statement.check(context);
  }


  public Jumps eliminate(ErrorListener context)
  {
    Jumps jumps = _statement.eliminate(context);
    switch(_condition.conditionOf()) {
    case Expression.IS_FALSE:
      jumps = new Jumps();
      break;
  
    case Expression.IS_DYNAMIC:
      jumps.setBlocked(false);
      break;
           
    case Expression.IS_TRUE:
      jumps.setBlocked(!jumps.hasBreak());
      break;

    }
    //jumps.print("while");
    return jumps.shift();
  }
  

  public Source getStartOfScope()
  {
    return _startscope;
  }
 

  public Source getEndOfScope()
  {
    return _endscope;
  }
 

  public void compile(ByteCompiler context)
  {
    Code code = context.getCode();
    Target start = code.getTarget();

    switch(_condition.conditionOf()) {
    case Expression.IS_FALSE:
      return;

    case Expression.IS_TRUE:
      _startscope = code.getSource();
      _endscope = code.getSource();
      _statement.compile(context);
      code.go_to(_startscope);
      _startscope.bind(start);
      _endscope.bind();
      break;

    case Expression.IS_DYNAMIC:
      _startscope = code.getSource();
      _endscope = code.getSource();
      if (_condition.needLineNumbers()) {
        context.location(_condition.getLocation());
      }
      _condition.compile(context, Expression.GET_BOOLEAN);
      code.if_eq(_endscope);
      _statement.compile(context);
      if (!_statement.isBlocked()) {
        code.go_to(_startscope);
      }
      _startscope.bind(start);
      _endscope.bind();
      break;
    }
  
  }

}
TOP

Related Classes of anvil.script.statements.WhileStatement

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.