package com.drawbridge.jsengine.ast;
import java.util.LinkedList;
import java.util.List;
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.utils.Utils;
import com.drawbridge.vl.blocks.BlockIdentifier;
import com.google.caja.parser.js.Block;
import com.google.caja.parser.js.FormalParam;
import com.google.caja.parser.js.FunctionConstructor;
import com.google.caja.parser.js.Identifier;
public class FunctionConstructorEvaluator extends Evaluator
{
FunctionConstructor mFc;
Block mBlock;
public Identifier mIdentifier;
List<FormalParam> params;
Evaluator blockEvaluator;
public FunctionConstructorEvaluator(Evaluator parent, Scope scope, FunctionConstructor fc) {
super(parent, scope, fc.getFilePosition());
mFc = fc;
params = mFc.getParams();
mIdentifier = (Identifier) mFc.children().get(0);
// Children between identifier and block are params.
mBlock = (Block) mFc.children().get(mFc.children().size()-1);
blockEvaluator = Evaluator.getEvaluator(this, mScope, mBlock);
}
@Override
public JSType evaluate() throws EvaluatorException
{
//All we do here is register the function, we don't actually run the code
return null;
}
public JSType executeFunction(JSType [] inputParams) throws EvaluatorException, ExecutionException{
//TODO execute the function
if(inputParams.length == params.size()){
}
else{
throw new RuntimeException("Cannot accept incorrect size of params! in FunctionConstructorEvaluator");
}
//Register params as local variables
mScope.resetVariables();
for(int i = 0; i < inputParams.length; i++){
mScope.declareVariable(null, params.get(i).getIdentifierName(), new SymbolTableEntry(inputParams[i]));
}
Utils.out.println(this.getClass(),"EXECUTION OF FUNCTION");
return blockEvaluator.evaluate();
}
@Override
public LinkedList<com.drawbridge.vl.blocks.Block> getBlocks()
{
LinkedList<com.drawbridge.vl.blocks.Block> result = new LinkedList<com.drawbridge.vl.blocks.Block>();
for(FormalParam param: params){
BlockIdentifier ib = new BlockIdentifier(param.getFilePosition(), this, param.getIdentifierName());
result.add(ib);
}
LinkedList<com.drawbridge.vl.blocks.Block> blockResults = blockEvaluator.getBlocks();
if(blockResults != null)
result.addAll(blockResults);
return result;
}
}