/*
* $Id: ReturnStatement.java,v 1.17 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.Location;
import anvil.core.Any;
import anvil.codec.Code;
import anvil.codec.ConstantPool;
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.StackFrame;
import anvil.script.Grammar;
import java.io.IOException;
/**
* class ReturnStatement
*
* @author: Jani Lehtim�ki
*/
public class ReturnStatement extends Statement
{
private Expression _expression = null;
public ReturnStatement(Statement parent, Location location) {
super(parent, location);
}
public ReturnStatement(Statement parent, Location location, Expression expression) {
super(parent, location);
_expression = expression;
}
public int typeOf()
{
return Statement.ST_RETURN;
}
public String name() {
return "return";
}
public void parse(TemplateParser parser, Tag tag)
{
String expr = tag.getValue("value");
if (expr != null) {
_expression = Grammar.parseExpression(expr, getLocation(), parser);
} else {
_expression = Expression.NULL;
}
}
public void check(ErrorListener context)
{
if (_expression != null) {
_expression.check(context);
}
}
public Jumps eliminate(ErrorListener context)
{
return new Jumps().setReturn().setBlocked(true);
}
public void compile(ByteCompiler context)
{
Code code = context.getCode();
ConstantPool pool = code.getPool();
FunctionStatement function = getFunctionStatement();
if (function.isGenerator()) {
code.aload(function.getFrameIndex());
code.invokevirtual(pool.addMethodRef("anvil/script/Generator", "setClosedState", "()V"));
}
int rv = 0;
if (_expression != null) {
if (_expression.needLineNumbers()) {
context.location(_expression.getLocation());
}
rv = code.addLocal();
_expression.compile(context, Expression.GET);
code.astore(rv);
}
boolean blocked = false;
Statement stmt = this;
while(!blocked && stmt!=null) {
blocked = stmt.callFinalizer();
if (stmt.typeOf() == ST_FUNCTION) {
break;
}
stmt = stmt.getParentStatement();
}
if (!blocked) {
if (_expression != null) {
code.aload(rv);
code.areturn();
} else {
code.self();
code.areturn();
}
}
}
public boolean isBlocked()
{
return true;
}
}