I had to design a simple general purpose language for university, so I tried creating "ZoomerScript" with Jetbrains MPS

prof@infosec.pub to Programmer Humor@programming.dev – 507 points –

Insert <it's not much but it's honest work> meme. It only supports ints and bools, some logic and simple arithmetics and it compiles to Java but damn was it hard to get that far.

Can you guess what everything does?

62

It only supports ints and bools, some logic and simple arithmetics and it compiles to Java but damn was it hard to get that far.

I have a bachelor’s in computer science and I don’t think I would be able to do that…

Compiler courses are typically master level.

My college must have been full of sadists. They had undergrad compiler courses and required students to take them.

Same, it usually whacked about half the attempted majors into another major. In the first half of senior year. They kept wondering why their program wasn't growing much even though similar colleges' programs were growing like mold on a dorm shower curtain. I enjoyed the course and never used the primary skills taught in it again.

It really depends on what you do, but somehow, I actually did end up using some of the things those courses were teaching. It turns out the visitor pattern is extremely useful for writing JavaScript code transformers.

I had an entry-level compiler course during undergrad. We used JavaCC to generate a lexer and parser based on eBNF grammar.

Im having a mandatory compiler course on my 3rd year of (french) bachelor

I definitely read this as you were a third year French major being forced to taking a compiler course for a moment and went wtf. Then my brain slapped itself and realized you mean you're a student in France.

Haha yeah I meant that I was studying computer science im France and not studying french. Here in France, bachelor are called licence and are only 3 years compared to 4 in north america afaik, but there are way fewer electives

Nah, I think they’ve won the French version of The Bachelor 2 years in a row and are about to win the third.

It was part of my BSc, but that was over 20 years ago.

Compilers are a specialized topic - and syntax design is fiddly - but it really is no harder than any other sort of program. A lot of the hard theoretical work was done back in the sixties and seventies. You don't have to start from scratch. These days it's "only" a matter of implementing the features you want and making sure your syntax doesn't leave itself open to multiple interpretations. (just as arithmetic, e.g. '5 × 4 - 1' requires some rules to make sure there's only one correct interpretation, so do language syntaxes need to be unambiguous to parse. )

Don't get me wrong - writing a language is a lot of work and it's super cool that OP has done this! I just want to stress that language development is 100% doable with an undergrad degree. If you understand recursion and how to parse a string you already have all the theory you need to get started.

Valuable input! I actually am an undergrad student. There are a lot of frameworks out there that support writing languages, with MPS being one of them.

If I'd start from scratch again and had a little more time, I'd frankly try writing an interpreter myself, instead of trying to conform to weird framework syntax, which I won't be able to reuse in any other context.

Saying syntax design is fiddly is an understatement. I focused very hard on getting an abstract syntax somehow finished before working on generation in my first iteration. Then I had so much technical debt, that I couldn't get anything to work and had to rewrite a lot. So I scrapped it all and started again, starting with top level concepts including generation and only implementing some lower level ones, once everything around it worked properly.

1 more...

It’s likely transpiring and not compiling, so it’s a lot easier than it seems. Source: made a language that adds features to Python and transpiles to valid Python.

It doesn't compile or transpile in actuality. It generates Java based on an abstract syntax tree. The concrete syntax is not considered in Java generation by MPS.

2 more...
class Scratch {
  // Start of file

  public static void main(args: string[]) {
    int number1 = 2;
    number 1 = 10;
    int number2 = 13;
    boolean fo_sure = true;
  
    if (fo_sure) {
      number1 = number1 + 5 - 10 * 2 / 3;
    }
  
    System.out.println(number1);
  
    boolean canYouSeeMee = false;
    System.out.println(canYouSeeMe);
    if (false) {
      canYouSeeMe = false;
    } else {
      canYouSeeMe = true;
    }
    System.out.println(canYouSeeMe);
  } 
}

What’d I win?

I find it interesting and unnerving that I understood the code, but not the youthspeak.

Well done, here's your price: 🏅

You may redeem it for a star on a GitHub repo of your choice.

It all gets put into the main method though in this version 😄

canYouSeeMe = !canYouSeeMe;

There are even more optimization possibilities, but I wanted to stay as close to the original as possible.

Yeah, it can be optimized down to a single constant print statement.

Vibe check is your scope declaration (class?)

num is obviously your int class

fr? is your Boolean class

if __ no cap is ' if __ then', if cap is 'else', sheesh ends the conditional

flex __ on the haters is your echo/print

frfr is your scope ending

Correct!

Vibe check is pretty much the scope. Classes aren't a thing (yet).

It's so beautiful!

Now I'm thinking about how to alias "flex X on the haters" into other development environments...

Is flex X on the haters a way of logging to console?

Yes, it pretty much just wraps the expression in a "System.out.println();"

Maybe "flex X" outputs to stdout and "flex X on the haters" outputs to stderr?

I like the way you think! 😂

flex X on the fools does verbose logs only.

I'm curious to know how your language throws and catches errors :)

People are designing languages with JetBrains MPS and I am making an AWK interpreter in C with Yacc, Lex and my own implementation of ASDL. Why do I do this to myself? It seems like the technology I like is way behind. Like C is a language created for freaking mini-computers like VAX and PDP-11 and I still use it? I knew about MPS, I just felt a strong dislike towards it. Now that I am no longer a pill addict, I have to reconsider the technology I choose to implement my stuff. C lacks portability, and QoL features.

(Psyche! All you n000bz can be stuck with your Fischer-Price toys --- I'll do it MY way, the 70s WAY!!!!1)

We think alike friend

Don't lose your passion, doing things the shit old way can also make you a better programmer in the newer paradigms

Although, I recommend you at least learn C++23

Reading this comment and then looking up and seeing that your username ends with PDP11 was *chef’s kiss*

It’s really cool, but the example doesn’t produce any sensible output? If you have created something like this, why wouldn’t you have your demo output something sensible like Fibonacci or 1337 or whatever.

Great idea if I have to extend it

I'm opening an issue on your ticket tracker to add filesystem i/o. Let the nightmare commence

Oh metaprogramming! I'm doing a dissertation on this.

Very cool, I'd be interested in your publications once you're done. I like metaprogramming, but once you realise you might have needed it, you're already knee deep in fresh legacy code.

You essential have a compiler written through metaprogramming. For your implementation, did you use a find and replace or did you define and parse a grammar like a true compiler.

MPS uses projectional editing. Which means for the user that everything you do is free from concrete syntax, and you basically edit a graphical representation of that abstract syntax tree directly, while it looks like you're in a textual editor.

So I define abstract nodes that may have certain relationships with each other and then give them a representation in the editor (which is what you see in the screenshot). These nodes may also have generators assigned to them, which use map/reduce operations to generate whatever source code I desire. It usually includes its own bit of code, and triggers code generation of its children as well.

I hope that was somehow clear 😄

I swear, Zoomers are like the steve buscemi "fellow kids" meme, but somehow everyone in the scene is young

Anyway, nice compiler. Might feel basic to you, but writing a back end for a low level IR format is not that much harder.

It only supports ints and bools, some logic and simple arithmetics

That’s more than you need.

Why aren’t the booleans like “facts” or “no cap” for true and cap for “false”?

Also, you could have exceptions be called “Sus”

Because it was easier to use Java primitives than implement the constants myself.

Looks like you've got a bug in there.

if false no cap
    canYouSeeMe = false
if cap
    canYouSeeMe = true
sheesh

Won't this always go into the else/cap condition since the if condition is checking to see that false == true?

You're correct, but it doesn't really matter for demo purposes. In an actual use case (whatever that would be for this language) you would of course want to use some kind of variable or expression there instead of a constant.

No cap is cracking me up. This is great stuff

Jetbrains MPS?

It's a tool for designing domain specific languages. Really interesting!

Does it compile into JVM bytecode or Java source code?

JVM bytecode is one of the most infuriating IRs I ever had the displeasure to work with, and if you managed to make a compiler for that, I applaud you.

Fortunately I generate Java source code from it. However MPS generates both source and byte code when you build the solution. For some reason I can't get the byte code to run though, but the source code does, so I don't care too much.

That sounds about right for JVM bytecode... In any case, great work!