/*
* $Id: IfStatement.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.parser.Tag;
import anvil.codec.Code;
import anvil.codec.Source;
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 IfStatement
*
* @author: Jani Lehtim�ki
*/
public class IfStatement extends ScopedStatement
{
private Expression _condition;
private Statement _thenStatements = EMPTY;
private Statement _elseStatements;
private boolean _onthen = true;
public IfStatement(Statement parent, Location location)
{
super(parent, location);
}
public IfStatement(Statement parent, Location location, Expression condition)
{
super(parent, location);
_condition = condition;
}
public int typeOf()
{
return Statement.ST_IF;
}
public String name()
{
return "if";
}
public boolean isOnThen()
{
return _onthen;
}
public void onElse()
{
_onthen = false;
}
public Statement getChildStatement()
{
if (_onthen) {
return _thenStatements;
} else {
return _elseStatements;
}
}
public void setChildStatement(Statement statement)
{
if (_onthen) {
_thenStatements = statement;
} else {
_elseStatements = statement;
}
}
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();
}
}
public boolean onTag(TemplateParser parser, int type, Tag tag)
{
switch(type) {
case ST_ELSE:
if (isOnThen()) {
onElse();
ImplicitBlockStatement block = new ImplicitBlockStatement(this, parser.getLocation());
setChildStatement(block);
return true;
} else {
return false;
}
case ST_ELSEIF:
if (isOnThen()) {
parser.pop();
onElse();
ImplicitBlockStatement elseblock = new ImplicitBlockStatement(this, parser.getLocation());
setChildStatement(elseblock);
IfStatement ifstmt = new IfStatement(getParentStatement(), parser.getLocation());
elseblock.add(ifstmt);
ifstmt.parse(parser, tag);
ImplicitBlockStatement thenblock = new ImplicitBlockStatement(ifstmt, parser.getLocation());
ifstmt.setChildStatement(thenblock);
parser.push(ifstmt);
return true;
} else {
return false;
}
case ST_ENDIF:
parser.pop();
return true;
default:
return super.onTag(parser, type, tag);
}
}
public void check(ErrorListener context)
{
boolean blocking = false;
int mask = 0;
int condition = _condition.conditionOf();
_condition.check(context);
_thenStatements.check(context);
if (_elseStatements != null) {
_elseStatements.check(context);
}
}
public Jumps eliminate(ErrorListener context)
{
Jumps jumps = null;
switch(_condition.conditionOf()) {
case Expression.IS_FALSE:
_thenStatements.eliminate(context);
if (_elseStatements != null) {
jumps = _elseStatements.eliminate(context);
} else {
jumps = new Jumps();
}
break;
case Expression.IS_DYNAMIC:
if (_elseStatements != null) {
Jumps _then = _thenStatements.eliminate(context);
Jumps _else = _elseStatements.eliminate(context);
boolean blocked = _then.isBlocked() && _else.isBlocked();
jumps = _then;
jumps.merge(_else);
jumps.setBlocked(blocked);
} else {
jumps = _thenStatements.eliminate(context);
jumps.setBlocked(false);
}
break;
case Expression.IS_TRUE:
jumps = _thenStatements.eliminate(context);
if (_elseStatements != null) {
_elseStatements.eliminate(context);
}
break;
}
return jumps;
}
public void compile(ByteCompiler context)
{
Code code = context.getCode();
switch(_condition.conditionOf()) {
case Expression.IS_FALSE:
if (_elseStatements != null) {
_elseStatements.compile(context);
}
break;
case Expression.IS_TRUE:
_thenStatements.compile(context);
break;
case Expression.IS_DYNAMIC:
if (_condition.needLineNumbers()) {
context.location(_condition.getLocation());
}
_condition.compile(context, Expression.GET_BOOLEAN);
Source to_then = code.if_eq();
_thenStatements.compile(context);
if (_elseStatements != null) {
Source to_end = code.go_to();
to_then.bind();
_elseStatements.compile(context);
to_end.bind();
} else {
to_then.bind();
}
break;
}
}
public boolean isBlocked()
{
boolean blocked = _thenStatements.isBlocked();
if (_elseStatements != null) {
blocked = blocked && _elseStatements.isBlocked();
}
return blocked;
}
}