-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOperator.cs
85 lines (76 loc) · 2.69 KB
/
Operator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//*****************************************************************************
// Operator.cs - Represents a logical operator with a predefined set of
// operators.
//*****************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
namespace QEngine
{
public class Operator
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="precedence">Operator precedence.</param>
/// <param name="name">Name of the operator.</param>
public Operator(int precedence, string name)
{
Name = name;
Precedence = precedence;
}
/// <summary>
/// Provides conversion functionality from string type to this(Operation) type.
/// </summary>
/// <param name="oper">Operator to be converted to Opration type.</param>
/// <returns>Operation with given operator.</returns>
public static explicit operator Operator(string oper)
{
Operator result;
if (sOperators.TryGetValue(oper, out result))
{
return result;
}
else
{
throw new InvalidCastException();
}
}
/// <summary>
/// Name of this operator.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Precedence of this operator.
/// </summary>
public int Precedence { get; private set; }
/// <summary>
/// Checks if the given string is an operator.
/// </summary>
public static bool IsOperator(string str)
{
return sOperators.Keys.Contains(str);
}
/// <summary>
/// Gets the collection of defined operators.
/// </summary>
public static IEnumerable<string> Operators { get { return sOperators.Keys; } }
static Operator sEqual = new Operator(1, "Equals");
static Operator sGreaterThan = new Operator(1, "GreaterThan");
static Operator sLessThan = new Operator(1, "LessThan");
static Operator sAnd = new Operator(4, "AndAlso");
static Operator sOr = new Operator(4, "OrElse");
static Operator sLeftPar = new Operator(3, "LeftPar");
static Operator sRightPar = new Operator(3, "RightPar");
private static Dictionary<string, Operator> sOperators = new Dictionary<string, Operator>
{
{ "=", sEqual },
{ ">", sGreaterThan },
{ "<", sLessThan},
{ "AND", sAnd },
{"OR", sOr},
{"(", sLeftPar},
{")", sRightPar}
};
}
}