Choosing Embedded Scripting Language for C++

What is a good scripting language for a small embedded system?

We use Squirrel for this job. It is similar to Lua, but reference counted instead of garbage collected, so it tends to work better in very tight memory. On the downside, its community is much smaller.

I have also seen Lisp embedded successfully, particularly a Scheme-like derivative.

See also this other StackOverflow question: What are the available interactive languages that run in tiny memory?

What is the best scripting language to embed in a C# desktop application?

I've used CSScript with amazing results. It really cut down on having to do bindings and other low level stuff in my scriptable apps.

Why should I embed a scripting language?

Actually, a lot of game engines like to build interfaces to the engine by embedding Lua or Python. There are advantages to this:

  • Non-programmers can interface with the engine.
  • You do not need to recompile for minor script changes.
  • Errors in the script might not crash the entire system.

C++ is quite useful as a backend for projects that want the flexability of scripting languages, but want the performance of C++. I have not heard of projects that use C++ as a frontend, with a scripting language as the backend.

API Style

We use this style in my company's software. We expose an API through a Windows DLL that can be called by most languages fairly easily. We specifically support VB and VBA. This is great when the backend is from outside the script maker's control. However, it is hard to debug issues that arise from the script maker's perspective.

Advantages

  • Strong decoupling
  • Accessible from different languages

Disadvantages

  • Hard to debug 2 processes

Embedded Style

The software actually embeds the script interpreter into the software. This way you can expose features as if they were native functions. In this style, the script makers and the backend programmers typically are in the same company. It can also be used by traditional software to allow others to extend the functionality of the application. If they share source code, you can debug problems arising from scripts in a much easier manner. The application also takes care of when and how to launch your scripts. However, in order to support additional languages, the application developer has to embed other interpreters.

Advantages

  • Stronger coupling
  • Easier to debug one process

Disadvantages

  • Only accessible through the approved language

Selecting An Embedded Language

Yes, tons. Lua and Python seems to be the most popular:

Embedding Lua

  • http://www.lua.org/pil/24.html
  • https://stackoverflow.com/questions/38338/why-is-lua-considered-a-game-language
  • Lua as a general-purpose scripting language?

Embedding Python

  • http://docs.python.org/extending/embedding.html

Embedding Tcl

  • http://wiki.tcl.tk/3474
  • http://wiki.tcl.tk/2265

Embedding Ruby

  • How to embed Ruby in C++?

Embed Perl

  • http://perldoc.perl.org/perlembed.html

Embed JavaScript

  • http://spiderape.sourceforge.net/

There are dozens of JavaScript engines around, this is just an example. Some of them are also frighteningly quick.

Which scripting language is better for embedding in multi-threaded C/C++ application

You might consider embedding a popular JavaScript engine. Not only will they be fast and well-supported, but so many people know how to program in JavaScript that it will be easily adopted and read by a large audience.

According to this answer the SpiderMonkey engine is thread-safe, while Google/Chrome's V8 may not be.

Scripting language for embedding into C#/.NET applications?

I know the other question references IronPython, but I still feel like it should be here because I think it's one of the best options.

Another great option would be IronRuby. The main difference I see would be if your developers/users had any experience with either Python or Ruby that could be transferable.

Really, in the end your best option is going to be a language that is most easily adapted to by the users. Unless they accept it and are able to be more productive than they were with the old method it isn't worth the effort.

Choosing correct datatypes for script language

Well, yes, of course using very large types will have a (massive) impact in the execution cost for your language.

Many embedded platforms don't have floating-point arithmetic in hardware, and those that do often have only float, not even double. It's often the same with integer too, many platforms are 32-bit only still.

You would have to fall back to software emulation of these features, which would make execution quite costly both in terms of speed but also in the amount of code that is needed.

Is there any reason to use C instead of C++ for embedded development?

Two reasons for using C over C++:

  1. For a lot of embedded processors, either there is no C++ compiler, or you have to pay extra for it.
  2. My experience is that a signficant proportion of embedded software engineers have little or no experience of C++ -- either because of (1), or because it tends not to be taught on electronic engineeering degrees -- and so it would be better to stick with what they know.

Also, the original question, and a number of comments, mention the 4 Kb of RAM. For a typical embedded processor, the amount of RAM is (mostly) unrelated to the code size, as the code is stored, and run from, flash.

Certainly, the amount of code storage space is something to bear in mind, but as new, more capacious, processors appear on the market, it's less of an issue than it used to be for all but the most cost-sensitive projects.

On the use of a subset of C++ for use with embedded systems: there is now a MISRA C++ standard, which may be worth a look.

EDIT: See also this question, which led to a debate about C vs C++ for embedded systems.



Related Topics



Leave a reply



Submit