Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Script Engine Implemented by C# and Regular Expression

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
21 Nov 2009CPOL2 min read 24.8K   853   24  
Script engine to execute script codes, which is built by C# and regular expression
using System;
using System.Collections.Generic;
using System.Text;

namespace Zadesoft.Library.Script.Functions
{
    class BuiltInFunctions
        : IFunctionExecutor
    {
        private static BuiltInFunctions _current = new BuiltInFunctions();

        public static IFunctionExecutor Current
        {
            get
            {
                return _current;
            }
        }

        private Dictionary<string, IFunction> _functions;

        protected BuiltInFunctions()
        {
            _functions = new Dictionary<string, IFunction>();
            Init();
        }

        private void Init()
        {
            IFunction fn = new IsTodayFunction();
            _functions.Add(fn.Name, fn);

            fn = new IsBeforeTodayFunction();
            _functions.Add(fn.Name, fn);

            fn = new IsAfterTodayFunction();
            _functions.Add(fn.Name, fn);

            fn = new IsNullOrEmptyFunction();
            _functions.Add(fn.Name, fn);

            fn = new DoesMatchRegexFunction();
            _functions.Add(fn.Name, fn);

            fn = new DoesContainWordFunction();
            _functions.Add(fn.Name, fn);
        }

        public void Register(IFunction fn)
        {
            if (fn != null)
            {
                if (_functions.ContainsKey(fn.Name))
                {
                    _functions[fn.Name] = fn;
                }
                else
                {
                    _functions.Add(fn.Name, fn);
                }
            }
        }

        #region IExternalFunctionExecutor Members

        public bool Contains(string fnName)
        {
            return _functions.ContainsKey(fnName);
        }

        public object Execute(string fnName, params object[] paras)
        {
            if (_functions.ContainsKey(fnName))
            {
                return _functions[fnName].Execute(paras);
            }
            else
            {
                throw new ExpressionException("no such function found in built in functions");
            }
        }

        #endregion

        #region IRuntimePortal Members

        public string Name
        {
            get { return "BuiltInFunctions"; }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Totaldata Web Data Mining(extractweb.com)
China China
Just develop simple softwares. C# is my favorite prgramming languare, though I hope to create a new one...

Comments and Discussions