Exemple #1
0
        /// <summary>
        /// Creates an <see cref="InterpreterScope"/> with all variables required for the function call.
        /// </summary>
        public InterpreterScope CreateCaptureScope(InterpreterScope scope)
        {
            var captureScope = new InterpreterScope(scope);

            captureScope.Context = this;

            // only have to capture variables for anonymous functions
            var userFunctionDefinition = this as AnonymousUserFunctionDefinitionExpression;

            if (userFunctionDefinition != null)
            {
                foreach (var captured in userFunctionDefinition.CapturedVariables)
                {
                    captureScope.DefineVariable(captured.Variable, captured.Expression);
                }
            }

            // set the context to the function definition and return the new context
            return(captureScope);
        }
Exemple #2
0
        /// <summary>
        /// Creates a new scope for calling a function and populates values for parameters passed to the function.
        /// </summary>
        /// <param name="function">The function defining the parameters to populate.</param>
        /// <param name="scope">The outer scope containing the function call.</param>
        /// <param name="error">[out] A <see cref="ParseErrorExpression"/> indicating why constructing the new scope failed.</param>
        /// <returns>The new scope, <c>null</c> if an error occurred - see <paramref name="error"/> for error details.</returns>
        public InterpreterScope GetParameters(FunctionDefinitionExpression function, InterpreterScope scope, out ExpressionBase error)
        {
            var parameterScope = new InterpreterScope(scope);

            var providedParameters = new List <string>(function.Parameters.Count);

            foreach (var parameter in function.Parameters)
            {
                providedParameters.Add(parameter.Name);
            }

            ArrayExpression varargs = null;

            if (providedParameters.Remove("..."))
            {
                varargs = new ArrayExpression();
                parameterScope.AssignVariable(new VariableExpression("varargs"), varargs);
            }

            var parameterCount = providedParameters.Count;

            int  index           = 0;
            bool namedParameters = false;

            foreach (var parameter in Parameters)
            {
                var assignedParameter = parameter as AssignmentExpression;
                if (assignedParameter != null)
                {
                    if (!providedParameters.Remove(assignedParameter.Variable.Name))
                    {
                        if (!function.Parameters.Any(p => p.Name == assignedParameter.Variable.Name))
                        {
                            error = new ParseErrorExpression(String.Format("'{0}' does not have a '{1}' parameter", function.Name.Name, assignedParameter.Variable.Name), parameter);
                            return(null);
                        }

                        error = new ParseErrorExpression(String.Format("'{0}' already has a value", assignedParameter.Variable.Name), assignedParameter.Variable);
                        return(null);
                    }

                    var assignmentScope = new InterpreterScope(scope)
                    {
                        Context = assignedParameter
                    };

                    ExpressionBase value;
                    if (!assignedParameter.Value.ReplaceVariables(assignmentScope, out value))
                    {
                        error = new ParseErrorExpression(value, assignedParameter.Value);
                        return(null);
                    }

                    assignedParameter.Value.CopyLocation(value);

                    parameterScope.DefineVariable(new VariableDefinitionExpression(assignedParameter.Variable), value);
                    namedParameters = true;
                }
                else
                {
                    if (namedParameters)
                    {
                        error = new ParseErrorExpression("Non-named parameter following named parameter", parameter);
                        return(null);
                    }

                    if (index >= parameterCount && varargs == null)
                    {
                        error = new ParseErrorExpression("Too many parameters passed to function", parameter);
                        return(null);
                    }

                    var variableName    = (index < parameterCount) ? function.Parameters.ElementAt(index).Name : "...";
                    var assignmentScope = new InterpreterScope(scope)
                    {
                        Context = new AssignmentExpression(new VariableExpression(variableName), parameter)
                    };

                    ExpressionBase value;
                    if (!parameter.ReplaceVariables(assignmentScope, out value))
                    {
                        error = new ParseErrorExpression(value, parameter);
                        return(null);
                    }

                    parameter.CopyLocation(value);

                    if (index < parameterCount)
                    {
                        providedParameters.Remove(variableName);
                        parameterScope.DefineVariable(new VariableDefinitionExpression(variableName), value);
                    }
                    else
                    {
                        varargs.Entries.Add(value);
                    }
                }

                ++index;
            }

            foreach (var parameter in providedParameters)
            {
                ExpressionBase value;
                if (!function.DefaultParameters.TryGetValue(parameter, out value))
                {
                    error = new ParseErrorExpression(String.Format("Required parameter '{0}' not provided", parameter), FunctionName);
                    return(null);
                }

                var assignmentScope = new InterpreterScope(scope)
                {
                    Context = new AssignmentExpression(new VariableExpression(parameter), value)
                };
                if (!value.ReplaceVariables(assignmentScope, out value))
                {
                    error = new ParseErrorExpression(value, this);
                    return(null);
                }

                parameterScope.DefineVariable(new VariableDefinitionExpression(parameter), value);
            }

            error = null;
            return(parameterScope);
        }