I have created a programming language for fun

Perhaps you will remember my last written article in which I explained why I have had created a video game for a very old computer using Assembler. If not, or you haven’t read it yet, here is the link.

Boredom is a great motivator, especially on weekends. You feel tired to try outdoor stuff and, as in my case, you don’t have TV (Well yes, I have one, but I only use it for Netflix… and I already saw all of it). In any case, be bored was always a great motivator, especially if something in the near future will cause me more boredom.

Coding repetitive code is, usually, one of those things. Once you did a connection to a database or iterate through an array or write something in the browser, the second time started to look quite similar than the first one, the third one is even more alike and so on.

When you did thousands of these operations like in my case, considering that I wrote my first line of code when I was 6 years old (42 now)… for sure you would pay whatever for something that give you the same result with a simple mouse click. And that is the idea behind Macro, this new programming language.

But, first some clarifications, in particular for those that don’t know me: I am not fan of having new programming languages emerging every day. Cannot negate a good quota of evolution and new ones to solve specific domain problems, but just that. Having tons of new ones, including frameworks, dependencies, tools and libraries makes the life of a developer very difficult. But, as I said, there are cases in which a domain specific language is required and very appreciated, so welcome those. In the other hand, again, do not misunderstand me, I am not an old dinosaur that feels threatened by new things. I just learnt to take the needed time to analyze, in deep, the benefits of the new language or tool. So, welcome the new, but…

But, Macro, is one of those languages that no one asked for, but is fun if you built it because the challenge (And boredom… I already mentioned that?). So, what is this programming language?

No alt text provided for this image

Macro can be considered a “scripting” language. You can write a script based on keywords (Statements) and a parser will read each line executing the expected actions.

One of the key concepts that I like to implement when I code for fun is to push boundaries a little bit. If we think on the most common programming languages the grammar, syntax and semantics are part of the language (Which is perfect, of course). But you cannot go further and modify the language (There are languages in which you can, of course) to do something else, or even better, become something else after the modification. So, for Macro, I tried to implement an architecture that allowed the modelling of the language as the developer would like to be (Part of the reason for its name: Macro). As in the case of one old videogame framework for JavaScript and HTML5 that I did years ago, the main idea was to allow third party components to be plugged in into the framework. Let face it, being the solely developer of a videogame framework could be difficult to implement all the small pieces by your own. In any case, Macro followed similar principles: Allow the user to model the language and, even, create ports for other existent languages.

Yes, I know that sounds like big stuff, lot of lines of code, but I just created the first alpha after 20 hours of work. Is more a concept than a reality (Remember, this is not a language that you really want to use). So, as I said: is a scripting language, and can be modeled.

For modelling the language, you requires a file with the language structure:

# macro lang definition
# * => text (any type)
# ? => expression (operations, variables)

# NetCore requires Macro.NetCore.Package.dll 
> negotiator	NetCore

> lang
print *
print ?
get * => create ?
send ? to *

The definition has two parts. The “negotiator” which is a component created using .Net Core 3.1 that acts as bridge between the language interpreter and any other way to resolve the language. To clarify this better, think that the different statements of the language doesn’t belong to it but a third party service that you can plug in and use. By default I built a simple one that use, as Macro, .Net Core 3.1 to process each statements. This means that “print”, “get”, “create”, “*”, “?”, “send” and “to”, each one of these token (I will continue using “statement” instead of token) are inside of “Macro.NetCore.Package.dll”. We could say almost the whole language is there. But, as I said, you can change this, write your own language package and make the “print” statement instead of showing something in the console, send that information to the printer. Or, you could change the language at the point that “print” is now called “display”.

This bridge can be implemented in other language. Let say that you love Python and prefers to implement the each sentence in it. Macro should allow you to, with the proper bridge, use Python at the moment that the code is parsed.

And here is where I tried to push the boundaries. There are other languages such as Robot Framework that does or behaves in similar ways (If you go deeper, you can see Chez Scheme). But in the case of Robot Framework, is a domain based language (Which is awesome by the way). In comparison with Macro, you could implement whatever you would like providing to the user a natural language writing approach.

There are more beneath all of what I have said and don’t want to do a long post about this language that just did its first steps. Nevertheless, as final words, let see what you could do with the previous definition that we made.

get http://jsonplaceholder.typicode.com/posts/1 => create result
print result

As result:

No alt text provided for this image

I will release the source code when consider that is mature enough to play with it but, if you would like to participate or think this is a good opportunity to read the code, let me know.