Is it possible to export a DSL compiler created by JetBrains MPS and use it independently (e.g. invoke it from another Java program) - dsl

I'd like to build a DSL and use it as follows:
The DSL compiles to Java.
Export the DSL compiler and package it (i.e. as a JAR), so I can invoke the DSL compiler from a Java application to compile "code written in my DSL" into "Java source code" (I'll use other libraries to programmatically compile Java into bytecode).
Can I use JetBrains MPS to build a DSL and export its compiler as described? If no, other suggestions are appreciated?

I raised the question on MPS Support forum, and the answer I got was that it's not possible to export a compiler for my DSL (e.g. as JAR) from MPS IDE and then invoke the exported compiler from some Java application (think of a Java backend service) passing a text input representing a program written on my DSL.
Though you can use ant to invoke the "MPS code generator" (which is responsible for generating the target language code, e.g. Java, representing the input DSP program), but the generator expects as input "the MPS model" of your DSL program (I guess it's some AST like MPS internal representation of the DSL program). But the only way to generate "the MPS model" of your DSL program is by using Jetbrains' MPS IDE (or a stripped version of it, or intellij with a plugin for your DSL). In other words, the only way to write/edit programs in your DSL and be able to compile them, is by using Jetbrains MPS IDE (or one of its derivatives).
Link to the question I posted on MPS Support forum and the answer.

It seems to me your question is not so far from this documentation entry: https://confluence.jetbrains.com/display/MPSD32/Building+standalone+IDEs+for+your+languages

Maybe you cannot do it directly as a jar library, but it is possible, with some ant or gradle magic, to call a DSL compiler (or, as it's called in MPS, a generator) from an ant task. Documentation about this can be found at https://www.jetbrains.com/help/mps/building-mps-language-plugins.html#
I know it says building plugins but the same mechanism is used.
Why you would want to do this, though, eludes me, since the strong point of MPS is IDE support and very advanced multi-language integration, not necessarily code generation.

invoke the exported compiler [...] passing a text input representing a program written on my DSL
Your idea is sadly inherently flawed. There is no such thing as an MPS "DSL compiler" which takes text as input. In MPS there are generators which transform your DSL into another MPS language, in your case your target language would be BaseLanguage (MPS version of Java). After the transformation, the Java source code is generated as .java files and is automatically compiled as .class files. So yeah, this can be done with an Ant script built in BuildLanguage and called from cmd. But, the generator does NOT take as input text but an AST. The AST is your program "coded" (proper term would be modeled) in MPS.
So what you actually want is a parser (if your language is textual and parseable that is), which has text as input and AST as output. Once you have the AST in any form, you can somehow put it into an MPS model.
Please refer to my other answer where I commented on some portability (basically import, export) in MPS here. I have mentioned (not only) a project I am working on there. It allows to import a language and programs into MPS.
If you don't want to use MPS' IDE at all, but to work with text, it loses the advantage of MPS as a language workbench (LWB) with projectional editor. Maybe you should use another textual LWB (f.e. Xtext) or a parser generator (f.e. ANTLR). If the grammar definitions in parser generators scare you, you could use a model-based parser generator like YAJCo (I have contributed).

Related

What can I do with DSL languages generated inside JetBrains MPS?

I've just started a couple of hours ago reading about DSL modeling.
But right now, I'm tied to using the JetBrains MPS IDE or it's plugin for JetBrains Intellij Idea and I'd like to know how can I export those DSL models to something available to use for e.g. console applications or whatever (in case it's possible or it makes sense).
You can do several things already in MPS without exporting the models:
Analyze the models to check for errors, business rule violations or inconsistencies.
Interpret the models then display the result of the interpretation in MPS directly. Useful if you implement a specification and an example/test of that specification, then you can run tests in MPS and show the results as green/red highlight, for example.
Define a generator to translate the model into text (executable code or input for a tool such as Liquibase to create database schemas for example).
If you're looking to export your data from MPS for use in a different application there are two approaches I would
recommend:
The simplest way: NodeSerializer from MPS-extensions. I have more details on how to use it in a blog post. This lets you quickly export your data in a rather nice XML structure.
The most flexible approach: writing a custom exporter by using the MPS Open
API to recursively traverse a node
tree. You can output any format you want (XML, JSON, YAML, etc.) and customize the output as you like.
Here are two more approaches that you could be considering but that I would NOT recommend:
Accessing the model (*.mps) files directly. While they are already in XML format, their structure is adapted to
MPS' needs. It is normalized, meaning that a given piece of information is generally only stored once, and it also
encodes node IDs in a particular way to save space. The format is also undocumented and could change in the future
(although it hasn't changed for the past several years).
Using the MPS generator to convert your DSL to MPS' built-in XML language, jetbrains.mps.core.xml. I don’t recommend using the MPS generator because the generator’s sweet spot is translating between two different MPS languages, e.g. from your custom DSL to Java. If you try writing generator rules to convert anything to XML you would hit a few problems that are possible to overcome but totally unnecessary.
You can define a generator which transforms a sentence (file, AST) of your language into another MPS language. The target language must exist in MPS first.
Alternatively, you could generate text with the TextGen aspect, but that is more suitable to just print the textual representation of your language. If you would like something more sophisticated (like generating text code of another language), you can use plaintextgen language from MPS-extensions or mbeddr.platform.
If you want to input (import) a textual program into MPS , you can code a paste handler where you could put your parser, or you can change the format in which the AST is stored (from XML to maybe directly your language, but this would again require a parser to read) with custom persistence.
I am currently working on a solution which enables to import an MPS language from a YAJCo model (model-based parser generator, where the input is not a grammar, but Java classes representing the semantic model). Then you can import a sentence (file) which creates and populates a model (AST). From the program in MPS you can generate Java source code which fills the original Java classes. So if you want a textual MPS language and use the IDE but then export the AST into Java objects you can use, maybe YtM is for you.

Is it possible to use JetBrains MPS, or part of it, inside another application as JIT Compiler/Translator?

Does JetBrains MPS provide an JIT compiler which can be used inside other applications?
We have a legacy application with its on script language. Because this script language is very difficult to use to our customer, we would like to provide a new DSL to them.
So the question is: Can we use Jetbrains MPS to design our DSL and then use the MPS JITCompiler/Translator to transform it to Java or whatever after the user wrote his script in our Software?
If you mean by JITCompiler/Translator, to take your DSL generate Java from it and then run that compiled java code, yes that is possible. But it would be an extra transformation step like: write code -> generate/compile -> run (the resulting jar).
If you mean interpreting the model without doing a transformation step first then the answer is, not out of the box. We have build a interpreter framework for MPS and build two interpreters with it so far. One for Java and one for C. Though the focus is not on performance there. We use it for small calculations in formulas or REPL like things. It is currently work in progress but work quite nice. You can look here for Interpreter and find some more information and where to look. As a midterm project we might want to integrate this interpreter definition with the Graal compiler which would then be much more a JITCompiler then just a interpreter.

Is groovy native to JVM or ported to JVM?

I know Jython and JRuby is ported to JVM, and scala/Clojure is native to JVM, what about Groovy? Groovy looks like a dynamic language, I guess it is ported, but it seems it could also be compiled.
For those language native to JVM such as Scala, is that some tool to decompile the code to the source code?
"Ported" usually means "retargeted to run on." Groovy was designed to bring dynamic features from languages like Python and Smalltalk to Java. It was designed to be an extension of Java and in that sense it's native to the JVM and to the Java language. (The Groovy language, object model, and run-time libraries are extensions of Java's.)
But it sounds like you're asking about whether Groovy is interpreted or compiled. You can use groovyc to compile Groovy source code to Java .class files and run them in the JVM (linking in some Groovy run-time libraries). Or you can run Groovy source code interactively in GroovyShell, but what that does is compile, load, and run code for you incrementally.
A web search for [groovy decompiler] returns some possibilities for you.
I'm not sure whether it answers the entirety of your question, but the vast majority of Groovy and Groovy-Eclipse compiler is written in java, as seen on both projects' GitHub repositories.

JVM languages for J2ME platform

I'm currently writing an embedded application for J2ME environment (CLDC 1.1 configuration and IMP-NG profile). Being spoiled by all those new features in JVM-based languages (Groovy, Scala, Clojure, you name it), I was considering using one of them for my code.
However, most of the mentioned languages require pretty decent JVM environment. Most so-called "dynamic" languages require the VM to have reflection. Many ask for annotations support. None of the above features are available under J2ME.
From what I found, Xtend looks like a viable options, as its compiler spits out plain Java, not bytecode, and doesn't require any library for runtime needs. Of course, the generated Java code also must meet some requirements, but Xtend webpage looks promising in this regard:
Xtend just does classes and nothing else
Interface definitions in Java are already nice and concise. They have a decent default visibility and also in other areas there is very little to improve. Given all the knowledge and the great tools being able to handle these files there is no reason to define them in a different way. The same applies for enums and annotation types.
That's why Xtend can do classes only and relies on interfaces, annotations and enums being defined in Java. Xtend is really not meant to replace Java but to modernize it.
Am I right and it is possible to compile Xtend-generated code for J2ME platform, or there are some constructs that will not work there?
Alternatively, can you recommend any other "rich" Java modification language that can be run on J2ME?
Update: Knowing that the "compiler" producing results as another source code is called transcompiler, one can also find Mirah, a tool which requires no runtime library and specific Java features.
Xtend's generated code uses google guava heavily. If that is compatible to the J2ME, Xtend could be the language of your choice. I'm not aware of anything that prevents from using it on other platforms that provide a dedicated development kit (e.g. Android).
In addition to being able to generate Java source, Mirah recently added support for javac's --bootclasspath option, which allows you to generate your bytecode against a non-standard version of the java core classes, e.g. LeJOS.
It's still a little fresh, but it'd be nice to have more people using it on different javas.

Groovy And Groovy++,Are they different?

i recently came to know that groovy++ have been released, what is the major difference is in Groovy and Groovy++?
From the groovy++ page http://code.google.com/p/groovypptest/
Groovy++ is statically typed extension of Groovy programming language. Additionally to all goodies of standard Groovy it adds a lot of functionality
compile time checking of code
as fast as Java performance of compiled code
easy mixing of statically and dynamically typed code
very powerful type inference
tail recursion traits (interfaces with default implementation)
extension methods (compile time categories)
standard library of utilities for functional programming, concurrency and distributed computing (early prototype stage)
There is a good article on what Groovy++ brings to Groovy here. It includes benchmark results, so you can see the performance difference vs. Groovy and straight Java.
Groovy++ is an extension to the core Groovy 1.x language. You drop the GroovyPP.jar file into the Groovy library directory, add #Typed in front of the package keyword in your code, then run your code just as you would in core Groovy. It infers the types, AND runs faster!

Resources