Click here to Skip to main content
15,887,979 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.Compare
{
    public class LessThanExpression
        : Expression
    {
        public override string Name
        {
            get
            {
                return "LessThan";
            }
        }

        public override Expression Evaluate(ScriptContext context)
        {

            if (_subExpressions.Count < 2)
            {
                throw new ExpressionException(this, "lack of operand");
            }

            Expression left, right;

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

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

            if (left is ObjectExpression && right is ObjectExpression)
            {
                object leftobj, rightobj;
                leftobj = ((ObjectExpression)left).DirectValueObject;
                rightobj = ((ObjectExpression)right).DirectValueObject;

                if (leftobj == null)
                {
                    return BooleanExpression.True;
                }
                else if (rightobj == null)
                {
                    return BooleanExpression.False;
                }
                else
                {
                    if (leftobj is IComparable && rightobj is IComparable)
                    {
                        return new BooleanExpression(((IComparable)leftobj).CompareTo(rightobj) < 0);
                    }
                    else
                    {
                        throw new ExpressionException(this, "operands cannot be comparable");
                    }
                }
            }
            else
            {
                throw new ExpressionException(this, "any operand has invalid value");
            }
        }

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

        internal class LessThanExpressionParser
            : IExpressionParser
        {
            public const string REGEX2 = @"^([A-Za-z]+\(@[A-Za-z0-9_]+\))[ ]*(<)[ ]*([A-Za-z]+\(@[A-Za-z0-9_]+\))$";
            public const string REGEX = @"([\w.!,""@\(\)& ]+)[ ]*(<)[ ]*([\w.!,""@\(\)& ]+)";

            #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 < 3)
                {
                    throw new ExpressionParseFailedException(this, "code not match");
                }

                LessThanExpression lt = new LessThanExpression();

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

                try
                {
                    lt._subExpressions.Add(Expression.Parser.Parse(match.Groups[3].Value));
                }
                catch (Exception e)
                {
                    throw new ExpressionParseFailedException(this, "right operand expression cannot be parse", e);
                }

                return lt;
            }

            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