7

I'm desperately looking for a fast, C-like syntax, easy to embed, easy to wrap scripting language to embed in my C++ games.

So far I've tried:

  • Lua: it works, but wrapping global C++ functions around it is painful, and wrapping objects is even more difficult. Also, I really dislike Lua's syntax.
  • AngelScript: couldn't get it to work. Beginner documentation is absymal, as the first examples do not compile, and you have to build a lot of add-ons first. Wrapping C++ objects and functions looks easier than Lua but it still could be cleaner. Syntax looks fine.
  • ChaiScript: couldn't get it to work. I got a lot of errors both with the non-git and git C++11 versions. I don't want to use the boost version, as I don't want to introduce boost as a dependency in my project. Wrapping looks easy, and syntax is ok.

I've also investigated:

  • Pike: syntax looks good, but I found no documentation about embedding.
  • Squirrel: I don't like the syntax, and embedding/wrapping is as annoying as Lua, having to deal with the stack.

So:

  • Is there a good fast, C-like syntax, easy to embed, easy to wrap alternative?
  • If there isn't - what are the best learning resources on the creation of a scripting language? I like reinventing the wheel, and this could be an interesting learning experience.
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • What compiler are you using? VC++ currently is not supported by ChaiScript 5 as VC++ lacks fundamental C++11 features… – MFH Jul 16 '13 at 12:46
  • Creating a scripting language yourself is a great learning experience. Although it will take some time, there are pretty good tools which eases the pain. For the frontend I recommend looking at: http://goldparser.org/ – Tim Jul 16 '13 at 17:17
  • 4
    "*I don't like the syntax*" That's not a good reason to avoid using well-tested, well-designed languages. That's a childish reason, not a professional one. You will never be able to develop a language as well-grounded, well-tested, and widely used as even newer scripting languages, let along old standbys like Lua that are in widespread use. If you have some pain points in syntax, the best thing you can do is ignore them. Failing that, use a small front-end to convert from a minor alteration of that syntax into the real language. – Nicol Bolas Jul 16 '13 at 18:48
  • That's odd. I was once testing ChaiScript 5 with VS2012 - it almost compiled. Based on the fact that Clang supports the whole standard I would've bet that Clang could compile ChaiScript 5… That said I disagree that the syntax is not important - personally Syntax and easy of integration would be my main concerns (the latter being the reason why I avoid stuff like Lua…) – MFH Jul 17 '13 at 10:08

8 Answers8

4

For C-like syntax, checkout

  • Ch a commercial, embeddable C interpreter
  • CINT an open source C/C++ interpreter
  • Pawn - a "simple, typeless, 32-bit extension language with a C-like syntax"

Probably not for you, but as this question might turn up a list of alternatives others would find interesting, I offer you QtScript which gives you a Javascript-like syntax. Wrapping can be straightforward, but you need to adopt the Qt framework to do it, particularly concept of signals and slots.

There's also SpiderMonkey, the JS engine from Firefox.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
4

You might look at using JavaScript. The V8 scripting engine can be embedded in your program and there is documentation on how to expose your types.

Graznarak
  • 3,626
  • 4
  • 28
  • 47
3

You might be interested in Dao (http://daoscript.org/, https://github.com/daokoder/dao).

Dao is implemented in standard C with minimum dependency (if you exclude the optional modules and tools). It is lightweight and efficient with good support for embedding and extending. Its interface for calling C functions is not based on stack. Here is a simple example:

#include "stdio.h"
#include "daoValue.h"
static void Square( DaoProcess *proc, DaoValue *param[], int nparam )
{
    daoint num = param[0]->xInteger.value;
    DaoProcess_PutInteger( proc, num*num );
}
int DaoOnLoad( DaoVmSpace *vmspace, DaoNamespace *nspace )
{
    DaoNamespace_WrapFunction( nspace, Square, "Square( num : int ) => int" );
    return 0;
}

You may noticed that there is NO boilerplate code for checking the parameter types in the wrapped function. This is because this function is registered as Square(num:int)=>int, which means this function can only accept an integer as parameter, and is guaranteed by the Dao runtime.

You may also be interested to know that it also has a tool based on Clang for automatic/semiautomatic generation of C/C++ bindings.

Disclaimer: I am the author of this language.

TuxStash.de
  • 187
  • 1
  • 8
2

You could just use C++, via something like Cling.

You get familiar syntax and easy integration with your static C++ program.

Qt + Cling, the LLVM based C++ interpreter (2:05)

bames53
  • 86,085
  • 15
  • 179
  • 244
1

I seconded using python as scripting language, I used boost python before in my program (not a game) and quite satisfied with it. If you want to try creating you own script, you may want to try boost spirit

Kamil Zubair
  • 229
  • 2
  • 10
  • Good as a reference, but the OP don't want to introduce boost as a dependency in his project. – PaperBirdMaster Jul 16 '13 at 10:13
  • 2
    @PaperBirdMaster not wanting to use Boost.Python because "it introduces a dependency on Boost" reminds me of the this [joke about a drowning man](http://www.spiritual-short-stories.com/spiritual-short-story-101-Drowning+Man.html) Any scripting language will introduce a dependency, and using BCP can isolate only the required parts of Boost. – TemplateRex Jul 16 '13 at 19:01
  • I basically agree with you @TemplateRex, but tell to the OP, it's him who had put the restriction ;) – PaperBirdMaster Jul 17 '13 at 07:14
1

Aside from what others have suggested, there's also Cling which is deemed experimental. Writing a scripting language is not easy, but nowadays you can resort to LLVM, at least for the back-end. Programming language design is discussed briefly in the old "Algorithms+Data Structure=Programs" by N. Wirth (but do check the content topic, in the latest edition that chapter was removed) and if you search for the author on Google, you'll end up surely with some other publication about the topic.

1

You might be interested in ObjectScript

ObjectScript, OS for short, is a new programming language. It's free, cross-platform, lightweight, embeddable and open-source. It combines the benefits of multiple languages, including: JavaScript, Lua, Ruby, Python and PHP. OS features the syntax of Javascripts, the "multiple results" feature from lua, syntactic shugar from Ruby as well as magic methods from PHP and Ruby - and even more!

Minimal program used ObjectScript could be like this

#include <objectscript.h>
using namespace ObjectScript;
int main(int argc, char* argv[])
{
    OS * os = OS::create(); // craete ObjectScript instance
    os->require("main.os"); // run ObjectScript program
    os->release();          // release the ObjectScript instance
    return 0;
}
UNIT POINT
  • 31
  • 1
0

You might consider looking at python: http://www.codeproject.com/Articles/14192/Embedding-Python-In-Your-C-Application

ogni42
  • 1,232
  • 7
  • 11
  • OP asked specifically for a "C-like" syntax, with an interpreter that's easy to embed. Latter is quite subjective, still Python isn't as easy to embedd –  Jun 23 '17 at 18:24