Version 13 of D

Updated 2006-04-01 18:28:56

The D programming language is a new language designed as an evolution of C/C++. The language specification is currently at http://www.digitalmars.com/d/index.html

To see how D compares to other languages, check out http://www.digitalmars.com/d/comparison.html .


We can call Tcl from D fairly simply. In fact, it looks a lot like C++ in most places. The main issue is to generate an import symbol file for the Tcl API (which can be done by using the genStubs script file).

Under Microsoft Windows I had to convert the standard ActiveTcl tcl83.lib to an OMF format library. To do this we convert to COFF format using Microsoft's LINK program and then Digital Mars' COFF2OMF utility

  link /lib /convert tcl83.lib
  coff2omd tcl83.lib

To illustrate, here is an initial stab. Build this using

 dmd simple.d tcl83.lib  (Using Windows)

or

 dmd simple.d -L-ltcl    (Using UNIX)

 // simple.d - Copyright (C) 2003 Pat Thoyts <[email protected]>
 //
 // Demonstrate linking to Tcl from the D programming language. 
 // See http://www.digitalmars.com/d/index.html for information
 // about ``D''
 //
 // $Id: 6261,v 1.14 2006-04-02 06:00:12 jcw Exp $

 import c.stdio;
 import stream;
 import compiler;

 // ----------------------------------------------------------------------
 // Define the bits we need for interfacing to Tcl API
 //
 extern (C) {
     alias void* ClientData;
     alias void (*Tcl_FreeProc)(char* blockPtr);
     alias void (*Tcl_CmdDeleteProc)(ClientData clientData);
     alias int (*Tcl_CmdProc)(ClientData clientData, Tcl_Interp * interp,
                              int argc, char* argv[]);
     alias void* Tcl_Command;

     struct Tcl_Interp {
         char* result;
         Tcl_FreeProc blockPtr;
         int errorLine;
     }

     enum {
         TCL_OK = 0,
         TCL_ERROR = 1,
         TCL_RETURN = 2,
         TCL_BREAK = 3,
         TCL_CONTINUE = 4,
     }

     enum : Tcl_FreeProc {
         TCL_VOLATILE = (Tcl_FreeProc)1,
         TCL_STATIC = (Tcl_FreeProc)0,
         TCL_DYNAMIC = (Tcl_FreeProc)3,
     }

     export {
        Tcl_Interp* Tcl_CreateInterp();
        Tcl_Command Tcl_CreateCommand(Tcl_Interp * interp, char * cmdName,
                                      Tcl_CmdProc proc,
                                      ClientData clientData,
                                      Tcl_CmdDeleteProc deleteProc);
        int Tcl_Eval(Tcl_Interp * interp, char * string);
        int Tcl_EvalFile(Tcl_Interp * interp, char * fileName);
        char * Tcl_GetStringResult(Tcl_Interp* interp);
        void Tcl_SetResult(Tcl_Interp * interp, char * str,
                           Tcl_FreeProc freeProc);

     }
 }

 // ----------------------------------------------------------------------

 int main(char[][] args)
 {
     Tcl_Interp* interp;
     interp = Tcl_CreateInterp();

     Tcl_CreateCommand(interp, "ddemo", &DDemoCmd,
                       (ClientData)null, (Tcl_CmdDeleteProc)null);

     int r;
     if (args.length < 2)
     {
         r = Tcl_Eval(interp, "puts "Tcl version: [info tcl]"; ddemo");
     }
     else
     {
         int r = Tcl_EvalFile(interp, args[1]);
     }
     printf(Tcl_GetStringResult(interp));
     return r;
 }

 // Add a new command to the Tcl interpreter.
 // In this case: return some information about the D compiler.
 extern (C):
 int 
 DDemoCmd(ClientData clientData, Tcl_Interp *interp, int argc, char* argv[])
 {
     MemoryStream stm = new MemoryStream;
     stm.printf(compiler.name);
     stm.printf(" %d.%d", compiler.version_major, compiler.version_minor);
     Tcl_SetResult(interp, stm.toString(), TCL_STATIC);
     return TCL_OK;
 }

 // ----------------------------------------------------------------------
 //
 // Local variables:
 //   mode: c
 //   compile-command: "dmd simple.d tcl83.lib"
 // End:

RLH That seems like a good language. I wonder why it isn't used more?

MSW From a quick glance at it, probably because there's no open source compiler and thus the thing only runs on a very limited set of (OS x CPU/arch). There's http://www.opend.org and it seems dead for soon half a decade...


[ Category Language ]