How I built a programming language: The Outcome

Yashrajvishwakarma
4 min readJul 18, 2023

--

I mean, it’s not as elaborate or fancy as what one uses, like Python, C or Java. It’s what one would call a tiny programming language, one which has limited functionality, but works nonetheless.

But it works (pretty decently), handles errors, and serves a purpose.

Photo by Markus Spiske on Unsplash

What does it do?

I’m a big maths person. But in terms of programming languages specifically for maths, I couldn’t find much aside from MATLAB and Mathematica, both of which are paid software. And even in the popular languages, such as Java & Python, you need to go through the learning curve and learn all the basic syntax before getting to the libraries which you can use for math operations.

While GNU Octave is a language which is maths-oriented and free-to-use, the syntax can seem a little daunting at first. As a result, I envisioned a free-to-use programming language which is as simple as possible to the point where anyone could determine what each line of code does.

Getting started (not quite)

I soon realized that everyone doesn’t just cook up a programming language because well, it’s hard.

For one, you need to define the syntax of the language; variable names, literals, data types and functions.

Once you get past that barrier, you need to do the following:

  • Develop a lexer: A lexer is a simple tool that reads the code you’ve written and splits it up into tokens, which are the parts of your syntax mentioned above.
  • Develop a parser: A parser is the tool which takes the tokenized data, and utilizes it in a logical manner by inserting them into an abstract syntax tree (AST). (I’m not going to explain every single concept that went into making this, because 1. I don’t have the time or energy for it and 2. I’m not qualified enough to).

Because I chose to build an interpreted language rather than a compiled one (because building a compiler using LLVM or libgccjit is not easy), the rest was rather straightforward. However, the three steps above were quite the challenge.

I chose to use Python because of my experience in it, but in the future I’ll hopefully port the source code over to a language such as C or C++, which is ideal for interpreted languages.

About my language

Based on inspiration from a friend, I chose to name it MANGO (Mathematical and Analytical Nucleus for Global Operations).

You can define upto two variables; one is a function named func, and the other is a floating-point number called a (for the latter, the name is arbitrary).

In the language, there’s some predefined functions for both types of variables, which I’ve outlined below:

differentiate(func) %% returns the derivative of the function

Oh yeah, in MANGO, comments are given by using a %%.

The function differentiate(func) does what you expect; it gives you the derivative of the function. For now, it’s restricted to only polynomials, but in the future, I aim to expand it to all types of function (logarithmic, exponential, and with multiple variables).

Next up is:

integrate(func, A, B) %% return integral

Once again, the above function’s purpose is rather obvious: it computes the integral of the function “func” from x = A to x = B. However, unlike the previous one, this function works for all single-variable functions (i.e.: greater usability).

det(A, B, C, D) %% return determinant

Out of all the functions in this language, det() is probably the least interesting one, as for a 2x2 matrix, it computes the determinant, which as you all know is just ad — bc. In the future, however, I’ll be looking to add an extra parameter relating to the dimensions of the matrix, so that one can easily compute the determinant of matrices of any size.

graph(func, A, B)

This function graph() allows you to sketch any function between the intervals A and B. Pretty straightforward, really. If you were to do this in Python for example, you’d need to first learn Python then learn matplotlib to do this. On the other hand, MANGO makes graphing functions a trivial activity.

That’s it for now for the functions related to the analytical side of maths. I’ve also added some functions with operate on a single numerical variable a.

double(a) %% return 2 * a
square(a) %% return a * a
cube(a) %% return a * a * a
exp(a) %% return e ^ a
log(a) %% return ln(a)

Once again, this is quite trivial, and accentuates the purpose of the language as a whole: simplicity, but taken to the next level.

Error handling

One of many limitations of the language is the lack of error handling, whether it be a logical one or syntax-related. However, it is there to an extent. For one, you must close all functions with correct parentheses, otherwise an error message will be returned. Also, the language returns appropriate messages for incorrect spelling of functions, as well as an incorrect number of parameters (this one is still under development).

Using the language

In the foreseeable future, I’ll be releasing the documentation and installation process for MANGO.

All MANGO program files end with the .mgo extension. In order to run a program, you need to ensure that the .exe file installed is in the same directory as the program file(s), and then use the command line to run the executable file. In a matter of moments, the output for your .mgo programs will be returned to you.

Moving forward from here

This has been one of the most challenging, yet rewarding things I’ve ever worked on. In the next months (and maybe even years), I hope to extend the features of the language by including more advanced logical operators & mathematical functions while maintaining its ethos. Additionally, I’ll look to resolve any prominent bugs in the language’s syntax and maybe even bring on some more people to help in developing it further.

--

--