PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/NerdSharp.Net_Studio/NerdSharp_UberNet/Science/Math/MathCompiler.cs

https://bitbucket.org/pastageek/scide-cad-scilife
C# | 179 lines | 174 code | 4 blank | 1 comment | 0 complexity | 2667d1c065b1429604ea1c2442f2aa54 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Xml;
  11. using System.Xml.Linq;
  12. using System.Xml.Serialization;
  13. using System.Xml.XPath;
  14. using System.Numerics;
  15. //these libraries used for debugging purposes
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. using System.Windows.Shapes;
  25. namespace NerdSharp.Net_Studio.NerdSharp_UberNet.Science.Math
  26. {
  27. public static partial class MathCompiler
  28. {
  29. public enum CompileOptions { Method, Object }
  30. public static void CleanMethod(string methodBody)
  31. {
  32. }
  33. private static string pathToDecoder = @"";
  34. /// <summary>
  35. /// This method will transform/compile texCode as defined by Donald Knuth
  36. /// into Sci#.UberNet code. It can produce methods as well as objects for use
  37. /// inside other things. It adds a RAD aspect in a similar way that
  38. /// visual programming does.
  39. /// </summary>
  40. /// <param name="texCode">From Donald Knuth a type setting language
  41. /// that can be input to this method plus some directives to produce
  42. /// C# code from the tex input
  43. /// </param>
  44. ///
  45. /// <param name="outputType">Different uses of the compiler such as producing
  46. /// a method form an equation or a matrix object from a texmatrix
  47. /// </param>
  48. ///
  49. /// <param name="returnType">If compiling to a method, can have a return type.
  50. /// <code>If outputType == CompileOptions.Object</code>, then return type should be either
  51. /// <value>null</value> || <value>void</value> otherwise it will throw
  52. /// and invallid argument exception
  53. /// </param>
  54. ///
  55. /// <returns>Returns a string that is the C# code compiled
  56. /// from the input Knuth's tex</returns>
  57. public static string TexToSciSharp(string texCode, CompileOptions outputType, Type returnType)
  58. {
  59. #region local vars
  60. Token einToken = null;
  61. #region flags
  62. bool newToken = false;
  63. bool readingDirectives = false;
  64. bool lookingForSymbol = false;
  65. bool previousSlashToken = false;
  66. bool currentlyInSlashToken = false;
  67. #endregion
  68. char neededSymbol = ' ';
  69. char[] codeCharArray = texCode.ToCharArray();
  70. XPathDocument tokenXPthDoc = new XPathDocument(new StreamReader(@""));
  71. XPathNavigator xNav = tokenXPthDoc.CreateNavigator();
  72. List<Token> tokens = new List<Token>();
  73. StringBuilder tokenBuffer = new StringBuilder();
  74. StringBuilder methodBody = new StringBuilder();
  75. StringBuilder methodHeader = new StringBuilder();
  76. string method = "";
  77. #endregion
  78. CharacterItterator(ref einToken, ref newToken, ref previousSlashToken,
  79. ref currentlyInSlashToken, codeCharArray, xNav, tokens, methodBody);
  80. return method;
  81. }//end method
  82. private static void CharacterItterator(ref Token einToken, ref bool newToken,
  83. ref bool previousSlashToken, ref bool currentlyInSlashToken, char[] codeCharArray,
  84. XPathNavigator xNav, List<Token> tokens, StringBuilder methodBody)
  85. {
  86. for (long i = 0; i < codeCharArray.LongLength; i++)
  87. {
  88. if (!newToken)
  89. {
  90. if (char.IsLetter(codeCharArray[i]))
  91. {
  92. //works with the current token
  93. einToken.TokenData.Append(codeCharArray[i]);
  94. //needs to see if this token matches any
  95. if (true)
  96. {
  97. }
  98. }
  99. else if (char.IsWhiteSpace(codeCharArray[i]))
  100. {
  101. newToken = true;
  102. }
  103. }//end if newtoken
  104. else
  105. {
  106. newToken = true;
  107. einToken = new Token();
  108. if (codeCharArray[i] == '\\')
  109. {
  110. currentlyInSlashToken = true;
  111. einToken.TokenType = Token.TokenEnum.slash;
  112. tokens.Add(einToken);
  113. }
  114. else if (codeCharArray[i] == '+')
  115. {
  116. if (currentlyInSlashToken)
  117. {
  118. currentlyInSlashToken = false;
  119. previousSlashToken = true;
  120. }
  121. einToken.TokenType = Token.TokenEnum.symbOperator;
  122. tokens.Add(einToken);
  123. newToken = true;
  124. }
  125. else if (codeCharArray[i] == '#')
  126. {
  127. }
  128. }
  129. }//end main token creator for loop
  130. for (int i = 0; i < tokens.Count; i++)
  131. {
  132. if (tokens[i].TokenType == Token.TokenEnum.slash)
  133. {
  134. string nameSpace = xNav.GetAttribute
  135. ("", @"/functions/function[@texText=" + @tokens[i].TokenData.ToString()) + "]";
  136. methodBody.Append(nameSpace);
  137. }
  138. else if (tokens[i].TokenType == Token.TokenEnum.symbOperator ||
  139. tokens[i].TokenType == Token.TokenEnum.parens)
  140. {
  141. methodBody.Append(tokens[i].TokenData.ToString());
  142. }
  143. }//end token converter for loop
  144. }
  145. }//end compiler class
  146. public class Token
  147. {
  148. public enum TokenEnum { slash, symbOperator, parens, directive, variable }
  149. private TokenEnum tokenType;
  150. public TokenEnum TokenType
  151. {
  152. get { return tokenType; }
  153. set { tokenType = value; }
  154. }
  155. private StringBuilder tokenData;
  156. public StringBuilder TokenData
  157. {
  158. get { return tokenData; }
  159. set { tokenData = value; }
  160. }
  161. }
  162. }