/*
* $Id: DoStatement.java,v 1.13 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 java.io.IOException;
/**
* class DoStatement
*
* @author: Jani Lehtim�ki
*/
public class DoStatement extends ScopedStatement implements Labeled
{
private Expression _condition = Expression.NULL;
private Statement _statement = EMPTY;
private String _label = null;
private Source _startscope;
private Source _endscope;
public DoStatement(Statement parent, Location location) {
super(parent, location);
}
public DoStatement(Statement parent, Location location, String label) {
super(parent, location);
_label = label;
}
public int typeOf()
{
return Statement.ST_DO;
}
public String name() {
return "do";
}
public String getLabel()
{
return _label;
}
public void onWhile(Expression condition)
{
_condition = condition;
}
public Statement getChildStatement()
{
return _statement;
}
public void setChildStatement(Statement statement)
{
_statement = statement;
}
public void check(ErrorListener context)
{
_condition.check(context);
_statement.check(context);
}
public boolean onTag(TemplateParser parser, int type, Tag tag)
{
return true;
}
public Jumps eliminate(ErrorListener context)
{
Jumps jumps = _statement.eliminate(context);
boolean blocking = false;
switch(_condition.conditionOf()) {
case Expression.IS_FALSE:
//break breaks out, continue goes to false and falls out
blocking = jumps.isBlocked();
if (jumps.hasBreak()) {
blocking = false;
} else if (jumps.hasContinue()) {
blocking = false;
}
break;
case Expression.IS_DYNAMIC:
// break breaks out
blocking = jumps.isBlocked() && !jumps.hasBreak();
// with continue might pass
if (jumps.hasContinue()) {
blocking = false;
}
break;
case Expression.IS_TRUE:
blocking = !jumps.hasBreak();
break;
}
jumps.setBlocked(blocking);
//jumps.print("do");
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();
_startscope = code.getSource();
_endscope = code.getSource();
_statement.compile(context);
Target condition = code.getTarget();
if (!_statement.isBlocked()) {
switch(_condition.conditionOf()) {
case Expression.IS_FALSE:
break;
case Expression.IS_TRUE:
code.go_to(start);
break;
case Expression.IS_DYNAMIC:
_condition.compile(context, Expression.GET_BOOLEAN);
code.if_ne(start);
break;
}
}
_endscope.bind();
_startscope.bind(condition);
}
}