2

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?

Khaine775
  • 2,715
  • 8
  • 22
  • 51
mohaseeb
  • 389
  • 4
  • 8
  • Java itself needs to be compiled. Or you mean it compiles to Java byte-code? Because you need to be specific. Java is not interpreted, and Java 9+ doesn't really lend itself to JAR packaging anymore (since you're now supposed to use `jlink` to create a custom runtime image of your application). Also, I'm not your attorney and I can't legally indemnify you from any (possible) violations to the JetBrains license. Or the Java Compiler license. But I think you're okay with any OpenJDK compiler. If you want to use the Oracle release, I hope you have your checkbook ready. – Elliott Frisch Apr 16 '20 at 01:57
  • I expect the DSL compiler to compile from "a code written in my DSL" to "Java source code" (I'll update the question). I've seen other libraries that I can use to programmatically compile Java into byte code. And thanks for reminding to investigate possible license violations when using these tools. – mohaseeb Apr 16 '20 at 06:02

4 Answers4

1

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.

mohaseeb
  • 389
  • 4
  • 8
0

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

user1292456
  • 778
  • 4
  • 12
  • The document describe creating a standalone IDE for your DSL, and does not describe how I could realize my case where I'm not interested on creating an IDE, but on exporting just the DSL compiler part. I intend to the invoke the DSL compiler programmatically from within another Java application. Thanks though for sharing the link. – mohaseeb Apr 16 '20 at 12:33
  • Seems to me a "standalone IDE" is "another Java application" containing the "DSL compiler part". You probably '''just''' have to figure how much of IntelliJ will come as a dependency. – user1292456 Apr 16 '20 at 13:09
  • 1
    From the introduction section of that article: "The term standalone IDE refers to a stripped down version of the IDE that only contains those artifacts that are necessary for a given business purpose. Standalone IDEs provide a convenient way to distribute DSLs to the end users" It's talking about distributing a stripped down version of IntelliJ, not about programming your own IDE. – sepp2k Apr 16 '20 at 17:02
0

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.

Eugen Schindler
  • 351
  • 1
  • 6
0

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).

Hawk
  • 2,042
  • 8
  • 16