Click here to Skip to main content
15,886,055 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;
using System.Text.RegularExpressions;

using Zadesoft.Library.Script.Common;

namespace Zadesoft.Library.Script.Logic
{
    public class NotExpression
        : Expression
    {
        public override string Name
        {
            get
            {
                return "Not";
            }
        }

        public override Expression Evaluate(ScriptContext context)
        {
            if (_subExpressions.Count < 1)
            {
                throw new ExpressionException(this, "lack of operand");
            }

            Expression expr = null;

            try
            {
                //operand evaluate
                expr = _subExpressions[0].Evaluate(context);
            }
            catch (Exception e)
            {
                throw new ExpressionException(this, "operand evaluate failed", e);
            }

            //return boolean value
            if (expr is ObjectExpression)
            {
                return new BooleanExpression(!BooleanExpression.GetBoolValue((ObjectExpression)expr));
            }
            else
            {
                throw new ExpressionException(this, "bad value from operand");
            }
        }

        public new static IExpressionParser Parser
        {
            get
            {
                return new NegateExpressionParser();
            }
        }

        internal class NegateExpressionParser
            : IExpressionParser
        {
            public const string REGEX = @"^!([@]?[a-zA-Z][a-zA-Z0-9_]*|[A-Za-z]+\([@ ,.""A-Za-z0-9_]+\))$";

            #region IExpressionParser Members

            public ExpressionPriority Priority { get { return ExpressionPriority.OperatorHigh; } }

            public virtual Expression Parse(string code)
            {
                Match match = Regex.Match(code, REGEX);

                if (match.Groups.Count < 2)
                {
                    throw new ExpressionParseFailedException(this, "code not match");
                }

                NotExpression negate = new NotExpression();

                try
                {
                    negate._subExpressions.Add(Expression.Parser.Parse(match.Groups[1].Value));
                }
                catch (Exception e)
                {
                    throw new ExpressionParseFailedException(this, "cannot parse the operand part", e);
                }

                return negate;
            }

            public virtual Expression Parse(string[] words)
            {
                throw new NotImplementedException();
            }

            public virtual bool CheckGrammar(string code)
            {
                return Regex.IsMatch(code, REGEX);
            }

            public virtual bool CheckGrammar(string[] words)
            {
                throw new NotImplementedException();
            }

            #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