/// <summary> /// Creates a new instance of the <see cref="EvaluationContext"/> class. /// </summary> /// <param name="globalFunctions">The global functions of the evaluation context.</param> /// <param name="globalVariables">The global variables of the evaluation context.</param> /// <returns>A new immutable instance of the evaluation context.</returns> public static EvaluationContext Create(FunctionsDictionary globalFunctions, VariablesDictionary globalVariables) { return(new EvaluationContext( new FunctionsDictionary(globalFunctions), new VariablesDictionary(globalVariables))); // Makes them immutable }
private EvaluationContext( // Private ctor forces the creation of evaluation context from Create method FunctionsDictionary globalFunctions, VariablesDictionary globalVariables) // Globals are only created once { _globalFunctions = globalFunctions; _globalVariables = globalVariables; }
/// <summary> /// Adds a local <see cref="TemplateVariable"/> to the context under the supplied name, /// overriding any existing <see cref="TemplateVariable"/> with the same name /// </summary> public void SetLocalVariable(string name, TemplateVariable variable) { if (_localVariables == null) { _localVariables = new VariablesDictionary(); } _localVariables[name] = variable; }
//IStatement public void Execute() { if (!IsObject) { VariablesDictionary.SetVariable(variable, expression.Evaluate()); } else { ExecuteArray(); } }
private void ExecuteArray() { IObject obj = VariablesDictionary.GetObject(variable); IObject result = obj; GetIndexes(); for (int i = 0; i < indexes.Count; ++i) { if (i == indexes.Count - 1) { if (obj is ArrayIndex && indexes[i] is int) { ((ArrayIndex)obj)[(int)indexes[i]] = getObject(); } else if (obj is ArrayObject && indexes[i] is string) { ((ArrayObject)obj)[(string)indexes[i]] = getObject(); } else { throw new Exception("wrong index or type"); } break; } if (obj is ArrayIndex && indexes[i] is int) { obj = ((ArrayIndex)obj)[(int)indexes[i]]; } else if (obj is ArrayObject && indexes[i] is string) { obj = ((ArrayObject)obj)[(string)indexes[i]]; } else { throw new Exception("wrong index or type"); } } VariablesDictionary.SetVariable(variable, result); }
//IStatement public void Execute() { VariablesDictionary.PutVariable(variable, expression.Evaluate()); }
/// <summary> /// Initializes a new instance of the <see cref="EvaluationContext"/> class. /// Making it private forces the creation of evaluation context from the /// <see cref="Create(FunctionsDictionary, VariablesDictionary)"/> method. /// </summary> /// <param name="globalFunctions">The global functions of the evaluation context.</param> /// <param name="globalVariables">The global variables of the evaluation context.</param> private EvaluationContext(FunctionsDictionary globalFunctions, VariablesDictionary globalVariables) { _globalFunctions = globalFunctions; _globalVariables = globalVariables; }
public VariablesDictionary(VariablesDictionary dic) : base(dic, StringComparer.OrdinalIgnoreCase) { }
//default private IExpressionData DefaultEvaluate() { IObject data = VariablesDictionary.GetObject(name); return(getVariable(data)); }