/* --------------------------------------------------------- *
* __________ D E L T A S C R I P T *
* (_________() *
* / === / - A fast, dynamic scripting language *
* | == | - Version 4.13.11.0 *
* / === / - Developed by Adam R. Nelson *
* | = = | - 2011-2013 *
* / === / - Distributed under GNU LGPL v3 *
* (________() - http://github.com/ar-nelson/deltascript *
* *
* --------------------------------------------------------- */
package com.sector91.delta.script.instrs;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import com.sector91.delta.script.objects.DS_Tag;
public class VarCountVisitor implements InstrVisitor
{
private static final int INCLUDE_WEIGHT = 5;
private Set<DS_Tag> encounteredVariables;
private int encounteredIncludes;
public VarCountVisitor()
{
encounteredVariables = new HashSet<DS_Tag>();
}
public VarCountVisitor(DS_Tag... existingVars)
{
this();
for (DS_Tag tag : existingVars)
encounteredVariables.add(tag);
}
public VarCountVisitor(Collection<DS_Tag> existingVars)
{
this();
encounteredVariables.addAll(existingVars);
}
public InstrVisitorResult visit(DSInstr instr)
{
switch (instr.type())
{
case SET:
encounteredVariables.add(((SetInstr)instr).cachedTag);
return InstrVisitorResult.SKIP_TAIL;
case DEF:
encounteredVariables.add(((DefInstr)instr).cachedTag);
return InstrVisitorResult.SKIP_TAIL;
case FOR:
if (((ForInstr)instr).cachedTag != null)
encounteredVariables.add(((ForInstr)instr).cachedTag);
return InstrVisitorResult.CONTINUE;
case COMPRH:
if (((ComprehensionInstr)instr).cachedTag != null)
encounteredVariables.add(((ComprehensionInstr)instr).cachedTag);
return InstrVisitorResult.CONTINUE;
case FUNC:
case SCOPE:
return InstrVisitorResult.SKIP_TAIL;
case INCLUDE:
encounteredIncludes++;
return InstrVisitorResult.CONTINUE;
default:
return InstrVisitorResult.CONTINUE;
}
}
public int getVariableCount()
{return encounteredVariables.size() + encounteredIncludes*INCLUDE_WEIGHT;}
}