Exemple #1
0
        /// <summary>
        /// Adds a function to the current value with the given name.
        /// </summary>
        /// <param name="name">The name of the function.</param>
        public void AddFunction(String name)
        {
            var function = new FunctionBuffer(name);

            function.StartIndex = _values.Count;
            _functions.Push(function);
        }
        /// <summary>
        /// Tries to load a function from a given file.
        /// </summary>
        /// <param name="symbolName">The name of the function (equals the name of the file).</param>
        /// <returns>The function (if found) or NULL.</returns>
        public override IFunction LoadFunction(String symbolName)
        {
            if (!Parser.UseScripting)
            {
                return(null);
            }

            var script = String.Empty;

            var function = new FunctionBuffer
            {
                Directory    = Environment.CurrentDirectory,
                FunctionName = symbolName
            };

            if (!File.Exists(function.FileName))
            {
                return(null);
            }

            for (var i = buffer.Count - 1; i >= 0; i--)
            {
                if (!buffer[i].Directory.Equals(Environment.CurrentDirectory, StringComparison.CurrentCultureIgnoreCase))
                {
                    buffer.RemoveAt(i);
                }
            }

            var original = buffer
                           .Where(m => m.FileName.Equals(function.FileName, StringComparison.CurrentCultureIgnoreCase))
                           .FirstOrDefault();

            try
            {
                function.LastUpdated = File.GetLastWriteTime(function.FileName);

                if (original != null)
                {
                    if (function.LastUpdated.CompareTo(original.LastUpdated) <= 0)
                    {
                        return(original.Lookup());
                    }

                    buffer.Remove(original);
                }

                script = File.ReadAllText(function.FileName);
            }
            catch
            {
                if (buffer != null)
                {
                    return(original.Lookup());
                }

                return(null);
            }

            if (!String.IsNullOrEmpty(script))
            {
                function.Context = new ParseContext(parent);
                var p = Parser.Parse(function.Context, script);

                if (!p.Context.Parser.HasErrors)
                {
                    try { p.Execute(); }
                    catch { return(null); }

                    buffer.Add(function);
                    return(function.Lookup());
                }
            }

            return(null);
        }