Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C#

Building a General Purpose Interpreter, Math Engine and Parser in C#

Rate me:
Please Sign up or sign in to vote.
4.97/5 (70 votes)
13 Feb 2015GPL313 min read 85K   7.9K   224  
An easy to understand and use language interpreter, run time parser and calculation engine from scratch in C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MathProcessorLib
{
    public static class LogsAndPowers
    {        
        public static void CreateFunctions()
        {
            Function.AddFunction("lg",  FindLogExponent);
            Function.AddFunction("ln",  FindLogExponent);
            Function.AddFunction("exp", FindLogExponent);
            Function.AddFunction("pow", CalculatePowLog);
            Function.AddFunction("log", CalculatePowLog);
        }
     
        static Token FindLogExponent (string operation, List<Token> arguments)
        {
            if (arguments.Count != 1)
                return Token.Error ( "Function expects single parameter");
            Token result = Token.Error ( "Argument not numeric");
            if (arguments[0].TokenType == TokenType.Vector || arguments[0].TokenType == TokenType.Matrix)
            {
                double[] resultVector = new double[arguments[0].Count];
                switch (operation)
                {
                    case "lg":
                        for (int i = 0; i < resultVector.Count(); i++)
                        {
                            resultVector[i] = Math.Log(arguments[0].VectorArray[i], 2);
                        }
                        break;

                    case "ln":
                        for (int i = 0; i < resultVector.Count(); i++)
                        {
                            resultVector[i] = Math.Log(arguments[0].VectorArray[i]);
                        }
                        break;

                    case "exp":
                        for (int i = 0; i < resultVector.Count(); i++)
                        {
                            resultVector[i] = Math.Exp(arguments[0].VectorArray[i]);
                        }
                        break;
                }                
                result = new Token(arguments[0].TokenType,arguments[0].Extra, resultVector);

            }
            return result;
        }

        static Token CalculatePowLog(string operation, List<Token> arguments)
        {
            if (arguments.Count < 1)
                return Token.Error ( "No arguments passed.");

            Token result = Token.Error ( "Argument not numeric");

            if (arguments.Count == 1 && operation == "log")
            {
                double[] values = arguments[0].VectorArray;
                for (int i = 0; i < values.Count(); i++)
                {
                    values[i] = Math.Log10(values[i]);
                }
                result = new Token(arguments[0].TokenType, arguments[0].Extra, values);
            }
            else if (arguments[0].Count >= 1 && arguments[1].Count == 1)
            {
                double[] values = arguments[0].VectorArray;
                for (int i = 0; i < values.Count(); i++)
                {
                    if (operation == "log")
                    {
                        values[i] = Math.Log(values[i], arguments[1].FirstValue);
                    }
                    else
                    {
                        values[i] = Math.Pow(values[i], arguments[1].FirstValue);
                    }
                }
                result = new Token(arguments[0].TokenType, arguments[0].Extra, values);
            }

            else if (arguments[0].Count == 1 && arguments[1].Count >= 1)
            {
                double[] values = arguments[1].VectorArray;
                for (int i = 0; i < values.Count(); i++)
                {
                    if (operation == "log")
                    {
                        values[i] = Math.Log(arguments[0].FirstValue, values[i]);
                    }
                    else
                    {
                        values[i] = Math.Pow(arguments[0].FirstValue, values[i]);
                    }
                }
                result = new Token(arguments[1].TokenType, arguments[1].Extra, values);
            }

            else if (arguments[0].Count > 1 && arguments[1].Count > 1)
            {
                double[] resultVector = arguments[0].VectorArray;
                double[] inputVector = arguments[1].VectorArray;
                if (resultVector.Count() != inputVector.Count())
                    Token.Error ( "Argument mismatch");

                for (int i = 0; i < resultVector.Count(); i++)
                {
                    if (operation == "log")
                    {
                        resultVector[i] = Math.Log(resultVector[i], inputVector[i]);
                    }
                    else
                    {
                        resultVector[i] = Math.Pow(resultVector[i], inputVector[i]);
                    }
                }
                result = new Token(arguments[0].TokenType, arguments[0].Extra, resultVector);
            }
            return result;
        }        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Technical Lead https://mathiversity.com
Unknown
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions