Click here to Skip to main content
15,884,298 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 84.8K   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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathProcessorLib;

namespace MathProcessor
{
    public class KitsBase : Form
    {
        protected MainWindow parent = null;
        protected TextBox commandBox;
        protected Button goButton;

        protected void SetCommandBoxLocation(Point location)
        {
            commandBox.Location = location;
            goButton.Location = new Point(commandBox.Right + 1, commandBox.Top);
        }

        static KitsBase()
        {
            Application.EnableVisualStyles();
        }

        public KitsBase(MainWindow parent, bool createCommandBox)
        {
            if (createCommandBox)
            {
                goButton = new Button();
                goButton.Size = new Size(48, 23);
                goButton.Text = "Go";
                goButton.Click += new EventHandler(goButton_Click);

                commandBox = new TextBox();
                commandBox.Font = new Font("Microsoft Sans Serif", 12);
                commandBox.Size = new Size(380, 26);
                commandBox.Location = new Point(3, 40);
                goButton.Location = new Point(commandBox.Right + 1, commandBox.Top);
                this.Controls.Add(goButton);
                this.Controls.Add(commandBox);
            }
            this.parent = parent;            
            MaximizeBox = false;    
        }
       
        void goButton_Click(object sender, EventArgs e)
        {
            string expr = commandBox.Text.Trim() + ";";
            try
            {
                Token token = Calculator.ProcessCommand(expr);
                if (token.TokenType == TokenType.Error)
                {
                    MessageBox.Show(token.StrData, "Bad Input");
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, "Error");
            }
        }
    }
}

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