home mail me! RSS (2.0) feed

cpsh - scripting in C++

I woke up last night with a conviction that C++ is not a worse “scripting” language than Perl or Ruby. After a few minutes awake, I had to turn on the computer and prove this nightly conjecture. I created a draft scripting environment for C++ in a few hours.

I call my nocturnal embryo cpsh for “C++ Shell.”

Of course, after writing it, I discovered one quite more ambitious scripting project for C++: CINT. This alternative is - I believe - less compliant with C++ than my scripting environment and far more verbose than mine and, yes, they use C. And, I bet they did not implement it over one drowsy night ;-)

My solution was very simplistic, just build a layer on top of GCC. I will port this to work properly with VC 7.1 when I get a few more hours over. What cpsh does, in essence, is to feed GCC with the code as the user types it, or as provided by a script file.

The scripting language is C++, with most goodies included. There is a flag one can switch, which decides whether to use Boost or not.

cpsh can read the script from standard input or from a file. It can also work in interactive mode, yielding the result of each statement as typed.

I first (like between 2 AM and 3 AM…) played around with trying to execute the construct as is, and if that failed, it will add proper constructions to output the given expression. I finally (around 4:15 AM) decided to have four types of statements for my script language:

  1. top-level definitions - these are put at the global level
  2. regular statements - these are put inside the main function and will be executed
  3. queries - these expressions, which will be executed and have its value displayed
  4. admin commands - changes the environment, such as enabling and disabling use of Boost

Ok, enough talk. Let us show an interactive session with this scripting environment (yes, it is a C++ shell as well :-) )


c:\proj\cpsh> cpsh -i
> ? “hello ” + “world”
# Problem. Compilation error. query:1: error: invalid operands of types ‘const char[7]’ …
> ? string(”hello “) + “world
< hello world

You see that I run it on Windows, which is fine, as long as you have GCC available and the shell can interpret the standard output redirector directives of Unix. Try MinGW, for instance.

Here I tried to add two char pointers, which is an invalid operation. Coercing one of the char pointers to a string did the trick, though. I only used query statements, starting with ‘?’.

Let us look at a more interesting interactive session, where we try to find out how vectors work:


c:\proj\cpsh> cpsh -i
> vector<string> names;
> ? names.size()
< 0
> const char* namePtrs[] = { “Hilda”, “David”, “Nicole” };
> names.assign(namePtrs, namePtrs+3);
> ? names.size()
< 3
> copy(names.begin(), names.end(), \
>> ostream_iterator(cout, ” “));
{Hilda David Nicole }
> ? sizeof(names)
< 12
> .template<class T>\

>> T twice(T t) { return t + t; }
> ? twice(2)
< 6
> ? twice(string(”david is best “))
< david is best david is best

Yes, I am. And humbly so. Lines beginning with ‘.’ mark top-level declarations and definitions, and trailing a line with ‘\’ allows you to continue expressing yourself on more lines before the interactive environment compiles and runs the code. NOTE 1: Any output from the statements is embedded in {…}. NOTE: if you happen to type invalid code, do not fear; the environment automatically rolls back to the last happy compilation.

This is a great tool for exploring not only C++ but also Boost (whereof some libraries will be part of TR1), and the runtime constraints. We continue the session:


> ? sizeof (int)
< 4
> ? sizeof (long long)
< 8
> :b+
# Enabled Boost
> function<int (int)> myFun;
> myFun = twice;
> ? myFun(10)
< 20
> myFun = bind(twice, 2);
> myFun(10)
> 4

NOTE: the reponse time is currently a bit slower when Boost is enabled. “A bit” as in “300%”… Here I tested both the function and bind from Boost.

I believe this tool will also come in handy when teaching C++, where the student can use trial-and-error with immediate response.

Once having this scripting engine, working closely with a compiler, one can fantasize about producing a proper web framework, mush like Joe Morrison’s Eleven. Think “C++ on Rails” :-)

I will write more about both the use and implementation of this cpsh later, with a focus on how typical scripting tasks (like traversing files looking for patterns) are achieved in cpsh. Additionally, I will provide a compiled version for Windows and Ubuntu.

Just need a few free hours :-)

kov said,

August 29, 2006 @ 10:51 am

Very cool, I agree that it’ll be very useful to play with stuff in a fast-n-dirty way. Linking in Boost does kind of imply a weightier feature of providing a ‘menu’ of libraries one could choose to link in. Now the questions —

What parts of GCC does it replace (if any)? Is this essentially a preprocessor?

How do you know what includes to provide for the entered code? I’m not clear on what is output, do you output the result of each statement evaluation?

A webservice would be cool ….. if somebody else can solve the security issues.

And finally — Wow! Cool!

davber said,

August 29, 2006 @ 9:01 pm

It is a preprocessor, and uses GCC for the “real stuff”.

With regards to Boost, it is not a regular library in that most of the included parts will be part of the upcoming C++ 0x Standard, and are already part of the TR1.

In deciding what includes to use - and later libraries to link with - I have two sets: (1) the ordinary STD namespace includes and (2) the most core Boost includes.

In order to use Boost, one has to have the BOOST_HOME environment variable defined. Hmm, I will change cpsh to default to “include Boost” only if that environment is defined. I will also support VC 7.1 and VC 8.0 very soon. Hopefully tonight.

Thanks for the kind words!

ben said,

November 28, 2006 @ 4:49 pm

This is great, but is it available to play with? I am really eager to mess around with this.

davber said,

November 28, 2006 @ 7:26 pm

Yes, it is available at Google Code repository: http://code.google.com/p/cpsh/. That version assumes MS VC++, I think, but that will be changed soon. I will also add precompiled versions for Linux, Mac OS X and Windows.

ben said,

November 28, 2006 @ 9:06 pm

It seems there are no files on the google repository. I tried checking out usinv subversion and it brought down nothing but file structure.

Browsing using the web interface also shows no source files.

davber said,

November 28, 2006 @ 10:22 pm

You are right! I will upload the soure and makefile(s) (I am using SCons) as soon as possible.

So, it apparently is not enough to have an intention, not even with Google tools ;-)

Thanks!

ben said,

November 28, 2006 @ 11:11 pm

No problem, I am very much looking forward to downloading it. I have always wanted to be able to experiment with the rules of the language but its so tedious to constantly use a compiler and IDE to do this.

Many thanks!

ben said,

December 1, 2006 @ 10:37 am

Sorry to be a nudge.. Just in case you forgot :) I’m so eager to try this out! Let the code flow!! :)

ben said,

December 7, 2006 @ 10:06 am

….

:)

davber said,

December 7, 2006 @ 2:31 pm

Ok, you got my attention, Ben ;-)

I will make sure to free up some hours tonight to upload something people can test.

Thanks for your patience.

ben said,

December 7, 2006 @ 11:45 pm

Many thanks. I consider it a gift by the way, naturally I don’t feel entitled. It’s your personal time and your personal creation. Thank you for sharing it. I think it would be the ultimate learning/proofing tool for C++.

I eagerly await a chance to try it :)

ben said,

December 10, 2006 @ 5:18 pm

hmmm… maybe it was all a dream

davber does IT » cpsh - a Windows executable said,

December 10, 2006 @ 7:31 pm

[…] First of all, sorry for not being active on the blog for quite some time, but here I am again. And this time with a binary of my cpsh tool. […]

davber said,

December 10, 2006 @ 7:36 pm

A dream? Well, at least not a nightmare.

I decided, after I got some interest from Ben and others, to start a major rewrite of cpsh, in order to better support more C++ compilers and platforms. But, for the anxious (;-)), I uploaded a Windows executable, as part of a new post

RSS feed for comments on this post · TrackBack URI

Leave a Comment

You must be logged in to post a comment.