package com.drawbridge.jsengine.ast;
import java.util.LinkedList;
import com.drawbridge.jsengine.Scope;
import com.drawbridge.jsengine.SymbolTableEntry;
import com.drawbridge.jsengine.jsobjects.ExecutionException;
import com.drawbridge.jsengine.jsobjects.JSType;
import com.drawbridge.vl.blocks.BlockAssignment;
import com.drawbridge.vl.blocks.BlockDeclaration;
import com.google.caja.parser.js.Declaration;
import com.google.caja.parser.js.Expression;
import com.google.caja.parser.js.Identifier;
public class DeclarationEvaluator extends Evaluator
{
public Identifier identifier;
public Expression initialiser;
/**
* Used to declare a var. No other types can be used (rules of Js).
*
* @param parser
* @param declaration
*/
public DeclarationEvaluator(Evaluator parent, Scope scope, Declaration declaration) {
super(parent, scope, declaration.getFilePosition());
identifier = declaration.getIdentifier();
initialiser = declaration.getInitializer();
scope.declareVariableForStaticAnalysis(identifier);
Evaluator initEval = Evaluator.getEvaluator(this, mScope, initialiser);
mChildren.add(initEval);
}
@Override
public JSType evaluate() throws EvaluatorException, ExecutionException
{
// Evaluate the initializer: could be primitive, or function call, etc.
JSType initialiserResult = null;
if (mChildren.get(0) != null)
{
// Utils.out.println("initialiser evaluator: " +
// mChildren.get(0).getClass().getName());
initialiserResult = mChildren.get(0).evaluate();
// Utils.out.println("Attempted to add: " + ((initialiserResult ==
// null) ? "NULL" : initialiserResult.toString()) +
// " as the value for: " + identifier.getValue());
try
{
mScope.declareVariable(null, identifier.getValue(), new SymbolTableEntry(initialiserResult, this.getFilePosition().startLineNo()));
} catch (Exception e)
{
throw new EvaluatorException("DeclarationEvaluation error: SymbolTable Addition:" + e.getMessage());
}
}
else
{
throw new RuntimeException("DeclarationEvaluator returned null!");
}
return null;
}
@Override
public LinkedList<com.drawbridge.vl.blocks.Block> getBlocks()
{
LinkedList<com.drawbridge.vl.blocks.Block> results = new LinkedList<com.drawbridge.vl.blocks.Block>();
BlockDeclaration db = new BlockDeclaration(identifier.getFilePosition(), this, identifier.getValue());
results.add(db);
if (mChildren != null && mChildren.size() > 0 && mChildren.get(0) != null)
{
BlockAssignment ab = new BlockAssignment(initialiser.getFilePosition(), null);
ab.setFilePosition(identifier.getFilePosition());
results.add(ab);
LinkedList<com.drawbridge.vl.blocks.Block> childResult = ((mChildren.get(0) == null) ? null : mChildren.get(0).getBlocks());
if (childResult != null)
results.addAll(childResult);
}
return results;
}
}