Inline domain specific language for generating java code

I'm working on a program that performs matrix and vector operations in Java Multiple function calls and object creation in my current implementation make it slow and difficult to understand

For example, I want to update the position of mechanical points through speed integration:

void update(Vector3 position,Vector3 speed,float dt){
   Vector3 displacement = new Vector3(speed);
   displacement.assignMul(dt);
   position.assignAdd(displacement);
}

The API here is unnatural, and I need to build a new vector3 reference for one Obviously, when calculating inline in this way, I measured the performance improvement of the actual use case:

void update(Vector3 position,float dt){
   position.x += speed.x * dt;
   position.y += speed.y * dt;
   position.z += speed.z * dt;
}

Is there any tool that can generate this code from domain specific languages as needed? A grammar like cog would be good (COG is the code generation tool of ned Batchelder)

void update(Vector3 position,float dt){
   // [[[DSL position += speed * dt ]]] 
   position.x += speed.x * dt;//Generated Code
   position.y += speed.y * dt;//Generated Code
   position.z += speed.z * dt;//Generated Code
   // [[[END]]]
}

Solution

If you are interested in code generation, I strongly recommend this book language implementation patterns by Terence parr He will show you how to create an abstract syntax tree (AST) based on your DSL and then use rewrite rules to generate code

In addition, he uses vector DSL as an example, including showing how to allocate constant multiplication in the syntax tree for example

Your related section will be Chapter 15, tree pattern matcher

I agree with some other posters and think it may be a little important for your purpose Are you sure you can't achieve a smoother interface because @ Alessandro Vermeulen showed it in his gist comment? The speed difference seems insignificant

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>