📜 ⬆️ ⬇️

FiM ++

image

Once at a lecture, a friend told me that there is a pony programming language (based on the series, of course). He said that he saw it on reddit, but even after a long search I could only find an empty repository on Google Code without implementation details. I was very disappointed with this, but only for a few minutes, until I realized that I could write the implementation of this language myself. Perhaps it will not be good or generally used in the sense that not everyone can write an interpreter for it, but I wanted to test my skills. And I also like procrastination jokes.

So, I present to your attention a rather crude version of the syntax of what I call FiM ++.

FiM ++ is a dynamically typed, interpreted, object-oriented and Turing-full language for writing letters to Princess Celestia on behalf of any of the ponies (I used Twilight for my specification). Functions in the language are formatted as paragraphs of the letter. This is done so that the code can be easily read and be as close to the letter as possible. Each such letter is a class, so each program can consist of several letters if the user wants it. However, the minimally interpretable unit is a class, almost like in Java. The language does not support libraries, since we are sending letters, which means the code should be as easy as possible. The desired data types can be user defined.

Each class begins with a heading that is always the same:
Dear Princess Celestia: Letter One 

')
Operator brackets for parts of the code were removed, because they violate the letter format, but can be used if the user desires. They will not be mentioned further, indenting is used instead, almost like in Python. “Princess Celestia” is a reserved keyword for class creation and therefore must always be the first recipient. 'Letter One' is the name of the class.

Interfaces can be added to the first line of the class header, separated by commas:
 Dear Princess Celestia, and Princess Luna, and Princess Cadance: Letter One 


Each interface is defined by paragraphs (functions) that must be inside a class. An ordinary class is called like this:
 I wrote you another letter! It's titled Letter Two. 


'Letter Two' is now an illustrated object and can be used as such; for example, it can be used to transfer a function from a new class to the current one. It can also declare an object for further use in the code:
 I wrote you another letter! // code // code Oh, about that other letter. It's titled Letter Two! 


The “main” function looks like this:
 Today I learned // code // code Your faithful student, Twilight Sparkle. 


It never accepts arguments, such as, for example, command line arguments. Returning a value consists of the string "Your faithful student," and an argument, which is the name. Returning a value may not be necessary, depending on the interpreter.

Any other function looks like this:
 I learned about ducks // code // code 


It is called by name, in this case 'friendship'. Returning values ​​is not strictly necessary and can be ignored. Of course, the function can be called with parameters:
 I learned about friendship with Spike and Rarity // code // code 


Spike and Rarity can now be used inside a function. All arguments are passed in a magical way (just in case).

The keyword “I” works like the “self” operator in most languages. However, he refers not to the copy of the class, but to the writer of the letter. It can be defined anywhere.

Assigning a variable looks like this:

 Did you know that Spike is a dragon? 


In this example, we assign the string value “dragon” to the variable “Spike”. Spike could also have any other value, and the exact data type is determined at runtime.

Despite the fact that the language is dynamically typed, certain checks are still performed on operations with two variables. If someone writes something like “Pinkie Pie is going on a date with a number”, the interpreter will have to throw an exception, in this case RidiculousException.

Cycles in FiM ++:
 I did this 100 times… // code // code 

This should be enough to write an interpreter for a given language. And one more thing: the reserved keyword "friendship" should always be interpreted as "magic", otherwise you risk breaking the foundations of the rule of the Universe in which Equestria is located.

Hello world

 Dear Princess Celestia: Hello World! Today I learned how to say Hello World! I said “Hello World”! That's all about how to say Hello World! Your faithful student, Twilight Sparkle. 


Links

Document describing current language specification
Simple JavaScript interpreter
Sample Code: 99 Jugs of Cider

Source: https://habr.com/ru/post/155131/


All Articles