|
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.
Just develop simple softwares. C# is my favorite prgramming languare, though I hope to create a new one...