Click here to Skip to main content
15,893,904 members
Articles / Programming Languages / C#

General Expression Parser and Evaluator

Rate me:
Please Sign up or sign in to vote.
4.10/5 (12 votes)
26 Jun 2011CPOL4 min read 69.3K   1.8K   46  
A user configurable expression parser and evaluator
using System;
using System.Collections.Generic;
using System.Text;

namespace RpnParser {
  public enum OperatorType {
    Unknown,
    BitwiseAnd = (int)'&',
    BitwiseOr = (int)'|',
    BitwiseXor = (int)'^',
    LogicalAnd = 128 + (int)'&',
    LogicalOr = 128 + (int)'|',
    LogicalNot = 128 + (int)'^',
    Negation = 128 + (int)'!',
    CompEqual = (int)'=',
    CompNotEqual = (int)'!',
    CompGreater = (int)'>',
    CompGreaterEqual = 128 + (int)'>',
    CompLessThan = (int)'<',
    CompLessThanEqual = 128 + (int)'|',
    Plus = (int)'+',
    Minus = (int)'-',
    Multiply = (int)'*',
    Divide = (int)'/',
    Remainder = (int)'\\',
    IntegerDivide = (int)'%',
    Exponent = 255,
    CondTrue = (int)'?',
    CondFalse = (int)':',
    AssignEqual = 128 + (int)':',
    AssignPlus = 128 + (int)'+',
    AssignMinus = 128 + (int)'-',
    AssignMultiply = 128 + (int)'*',
    AssignDivide = 128 + (int)'/',
    AssignIntegerDivide = 128 + (int)'%',
    AssignRemainder = 128 + (int)'\\',
  }
	public enum OperatorGroup {
		Unknown,
		Arithmetic,
		Comparison,
		Logical,
		Bitwise,
		Conditional,
		Assignment,
    StructField
	}

	public enum Association {
		NA,
		Left,
		Right
	}

	/// <summary>
	/// Base class of all operators.  Provides datastorage
	/// </summary>
	public class RpnOperator : RpnElement {
		private OperatorType opType;
		private OperatorGroup opGroup;
		private Association assoc;
		private int precedence;
		private int condGoto;

		public RpnOperator(RpnToken token)
			: base(token) {
			this.precedence = token.Precedence;
			this.assoc = token.Association;
		}

		public bool IsMonadic {
			get { return (this.precedence == 16); }
		}
		public OperatorType OpType {
			get { return this.opType; }
			set { this.opType = value; }
		}
		public OperatorGroup OpGroup {
			get { return this.opGroup; }
			set { this.opGroup = value; }
		}
		public int CondGoto {
			get { return this.condGoto; }
			set { this.condGoto = value; }
		}
		public Association Association {
			get { return this.assoc; }
		}

		public override string ToString() {
			string data = String.Format("{0} {1}", base.ElementType, base.StrValue);
			if (this.opGroup == OperatorGroup.Conditional)
				data += ' '+this.condGoto.ToString();
			return data;
		}

		public new void Negate() {
			switch (opType) {
				case OperatorType.CompEqual:
					this.opType = OperatorType.CompNotEqual;
					break;
				case OperatorType.CompGreater:
					opType = OperatorType.CompLessThanEqual;
					break;
				case OperatorType.CompGreaterEqual:
					this.opType = OperatorType.CompLessThan;
					break;
				case OperatorType.CompLessThan:
					this.opType = OperatorType.CompGreaterEqual;
					break;
				case OperatorType.CompLessThanEqual:
					this.opType = OperatorType.CompGreater;
					break;
			}
		}

	}

}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions