internal virtual DeclaredVariable DeclareVariable(string name, Expression valueAtTopOfScope = null, bool writable = true, bool deletable = false)
{
if (name == null)
throw new ArgumentNullException("name");
// If variables cannot be declared in the scope, try the parent scope instead.
if (this.CanDeclareVariables == false)
{
if (this.ParentScope == null)
throw new InvalidOperationException("Invalid scope chain.");
return this.ParentScope.DeclareVariable(name, valueAtTopOfScope, writable, deletable);
}
DeclaredVariable variable;
this.variables.TryGetValue(name, out variable);
if (variable == null)
{
// This is a local variable that has not been declared before.
variable = new DeclaredVariable() { Scope = this, Index = this.variables.Count, Name = name, Writable = writable, Deletable = deletable };
this.variables.Add(name, variable);
}
// Set the initial value, if one was provided.
if (valueAtTopOfScope != null)
{
// Function expressions override literals.
if ((valueAtTopOfScope is LiteralExpression && variable.ValueAtTopOfScope is FunctionExpression) == false)
variable.ValueAtTopOfScope = valueAtTopOfScope;
}
return variable;
}