Reference CSharp Parser


SUBMITTED BY: Guest

DATE: Jan. 2, 2014, 12:43 p.m.

FORMAT: C#

SIZE: 13.9 kB

HITS: 1545

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Lib.Mathematics
  5. {
  6. public class Expression
  7. {
  8. internal Expression()
  9. {
  10. }
  11. private List<Stack> _stacks = new List<Stack>();
  12. public object Result { get; set; }
  13. public string Term
  14. {
  15. get { return _term; }
  16. internal set { _term = value;}
  17. }
  18. public readonly Dictionary<string, Expression> Variables = new Dictionary<string, Expression>();
  19. private string _term;
  20. public static Expression Parse(string expr)
  21. {
  22. var returns = new Expression {Term = expr};
  23. expr = expr.Replace("²", "²0");
  24. expr = expr.Replace("³", "³0");
  25. expr = expr.Replace("sqrt[", "√");
  26. expr = expr.Replace("pi", "π");
  27. expr = expr.Replace("]", "");
  28. expr = expr.Replace("√", "1√");
  29. expr = expr.Replace("π", "1π1");
  30. expr = expr.Replace("True", "1");
  31. expr = expr.Replace("False", "0");
  32. expr = returns.Variables.Aggregate(expr, (current, var) => current.Replace(var.Key + "*1", var.Value));
  33. var chars = expr.ToCharArray();
  34. var result = "";
  35. foreach (var c in chars)
  36. {
  37. if (isNum(c))
  38. {
  39. result += c;
  40. }
  41. else
  42. {
  43. if (isOP(c))
  44. {
  45. returns._stacks.Add(new NumStack(Convert.ToDouble(result)));
  46. returns._stacks.Add(new OpStack(c.ToString()));
  47. result = "";
  48. }
  49. }
  50. }
  51. returns._stacks.Add(returns.getLastNum(expr));
  52. returns._stacks = returns.dotbeforestatement(returns._stacks);
  53. returns.Calculate();
  54. return returns;
  55. }
  56. internal void Calculate()
  57. {
  58. for (var i = 0; i < _stacks.Count - 1; i++)
  59. {
  60. var stack = _stacks[i];
  61. var opStack = stack as OpStack;
  62. if (opStack != null)
  63. {
  64. var first = (NumStack)_stacks[i - 1];
  65. var second = (NumStack)_stacks[i + 1];
  66. var op = opStack;
  67. var _result = new NumStack(0);
  68. var resultisbool = false;
  69. var boolresult = false;
  70. switch (op.Value)
  71. {
  72. case "+":
  73. _result = new NumStack(first.Value + second.Value);
  74. break;
  75. case "-":
  76. _result = new NumStack(first.Value - second.Value);
  77. break;
  78. case "*":
  79. _result = new NumStack(first.Value * second.Value);
  80. break;
  81. case "/":
  82. _result = new NumStack(first.Value / second.Value);
  83. break;
  84. case "^":
  85. _result = new NumStack(Math.Pow(first.Value, second.Value));
  86. break;
  87. case "%":
  88. _result = new NumStack(first.Value % second.Value);
  89. break;
  90. case "&":
  91. _result = new NumStack(Convert.ToDouble(first.Value.ToString() + second.Value.ToString()));
  92. break;
  93. case "²":
  94. _result = new NumStack(first.Value * first.Value);
  95. break;
  96. case "³":
  97. _result = new NumStack(first.Value * first.Value * first.Value);
  98. break;
  99. case "√":
  100. _result = new NumStack(Math.Sqrt(second.Value));
  101. break;
  102. case "π":
  103. _result = new NumStack(Math.PI);
  104. break;
  105. case "<":
  106. resultisbool = true;
  107. boolresult = first.Value < second.Value;
  108. break;
  109. case ">":
  110. resultisbool = true;
  111. boolresult = first.Value > second.Value;
  112. break;
  113. case "!":
  114. resultisbool = true;
  115. boolresult = first.Value != second.Value;
  116. break;
  117. }
  118. _stacks.RemoveAt(0);
  119. _stacks.RemoveAt(0);
  120. _stacks.RemoveAt(0);
  121. _stacks.Insert(0, _result);
  122. Result = (_stacks[0] as NumStack).Value;
  123. if (_stacks.Count != 1)
  124. {
  125. if (_stacks[0] != null && _stacks[1] != null && _stacks[2] != null)
  126. {
  127. Calculate();
  128. }
  129. }
  130. if (resultisbool)
  131. {
  132. Result = boolresult;
  133. }
  134. }
  135. }
  136. }
  137. #region "ObjectOperators"
  138. public static Expression operator +(Expression e1, Expression e2)
  139. {
  140. return Parse(e1.Result + "+" + e2.Result);
  141. }
  142. public static bool operator ==(Expression e1, Expression e2)
  143. {
  144. return e1 == e2;
  145. }
  146. public static bool operator !=(Expression e1, Expression e2)
  147. {
  148. return e1 != e2;
  149. }
  150. public static Expression operator +(Expression e1, string e2)
  151. {
  152. e2 = e2.Replace("=", "");
  153. return Parse(e2);
  154. }
  155. public static Expression operator -(Expression e1, Expression e2)
  156. {
  157. return Parse(e1.Result + "-" + e2.Result);
  158. }
  159. public static Expression operator *(Expression e1, Expression e2)
  160. {
  161. return Parse(e1.Result + "*" + e2.Result);
  162. }
  163. public static Expression operator /(Expression e1, Expression e2)
  164. {
  165. return Parse(e1.Result + "/" + e2.Result);
  166. }
  167. public static Expression operator <(Expression e1, Expression e2)
  168. {
  169. return Parse(e1.Result + "<" + e2.Result);
  170. }
  171. public static Expression operator >(Expression e1, Expression e2)
  172. {
  173. return Parse(e1.Result + ">" + e2.Result);
  174. }
  175. public static implicit operator Expression(string v)
  176. {
  177. return Parse(v);
  178. }
  179. public static implicit operator double(Expression instance)
  180. {
  181. return (double) instance.Result;
  182. }
  183. public static implicit operator bool(Expression instance)
  184. {
  185. return (bool) instance.Result;
  186. }
  187. public static implicit operator string(Expression instance)
  188. {
  189. return instance.Result.ToString();
  190. }
  191. public override bool Equals(object obj)
  192. {
  193. if (obj == null || GetType() != obj.GetType())
  194. {
  195. return false;
  196. }
  197. return base.Equals(obj);
  198. }
  199. #endregion
  200. #region "Help Functions"
  201. private static bool isNum(char s)
  202. {
  203. double result;
  204. return double.TryParse(s.ToString(), out result);
  205. }
  206. private static bool isOP(char s)
  207. {
  208. return s == '+' | s == '-' | s == '*' | s == '/' | s == '^' | s == '%' | s == '&' | s == '²' | s == '3' | s == '<' | s == '>' | s == '!' | s == '√' | s == 'π';
  209. }
  210. private bool isPunctiaton(char s)
  211. {
  212. return s == '*' | s == '/';
  213. }
  214. private bool isBracketOpen(char s)
  215. {
  216. return s == '(';
  217. }
  218. private bool isBracketClosed(char s)
  219. {
  220. return s == ')';
  221. }
  222. private NumStack getLastNum(string s)
  223. {
  224. var c = s.Split(Convert.ToChar((_stacks[_stacks.Count - 1] as OpStack).Value));
  225. return new NumStack(Convert.ToInt32(c[c.Length - 1]));
  226. }
  227. private List<Stack> dotbeforestatement(List<Stack> st)
  228. {
  229. var returns = st;
  230. for (var i = 0; i < st.Count; i++)
  231. {
  232. var s = st[i];
  233. if (s is OpStack)
  234. {
  235. var op = s as OpStack;
  236. if (isPunctiaton(Convert.ToChar(op.Value)))
  237. {
  238. if (st.Count < 3)
  239. {
  240. var firstop = st[i - 2];
  241. var first = st[i - 1];
  242. var ops = s;
  243. var second = st[i + 1];
  244. returns.Remove(first);
  245. returns.Remove(ops);
  246. returns.Remove(second);
  247. returns.Remove(firstop);
  248. returns.InsertRange(0, new[] { first, ops, second, firstop });
  249. }
  250. }
  251. }
  252. }
  253. return returns;
  254. }
  255. #endregion
  256. #region "Implementations"
  257. public override int GetHashCode()
  258. {
  259. return base.GetHashCode();
  260. }
  261. public Expression Clone()
  262. {
  263. return this;
  264. }
  265. public override string ToString()
  266. {
  267. return Result.ToString();
  268. }
  269. #endregion
  270. }
  271. }
  272. namespace Lib.Mathematics
  273. {
  274. public class CalcedStack : Stack
  275. {
  276. public NumStack first { get; set; }
  277. public OpStack op { get; set; }
  278. public NumStack second { get; set; }
  279. public NumStack result { get; set; }
  280. public CalcedStack()
  281. {
  282. first = new NumStack(0);
  283. op = new OpStack("");
  284. second = new NumStack(0);
  285. }
  286. public override string ToString()
  287. {
  288. return first.Value.ToString() + op.Value + second.Value.ToString();
  289. }
  290. }
  291. }
  292. namespace Lib.Mathematics
  293. {
  294. public class NumStack : Stack<double>
  295. {
  296. public NumStack(double v)
  297. : base(v)
  298. {
  299. }
  300. }
  301. }
  302. namespace Lib.Mathematics
  303. {
  304. public class OpStack : Stack<string>
  305. {
  306. public OpStack(string v)
  307. : base(v)
  308. {
  309. }
  310. }
  311. }
  312. namespace Lib.Mathematics
  313. {
  314. public abstract class Stack<t> : Stack
  315. {
  316. public t Value { get; set; }
  317. protected Stack(t v)
  318. {
  319. Value = v;
  320. }
  321. public override string ToString()
  322. {
  323. return Value.ToString();
  324. }
  325. }
  326. public abstract class Stack
  327. {
  328. }
  329. }

comments powered by Disqus