Sign in to follow this  

Binder code generation using libclang + a preprocessor for c++ syntactic sugar

Recommended Posts

Writing binder code is a chore and I figured it wouldn't be too hard to use libclang + a [url=""]python script[/url] to automatically generate them. In the end I spent far, far, faaaar more time on the python script than what it would have taken to write the binding code manually, but now it's at a state where it is actually useful in my code base. Configuration is done via a [url=""]json script[/url] with lots of options for regular expression matching.

The [url=""]very simple preprocessor[/url], unrelated to the above allows you to use -> object dereferencing and * pointers, allowing more code to be copy and pasted between your c++ code base and AngelScript scripts and set typedefs for types that aren't AngelScript builtins. This also lets you tell your editor that your AngelScript files are c++ files so you get code completion, error checking etc.

As an example the following is all AngelScript code for a simple test in my engine before it goes through the preprocessor:
[code]#include "System/Engine.h"
#include "Effect/MarchingCubes.h"
#include "Effect/MetaSphere.h"
#include "Physics/PhysicsWorld.h"
#include "Renderer/Renderer.h"
#include "Renderer/GfxDevice.h"
#include "Physics/RigidBody.h"
#include "Physics/RigidBodyNode.h"
#include "3D/Entity.h"
void mainscript(Engine* engine, RenderNode* n)
PhysicsWorld* w = engine->GetPhysicsWorld();
Shape* sphere = w->CreateSphereShape(1.0);
Shape* plane = w->CreateStaticPlaneShape(Vector3(0,1,0), 1);
RigidBody* sphereBody = w->CreateRigidBody(1.0, sphere);
sphereBody->SetTransform(Transform(Quaternion(0,0,0,1), Vector3(0, 50, 0)));
RigidBody* planeBody = w->CreateRigidBody(0.0, plane);
planeBody->SetTransform(Transform(Quaternion(0,0,0,1), Vector3(0, -1, 0)));
MetaSphere ms;
MarchingCubes mc(&ms, 0.7f);
Camera camera;
camera.SetLocalTransform(Transform(Quaternion(0, 0, 0, 1), Vector3(0, 0, 40)));
GfxDevice* gfx = engine->GetGfxDevice();
Renderer* rend = gfx->GetRenderer();
float add = 3.0/60.0f;
float time = 0;
while (gfx->IsOpen())
time += add;
for (int i = 0; i < ms.GetCount(); i++)
// TODO: this starts walking from inside the center of the sphere.
// As we only care about the surface, it'd be better if we could
// start at a surface cube rather than having to scan all cubes
// from the center of the sphere out to the surface.
Vector3 pos = ms.GetCenter(i);

Use if you want, don't if you don't [img][/img]

Share this post

Link to post
Share on other sites
Nice! Just what I need: Angelscript code looking extremely similar to C++! Just gave a quick look at the preprocessor... you´re basically performing a "search and replace" so "->" goes "." and " * " turns to "@", right? In other words, pointers end up being (safe) references? Haven´t tried it yet, but just came to my mind: What happens if I want to print some text that includes "->" and/or " * "? Will the preprocessor change those to "." and "@"?

I´ve also noticed I´ll need Boost to use this, right? Edited by Tzarls

Share this post

Link to post
Share on other sites
It's a regular expression search and replace pretty much, and I don't intend to do anything other than that with it. I consider it to be public domain so do with it what you want. "->" is directly replaced as is, * is doing a little bit more clever stuff to avoid replacing legitimate uses of it (such as a multiply operation). Boost is used for the regex capabilities and the foreach statement.

Share this post

Link to post
Share on other sites
It would be nice to have "new" and "delete" now that we have "->" and " * "... ;)

I´ll see if I can add those myself, as soon as i can get rid of one nasty bug that´s been crashing my code for the last few days.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this