MPS for dynamic languages

I am wondering if you have experience building dynamic languages support in MPS.

I think they bring some interesting challenges. For example I am thinking of python where you do not declare a variable, you just use it. In MPS I would see access to a variables as references to a declaration. However a declaration is not there, unless we count the first assignment as an implicit declaration. This makes also editing different because in Java when I declare a type a use a keyword (class), when I declare a variable I use a type to introduce the statement. However in Python I would just type a name, without a keyword to let MPS now I am about to declare a variable. Of course the user could select the element "VariableDeclaration" from the auto-completion menu, but I am not sure what would be the best strategy to make the editing experience nicer.

Any thoughts on that? Any project I should look at?

5 comments

This reminds me of how adding cells in the editor language works: I can type an alias, e.g. "[-" and I get offered whether I want to insert a collection cell or have "[-" be a constant cell. For strings that do not correspond to any known alias a constant cell is created automatically. So maybe the editor for a dynamic language could work similarly: when you type the name of a variable in scope the editor creates a "variable reference". If you type the name of an unknown variable, a "variable declaration" is created.

If declarations are shown in a different color than references, it would even alert the user when they make a mistake and declare a new variable instead of referencing an existing one.

0

To learn more about what Sergej has nicely described, you can check out the "languagePatterns" sample project, namely the "seamless-substitution" pattern.

As you can test in any of the sandbox "Request" nodes, e.g. "NewScreen", plain typing in the "description" will create a "StringDescription" node. The completion menu after typing will hold a corresponding entry:

The actual transformation to get inspiration from is defined in "PickTheRightDescriptionType" substitution rule.

0

Frederico,

I've just documented the case that you are describing, since it is not a rare need, in my opinion. You may check out the corresponding tip in the editor cookbook.

Vaclav

 

0

Thanks, that is great!

I am playing with the idea of building an editor for Python in MPS. However probably I should probably add implicit (and not visible) variable declaration and show just variable references.

Let's see what works :)

0

Not having explicit distinction between variable declarations and references can, indeed, be quite tricky. E.g. what should happen when renaming a variable in an assignment - shall all other references be renamed shall just this one change and possibly become a reference to another already existing variable?

I'm curious to hear from you what approached will have worked best for you.

0

Please sign in to leave a comment.