Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BinaryOperator.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 namespace SiliconStudio.Shaders.Ast
6 {
7  /// <summary>
8  /// Binary operator used in all binary expressions (except assignment expression).
9  /// </summary>
10  public enum BinaryOperator
11  {
12  /// <summary>
13  /// No operator defined.
14  /// </summary>
15  None,
16 
17  /// <summary>
18  /// Logical And operator "&amp;&amp;"
19  /// </summary>
20  LogicalAnd,
21 
22  /// <summary>
23  /// Logical Or operator "||"
24  /// </summary>
25  LogicalOr,
26 
27  /// <summary>
28  /// Bitwise And operator "&amp;"
29  /// </summary>
30  BitwiseAnd,
31 
32  /// <summary>
33  /// Bitwise Or operator "|"
34  /// </summary>
35  BitwiseOr,
36 
37  /// <summary>
38  /// Bitwise Xor operator "^"
39  /// </summary>
40  BitwiseXor,
41 
42  /// <summary>
43  /// Left shift operator "&lt;&lt;"
44  /// </summary>
45  LeftShift,
46 
47  /// <summary>
48  /// Right shift operator "&gt;&gt;"
49  /// </summary>
50  RightShift,
51 
52  /// <summary>
53  /// Minus operator "-"
54  /// </summary>
55  Minus,
56 
57  /// <summary>
58  /// Plus operator "+"
59  /// </summary>
60  Plus,
61 
62  /// <summary>
63  /// Multiply operator "*"
64  /// </summary>
65  Multiply,
66 
67  /// <summary>
68  /// Divide operator "/"
69  /// </summary>
70  Divide,
71 
72  /// <summary>
73  /// Modulo operator "%"
74  /// </summary>
75  Modulo,
76 
77  /// <summary>
78  /// Less than operator "&lt;"
79  /// </summary>
80  Less,
81 
82  /// <summary>
83  /// Less or equal operator "&lt;="
84  /// </summary>
85  LessEqual,
86 
87  /// <summary>
88  /// Greater operator "&gt;"
89  /// </summary>
90  Greater,
91 
92  /// <summary>
93  /// Greater or equal operator "&gt;="
94  /// </summary>
95  GreaterEqual,
96 
97  /// <summary>
98  /// Equality operator "=="
99  /// </summary>
100  Equality,
101 
102  /// <summary>
103  /// Inequality operator "!="
104  /// </summary>
105  Inequality,
106  }
107 
108  /// <summary>
109  /// Helper for <see cref="BinaryOperator"/>.
110  /// </summary>
111  public static class BinaryOperatorHelper
112  {
113  #region Public Methods
114 
115  /// <summary>
116  /// Converts from operator to string
117  /// </summary>
118  /// <param name="binaryOperator">
119  /// The binary operator.
120  /// </param>
121  /// <returns>
122  /// A string representation of an binary operator
123  /// </returns>
124  public static string ConvertToString(this BinaryOperator binaryOperator)
125  {
126  switch (binaryOperator)
127  {
128  case BinaryOperator.LogicalAnd:
129  return "&&";
130  case BinaryOperator.LogicalOr:
131  return "||";
132  case BinaryOperator.BitwiseAnd:
133  return "&";
134  case BinaryOperator.BitwiseOr:
135  return "|";
136  case BinaryOperator.BitwiseXor:
137  return "^";
138  case BinaryOperator.LeftShift:
139  return "<<";
140  case BinaryOperator.RightShift:
141  return ">>";
142  case BinaryOperator.Minus:
143  return "-";
144  case BinaryOperator.Plus:
145  return "+";
146  case BinaryOperator.Multiply:
147  return "*";
148  case BinaryOperator.Divide:
149  return "/";
150  case BinaryOperator.Modulo:
151  return "%";
152  case BinaryOperator.Less:
153  return "<";
154  case BinaryOperator.LessEqual:
155  return "<=";
156  case BinaryOperator.Greater:
157  return ">";
158  case BinaryOperator.GreaterEqual:
159  return ">=";
160  case BinaryOperator.Equality:
161  return "==";
162  case BinaryOperator.Inequality:
163  return "!=";
164  }
165 
166  return string.Empty;
167  }
168 
169  /// <summary>
170  /// Converts from string an operator.
171  /// </summary>
172  /// <param name="operatorStr">
173  /// The operator text.
174  /// </param>
175  /// <exception cref="ArgumentException">
176  /// If operatorStr is invalid
177  /// </exception>
178  /// <returns>
179  /// An binary operator
180  /// </returns>
181  public static BinaryOperator FromString(string operatorStr)
182  {
183  if (operatorStr == "&&")
184  {
185  return BinaryOperator.LogicalAnd;
186  }
187 
188  if (operatorStr == "||")
189  {
190  return BinaryOperator.LogicalOr;
191  }
192 
193  if (operatorStr == "&")
194  {
195  return BinaryOperator.BitwiseAnd;
196  }
197 
198  if (operatorStr == "|")
199  {
200  return BinaryOperator.BitwiseOr;
201  }
202 
203  if (operatorStr == "^")
204  {
205  return BinaryOperator.BitwiseXor;
206  }
207 
208  if (operatorStr == "<<")
209  {
210  return BinaryOperator.LeftShift;
211  }
212 
213  if (operatorStr == ">>")
214  {
215  return BinaryOperator.RightShift;
216  }
217 
218  if (operatorStr == "-")
219  {
220  return BinaryOperator.Minus;
221  }
222 
223  if (operatorStr == "+")
224  {
225  return BinaryOperator.Plus;
226  }
227 
228  if (operatorStr == "*")
229  {
230  return BinaryOperator.Multiply;
231  }
232 
233  if (operatorStr == "/")
234  {
235  return BinaryOperator.Divide;
236  }
237 
238  if (operatorStr == "%")
239  {
240  return BinaryOperator.Modulo;
241  }
242 
243  if (operatorStr == "<")
244  {
245  return BinaryOperator.Less;
246  }
247 
248  if (operatorStr == "<=")
249  {
250  return BinaryOperator.LessEqual;
251  }
252 
253  if (operatorStr == ">")
254  {
255  return BinaryOperator.Greater;
256  }
257 
258  if (operatorStr == ">=")
259  {
260  return BinaryOperator.GreaterEqual;
261  }
262 
263  if (operatorStr == "==")
264  {
265  return BinaryOperator.Equality;
266  }
267 
268  if (operatorStr == "!=")
269  {
270  return BinaryOperator.Inequality;
271  }
272 
273  throw new ArgumentException(string.Format("Invalid binary operator [{0}]", operatorStr));
274  }
275 
276  #endregion
277  }
278 }
Less or equal operator "<="
static BinaryOperator FromString(string operatorStr)
Converts from string an operator.
Greater or equal operator ">="
Right shift operator ">>"
BinaryOperator
Binary operator used in all binary expressions (except assignment expression).
static string ConvertToString(this BinaryOperator binaryOperator)
Converts from operator to string
Less than operator "<"
Logical Or operator "||"
Inequality operator "!="
Left shift operator "<<"
Equality operator "=="
Logical And operator "&&"