How to make crystal app scriptable?

Is there some simple possibility to make crystal app scriptable? Main points:

  • allow users to script the GUI application
  • some sort of scriptable config files (it is often possible to use python expressions in the python app config files)
  • syntax? something very simple and powerful (why not like crystal/ruby)
  • ability to communicate with objects from the main crystal application

Any ideas? Thanks!

You could use a embeddable language like Lua.

Lua is frequently used in the way you describe.
You could also check out cruby (ruby-c?) and there are others too, but Lua feels like the most common.

“How to make a crystal app scriptable” is already implying it’s a crystal app, right?

You mean like maybe how to embed Crystal into C++ or other languages?

I can’t give input (not knowledgeable enough), but I’m sure other devs can chime in

I need to embed some simple scripting abilities (with object support) to the Crystal app. I don’t like lua much - some sort of Crystal mini interpreter (written in Crystal) would be ideal :)

I have experimented with a basic “interpreter”, similar to Ruby’s Object#send (https://github.com/j8r/crystal-object-send). It is quite limited, I don’t think it will fit your use case.
Note that all method arguments have to be typed for the internal casting.

1 Like

crinja is a template engine, but it has a very powerful expression evaluation and integrates deeply with Crystal types.
The jinja syntax is somewhat usable for app scripting (for example ansible uses it for customization). But it would probably be not too difficult to hook up a parser for a different syntax that’s better suited for this use case.

Apart from that, you can embed different scripting languages into Crystal, for example lua, mruby, python or javascript. There should be shards available.

1 Like

Hope this helps

1 Like

quickjs or mruby

1 Like

Ideal solution is, when the user that writes scripts can transparently access Crystal objects (some classes/API from the main application, that user can use/call).

And here I see a problem with core C solutions.

An other option is to have a RPC mechanism to communicate between the main app and the user “script”/plugins, and embed a statically linked crystal compiler along with your app.

…or create an HTTP (REST) API and let the user control the application as he wants and in what language he wants :slight_smile:

1 Like

A bit late, but maybe somebody might still find this useful :slight_smile:

I want to bring up Anyolite (GitHub - Anyolite/anyolite: Embedded mruby for Crystal) as a solution, which allows for mruby scripting (regular Ruby soon) in Crystal (including automated module wrappers and passing of objects between Ruby and Crystal).

4 Likes