Adding Translations: Making your code work on the real chip - hpi-swa-teaching/Etoys-Calliope GitHub Wiki

What good is a simulation of a hardware chip, if what you're simulating is not working on the real thing?
On this page you'll find the necessary information to create translations for etoys.

The Player

It all starts out with the black button you see when you've opened a Calliope Mini.
Clicking this button calls the Players method "generateJSCode", which does just what the name says. This method is the key part in the translation: the logic. It opens a stream to a file in which you'll later find your JavaScript code. Afterwards it creates a dicitonary for all the triggers currently used by the scripts of the referenced player. The value of said dict being the script editors which have the key as their trigger. Then every key:value pair is translated.
The translation happends at the respective sites of the tiles we are visiting.

Tiles

Each tile in a script editor has its own type.
Some, like the CalliopeIconTile, are a subclass of TileMorph, making life very easy. We implement the method "generateJSCode" in the CalliopeIconTile class and then whenever the Tile is asked for its translation, it will be whatever generateJSCode returns. If the tile is a general TileMorph, we have to put its translation in the TileMorph Class.
The method generateJSCode here already implements translations for the basic functionalities depending on the tiles' type:
For example, we want to add the necessary brackets to numbers and strings if our tile is type literal.
If we encounter an object reference, we do not want to add any code. Right now it returns an empty string, which is something to be made better in the future.
If we encounter an operator, we want to add the respective prefixes for JavaScript and then add whatever the operator itself is after that.

PhraseTileMorph, TilePadMorph

These two are owners of TileMorphs and also implement "generateJSCode", in order to call said method on their submorphs.

ScriptEditor

This guy is responsible for translating the blocks of code it holds.
When we call "generateJSCode" on him, he will ask all of his *TileMorphs to give him their part of code, sewing it all together in the end.

ScriptInstantiation

Now, the translations for the triggers have to have their place as well. For this we use the fact that a script editor has an instance of ScriptInstantiation, whos status will tell us all we need to know. So ScriptInstantiation also holds a part of the final translation in the method generateJSCode.