Click here to Skip to main content
15,895,777 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
{
    static class Testbed
    {
        public static void CreateFunctions()
        {
            Function.AddFunction("iscong", IsCongruent);
            //Function.AddFunction("primesto", CreatePrimesArray4);
            //Function.AddFunction("primes3",  CreatePrimesArray3);
            //Function.AddFunction("primes2",  CreatePrimesArray2);
            //Function.AddFunction("primeat2", FindPrimeAt2);
        }

        // FindPrimeAt will return a Token of TokenType.Vector which will indicate the prime at a given index
        // Originally implemented by c0dejunkie, refined by KashifImran  
        public static Token FindPrimeAt2(string operation, List<Token> arguments)
        {
            if (arguments.Count != 1 || arguments[0].TokenType != TokenType.Vector || arguments[0].Count != 1 ||
                arguments[0].FirstValue < 1 || arguments[0].FirstValue != (int)arguments[0].FirstValue)
            {
                return Token.Error("Invalid argument to function. Please provide a positive integer");
            }

            // an index of 1 will return 2 as the prime number
            int primeIndex = (int)arguments[0].FirstValue;
            int primeCount = 0;
            double primeAt = 1;

            if (primeIndex == 1)
            {
                primeAt = 2;
            }
            else
            {
                primeCount = 1;
                List<Token> testL = new List<Token>();
                Token primeTest = new Token(TokenType.Vector, 0);
                Token primeResult = new Token(TokenType.Bool, 0);
                testL.Add(primeTest);
                while (primeCount < primeIndex)
                {
                    primeAt += 2;
                    primeTest[0] = primeAt;
                    if (Numerical.IsPrime("isprime", testL)[0] == 1)
                    {
                        primeCount++;
                    }
                }
            }
            return new Token(TokenType.Vector, primeAt);
        }

        // CreatePrimeArray will return a Token of TokenType.Vector containing first 'n' primes
        // implemented by c0dejunkie, refined by Kashif Imran
        public static Token CreatePrimesArray3(string operation, List<Token> arguments)
        {
            if (arguments.Count != 1 || arguments[0].TokenType != TokenType.Vector || arguments[0].Count != 1 ||
                arguments[0].FirstValue < 1 || arguments[0].FirstValue != (int)arguments[0].FirstValue)
            {
                return Token.Error("Invalid argument to function. Please provide a positive integer");
            }

            int numPrimes = (int)arguments[0].FirstValue;
            double[] primes = new double[numPrimes];

            int count = 0;
            long i = 2;
            List<Token> args = new List<Token>();
            Token number = new Token(TokenType.Vector, i);
            args.Add(number);
            while (count < numPrimes)
            {
                if (Numerical.IsPrime("", args).FirstValue != 0)
                {
                    primes[count] = i;
                    count++;
                }
                number[0] = ++i;
            }

            return new Token(TokenType.Vector, primes);
        }

        public static Token CreatePrimesArray2(string operation, List<Token> arguments)
        {
            if (arguments.Count != 1 || arguments[0].TokenType != TokenType.Vector || arguments[0].Count != 1 ||
                arguments[0].FirstValue < 1 || arguments[0].FirstValue != (int)arguments[0].FirstValue)
            {
                return Token.Error("Invalid argument to function. Please provide a positive integer");
            }

            int numPrimes = (int)arguments[0].FirstValue;
            double[] primes = new double[numPrimes];

            Token primeIndxN = new Token(TokenType.Vector, 0);
            List<Token> primeL = new List<Token>();
            primeL.Add(primeIndxN);

            for (int i = 0; i < numPrimes; i++)
            {
                primeIndxN[0] = i + 1;
                primes[i] = Numerical.FindPrimeAt("", primeL)[0];
            }

            return new Token(TokenType.Vector, primes);
        }

        

        /* IsCongruent will return a Token of TokenType.Bool to tell whehter 
         * the first given number is congurment to second number modulu third number*/
        public static Token IsCongruent(string operation, List<Token> arguments)
        {
            if (arguments.Count != 3)
            {
                return Token.Error("Function expects three integers");
            }
            for (int i = 0; i < arguments.Count; i++)
            {
                if (arguments[i].TokenType != TokenType.Vector || arguments[i].Count != 1 ||
                    arguments[i].FirstValue != (int)arguments[i].FirstValue)
                {
                    return Token.Error("Function expects three integers");
                }
            }

            int firstNumber = (int)arguments[0].FirstValue;
            int secondNumber = (int)arguments[1].FirstValue;
            int thirdNumber = (int)arguments[2].FirstValue;

            //this needs to be calculated
            //bool isCongruent = false;

            //delete this line after providing proper implemenation 
            return new Token(TokenType.Text, "", "Not yet implemented. Would you like to work on it?");

            //un-comment the following line after providing proper implemenation 

            //return new Token(TokenType.Bool, 1, isCongruent? 1:0);
        }
    }
}

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