The s2 Scripting Engine/Language

Main project page: https://fossil.wanderinghorse.net/r/cwal/wiki/s2
See also: cwal and whcl

print('Hello, world!');
print(<<<X Hello, world! X);
print(catch{throw "Hello, world!"}.message);
print(catch{throw {hi:"Hello, world!"}}.message.hi);
s2.io.output(catch{throw ["Hello, world!"]}.message[0], '\n');
s2out << scope{
    var ☺ = ["Hello","world!"];
    ☺.join(", ");
} << '\n';
print(["Hello","world!"].join(', '));
print(["Hello","world!"].withThis(function(){return this.join(', ')}));
print(for(;;) break "Hello, world!");
// Aaaaand, for good measure:
print.'operator<<' = proc(){ return this(argv.0) };
print << "Hello, world!";

The current (late 2018) "state of the art" of cwal is demonstrated in the s2 scripting language. s2 is officially "cwal's second scripting language", though it's actually the 3rd (the first was a throwaway prototype called s1, and the second, called th1ish, was inspired by the TH1 mini-dialect of TCL). In a nutshell, s2 is a memory-light, embeddable language which superficially resembles JavaScript (sharing the same core data types but syntactically more flexible and having completely different scoping rules). It's easy to embed in any C or C++ software and is about as memory-light as a more-or-less full-featured programming language can be, even comparing favorably to lua (while having a much more conventional syntax). Running s2's complete test script suite requires about two orders of magnitude fewer allocations and amounts of RAM than PHP requires for running an empty script.

s2 was conceived to be used primarily for scripting test code for near-arbitrary C/C++ libraries (it's easy to embed in C code and extend with client-side functions), but it has since grown well past that, and is suitable for a far wider range of scripting jobs. It is not, and will never be, and Enterprise-grade überlanguage, but it is an easy-to-use, memory-light option for adding scripting to just about any C/C++ software and handling certain types of "personal scripting needs". s2 is used for generating/compiling much of this website, and at least a couple of this site's backend CGI scripts are implemented in s2 (e.g. the site search backend uses s2's sqlite3 plugin to serve search results).

s2, though superficially similar to JavaScript, has a much more "fluid" syntax in which "everything is an expression" (even keywords are simply sub-expression parsers). This means, for example, that a for loop or if/else block (which are expressions in and of themselves) can be placed directly in a function argument list or inside an array index operation. e.g. the result of an if/else block is true if any of the if parts of the evaluates to true, otherwise the block as a whole evaluates to false.

Despite being relatively feature-rich, s2 is, and always will be a toy, but it's my toy and i play with it inordinately often.

Rather than repeat myself for the Nth time about s2's features, strengths, and weaknesses, interested readers should see s2's home page. There one can find links to the documentation (roughly 200 pages of it, not including the API docs) and anything else related to the language. If you find yourself interested in s2 and need some assistance, feel free to get in touch.

×