Base Technology

CodeScript - An Application Scripting Language

CodeScript lets your C/C++ application dynamically interpret C/C++-like expressions and logic. It runs under Windows and Windows NT as a DLL (dynamic link library) with a simple API that offers a powerful but easy to use object-oriented programming language.

The CodeScript language is very similar to Java since both are based on C/C++. If you think you need a Java interpreter as an extension language for your application, check out CodeScript. It's not 100% compatible with Java, but for your application it may be close enough.

If you think you need Java to quickly develop GUI applications for Windows, check out our other product, Liana. It's not 100% compatible with Java, but for your application it may be close enough and it does generate standalone .EXE files.

Liana is somewhat similar to Java and was developed around the same time, in the early 1990's.  A description was published in the July 1992 issue of the C Users Journal.   Unlike Java, Liana and CodeScript support dynamic execution of source code scripts and evaluation of source code expressions.

Typical Uses

Features

CodeScript manages a "workspace" which contains data and code. The type of a variable need not be specified since each data value is represented by a 32-bit "value descriptor" containing the datatype and offset to the value in the workspace. A given variable may hold values of different types (integer, real, string, object, etc.) The same is true of function parameters and class data members. For example, a single dynamic array can hold values as diverse as integers, reals, strings, other arrays, and user-defined objects. Memory for strings, arrays, and objects is managed automatically. The result is code that is easier to write and maintain and more flexible.

To create a workspace and initialize some global variables:

    CSWS ws;
    CS_CreateWorkspace (hInstance);
    CS_SetGlobal (ws, "k", CS_Int (ws, 123));
    CS_SetGlobal (ws, "x", CS_Real (ws, 2.1));
    CS_SetGlobal (ws, "flag", CS_Bool (ws, TRUE));
    CS_SetGlobal (ws, "str", CS_String (ws, "Hello"));

To evaluate an expression:

    CSVAL val = CS_Evaluate (ws, "sqrt (2) * pi / 7.4");

To convert a CSVAL "value descriptor" to a C datatype:

    BOOL b = CS_ToBool (ws, val);
    long n = CS_ToInt (ws, val);
    double x = CS_ToReal (ws, val);
    CS_ToString (ws, val, str, sizeof str);

You can read the value of a CodeScript global variable:

    val = CS_GetGlobal (ws, "x");

To declare a CodeScript function:

    CS_Decl (ws, "f (x, k) {return sin (x) * k;}");

To call the function from CodeScript:

    val = CS_Evaluate (ws, "f (0.3, 10)");

To call the function from C:

    CSVAL args [2];

    args [0] = CS_Real (ws, 0.3);
    args [1] = CS_Int (ws, 10);
    val = CS_Call (ws, "f", 2, args);

To declare an application function so CodeScript can call it:

    CS_DeclFunction (ws, "double f (double x, int k)",
        MakeProcInstance (hInstance, app_f));

You must declare "app_f" if it were a Windows dialog callback function. Types ARE needed for CodeScript declaractions of external functions.

To declare a DLL function so CodeScript can call it:

    CS_DeclFunction (ws, "double f (double x, int k)", "MYLIB.DLL", "f");

CodeScript supports all the C statements and operators (except cast) as well as the C/C++ rules for tokens, names, white space and comments:

    /* Old style comments. */
    if (a > b - LongName_default_offset)
	a = b * c / d % e & f | g;
    else {
	f (x);        // New style comments.
	return;
    }
    do
	f (i);
    while (i++ <& 10);
    switch (abc) {    // Does not need to be an int!
    case 1:
	g *= f (--x);
	break;
    case 2:
	s = "var: "+v+" - yeah";  // Automatic memory mgmt. for strings.
	return q;
    case 3:
	goto done;
    case "abc":      // Does not need to be an int!
	continue;
    default:
	;
    }
    while (a > b && (q || p) && ! r)
	g (z ^ 2 & ~ 0xf0f);

Functions accept arguments of any datatype which will be converted as they are used within the body of the function:

    max (a, b) {return a > b ? a : b;}

This function works for any combination of integers, reals, or strings:

    max (10, 20); max (1.2, 3.4); max ("hello", "world");
    max (10, 3.4); max ("hello", 20);

If an operand of a relational operator is a string, the other is converted to a string. String concatenation is performed if either operand of the "+" operator is a string. For the other math operations strings will be converted to "numbers" (real if there is a decimal point and integer otherwise.) Explicit conversions are available:

    2 + int (3.4)
    3.4 + real ("7.8e23")
    "Hello " + string (pi)
    bool (123)

You can easily create and manipulate dynamic arrays in CodeScript:

    a = new array;
    a <&<& "one" <&<& 2.1 <&<& 3;   // Append three elements.
    n = a.size;               // Current number of elements.
    a.remove (2);             // Delete third element (0-based.)
    a.insert (1, "one-a");    // Insert before 2nd element.
    a [0] = "one-prime";      // New first element (0-based.)
    i = a.find ("two");       // Search for element.
    a = load_lines ("MYFILE.TXT"); // Load entire file as lines of text.

To use an associative lookup table:

    pets = new table;
    pets ["cat"] = new cat;	// Subscript can be any type.
    s = pets ["cat"];

The CodeScript runtime/class library has over 350 functions and 150 classes, including many C runtime functions and Windows features:

    x = atan (y);                     // Most C math functions.
    f = fopen ("MYFILE.TXT", "w");    // Text and binary I/O.
    spawn ("ANYPROG.EXE");            // Run a Windows or DOS app.
    inform ("Yo");                    // Message box.
    n = ask_number ("Enter Amount");  // Prompt user for a number.
    beep (5);                         // Really annoy the user.
    play_media ("/windows/tada.wav"); // Multimedia!
    exit_windows ();                  // Hello DOS! (Logout on NT)
    (w = new file_edit_window).show;  // Bring up a text editor.
    bm = new bitmap ("PICTURE.BMP");  // Load a bitmap image.
    d = new dialog ("MyInfo");        // Create a dialog box.
    d <&<& new push_button ("&Save");   // Add a control to a dialog.
    ln = new hotlink ("Excel", "X1.XLS", "[R1C1]");  // DDE link.
    services ["MYAPP"] = new service; // DDE server.

You can declare user-defined classes:

    CS_Decl (ws,
        "class animal {"
        "public:"
        "    string name;"
        "void animal (string name) {this::name = name;}"
        "string sound () {return '';}
        "string text () {return 'My name is ' + name;}"
        "string speak (n) {
        "    string s = '';"
        "    for (int i = 0; i <& n; i++)"
        "        s += (i > 0 ? ' ' : '') + this.sound;
        "    return s;"
        "}"
        "};");

    CS_Decl (ws,
        "class dog: animal {"
        "void dog () {animal ();}"
        "void dog (string name) {animal (name);}"
        "string sound {return 'woof!';}"
        "};"
        "class cat: animal {"
        "void cat {animal ();}"   // Empty parens are optional.
        "void cat (string name) {animal (name);}"
        "string sound {return 'meowww';}"
        "};");

You can load declarations from a file:

    CS_LoadDecl (ws, "MYDECL.CSD");

To create an object from C:

    CS_Evaluate (ws, "d = new dog ('rover')");

You can instantiate a CodeScript class from C:

    args [0] = CS_String (ws, "Rover");
    CS_SetGlobal (ws, "d", CS_New (ws, "dog", 1, args));

You can access class data members and member functions:

    s = d.name;
    d.name = "George";
    t = d.text;         // Empty parens not needed for member functions.
    d.speak (3);
    c = d.class_name;   // Name of class.
    bool no_dog = d.is_a ("animal") && ! d.is_a ("dog");

You can determine the type and class of an object or value from C:

    char class_name [32];
    CSTYPE type = CS_Type (ws, val); // A simple C "enum".
    CS_Class (ws, val, class_name, sizeof class_name);

You can execute a code fragment (a sequence of statements):

    CS_Execute (ws, "if (x > z) g (x); else {h (x); y *= -1;}");
    CS_ExecuteFile (ws, "MYCODE.CS");  // Read from a file.

The fragment is not a complete function, so it has no parameters, but it may have local variables and may return a value.

To finish a CodeScript session:

    CS_FreeWorkspace (ws);

System Requirements

  1. Windows 3.1 or Windows NT 3.5.
  2. C/C++ compiler compatible with Microsoft DLL calling sequence.
  3. 5 MB available disk space.
  4. 0.5 MB RAM available for DLL use.
  5. 16K of stack space available for DLL use.

Documentation

Documentation is provided on the installation floppy as a set of ASCII text files which can be viewed and printed with any text editor.

Pricing

$129 full-featured Developer's Kit (for single CPU) $495 royalty-free Distribution License (up to 100 copies) $895 full Source Code (in Microsoft C)

Shipping is $5 for Domestic US PriorityMail, inquire for others. Payment must be in advance by check in US funds drawn on a US bank or International Money Order.

Also available now for Windows NT (Intel-only)

A FREE evaluation version of CodeScript is available for download.

Liana

CodeScript is based on the Liana object-oriented programming language. The Liana product includes an interactive development environment and generates Windows .EXE files for complete applications.

Please contact us with any questions or comments.


Updated: January 30, 2006 08:53:19 PM -0500

Copyright © 2001 John W. Krupansky d/b/a Base Technology