26.2 The Top-level Translation Functions

Above we basically treated all the special cases. Let us turn to the actual translation functions.

There are two functions that are called in the main while loop above.

adtranslateAdsnippetText
This function should be called for the lines in an adsnippet and adusage environment.
adtranslateLine
This function should be called to translate ordinary LATEX lines in aldordoc interface format to aldordoc implementation format.

From the implementation point of view adtranslateAdsnippetText is the simpler one. An adsnippet like environment is realized in aldordoc.sty.nw by escaping anything with verb that is not one of the active commands.

298adtranslateAdsnippetText 298  (326)
sub adtranslateAdsnippetText {
    my ($text) = @_;
    return &adtranslateActiveCommands($text, \&escapeForVerb, ’’)
}

Defines:
adtranslateAdsnippetText, used in chunk 297.

Uses adtranslateActiveCommands 314 and escapeForVerb 301b.

A bit more complex is the function adtranslateLine. It basically works as follows. If there is an adcode command within a line, the line is split into the text before adcode, the argument of adcode, and the text after the adcode argument. Of course, we take care of the argument delimiter character. In particular, we treat

\adcode{SOME CODE}

in the same way as

\adcode}SOME CODE}

where the closing brace } is considered to be the delimiting character.

The text before adcode is investigated for aldordoc interface-only commands via the function adtranslateInterfaceOnlyCommands. The string adcode is thrown away and its argument is replaced by the output of the function adtranslateAdcodeArgument.

The text after the argument of adcode is treated recursively. Well, we do it in a while loop.

300adtranslateLine 300  (326)
sub adidentity {my($s)=@_; return $s;}
sub adtranslateLine {
    my ($after) = @_; # we assume that there is no newline at the end!
    my ($before, $code, $ret, $delim) = (’’,’’,’’,’’);

    while ($after =~ /\\adcode(.)/) {
        $before = $‘;
        $delim = $1;
        $after = $’; #’
        if ($delim eq ’{’) {$delim = ’}’;}
        $pos = index($after, $delim);
        if ($pos == -1) {
            print STDERR "ERROR: aldordoc2tex.pl:\\adcode not closed.\n";
            print STDERR "$_[0]\n";
            die "";
        }
        $code = substr($after, 0, $pos);
        $after = substr($after, $pos+1);
        $ret .= &adtranslateInterfaceOnlyCommands($before, \&adidentity);
        $ret .= &adtranslateAdcodeArgument($code, $delim);
    }
    $ret .= &adtranslateInterfaceOnlyCommands($after, \&adidentity);
    return $ret;
}

Defines:
adtranslateLine, used in chunk 293.

Uses adtranslateAdcodeArgument 301a, adtranslateInterfaceOnlyCommands 303, and code 432.

Again, the translation of the adcode argument is as simple as for the adsnippet environment done by the function adtranslateAdsnippetText, since the main work is done by the function adtranslateActiveCommands.

301aadtranslateAdcodeArgument 301a  (326)
sub adtranslateAdcodeArgument {
    my ($code, $delim) = @_;
    return &adtranslateActiveCommands($code, \&escapeForVerb, $delim)
}

Defines:
adtranslateAdcodeArgument, used in chunk 300.

Uses adtranslateActiveCommands 314, code 432, and escapeForVerb 301b.

This text is considered by LATEX for expansion, so we must really turn it into verbatim text by generating a verb command. If an empty string is given, no verb command is generated. An empty delimiter means that we must choose a delimiter and take care of the case if the the delimiter is part of the string. We choose the character &, because it is a reserved character for the Aldor language and does usually not appear in an Aldor program. Nevertheless, the implementation below takes care of possible appearances of this character.

301bauxiliary subroutines 301b  (326)  323
sub escapeForVerb {
    my ($s, $delim) = @_;
    if ($s eq ’’) {return $s;}
    if ($delim eq ’’) {
        $delim = ’&’;
        $s = join("$delim\\verb|$delim|\verb$delim", split(/$delim/,$s));
    }
    return "\\verb$delim$s$delim";
}

Defines:
escapeForVerb, used in chunks 298 and 301a.