[JAVA] Run JVM-Math-Language as a Gradle project at hand

I will write an article for the first time in a long time.

TCG has no energy, so I may not write it for the time being.

Almost useful sentences are [Method 2] Specify the processor from the settings. ](# Method (2) Specify the processor from the settings.), So skip it.

Abstract

I dropped the JVM-Math-Language and ran it.

~~ It could be executed with the -processor option at compile time. ~~

When Annotation Processor was set from the settings, the source code was associated with the project.

moved.

Motivation

I wanted to create a programming language that runs on GraalVM using Truffle.

Graal's SL wasn't as simple as I expected, so I decided to implement my language based on a simpler interpreter.

-Graalvm / simplelanguage

Truffle Tutorial Slides

I also wrote a little translation of the slides before.

Truffle Tutorial Slides Personal Translation Memo ①

What is JVM-Math-Language?

JJUG CCC 2017 Fall I'll try to make an oleore JVM language (just do four rules), which was introduced in the session. A programming language created with ANTLR + Truffle.

Jyukutyo / JVM-Math-Language

An AST interpreter with Truffle. This program can execute four arithmetic operations. You can use numbers, +, -, *, /, "()", and "->" for running in another thread.

It seems that it is Truffle's AST (abstract syntax tree) interpreter that can perform four arithmetic operations.

Creating a project

Create a project.

In the Gradle project, the compiler uses JDK 1.8 I used it.

The IDE uses IntelliJ.

I will omit the method, but I wrote a similar article before, so I think you can look at it.

Then, zip the project folder from GitHub. (Clone or Download)

After unzipping, drop the src folder directly under the project.

JVM-Math-Language is like a Maven project, so you need to write build.gradle.

As an example, I roughly ported pom.xml of JVM-Math-Language (dependances written only) ) Leave him alone.

build.gradle


plugins {
    id 'java'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    //Needed to run ANTLR
    compile group: 'org.antlr', name: 'antlr4-runtime', version: '4.7'
    //Needed to run Truffle
    compile group: 'org.graalvm.truffle', name: 'truffle-api', version: '1.0.0-rc12'
    //Required to generate code from annotations
    compile group: 'org.graalvm.truffle', name: 'truffle-dsl-processor', version: '1.0.0-rc12'
}

As an aside, it seems that if you use Gradle nicely from the console, you can generate a project (and of course build.gradle) from pom.xml.

-Convert Maven Pom to gradle build

Specifically, enter the following command in the directory where pom.xml is placed (it will be the project directory).

gradle init --type pom

Compile error

By the way, for the time being, about 6 compile errors occur at the place such as build .....

By the way, there is also a red line.

When I check it, it seems that the following classes are missing.

・ Jvmmathlang.truffle.JvmMathLangTypesGen

・ Nodes.ops.AddNodeGen (Package omitted below)

・ DivNodeGen

・ MulNodeGen

・ SubNodeGen

~~ What is Gen class?

These classes use the Truffle DSL Processor using the JSR 269: Pluggable Annotation Processing API. Seems to be the code that is automatically generated by.

Roughly speaking, if you create an abstract class with a specific annotation, its implementation will be generated automatically.

Now, there is a compile error because nothing is set around the generation.

Now let's set up the annotation processor.

Also, regarding the Annotation Processing API, the following article was easy to understand.

Pluggable Annotation Processing API Usage Memo

** Method ① works only with CompileJava, but CompileJava seems to succeed in compiling without specifying any options. (That is meaningless) **

** However, I had a hard time getting to the article that explains the -processor option and how to set it with gradle, so I will leave the text itself. ** **

~~ Method (1) Specify with the compile option. ~~

~~ The Java compiler has an option to compile with a specified processor class. ~~

javac -processor fully qualified class name,Fully qualified class name,Fully qualified class name

~~ Also, in the Gradle project, compile options are described in the build script. ~~

~~ ・ The story I was addicted to when automatically generating with an annotation processor #Source code automatic generation ~~

build.gradle


compileTestJava.options.compilerArgs += ['-processor', 'Fully qualified class name,Fully qualified class name,Fully qualified class name']

~~ Here, one problem arises. ~~

~~ I don't have a list of processor classes. ~~

~~ But fortunately it was right at my fingertips. ~~

There is a file called javax.annotation.processing.Processor in META-INF-> service in ~~ truffle-dsl-processor-1.0.0-rc12.jar. ~~

~~ There is a complete list of class names in it. ~~

:javax.annotation.processing.Processor


com.oracle.truffle.dsl.processor.TruffleProcessor
com.oracle.truffle.dsl.processor.verify.VerifyTruffleProcessor
com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor
com.oracle.truffle.dsl.processor.InstrumentRegistrationProcessor
com.oracle.truffle.dsl.processor.InstrumentableProcessor
com.oracle.truffle.dsl.processor.verify.VerifyCompilationFinalProcessor
com.oracle.truffle.dsl.processor.OptionProcessor
com.oracle.truffle.dsl.processor.interop.InteropDSLProcessor
com.oracle.truffle.object.dsl.processor.LayoutProcessor

~~ Apply this to build.gradle earlier. ~~

build.gradle


//Abbreviation
compileJava.options.compilerArgs += ['-processor', 'com.oracle.truffle.dsl.processor.TruffleProcessor,' +
        'com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor,' +
        'com.oracle.truffle.dsl.processor.InstrumentRegistrationProcessor,' +
        'com.oracle.truffle.dsl.processor.InstrumentableProcessor,' +
        'com.oracle.truffle.dsl.processor.verify.VerifyCompilationFinalProcessor,' +
        'com.oracle.truffle.dsl.processor.OptionProcessor,' +
        'com.oracle.truffle.dsl.processor.interop.InteropDSLProcessor,' +
        'com.oracle.truffle.object.dsl.processor.LayoutProcessor']

~~ You can do it from Gradle-> Tasks-> compile Java in the sidebar. ~~

~~ Also, if you write all the class names in Gradle, the visibility will be bad, so ~~

You can omit the compile option by bringing javax.annotation.processing.Processor to ~~ src-> main-> resource-> META-INF-> services. ~~

Method (2) Specify the processor from the settings.

Yes, this is my favorite.

Open File-> Settings-> Build, Run, Deploy-> Compiler-> Annotation Processor.

Select Default and check Enable annotation processing.

In Save generated source associations in the following location, select the module content root.

This will generate code in src / main at build time.

However, the generated and generated_tests folders are not yet recognized as sources.

Therefore, edit build.gradle so that it will be recognized as a source folder.

· Reverse Manual: How to Use Annotation Processors in IntelliJ IDEA

build.gradle


apply plugin: 'idea'

idea {
    module {
        sourceDirs += file('src/main/generated')
        generatedSourceDirs += file('src/main/generated')
        testSourceDirs += file('src/test/generated_tests')
    }
}

I think that all the errors are gone.

Finally

It took about a week and a half to reach the setting called method (2), which seems to be completed in 30 seconds.

If you have such a thing, please write an article and let me know.

Let's reduce the number of people who have the same difficulty!

P.S.

Net source self-study is always tough

Recommended Posts

Run JVM-Math-Language as a Gradle project at hand
Build a Java project with Gradle
Run modular project tests in Gradle (JUnit5 + TestFX)
Run a Spring Boot project in VS Code
Run JUnit and Spock in a maven project
Java tips-Create a Spring Boot project in Gradle
Add a project in any folder with Gradle
Run autossh as a systemd service on CentOS
How to run the SpringBoot app as a service
Java11: Run Java code in a single file as is