| 1 | | # $Id$ |
| 2 | | |
| 3 | | =head1 TITLE |
| 4 | | |
| 5 | | Data::Replace - data replacing implemented in PIR |
| 6 | | |
| 7 | | =head1 SYNOPSIS |
| 8 | | |
| 9 | | ... |
| 10 | | |
| 11 | | load_bytecode "library/Data/Replace.pir" |
| 12 | | |
| 13 | | .local pmc replace |
| 14 | | |
| 15 | | replace = get_hll_global ['Data::Replace'], 'replace' |
| 16 | | |
| 17 | | replace( array, old, new ) |
| 18 | | |
| 19 | | ... |
| 20 | | |
| 21 | | |
| 22 | | =head1 DESCRIPTION |
| 23 | | |
| 24 | | Replaces every occurrence of a specified PMC with another PMC |
| 25 | | in a nested, possible self-referential data structure. |
| 26 | | |
| 27 | | =head1 FUNCTIONS |
| 28 | | |
| 29 | | This library provides the following function: |
| 30 | | |
| 31 | | =over 4 |
| 32 | | |
| 33 | | =cut |
| 34 | | |
| 35 | | .namespace ["Data::Replace"] |
| 36 | | |
| 37 | | =item replace( where, old, new ) |
| 38 | | |
| 39 | | Replaces every "old" with "new" inside the aggregate "where". |
| 40 | | |
| 41 | | =cut |
| 42 | | |
| 43 | | .sub replace |
| 44 | | .param pmc where |
| 45 | | .param pmc oldVal |
| 46 | | .param pmc newVal |
| 47 | | .local pmc cache |
| 48 | | |
| 49 | | new cache, 'ResizablePMCArray' |
| 50 | | __do_replace( where, oldVal, newVal, cache ) |
| 51 | | .end |
| 52 | | |
| 53 | | |
| 54 | | .sub __in_cache |
| 55 | | .param pmc val |
| 56 | | .param pmc cache |
| 57 | | .local int i |
| 58 | | .local pmc temp |
| 59 | | |
| 60 | | set i, cache |
| 61 | | LOOP: |
| 62 | | dec i |
| 63 | | if i < 0 goto END |
| 64 | | temp = cache[i] |
| 65 | | eq_addr val, temp, IS |
| 66 | | branch LOOP |
| 67 | | END: |
| 68 | | .begin_return |
| 69 | | .set_return 0 |
| 70 | | .end_return |
| 71 | | IS: |
| 72 | | .begin_return |
| 73 | | .set_return 1 |
| 74 | | .end_return |
| 75 | | .end |
| 76 | | |
| 77 | | .sub __do_replace |
| 78 | | .param pmc where |
| 79 | | .param pmc oldVal |
| 80 | | .param pmc newVal |
| 81 | | .param pmc cache |
| 82 | | .local pmc temp |
| 83 | | .local string name |
| 84 | | |
| 85 | | if_null where, END |
| 86 | | |
| 87 | | # call __replace if the PMC supports it |
| 88 | | can $I0, where, "__replace" |
| 89 | | unless $I0 goto CANT_REPLACE |
| 90 | | where."__replace"( oldVal, newVal, cache ) |
| 91 | | branch REPLACE_PROPS |
| 92 | | CANT_REPLACE: |
| 93 | | |
| 94 | | # find a Data::Replace method with the name of the type to replace |
| 95 | | typeof name, where |
| 96 | | .include 'errors.pasm' |
| 97 | | errorsoff .PARROT_ERRORS_GLOBALS_FLAG |
| 98 | | temp = get_hll_global ['"Data::Replace'], name |
| 99 | | errorson .PARROT_ERRORS_GLOBALS_FLAG |
| 100 | | # invoke it if found |
| 101 | | $I0 = defined temp |
| 102 | | unless $I0 goto REPLACE_PROPS |
| 103 | | temp( where, oldVal, newVal, cache ) |
| 104 | | |
| 105 | | REPLACE_PROPS: |
| 106 | | prophash temp, where |
| 107 | | set $I0, temp |
| 108 | | if $I0 == 0 goto END |
| 109 | | __do_replace( temp, oldVal, newVal, cache ) |
| 110 | | END: |
| 111 | | .end |
| 112 | | |
| 113 | | .sub __onload :load |
| 114 | | $P0 = get_hll_global ['Data::Replace'], 'ResizablePMCArray' |
| 115 | | set_hll_global ['Data::Replace'], 'PMCArray', $P0 |
| 116 | | set_hll_global ['Data::Replace'], 'StringArray', $P0 |
| 117 | | .end |
| 118 | | |
| 119 | | .sub ResizablePMCArray |
| 120 | | .param pmc where |
| 121 | | .param pmc oldVal |
| 122 | | .param pmc newVal |
| 123 | | .param pmc cache |
| 124 | | .local int i |
| 125 | | .local pmc val |
| 126 | | |
| 127 | | set i, where |
| 128 | | LOOP: |
| 129 | | dec i |
| 130 | | if i < 0 goto END |
| 131 | | |
| 132 | | val = where[i] |
| 133 | | |
| 134 | | ne_addr val, oldVal, SKIP |
| 135 | | where[i] = newVal |
| 136 | | SKIP: |
| 137 | | $I0 = __in_cache( val, cache ) |
| 138 | | if $I0 goto LOOP |
| 139 | | |
| 140 | | push cache, val |
| 141 | | __do_replace( val, oldVal, newVal, cache ) |
| 142 | | branch LOOP |
| 143 | | END: |
| 144 | | .begin_return |
| 145 | | .end_return |
| 146 | | .end |
| 147 | | |
| 148 | | .sub Hash :method |
| 149 | | .param pmc where |
| 150 | | .param pmc oldVal |
| 151 | | .param pmc newVal |
| 152 | | .param pmc cache |
| 153 | | .local pmc iter |
| 154 | | .local string key |
| 155 | | .local pmc val |
| 156 | | |
| 157 | | new iter, 'Iterator', where |
| 158 | | set iter, 0 |
| 159 | | |
| 160 | | LOOP: |
| 161 | | unless iter, END |
| 162 | | |
| 163 | | shift key, iter |
| 164 | | |
| 165 | | val = where[key] |
| 166 | | |
| 167 | | ne_addr val, oldVal, SKIP |
| 168 | | where[key] = newVal |
| 169 | | SKIP: |
| 170 | | $I0 = __in_cache( val, cache ) |
| 171 | | if $I0 goto LOOP |
| 172 | | |
| 173 | | push cache, val |
| 174 | | __do_replace( val, oldVal, newVal, cache ) |
| 175 | | branch LOOP |
| 176 | | |
| 177 | | END: |
| 178 | | .begin_return |
| 179 | | .end_return |
| 180 | | .end |
| 181 | | |
| 182 | | .sub Hash :method |
| 183 | | .param pmc where |
| 184 | | .param pmc oldVal |
| 185 | | .param pmc newVal |
| 186 | | .param pmc cache |
| 187 | | .local pmc iter |
| 188 | | .local string key |
| 189 | | .local pmc val |
| 190 | | |
| 191 | | new iter, 'Iterator', where |
| 192 | | set iter, 0 |
| 193 | | |
| 194 | | LOOP: |
| 195 | | unless iter, END |
| 196 | | |
| 197 | | shift key, iter |
| 198 | | |
| 199 | | val = where[key] |
| 200 | | |
| 201 | | ne_addr val, oldVal, SKIP |
| 202 | | where[key] = newVal |
| 203 | | SKIP: |
| 204 | | $I0 = __in_cache( val, cache ) |
| 205 | | if $I0 goto LOOP |
| 206 | | |
| 207 | | push cache, val |
| 208 | | __do_replace( val, oldVal, newVal, cache ) |
| 209 | | branch LOOP |
| 210 | | |
| 211 | | END: |
| 212 | | .begin_return |
| 213 | | .end_return |
| 214 | | .end |
| 215 | | |
| 216 | | =back |
| 217 | | |
| 218 | | =head1 AUTHOR |
| 219 | | |
| 220 | | Jens Rieks E<lt>parrot at jensbeimsurfen dot deE<gt> is the author |
| 221 | | and maintainer. |
| 222 | | Please send patches and suggestions to the Perl 6 Internals mailing list. |
| 223 | | |
| 224 | | =head1 COPYRIGHT |
| 225 | | |
| 226 | | Copyright (C) 2004-2008, The Perl Foundation. |
| 227 | | |
| 228 | | =cut |
| 229 | | |
| 230 | | # Local Variables: |
| 231 | | # mode: pir |
| 232 | | # fill-column: 100 |
| 233 | | # End: |
| 234 | | # vim: expandtab shiftwidth=4 ft=pir: |