Variadic Scanning in pure C First Example: *working*

This post from October 2017 announces a turning point in the API, which has been very fruitful.

Many things have changed (including wrapping in a variadic macro that automatically supplies the "END", and being able to access native arguments in the code strings). But the basic idea remains the same. The bit patterns for internal types carefully avoid having the first byte of those structures overlap legal leading bytes of a UTF-8 sequence, so you can splice together runs of code with cell and series pointers.

Modern naming would be DECLARE_NATIVE() instead of REBNATIVE(), Array* instead of REBARR*, and OUT instead of D_OUT.


I'm happy to report that a bit of hacking led to a successful first test flight of fully C-standards compliant, variadic scanning.

Here's a quick sample native:

//
//  variadic-scan-test: native [
//
//  {Demo the routine powering the variadic scanner with 3 items}
//
//      item1
//      item2
//      item3
//  ]
//
REBNATIVE(variadic_scan_test)
{
    INCLUDE_PARAMS_OF_VARIADIC_SCAN_TEST;

    REBARR *result = Scan_Va_Managed(
        "if not", ARG(item1), "[\n",
            ARG(item2), "| print {Close brace separate from content}\n",
        "] else [\n",
            ARG(item3), "| print {Close brace with content}]\n",
        END
    );
    Init_Block(D_OUT, result);
    return R_OUT;
}

Invoking it actually worked on the first try, as intended:

>> variadic-scan-test 1 {Two} [T h r e e]
== [if not 1 [
    "Two" | print "Close brace separate from content"
] else [
    [T h r e e] | print "Close brace with content"
]]

Pretty nifty, huh?

If you're not concerned about the newlines (most rebDo() or whatever you call it would perhaps not be) then it could look a bit cleaner. We could also consider some separate token for that so it didn't make the code as ugly.

1 Like