Package anvil.script.statements

Source Code of anvil.script.statements.ForStatement

/*
* $Id: ForStatement.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.parser.Tag;
import anvil.codec.Code;
import anvil.codec.Source;
import anvil.codec.Target;
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.parser.ExpressionParser;
import java.io.IOException;

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

  private Expression[] _init      = null;
  private Expression   _condition = null;
  private Expression[] _action    = null;
  private Statement    _statement = EMPTY;
  private String _label;
  private Source _startscope;
  private Source _endscope;

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


  public ForStatement(Statement parent, Location location, Expression[] init, Expression condition, Expression[] action, String label)
  {
    super(parent, location);
    _init = init;
    _condition = condition;
    _action = action;
    _label = label;
  }


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


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


  public String getLabel()
  {
    return _label;
  }


  public void parse(TemplateParser parser, Tag tag)
  {
    String expr = tag.getValue("expr");
    if (expr == null) {
      expr = tag.getValue("do");
    }
    if (expr != null) {
      ExpressionParser p = new ExpressionParser(parser, getLocation(), expr);
      Object[] list = p.parseForExpressionList();
      _init = (Expression[]) list[0];
      _condition = (Expression) list[1];
      _action = (Expression[]) list[2];
    }
    _label = parseLabel(parser, tag);
  }
 

  public boolean onTag(TemplateParser parser, int type, Tag tag)
  {
    switch(type) {
    case ST_ENDFOR:
      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)
  {
    if (_init != null) {
      int n = _init.length;
      for(int i = 0; i<n; i++) {
        _init[i].check(context);
      }
    }
    if (_condition != null) {
      _condition.check(context);
    }
    if (_action != null) {
      int n = _action.length;
      for(int i = 0; i<n; i++) {
        _action[i].check(context);
      }
    }
    _statement.check(context);
  }


  public Jumps eliminate(ErrorListener context)
  {
    Jumps jumps = _statement.eliminate(context)
    if (_condition == null) {
      jumps.setBlocked(!jumps.hasBreak());
     
    } else {
      switch(_condition.conditionOf()) {
      case Expression.IS_FALSE:
        break;

      case Expression.IS_TRUE:
        jumps.setBlocked(!jumps.hasBreak());
        break;

      case Expression.IS_DYNAMIC:
        jumps.setBlocked(false);
        break;
      }
     
    }
    return jumps.shift();
  }


  public Source getStartOfScope()
  {
    return _startscope;
  }
 

  public Source getEndOfScope()
  {
    return _endscope;
 
 

  public void compile(ByteCompiler context)
  {
    Code code = context.getCode();
    context.location(getLocation());

    if (_init != null) {
      int n = _init.length;
      for(int i = 0; i<n; i++) {
        _init[i].compile(context, Expression.GET);
        code.pop();
      }
    }

    Source skip = null;
    if (_action != null) {
      skip = code.go_to();
    }

    Target start = code.getTarget();
    _startscope = code.getSource();
    _endscope = code.getSource();
   
    if (_action != null) {
      context.location(getLocation());
      int n = _action.length;
      for(int i = 0; i<n; i++) {
        _action[i].compile(context, Expression.GET);
        code.pop();
      }
      skip.bind();
    }

    if (_condition != null) {
      switch(_condition.conditionOf()) {
      case Expression.IS_FALSE:
        _endscope.bind();
        _startscope.bind(start);
        return;

      case Expression.IS_TRUE:
        break;

      case Expression.IS_DYNAMIC:
        _condition.compile(context, Expression.GET_BOOLEAN);
        code.if_eq(_endscope);
        break;
      }
    }

    _statement.compile(context);
    code.go_to(_startscope);
    _endscope.bind();
    _startscope.bind(start);
  }
 



}
TOP

Related Classes of anvil.script.statements.ForStatement

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.