Manipulating Expressions in External Programs

Wolfram Language expressions provide a very general way to handle all kinds of data, and you may sometimes want to use such expressions inside your external programs. A language like C, however, offers no direct way to store general Wolfram Language expressions. It is nevertheless possible to do this by using the loopback links provided by the Wolfram Symbolic Transfer Protocol (WSTP) library. A loopback link is a local WSTP connection inside your external program, to which you can write expressions that can later be read back.

Functions for manipulating loopback links.

This opens a loopback link.

...
wstp = WSLoopbackOpen(stdenv, &errno);

This puts the expression (RefLink[Power, paclet:ref/Power])[x,3] onto the loopback link.

WSPutFunction(wstp, "Power", 2);
  WSPutSymbol(wstp, "x");
  WSPutInteger32(wstp, 3);
...

This gets the expression back from the loopback link.

WSGetFunction(wstp, &head, &n);
  WSGetSymbol(wstp, &sname);
  WSGetInteger32(wstp, &k);
...

This closes the loopback link again.

WSClose(wstp);

You can use (NonWLLink[WSTransferExpression(), paclet:ref/c/WSTransferExpression]) to take an expression that you get via (NonWLLink[stdlink, paclet:ref/c/stdlink]) from the Wolfram Language, and save it in a local loopback link for later processing.

You can also use (NonWLLink[WSTransferExpression(), paclet:ref/c/WSTransferExpression]) to take an expression that you have built up on a local loopback link, and transfer it back to the Wolfram Language via (NonWLLink[stdlink, paclet:ref/c/stdlink]).

This puts 21! onto a local loopback link.

...
WSPutFunction(wstp, "Factorial", 1);
  WSPutInteger32(wstp, 21);

This sends the head (RefLink[FactorInteger, paclet:ref/FactorInteger]) to the Wolfram Language.

WSPutFunction(stdlink, "FactorInteger", 1);

This transfers the 21! from the loopback link to (NonWLLink[stdlink, paclet:ref/c/stdlink]).

WSTransferExpression(stdlink, wstp);

You can put any sequence of expressions onto a loopback link. Usually you get the expressions off the link in the same order as you put them on.

And once you have got an expression off the link it is usually no longer saved. But by using (NonWLLink[WSCreateMark(), paclet:ref/c/WSCreateMark]) you can mark a particular position in a sequence of expressions on a link, forcing WSTP to save every expression after the mark so that you can go back to it later.

Setting up marks in WSTP links.

This puts the integer 45 onto a loopback link.

...
WSPutInteger32(wstp, 45);

This puts 33 onto the link.

WSPutInteger32(wstp, 33);

And this puts 76.

WSPutInteger32(wstp, 76);

This will read 45 from the link. The 45 will no longer be saved.

WSGetInteger32(wstp, &i);

This creates a mark at the current position on the link.

mark = WSCreateMark(wstp);

This will now read 33.

WSGetInteger32(wstp, &i);

And this will read 76.

WSGetInteger32(wstp, &i);

This goes back to the position of the mark.

WSSeekMark(wstp, mark, 0);

Now this will read 33 again.

WSGetInteger32(wstp, &i);

It is important to destroy marks when you have finished with them, so no unnecessary expressions will be saved.

WSDestroyMark(wstp, mark);

The way the WSTP library is implemented, it is very efficient to open and close loopback links, and to create and destroy marks in them. The only point to remember is that as soon as you create a mark on a particular link, WSTP will save subsequent expressions that are put on that link, and will go on doing this until the mark is destroyed.

Functions for getting pieces of expressions from a link.

Constants returned by (NonWLLink[WSGetNext, paclet:ref/c/WSGetNext])().

switch(WSGetNext(wstp)) {

This reads a composite function.

case WSTKFUNC:
    WSGetArgCount(wstp, &n);
     recurse for head
    for (i = 0; i < n; i++) {
         recurse for each argument
    }
    …

This reads a single symbol.

case WSTKSYM:
    WSGetSymbol(wstp, &name);
    …

This reads a machine integer.

case WSTKINT:
    WSGetInteger32(wstp, &i);
    … 
}

By using WSGetNext() it is straightforward to write programs that can read any expression. The way WSTP works, the head and arguments of a function appear as successive expressions on the link, which you read one after another.

Note that if you know that the head of a function will be a symbol, then you can use WSGetFunction() instead of WSGetNext(). In this case, however, you still need to call WSReleaseSymbol() to disown the memory used to store the symbol name.

Functions for putting pieces of expressions onto a link.

(NonWLLink[WSPutNext(), paclet:ref/c/WSPutNext]) specifies types of expressions using constants such as WSTKFUNC from the wstp.h header file—just like (NonWLLink[WSGetNext(), paclet:ref/c/WSGetNext]).