PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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