Language Skinning

Banate CAD is written in Lua, and so are the .fab files which you use to actually do your modeling and visualization.  Since the source code is part of the distribution, a user can get into infinite mischief by changing things anywhere in the system.

The more classic forms of software distribution involve a ‘compiled’ product instead.  In that case, the end consumer does not have access to changing anything core about the program.  In such cases, extensions and modifications are either done by recompiling the product, or through some ‘add-on’ mechanism.  That works fine in most cases, but what if you want something a bit more dynamic?

One of the things I have wanted in my text based visualizer, is the ability to extend the language itself.  The core language of Lua is fairly straight forward and flexible.  I don’t want to extend the core syntax of the language, but I want to add some “look and feel” from other languages.  There is one ‘language’ I find to be interesting, and that’s GLSL, the vertex and fragment shader language from OpenGL.

I don’t mean to replicate the pipeline architecture of OpenGL/GLSL, but I like a number of the built-in functions.  For example:

float smoothstep(float edge0, float edge1, float x)
vec2 smoothstep(vec2 edge0, vec2 edge1, float x)
vec3 smoothstep(vec3 edge0, vec3 edge1, float x)
vec4 smoothstep(vec4 edge0, vec4 edge1, float x)

When x= edge 1. In between, it will perform a hermite interpolation between 0 and 1. This is great for putting a bit of smoothness on things, whether it is motion paths, or colors ramps, or the corners of a solid being rendered. A very useful function.

How to add language extensions?  In this case, I have created a filed named “glsl.lua”.  I put it into the directory with all the other files of the distribution.  Perhaps they will be better organized in the future, but a the moment, you can just drop them in the main directory.  From there, you’ll need to get the file pulled in with the rest of the other sources.  The main “Language Skin” is contained in the SceneBuilder.lua file.  So, I add a single line to that file:

require(“glsl“)

And that’s it.  Now, from within my scripts, I can start using this new function, and the various others that I’ve added into the glsl.lua file.

The SceneBuilder.lua file is itself just a language skin.  It has all the convenience functions such as addshape(), addmesh(), cone(), bicubicsurface(), and the like.  If you don’t like the naming there, or if you want to have a completely different skin, you can just replace this file.  If you want your code to look/act more like JavaScript, for example, you can just Create a JavaScript skin instead.  In that case, just replace the SceneBuilder.lua file with your version, and carry on.

Of course, the downside to all this flexibility is that you end up with a situation where it becomes hard to share .fab files across multiple differently modified versions of the program.  To bring some sanity to the situation, extensions/modifications will have to be managed properly.  There will have to be an inviolable core, and an “Add-Ons” mechanism whereby the script can know what environment it requires.

Although it might be challenging to have such flexibility, I believe it gives Banate CAD an advantage in terms of development.  It can evolve much more rapidly, and new things can be added much more quickly that if it were done in the more classical style.

 



Leave a comment