Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Ch: A C/C++ Interpreter for Script Computing


For some tasks, C and its compile/ link/execute/debug process are not productive. As computer hardware becomes cheaper and faster, to be productive and cost effective, script computing in C/C++ can be an appealing solution. To this end, we have developed Ch, an embeddable C/C++ interpreter for cross-platform scripting, shell programming, 2D/3D plotting, numerical computing, and embedded scripting [1].

As a complete C interpreter, Ch supports all language features and standard libraries of the ISO C90 Standard. It also supports an increasing number of C/C++ libraries including POSIX, TCP/IP socket, Winsock, Win32, X11/Motif, GTK+, OpenGL, ODBC, SQLite, CGI, LAPACK, LDAP, PCRE, Gnome Libxml2, Oracle XDK for XML, NAG statistics library, Intel OpenCV for computer vision, ImageMagick for image processing, SigLib for signal processing, and National Instruments' NI-DAQ and NI-Motion.

Ch supports most new features added in the ISO C99, such as complex numbers, variable length arrays (VLA), IEEE floating-point arithmetic, and type-generic mathematical functions. C programmers are encouraged to use these new features because they significantly simplify many programming tasks. Ch also supports classes in C++ for object-based programming.

Ch has borrowed features and ideas from many other languages and software packages. The following is a short list of other languages and software packages that, in one way or another, have influenced the development of Ch.

  • Like the C shell, Ch can be used as a login shell and for shell programming. But, as a superset of C, Ch is a genuine C shell.
  • Like Basic, Ch is designed for, and has been used by, beginners who have no prior programming experience.
  • Like Perl, Ch has the built-in string and other features for text handling, and can be used for common gateway interface (CGI) applications in a web-server environment.
  • Like Java, Ch can be used for Internet computing. Safe Ch uses a sandbox model for security control. A Ch applet can be executed across a network on different computer platforms on the fly.
  • Like Tcl/TK, Ch can be embedded as a scripting engine in other applications and supports GTK+, Win32, and X/Motif for GUI development.
  • Like Fortran 90, Ch can be used for scientific computing.
  • Like MATLAB/Mathematica, Ch has computational arrays and can be used for numerical computing, graphical plotting, and rapid prototyping.

The relationship of Ch to some of these languages and software packages is shown in Figure 1.

Figure 1: Relationship of Ch to some other languages and software packages.

Unlike other languages and software packages, Ch bridges the gap between low-level languages and very high-level languages. As a superset of C, Ch retains low-level features of C such as accessing memory for hardware interface. However, unlike C, Ch can be considered a very high-level language (VHLL) environment. It makes hard things easy and easy things easier. Ch extends C with many high-level features such as string type and computational arrays as first-class objects, as in Fortran 90 and MATLAB for linear algebra and matrix computations. Ch supports shell programming with a built-in string type. Some problems, which might take thousands of lines of C code, can be easily solved with only a few lines of Ch code.

Furthermore, Ch is designed to be platform independent. It can run in a heterogeneous computing environment with different computer hardware and operating systems including Windows, Mac OS X, Linux in both Intel and PPC architectures, UNIX, FreeBSD, and QNX. A program developed on one platform can run on any other platform.

In this article, I will present some unique features offered by this C/C++ interpreter for script computing and illustrate how they are used in applications. Examples presented are portable across different platforms. One can readily try these examples by downloading Ch from http://www.softintegration.com/.

Interactive Execution Of C/C++ Statements And Expressions

I regularly teach engineering students on introductory computer programming in C. I use Ch interactively in classroom presentations using a laptop. Ch allows me to illustrate programming features quickly, especially in answering students' questions. Learners can also quickly try out different features of C without tedious compile/link/execute/debug cycles. To help beginners learn, Ch has been specially developed with many helpful warning and error messages, as an alternative to crashing with cryptic, arcane messages such as "segmentation fault" and "bus error."

All C statements and expressions can be executed interactively in a Ch command shell as shown in Figure 2. The output from the system resulting from executing an expression, statement, or command in Ch is displayed on the screen. For example, the output "Hello, world" can be obtained by calling function printf() interactively as shown in Figure 2. Note that the semicolon at the end of a statement in a C program is optional when the corresponding statement is executed in command mode. There is no semicolon in calling function printf in the previous execution. The default prompt in a Ch shell can be reconfigured. For simplicity, I'll only show the prompt > in a Ch command shell for the rest of this article.

Figure 2: The user interface in a Ch command shell.

If a C expression is typed in, it will be evaluated by Ch. The result will then be displayed on the screen. For example, if the expression 1+3*2 is typed in, the output will be 7, as shown:

> 1+3*2 
7

Any valid C expression can be evaluated in a Ch shell. Therefore, Ch can be conveniently used as a calculator. As another example, one can declare a variable at the prompt, then use the variable in the subsequent calculations:

> int i 
> sizeof(int) 
4 
> i = 30 
30 
> printf("%x", i) 
1e 
> printf("%b", i) 
11110 
> i = 0b11110 
30 
> i = 0x1E 
30 
> i = -2 
-2 
> printf("%b", i) 
11111111111111111111111111111110 
> printf("%32b", 2) 
00000000000000000000000000000010 

In these C statements, variable i is declared as int type with 4 bytes. Then, the integer value 30 for i is then displayed in decimal, hexadecimal, and binary numbers. The integral constants in different number systems can also be assigned to variable i. The 2's complement representation of the negative number -2 is displayed as well. Characteristics for all other data types in C can also be presented interactively. Different format specifiers for the families of input function fscanf() and output function fprintf() using file streams opened by function fopen() can also be tried out this way. All C operators can be used interactively:

> int i=0b100, j = 0b1001 
> i << 1 
8 
> printf("%b", i|j) 
1101 

The concept of pointers and addresses of variables can be illustrated as shown:

> int i=10, *p 
> &i 
1eddf0 
> p = &i 
1eddf0 
> *p 
10 
> *p = 20 
20 
> i 
20 

In this example, the variable p of the pointer to int points to the variable i. The relation of arrays and pointers can be illustrated as follows:

> int a[5] = {10,20,30,40,50}, *p; 
> a 
1eb438 
> &a[0] 
1eb438 
> a[1] 
20 
> *(a+1) 
20 
> p = a+1 
1eb43c 
> *p 
20 
> p[0] 
20 

Expressions a[1], *(a+1), *p, and p[0] all refer to the same element. Multidimensional arrays can also be handled interactively. The boundary of an array is checked in Ch to detect potential bugs; see Example 1.

Example 1: Array-boundary checking in Ch.

> int a[5] = {10,20,30,40,50} 
> a[-1] 
WARNING: subscript value -1 less than lower limit 0 
10 
> a[5] 
WARNING: subscript value 5 greater than upper limit 4 
50 
> char s[5] 
> strcpy(s, "abc") 
abc 
> s 
abc 
> strcpy(s, "ABCDE") 
ERROR: string length s1 is less than s2 in strcpy(s1,s2) 
ABCD 
> s 
ABCD

The allowed indices for array a of five elements are from 0 to 4. Array s can only hold five characters including a null character. Ch can catch bugs of existing C code related to the array boundary overrun. The alignment of a C structure or C++ class can also be examined as shown:

> struct tag {int i; double d;} s 
> s.i =20 
20 
> s 
.i = 20 
.d = 0.0000 
> sizeof(s) 
16 

In this example, although the sizes of int and double are 4 and 8, respectively, the size of structure s with two fields of int and double types is 16, instead of 12, for the proper alignment.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.