1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
# -*- mode: Perl -*-
# /=====================================================================\ #
# | expl3.lua | #
# | 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;
use List::Util qw(min max);
# Translation of the intarray portion of expl3.lua
# lua arrays are typically 1-based
# Apparently an expl3 intarray is encoded at the TeX level by \__intarray:w <Number>
# We store actual ints in the array, seems more efficient (could be Number or Tokens)
DefParameterType('Intarray', sub {
my ($gullet) = @_;
my $tok = $gullet->readXToken;
if ($tok && $tok->defined_as(T_CS('\__intarray:w'))) {
my $n = $gullet->readNumber;
my $address = '__intarray' . '_' . ToString($tok) . '_' . $n->valueOf;
my $table = LookupValue($address);
if (!$table) { # Create new table, if none
$table = [];
AssignValue($address => $table, 'global'); }
return $table; }
else {
$gullet->unread($tok);
Error('expected', '__intarray:w', $gullet,
"Expected an intarray identifier, got " . ToString($tok));
return []; } });
DefPrimitiveI('\__intarray:w', 'Number', sub {
Error('unexpected', '\__intarray:w', $_[0], "Unexpected isolated \\__intarray:w?");
return; },
protected => 1);
DefPrimitiveI('\__intarray_gset_count:Nw', 'Intarray Number', sub {
my ($stomach, $table, $newlength) = @_;
my $length = scalar(@$table);
$newlength = $newlength->valueOf;
$$table[$newlength - 1] = 0; # Grow in one step for efficiency
for (my $i = $length + 1 ; $i <= $newlength ; $i++) {
$$table[$i - 1] = 0; }
return; },
protected => 1);
DefMacroI('\intarray_count:N', 'Intarray', sub {
my ($gullet, $table) = @_;
return Explode(scalar(@$table)); });
DefMacroI('\__intarray_gset:wF', 'Number Intarray Number {}', sub {
my ($stomach, $pos, $table, $value, $ifmissing) = @_;
my $length = scalar(@$table);
$pos = $pos->valueOf;
if (($pos > 0) && ($pos <= $length)) {
$$table[$pos - 1] = $value->valueOf; }
else {
return $ifmissing; }
return; },
protected => 1);
DefMacroI('\__intarray_gset:w', 'Number Intarray Number', sub {
my ($stomach, $pos, $table, $value) = @_;
my $length = scalar(@$table);
$pos = $pos->valueOf;
if ($pos > 0) {
$$table[$pos - 1] = $value->valueOf; }
return; },
protected => 1);
DefPrimitiveI('\intarray_gzero:N', 'Intarray', sub {
my ($stomach, $table) = @_;
my $length = scalar(@$table);
for (my $i = 1 ; $i <= $length ; $i++) {
$$table[$i - 1] = 0; }
return; },
protected => 1);
DefMacroI('\__intarray_item:wF', 'Number Intarray {}', sub {
my ($stomach, $pos, $table, $ifmissing) = @_;
my $length = scalar(@$table);
$pos = $pos->valueOf;
if (($pos > 0) && ($pos <= $length)) {
return Explode($$table[$pos - 1]); }
else {
return $ifmissing; } });
DefMacroI('\__intarray_item:w', 'Number Intarray', sub {
my ($stomach, $pos, $table) = @_;
my $length = scalar(@$table);
$pos = $pos->valueOf;
if (($pos > 0) && ($pos <= $length)) {
return Explode($$table[$pos - 1]); }
return; });
DefMacroI('\__intarray_to_clist:Nn', 'Intarray', sub {
my ($stomach, $table) = @_;
return Tokenize(join(', ', @$table)); });
DefMacroI('\__intarray_range_to_clist:w', 'Intarray Number Number', sub {
my ($stomach, $table, $from, $to) = @_;
my $length = scalar(@$table);
$from = min(max($from->valueOf - 1, 0), $length - 1);
$to = min(max($to->valueOf - 1, $from), $length - 1);
return Tokenize(join(', ', @$table[$from .. $to])); });
DefPrimitiveI('\__intarray_gset_range:w', 'Number Intarray', sub {
my ($stomach, $from, $table) = @_;
my $length = scalar(@$table);
Error('unimplemented', '\__intarray_gset_range:w', $stomach,
"This command is not yet implemented");
# read commas, Numbers, store in successive positions starting at $from
return; },
protected => 1);
1;
|