Example #1
0
        /// <summary>
        /// Updates the provided scope with the new variable value.
        /// </summary>
        /// <param name="scope">The scope object containing variable values and function parameters.</param>
        /// <returns>
        ///   <c>null</c> if successful, or a <see cref="ParseErrorExpression" /> if not.
        /// </returns>
        public ParseErrorExpression Evaluate(InterpreterScope scope)
        {
            var assignmentScope = new InterpreterScope(scope)
            {
                Context = this
            };
            ExpressionBase result;

            var functionDefinition = Value as FunctionDefinitionExpression;

            if (functionDefinition != null)
            {
                scope.AddFunction(functionDefinition);
                result = new FunctionReferenceExpression(functionDefinition.Name.Name);
            }
            else
            {
                if (!Value.ReplaceVariables(assignmentScope, out result))
                {
                    return((ParseErrorExpression)result);
                }
            }

            return(scope.AssignVariable(Variable, result));
        }
Example #2
0
        /// <summary>
        /// Replaces the variables in the expression with values from <paramref name="scope" />.
        /// </summary>
        /// <param name="scope">The scope object containing variable values.</param>
        /// <param name="result">[out] The new expression containing the replaced variables.</param>
        /// <returns>
        ///   <c>true</c> if substitution was successful, <c>false</c> if something went wrong, in which case <paramref name="result" /> will likely be a <see cref="ParseErrorExpression" />.
        /// </returns>
        public override bool ReplaceVariables(InterpreterScope scope, out ExpressionBase result)
        {
            ExpressionBase value = scope.GetVariable(Name);

            if (value == null)
            {
                var func = scope.GetFunction(Name);
                if (func != null)
                {
                    // special wrapper for returning a function as a variable
                    result = new FunctionReferenceExpression(Name);
                    result.CopyLocation(this);
                    return(true);
                }

                result = new UnknownVariableParseErrorExpression("Unknown variable: " + Name, this);
                return(false);
            }

            return(value.ReplaceVariables(scope, out result));
        }