scripting language with c++ like syntax?

Started by
35 comments, last by HansDampf 17 years, 11 months ago
Quote:Original post by __ODIN__
I'd question WHY you want a C++ syntax in the first place...


I used to use and recommend Lua; however the syntax difference between Lua and C++ provided no benefit for using Lua over C++. Once you remove the syntactic sugar for Lua and re-candy coat it for a more C++ syntax, artists/level designers won't have any more problems vs. using Lua-syntax, and C++ developers will be able to work faster. Thus, it's a net win to use a C++ style syntax.

Quote:Original post by __ODIN__
- At the same time you loose the kick-ass debugger you're used to in C++, and probably loose some of your speed as well.


The (C++ -like syntax based) scripting language that I currently use and recommend (Squirrel) provides a high quality, syntax highlighted remote debugger (works reasonably well). It can do everything Lua can do (some parts derived from Lua), and scales better for large commercial projects (Lua may be faster for some smaller apps where memory issues aren't present).

Another big challenge is binding the scripting language to C++. I created SqPlus for Squirrel to make integation+binding simple+easy (borrowed ideas from LuaPlus and Luabind, then simplified and generalized). Luabind for Lua and boost.Python for Python are more powerful, but also far more complicated (must use boost, etc.).

I haven't used AngelScript, but from forum posts it appears to also be simple to use and supports a C++ style syntax (differs from Lua, Python, Squirrel by using typed variables).

Quote:Original post by __ODIN__
- If you're running on .NET, you could consider going for C# as your scripting language; there's a sample of how to do that in the DX SDK.... might be worth doing.


C# rocks; for a Win32-only target, C# scripting can't be beat in terms of compile-time + run-time (only slightly slower than C++ in some cases).
Advertisement
I have also been looking for a C/C++-like scripting language for a small uni project and Squirrel looks interesting, however how straightforward is embedding it and how feasible is it to transfer data between the host application and the scripts? In particular, how feasible is it to access C++ classes from the script, and custom script types from C++?
Also, is it actually suited to be run in a multi-threaded environment?
Thanks
Quote:Original post by Anonymous Poster
I have also been looking for a C/C++-like scripting language for a small uni project and Squirrel looks interesting, however how straightforward is embedding it and how feasible is it to transfer data between the host application and the scripts? In particular, how feasible is it to access C++ classes from the script, and custom script types from C++?
Also, is it actually suited to be run in a multi-threaded environment?
Thanks


Binding a class, functions, and variables is simple (_T() is for UNICODE compatibility):
  SQClassDef<MyClass>(_T("MyClass")).    func(&MyClass::process,_T("process")).    var(&MyClass::classVal,_T("classVal"));


Example from minimalSqPlus.cpp. Defines a C++ class, binds to the script system, calls script, which calls C++ function and accesses variable (formating blown out by forum software; see original source):
class MyClass {public:  int classVal;  // See examples in testSqPlus2.cpp for passing arguments to the constructor (including variable arguments).  MyClass() : classVal(123) {}  bool process(int iVal,const SQChar * sVal) {    scprintf(_T("classVal: %d, iVal: %d, sVal %s\n"),classVal,iVal,sVal);    classVal += iVal;    return iVal > 1;  } // process};int main(int argc,char * argv[]) {  SquirrelVM::Init();  // See examples in testSqPlus2.cpp for script read-only vars, constants,  // enums, static/global functions, variable arguments, constructor arguments,   // passing/returning classes/structs by value or by address, etc.  SQClassDef<MyClass>(_T("MyClass")).    func(&MyClass::process,_T("process")).    var(&MyClass::classVal,_T("classVal"));  SquirrelObject helloSqPlus = SquirrelVM::CompileBuffer(_T("    local myClass = MyClass();                           \n    local rVal = myClass.process(1,\"MyClass1\");        \n    print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n    rVal = myClass.process(2,\"MyClass2\");              \n    print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n    print(\"classVal: \"+myClass.classVal);              \n  "));  try {    SquirrelVM::RunScript(helloSqPlus);  } catch (SquirrelError & e) {    scprintf(_T("Error: %s, %s\n"),e.desc,_T("Squirrel::helloSqPlus"));  } // catch  SquirrelVM::Shutdown();  return 0;} // main


OS multithreading is not currently supported [would be complicated to implement/slow down the interpreter] (though you can use script 'generators/coroutines' as with Lua).

Using multiple VM's (virtual machines), it should be possible to run multiple Squirrel VM's in multiple threads (any thread intercommunication would require some form of mutex).
Quote:Original post by John Schultz
I used to use and recommend Lua; however the syntax difference between Lua and C++ provided no benefit for using Lua over C++. Once you remove the syntactic sugar for Lua and re-candy coat it for a more C++ syntax, artists/level designers won't have any more problems vs. using Lua-syntax, and C++ developers will be able to work faster. Thus, it's a net win to use a C++ style syntax.


While I personally use LUA (mainly for UI integration and some data-definition), I don't really consider myself an evangelist. I'm more pointing out (from having used different scripting languages, and having seen fellow professionals and some of my students alike using scripting languages), some common pitfalls.

1. The programmer, knowing C++, wants to use a scripting language similar to it.
2. However, the scripting language is often not targetting him (very often, he could do it faster in ACTUAL C++ instead)
3. The designer, who struggles with for loops, never mind multiple inheritance, wants something simple; basically some if statements and some "go here and do that" type logic.
4. Closely mirroring C++ classes (as through straight reflection) generally leaves you open to the kind of issues scripting should hide from you (underlying class hierarchy, implementation details, crashes on invalid pointers, etc).
5. This is a personal opinion, of course, but I see Scripting as a tool that should aim to provide a safe sandbox environment at a 'higher level' (so "go to and do", rather than "pathfinding and rendering").

Having said that; each user picks the best tradeoff for himself.. If you're a programmer working alone on a project, you might not need a scripting language at all. If you're working in a programmer heavy environment, something like Squirrel might be best for you (allowing C++ programmers to be immediatly productive). If you're working as a fascilitator for artist/designers to interact with your engine, you may go for something with a more simplified syntax, and more forgiving parser.

There's no wrong answer, just different advantages.

Allan

------------------------------ BOOMZAPTry our latest game, Jewels of Cleopatra
Quote:Original post by __ODIN__
While I personally use LUA (mainly for UI integration and some data-definition), I don't really consider myself an evangelist. I'm more pointing out (from having used different scripting languages, and having seen fellow professionals and some of my students alike using scripting languages), some common pitfalls.

1. The programmer, knowing C++, wants to use a scripting language similar to it.
2. However, the scripting language is often not targetting him (very often, he could do it faster in ACTUAL C++ instead)


True. I have prototyped code in script, then moved it over to C++ when I found such elements were faster to develop/debug in C++.

Quote:Original post by __ODIN__
3. The designer, who struggles with for loops, never mind multiple inheritance, wants something simple; basically some if statements and some "go here and do that" type logic.


This can be implemented in Lua, Python, Squirrel, etc., by binding the script to C++ appropriately.

Quote:Original post by __ODIN__
4. Closely mirroring C++ classes (as through straight reflection) generally leaves you open to the kind of issues scripting should hide from you (underlying class hierarchy, implementation details, crashes on invalid pointers, etc).
5. This is a personal opinion, of course, but I see Scripting as a tool that should aim to provide a safe sandbox environment at a 'higher level' (so "go to and do", rather than "pathfinding and rendering").


True, although it's important that it should be possible to implement any construct you see fit (such as full reflection if the project may benefit, or a very tightly controlled sand box-like environment). You haven't implemented your real-time raytracer or visual protein folding-program in Lua yet? [wink]

Quote:Original post by __ODIN__
Having said that; each user picks the best tradeoff for himself.. If you're a programmer working alone on a project, you might not need a scripting language at all. If you're working in a programmer heavy environment, something like Squirrel might be best for you (allowing C++ programmers to be immediatly productive). If you're working as a fascilitator for artist/designers to interact with your engine, you may go for something with a more simplified syntax, and more forgiving parser.


Again, I agree, however with respect to Lua and Squirrel as far as the script-writer is concerned, the biggest difference is syntax (they are both table-based typeless scripting languages; Squirrel directly supports classes (single inheritance), while Lua must emulate them). From experience with using Lua with LuaPlus and Squirrel with SqPlus, the later is far easier to integrate with C++, easier to write script code, and easier to debug with a visual debugger.

Since good artists/level designers have had no problem ([wink]) learning MAXScript (for 3DSMax) and MEL (for Maya), they will have no problem using Lua, Python, Squirrel, AngelScript, GameMonkey, &#106avascript, Java, or C# (the interface-to-game-engine environment can be sandboxed/simplified as needed). <br><br>This brings us back to chosing scripting languages that are easy to integrate with C++, ease of writing script code, and ease of debugging with a good debugger (clearly C# wins here (amazing IDE/debugger, almost as fast as C++, becoming more portable (Mono, dotGNU)), however I would rank Squirrel second due to its ease of integration (easier than C#, also portable) and simpler language). Interestingly, I was surprised to see that VS2005 syntax highlights Squirrel source, and Visual Assist X further applies syntax highlighting (Squirrel source files defined as script file type). Not as nice as C# with full code completion/suggestion-lists, but cool nonetheless.
Quote:Original post by John Schultz
Interestingly, I was surprised to see that VS2005 syntax highlights Squirrel source, and Visual Assist X further applies syntax highlighting (Squirrel source files defined as script file type). Not as nice as C# with full code completion/suggestion-lists, but cool nonetheless.


Interesting, I tried getting syntax highlighting working with Visual Studio 2005 Express (havn't tried since I got Standard though), and it would only highlite the c++ keywords, not squirrel keywords (ie, things like do, for, etc highlited, but things like function, were not). I tried implementing some custom highlighting rules, but they ended up highlighting c++ files with squirrel highliting rules :/ So, I dropped that, and use a simple Editor (Programmers Notebook2), which I created a syntax highlighting rule for, and use PN2 to load up a project full of Nut's, and have a quick launch to save open files, and launch my test App :) But, if 2005 Standard can highlite Squirrel, I would much prefer that. I have not yet tried the Exclipse debugger integration, but I will have to as sometimes debugging squirrel code can be hard.

Anyway, back on point, I also recommend Squirrel, easy to use, easy to bind, and pretty slick syntax once you get used to it :)
Quote:Original post by pjcast
Quote:Original post by John Schultz
Interestingly, I was surprised to see that VS2005 syntax highlights Squirrel source, and Visual Assist X further applies syntax highlighting (Squirrel source files defined as script file type). Not as nice as C# with full code completion/suggestion-lists, but cool nonetheless.


Interesting, I tried getting syntax highlighting working with Visual Studio 2005 Express (havn't tried since I got Standard though), and it would only highlite the c++ keywords, not squirrel keywords (ie, things like do, for, etc highlited, but things like function, were not). I tried implementing some custom highlighting rules, but they ended up highlighting c++ files with squirrel highliting rules :/ So, I dropped that, and use a simple Editor (Programmers Notebook2), which I created a syntax highlighting rule for, and use PN2 to load up a project full of Nut's, and have a quick launch to save open files, and launch my test App :) But, if 2005 Standard can highlite Squirrel, I would much prefer that. I have not yet tried the Exclipse debugger integration, but I will have to as sometimes debugging squirrel code can be hard.

Anyway, back on point, I also recommend Squirrel, easy to use, easy to bind, and pretty slick syntax once you get used to it :)


For basic debugging, sprinkling print() works fine, though the visual debugger is nice for more complex issues (and is faster than adding all the print() statements).

The syntax highlighting isn't perfect (VS2005Pro), but it looks nice (some of it is due to VAX):


Not sure if setting .nut as Script does anything, though saving the .nut files as .cpp got some form of IntelliSense/VAX to pop up.
This looks pretty good, but I am not using Win32-is there a vim syntax hightlighting file for squirrel available, too?
Thanks

I've been using spidermonkey( mozilla's &#106avascript engine ) for a few of my projects and one at work. I find its pretty easy to use and &#106avascript can be object oriented. I'm not sure if that's close enough to C++ for you but it fufills the problem of C++ being to "hard" for some scripters.<br><br>Cheers<br>Chris
CheersChris
Quote:Original post by Anonymous Poster
This looks pretty good, but I am not using Win32-is there a vim syntax hightlighting file for squirrel available, too?
Thanks


There was a post on the Squirrel forum about someone starting to implement VIM highlighting. Perhaps make a post there asking about VIM support.

Some example HL files (should be able to convert to other systems fairly quickly: if converted, please post your conversions for others to use):

Colorer HRC:
<?xml version="1.0" encoding="windows-1251"?><!DOCTYPE hrc PUBLIC "-//Cail Lomecb//DTD Colorer HRC take5//EN"  "http://colorer.sf.net/2003/hrc.dtd"><!--   Syntax highlighting for squirrel scripts   For installation put it into subdirectory hrc/auto of colorer (version take5)--><hrc version="take5" xmlns="http://colorer.sf.net/2003/hrc"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://colorer.sf.net/2003/hrc http://colorer.sf.net/2003/hrc.xsd">   <prototype name="squirrel" group="scripts" description="squirrel">     <filename>/\.nut$/i</filename>   </prototype>      <type name="squirrel">      <import type="def"/>      <import type="c"/>      <scheme name="squirrel">         <inherit scheme="def:PairedBrackets">            <virtual scheme="def:PairedBrackets" subst-scheme="squirrel"/>         </inherit>         <block start="/\/\//" end="/$/" scheme="def:Comment" region="Comment"/>         <block start="/\/\*/" end="/\*\//" scheme="def:Comment" region="Comment"                region00="def:PairStart" region10="def:PairEnd"/>         <inherit scheme="def:CHexNumber"/>         <inherit scheme="def:FloatNumber"/>         <inherit scheme="def:DecNumber"/>         <inherit scheme="c:String"/>         <inherit scheme="c:Character"/>         <keywords region="def:Error">            <symb name="["/><symb name="]"/>            <symb name="{"/><symb name="}"/>            <symb name="("/><symb name=")"/>            <symb name="*/"/>         </keywords>         <keywords region="def:Symbol">            <symb name=";" region="def:SymbolStrong"/>            <symb name="="/><symb name="+"/><symb name="-"/>            <symb name="/"/><symb name="*"/>            <symb name="&"/><symb name="^"/>            <symb name="|"/><symb name=":"/>            <symb name=","/><symb name="."/>            <symb name="!"/><symb name="~"/>            <symb name="<"/><symb name=">"/>            <symb name="%"/><symb name="?"/>         </keywords>         <keywords region="def:Keyword">            <word name="break"/>            <word name="case"/>            <word name="catch"/>            <word name="class"/>            <word name="clone"/>            <word name="continue"/>            <word name="default"/>            <word name="delegate"/>            <word name="delete"/>            <word name="else"/>            <word name="extends"/>            <word name="for"/>            <word name="foreach"/>            <word name="function"/>            <word name="if"/>            <word name="in"/>            <word name="local"/>            <word name="null"/>            <word name="resume"/>            <word name="return"/>            <word name="switch"/>            <word name="this"/>            <word name="throw"/>            <word name="try"/>            <word name="typeof"/>            <word name="while"/>            <word name="parent"/>            <word name="yield"/>            <word name="constructor"/>            <word name="vargc"/>            <word name="vargv"/>            <word name="instanceof"/>            <word name="true"/>            <word name="false"/>         </keywords>      </scheme>   </type></hrc>


UltraEdit:
/L20"Squirrel" Line Comment = // Block Comment On = /* Block Comment Off = */ File Extensions = NUT/Delimiters = ~!@%^&*()-+=|\/{}[]:;"'<> , .?/Function String = "%[ ^t]++function^(*(*)^)"/Function String 1 = "%[ ^t]++^(*^)=[ ^t]++function*(*)"/Indent Strings = "{"/Unindent Strings = "}"/Open Fold Strings = "{"/Close Fold Strings = "}"/C1"Keywords"breakcase catch class clone continuedefault delegate deleteelse extendsfalse for functionif in instanceoflocalnullresume returnswitchthis throw true try typeofwhileparentvargc vargvyield/C2"Objects"/C3"Functions"array assertcollectgarbage compilestringenabledebuginfoformatgetroottable getstackinfosnewthreadprintsetdebughook seterrorhandlertype/C4"Methods and Properties"_add _call _cloned _cmp _delslot _div _get _modulo _mul _newslot _nexti _set _sub _typeof _unmconstructor/C5"Operators"!?[]^|~/C5"Separators"(),;{} 


More highlighting info here.

This topic is closed to new replies.

Advertisement