# -*- mode: Perl -*-
# /=====================================================================\ #
# | multido.sty                                                         | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;

###########################################################################
##
###########################################################################
DefRegister('\multido@count' => Number(0));
DefRegister('\multidocount'  => Number(0));
DefRegister('\multido@stuff' => Tokens());

DefMacro('\multido',  '\multido@{}{\begingroup}{\endgroup}');
DefMacro('\mmultido', '\multido@{\multido@stepvar}{\begingroup}{\endgroup}');
DefMacro('\Multido',  '\multido@{}{}{}');
DefMacro('\MMultido', '\multido@{\multido@stepvar}{}{}');

DefMacro('\multido@{}{}{}{}{}{}',
  '#2'
    . '\multido@count=#5\relax'
    . '\ifnum\multido@count=\z@\else\multido@@{#1}{#4}{#6}\fi'
    . '#3'
    . '\ignorespaces');

# Simplified...
DefMacro('\multido@@{}{}{}',
  '\multido@@initvars@@{#2}'
    . '\ifnum\multido@count<\z@\multido@count=-\multido@count\fi'
    . ' \multidocount=1\relax'
    . '#1'
    . '\multido@stuff{#3}'
    . '\multido@loop');
DefMacro('\multido@loop',
  '\the\multido@stuff'
    . '\ifnum\multidocount<\multido@count'
    . '\advance\multidocount\@ne'
    . '\multido@stepvar'
    . '\expandafter\multido@loop'
    . '\fi');
DefMacro('\multidostop', '\multidocount=\multido@count');

# Annoyances with variables:
#   Dimensions are always printed in scaled points (sp)
#   Number are fixed point (and print that way!)
# concievably variables can be redefined in middle of loop?
DefMacro('\multido@@initvars@@{}', sub {
    my ($ogullet, $variables) = @_;
    $ogullet->readingFromMouth(LaTeXML::Core::Mouth->new(), sub {
        my ($gullet) = @_;
        $gullet->unread($variables);
        my @inits = ();
        my @steps = ();
        $gullet->skipSpaces;
        while (my $var = $gullet->readToken) {
          if ($var->getCSName =~ /^\\(d|n|i|r)/i) {
            my $type = lc($1);
            $gullet->readKeyword("=")
              or Error('expected', '=', $gullet, "Missing = in multido variables");
            my ($init, $step);
            if    ($type eq 'd') { $init = Tokens(Explode($gullet->readDimension->valueOf . "sp")); }
            elsif ($type eq 'n') { $init = Tokens($gullet->readFloat->revert); }
            elsif ($type eq 'i') { $init = Tokens($gullet->readNumber->revert); }
            elsif ($type eq 'r') { $init = Tokens($gullet->readFloat->revert); }
            push(@inits, T_CS('\def'), $var, T_BEGIN, $init, T_END);
            $gullet->readKeyword("+")
              or Error('expected', '+', $gullet, "Missing + in multido variables");
            if    ($type eq 'd') { $step = $gullet->readDimension; }
            elsif ($type eq 'n') { $step = $gullet->readFloat; }
            elsif ($type eq 'i') { $step = $gullet->readNumber; }
            elsif ($type eq 'r') { $step = $gullet->readFloat; }
            $step = $step->negate if LookupValue('\multido@count')->valueOf < 0;
            push(@steps, T_CS('\multido@step@' . $type), $var, T_BEGIN, $step->revert, T_END);
            last unless $gullet->readKeyword(","); }
          else {
            Error('unexpected', $var, $gullet,
              "Wrong format for multido variable '" . ToString($var) . "'"); }
          $gullet->skipSpaces; }
        DefMacroI('\multido@stepvar', undef, Tokens(@steps));
        # Return the tokens to initialize the vars
        @inits; }); });

DefMacro('\multido@step@d DefToken {Dimension}', sub {
    my ($gullet, $v, $step) = @_;
    my $value = Dimension(ToString(Expand($v)))->add($step);
    DefMacroI($v, undef, Tokens(Explode($value->valueOf . 'sp'))); });
DefMacro('\multido@step@i DefToken {Number}', sub {
    my ($gullet, $v, $step) = @_;
    my $value = Number(ToString(Expand($v)))->add($step);
    DefMacroI($v, undef, Tokens(Explode($value->valueOf))); });
DefMacro('\multido@step@r DefToken {Float}', sub {
    my ($gullet, $v, $step) = @_;
    my $value = Float(ToString(Expand($v)))->add($step);
    DefMacroI($v, undef, Tokens(Explode($value->valueOf))); });
# Note: n _should_ be fixed point!
# DefMacro('\multido@step@n DefToken {Float}',sub {
#   my($gullet,$v,$step)=@_;
#   my $value = Float(ToString(Expand($v)))->add($step);
#   DefMacroI($v,undef,Tokens(Explode($value->valueOf))); });
DefMacro('\multido@step@n DefToken {}', '\fpAdd{#1}{#2}{#1}');

# Should evolve these to work in fixed point (particularly, the formatting?)
DefMacro('\fpAdd {Float} {Float} DefToken', sub {
    my ($gullet, $a, $b, $token) = @_;
    my $value = $a->add($b);
    DefMacroI($token, undef, Tokens(Explode($value->valueOf))); });
DefMacro('\fpSub {Float} {Float} DefToken', sub {
    my ($gullet, $a, $b, $token) = @_;
    my $value = $a->subtract($b);
    DefMacroI($token, undef, Tokens(Explode($value->valueOf))); });
1;
