paulb@22 | 1 | #!/usr/bin/env python |
paulb@22 | 2 | |
paulb@22 | 3 | """ |
paulb@22 | 4 | Simple built-in classes and functions. |
paulb@22 | 5 | |
paulb@22 | 6 | Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@22 | 7 | |
paulb@22 | 8 | This software is free software; you can redistribute it and/or |
paulb@22 | 9 | modify it under the terms of the GNU General Public License as |
paulb@22 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@22 | 11 | the License, or (at your option) any later version. |
paulb@22 | 12 | |
paulb@22 | 13 | This software is distributed in the hope that it will be useful, |
paulb@22 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@22 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@22 | 16 | GNU General Public License for more details. |
paulb@22 | 17 | |
paulb@22 | 18 | You should have received a copy of the GNU General Public |
paulb@22 | 19 | License along with this library; see the file LICENCE.txt |
paulb@22 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@22 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@22 | 22 | """ |
paulb@22 | 23 | |
paulb@22 | 24 | class boolean: |
paulb@22 | 25 | def __true__(self): |
paulb@22 | 26 | return self |
paulb@22 | 27 | |
paulb@22 | 28 | def __str__(self): |
paulb@22 | 29 | if self: |
paulb@22 | 30 | return "True" |
paulb@22 | 31 | else: |
paulb@22 | 32 | return "False" |
paulb@22 | 33 | |
paulb@22 | 34 | class int: |
paulb@22 | 35 | def __iadd__(self, other): |
paulb@22 | 36 | if isinstance(other, int): |
paulb@22 | 37 | return int() |
paulb@22 | 38 | else: |
paulb@55 | 39 | raise TypeError |
paulb@22 | 40 | |
paulb@22 | 41 | def __isub__(self, other): |
paulb@22 | 42 | if isinstance(other, int): |
paulb@22 | 43 | return int() |
paulb@22 | 44 | else: |
paulb@55 | 45 | raise TypeError |
paulb@22 | 46 | |
paulb@22 | 47 | def __add__(self, other): |
paulb@22 | 48 | if isinstance(other, int): |
paulb@22 | 49 | return int() |
paulb@22 | 50 | else: |
paulb@55 | 51 | raise TypeError |
paulb@22 | 52 | |
paulb@22 | 53 | def __radd__(self, other): |
paulb@22 | 54 | if isinstance(other, int): |
paulb@22 | 55 | return int() |
paulb@22 | 56 | else: |
paulb@55 | 57 | raise TypeError |
paulb@22 | 58 | |
paulb@22 | 59 | def __sub__(self, other): |
paulb@22 | 60 | if isinstance(other, int): |
paulb@22 | 61 | return int() |
paulb@22 | 62 | else: |
paulb@55 | 63 | raise TypeError |
paulb@22 | 64 | |
paulb@22 | 65 | def __rsub__(self, other): |
paulb@22 | 66 | if isinstance(other, int): |
paulb@22 | 67 | return int() |
paulb@22 | 68 | else: |
paulb@55 | 69 | raise TypeError |
paulb@22 | 70 | |
paulb@22 | 71 | def __mul__(self, other): |
paulb@22 | 72 | if isinstance(other, int): |
paulb@22 | 73 | return int() |
paulb@22 | 74 | else: |
paulb@55 | 75 | raise TypeError |
paulb@22 | 76 | |
paulb@22 | 77 | def __rmul__(self, other): |
paulb@22 | 78 | if isinstance(other, int): |
paulb@22 | 79 | return int() |
paulb@22 | 80 | else: |
paulb@55 | 81 | raise TypeError |
paulb@22 | 82 | |
paulb@22 | 83 | def __div__(self, other): |
paulb@22 | 84 | if isinstance(other, int): |
paulb@22 | 85 | return int() |
paulb@22 | 86 | else: |
paulb@55 | 87 | raise TypeError |
paulb@22 | 88 | |
paulb@22 | 89 | def __rdiv__(self, other): |
paulb@22 | 90 | if isinstance(other, int): |
paulb@22 | 91 | return int() |
paulb@22 | 92 | else: |
paulb@55 | 93 | raise TypeError |
paulb@22 | 94 | |
paulb@22 | 95 | def __pow__(self, other): |
paulb@22 | 96 | if isinstance(other, int): |
paulb@22 | 97 | return int() |
paulb@22 | 98 | else: |
paulb@55 | 99 | raise TypeError |
paulb@22 | 100 | |
paulb@22 | 101 | def __lt__(self, other): |
paulb@22 | 102 | if isinstance(other, int): |
paulb@22 | 103 | return boolean() |
paulb@22 | 104 | else: |
paulb@55 | 105 | raise TypeError |
paulb@22 | 106 | |
paulb@22 | 107 | def __gt__(self, other): |
paulb@22 | 108 | if isinstance(other, int): |
paulb@22 | 109 | return boolean() |
paulb@22 | 110 | else: |
paulb@55 | 111 | raise TypeError |
paulb@22 | 112 | |
paulb@22 | 113 | def __le__(self, other): |
paulb@22 | 114 | if isinstance(other, int): |
paulb@22 | 115 | return boolean() |
paulb@22 | 116 | else: |
paulb@55 | 117 | raise TypeError |
paulb@22 | 118 | |
paulb@22 | 119 | def __ge__(self, other): |
paulb@22 | 120 | if isinstance(other, int): |
paulb@22 | 121 | return boolean() |
paulb@22 | 122 | else: |
paulb@55 | 123 | raise TypeError |
paulb@22 | 124 | |
paulb@22 | 125 | def __eq__(self, other): |
paulb@22 | 126 | if isinstance(other, int): |
paulb@22 | 127 | return boolean() |
paulb@22 | 128 | else: |
paulb@55 | 129 | raise TypeError |
paulb@22 | 130 | |
paulb@22 | 131 | def __ne__(self, other): |
paulb@22 | 132 | if isinstance(other, int): |
paulb@22 | 133 | return boolean() |
paulb@22 | 134 | else: |
paulb@55 | 135 | raise TypeError |
paulb@22 | 136 | |
paulb@22 | 137 | def __neg__(self): |
paulb@22 | 138 | return int() |
paulb@22 | 139 | |
paulb@22 | 140 | def __pos__(self): |
paulb@22 | 141 | return self |
paulb@22 | 142 | |
paulb@22 | 143 | def __str__(self): |
paulb@22 | 144 | return string() |
paulb@22 | 145 | |
paulb@22 | 146 | def __true__(self): |
paulb@22 | 147 | return self != 0 |
paulb@22 | 148 | |
paulb@22 | 149 | class long: |
paulb@22 | 150 | def __iadd__(self, other): |
paulb@22 | 151 | if isinstance(other, int): |
paulb@22 | 152 | return long() |
paulb@22 | 153 | elif isinstance(other, long): |
paulb@22 | 154 | return long() |
paulb@22 | 155 | else: |
paulb@55 | 156 | raise TypeError |
paulb@22 | 157 | |
paulb@22 | 158 | def __isub__(self, other): |
paulb@22 | 159 | if isinstance(other, int): |
paulb@22 | 160 | return long() |
paulb@22 | 161 | elif isinstance(other, long): |
paulb@22 | 162 | return long() |
paulb@22 | 163 | else: |
paulb@55 | 164 | raise TypeError |
paulb@22 | 165 | |
paulb@22 | 166 | def __add__(self, other): |
paulb@22 | 167 | if isinstance(other, int): |
paulb@22 | 168 | return long() |
paulb@22 | 169 | elif isinstance(other, long): |
paulb@22 | 170 | return long() |
paulb@22 | 171 | else: |
paulb@55 | 172 | raise TypeError |
paulb@22 | 173 | |
paulb@22 | 174 | def __radd__(self, other): |
paulb@22 | 175 | if isinstance(other, int): |
paulb@22 | 176 | return long() |
paulb@22 | 177 | elif isinstance(other, long): |
paulb@22 | 178 | return long() |
paulb@22 | 179 | else: |
paulb@55 | 180 | raise TypeError |
paulb@22 | 181 | |
paulb@22 | 182 | def __sub__(self, other): |
paulb@22 | 183 | if isinstance(other, int): |
paulb@22 | 184 | return long() |
paulb@22 | 185 | elif isinstance(other, long): |
paulb@22 | 186 | return long() |
paulb@22 | 187 | else: |
paulb@55 | 188 | raise TypeError |
paulb@22 | 189 | |
paulb@22 | 190 | def __rsub__(self, other): |
paulb@22 | 191 | if isinstance(other, int): |
paulb@22 | 192 | return long() |
paulb@22 | 193 | elif isinstance(other, long): |
paulb@22 | 194 | return long() |
paulb@22 | 195 | else: |
paulb@55 | 196 | raise TypeError |
paulb@22 | 197 | |
paulb@22 | 198 | def __lt__(self, other): |
paulb@22 | 199 | if isinstance(other, int): |
paulb@22 | 200 | return boolean() |
paulb@22 | 201 | elif isinstance(other, long): |
paulb@22 | 202 | return boolean() |
paulb@22 | 203 | else: |
paulb@55 | 204 | raise TypeError |
paulb@22 | 205 | |
paulb@22 | 206 | def __gt__(self, other): |
paulb@22 | 207 | if isinstance(other, int): |
paulb@22 | 208 | return boolean() |
paulb@22 | 209 | elif isinstance(other, long): |
paulb@22 | 210 | return boolean() |
paulb@22 | 211 | else: |
paulb@55 | 212 | raise TypeError |
paulb@22 | 213 | |
paulb@22 | 214 | def __le__(self, other): |
paulb@22 | 215 | if isinstance(other, int): |
paulb@22 | 216 | return boolean() |
paulb@22 | 217 | elif isinstance(other, long): |
paulb@22 | 218 | return boolean() |
paulb@22 | 219 | else: |
paulb@55 | 220 | raise TypeError |
paulb@22 | 221 | |
paulb@22 | 222 | def __ge__(self, other): |
paulb@22 | 223 | if isinstance(other, int): |
paulb@22 | 224 | return boolean() |
paulb@22 | 225 | elif isinstance(other, long): |
paulb@22 | 226 | return boolean() |
paulb@22 | 227 | else: |
paulb@55 | 228 | raise TypeError |
paulb@22 | 229 | |
paulb@22 | 230 | def __eq__(self, other): |
paulb@22 | 231 | if isinstance(other, int): |
paulb@22 | 232 | return boolean() |
paulb@22 | 233 | elif isinstance(other, long): |
paulb@22 | 234 | return boolean() |
paulb@22 | 235 | else: |
paulb@55 | 236 | raise TypeError |
paulb@22 | 237 | |
paulb@22 | 238 | def __ne__(self, other): |
paulb@22 | 239 | if isinstance(other, int): |
paulb@22 | 240 | return boolean() |
paulb@22 | 241 | elif isinstance(other, long): |
paulb@22 | 242 | return boolean() |
paulb@22 | 243 | else: |
paulb@55 | 244 | raise TypeError |
paulb@22 | 245 | |
paulb@22 | 246 | def __neg__(self): |
paulb@22 | 247 | return long() |
paulb@22 | 248 | |
paulb@22 | 249 | def __pos__(self): |
paulb@22 | 250 | return self |
paulb@22 | 251 | |
paulb@22 | 252 | def __str__(self): |
paulb@22 | 253 | return string() |
paulb@22 | 254 | |
paulb@22 | 255 | def __true__(self): |
paulb@22 | 256 | return self != 0 |
paulb@22 | 257 | |
paulb@22 | 258 | class float: |
paulb@22 | 259 | def __iadd__(self, other): |
paulb@22 | 260 | if isinstance(other, int): |
paulb@22 | 261 | return float() |
paulb@22 | 262 | elif isinstance(other, long): |
paulb@22 | 263 | return float() |
paulb@22 | 264 | elif isinstance(other, float): |
paulb@22 | 265 | return float() |
paulb@22 | 266 | else: |
paulb@55 | 267 | raise TypeError |
paulb@22 | 268 | |
paulb@22 | 269 | def __isub__(self, other): |
paulb@22 | 270 | if isinstance(other, int): |
paulb@22 | 271 | return float() |
paulb@22 | 272 | elif isinstance(other, long): |
paulb@22 | 273 | return float() |
paulb@22 | 274 | elif isinstance(other, float): |
paulb@22 | 275 | return float() |
paulb@22 | 276 | else: |
paulb@55 | 277 | raise TypeError |
paulb@22 | 278 | |
paulb@22 | 279 | def __add__(self, other): |
paulb@22 | 280 | if isinstance(other, int): |
paulb@22 | 281 | return float() |
paulb@22 | 282 | elif isinstance(other, long): |
paulb@22 | 283 | return float() |
paulb@22 | 284 | elif isinstance(other, float): |
paulb@22 | 285 | return float() |
paulb@22 | 286 | else: |
paulb@55 | 287 | raise TypeError |
paulb@22 | 288 | |
paulb@22 | 289 | def __radd__(self, other): |
paulb@22 | 290 | if isinstance(other, int): |
paulb@22 | 291 | return float() |
paulb@22 | 292 | elif isinstance(other, long): |
paulb@22 | 293 | return float() |
paulb@22 | 294 | elif isinstance(other, float): |
paulb@22 | 295 | return float() |
paulb@22 | 296 | else: |
paulb@55 | 297 | raise TypeError |
paulb@22 | 298 | |
paulb@22 | 299 | def __sub__(self, other): |
paulb@22 | 300 | if isinstance(other, int): |
paulb@22 | 301 | return float() |
paulb@22 | 302 | elif isinstance(other, long): |
paulb@22 | 303 | return float() |
paulb@22 | 304 | elif isinstance(other, float): |
paulb@22 | 305 | return float() |
paulb@22 | 306 | else: |
paulb@55 | 307 | raise TypeError |
paulb@22 | 308 | |
paulb@22 | 309 | def __rsub__(self, other): |
paulb@22 | 310 | if isinstance(other, int): |
paulb@22 | 311 | return float() |
paulb@22 | 312 | elif isinstance(other, long): |
paulb@22 | 313 | return float() |
paulb@22 | 314 | elif isinstance(other, float): |
paulb@22 | 315 | return float() |
paulb@22 | 316 | else: |
paulb@55 | 317 | raise TypeError |
paulb@22 | 318 | |
paulb@22 | 319 | def __mul__(self, other): |
paulb@22 | 320 | if isinstance(other, int): |
paulb@22 | 321 | return float() |
paulb@22 | 322 | elif isinstance(other, long): |
paulb@22 | 323 | return float() |
paulb@22 | 324 | elif isinstance(other, float): |
paulb@22 | 325 | return float() |
paulb@22 | 326 | else: |
paulb@55 | 327 | raise TypeError |
paulb@22 | 328 | |
paulb@22 | 329 | def __rmul__(self, other): |
paulb@22 | 330 | if isinstance(other, int): |
paulb@22 | 331 | return float() |
paulb@22 | 332 | elif isinstance(other, long): |
paulb@22 | 333 | return float() |
paulb@22 | 334 | elif isinstance(other, float): |
paulb@22 | 335 | return float() |
paulb@22 | 336 | else: |
paulb@55 | 337 | raise TypeError |
paulb@22 | 338 | |
paulb@22 | 339 | def __div__(self, other): |
paulb@22 | 340 | if isinstance(other, int): |
paulb@22 | 341 | return float() |
paulb@22 | 342 | elif isinstance(other, long): |
paulb@22 | 343 | return float() |
paulb@22 | 344 | elif isinstance(other, float): |
paulb@22 | 345 | return float() |
paulb@22 | 346 | else: |
paulb@55 | 347 | raise TypeError |
paulb@22 | 348 | |
paulb@22 | 349 | def __rdiv__(self, other): |
paulb@22 | 350 | if isinstance(other, int): |
paulb@22 | 351 | return float() |
paulb@22 | 352 | elif isinstance(other, long): |
paulb@22 | 353 | return float() |
paulb@22 | 354 | elif isinstance(other, float): |
paulb@22 | 355 | return float() |
paulb@22 | 356 | else: |
paulb@55 | 357 | raise TypeError |
paulb@22 | 358 | |
paulb@22 | 359 | def __pow__(self, other): |
paulb@22 | 360 | if isinstance(other, int): |
paulb@22 | 361 | return float() |
paulb@22 | 362 | elif isinstance(other, long): |
paulb@22 | 363 | return float() |
paulb@22 | 364 | elif isinstance(other, float): |
paulb@22 | 365 | return float() |
paulb@22 | 366 | else: |
paulb@55 | 367 | raise TypeError |
paulb@22 | 368 | |
paulb@22 | 369 | def __rpow__(self, other): |
paulb@22 | 370 | if isinstance(other, int): |
paulb@22 | 371 | return float() |
paulb@22 | 372 | elif isinstance(other, long): |
paulb@22 | 373 | return float() |
paulb@22 | 374 | elif isinstance(other, float): |
paulb@22 | 375 | return float() |
paulb@22 | 376 | else: |
paulb@55 | 377 | raise TypeError |
paulb@22 | 378 | |
paulb@22 | 379 | def __lt__(self, other): |
paulb@22 | 380 | if isinstance(other, int): |
paulb@22 | 381 | return boolean() |
paulb@22 | 382 | elif isinstance(other, long): |
paulb@22 | 383 | return boolean() |
paulb@22 | 384 | elif isinstance(other, float): |
paulb@22 | 385 | return boolean() |
paulb@22 | 386 | else: |
paulb@55 | 387 | raise TypeError |
paulb@22 | 388 | |
paulb@22 | 389 | def __gt__(self, other): |
paulb@22 | 390 | if isinstance(other, int): |
paulb@22 | 391 | return boolean() |
paulb@22 | 392 | elif isinstance(other, long): |
paulb@22 | 393 | return boolean() |
paulb@22 | 394 | elif isinstance(other, float): |
paulb@22 | 395 | return boolean() |
paulb@22 | 396 | else: |
paulb@55 | 397 | raise TypeError |
paulb@22 | 398 | |
paulb@22 | 399 | def __le__(self, other): |
paulb@22 | 400 | if isinstance(other, int): |
paulb@22 | 401 | return boolean() |
paulb@22 | 402 | elif isinstance(other, long): |
paulb@22 | 403 | return boolean() |
paulb@22 | 404 | elif isinstance(other, float): |
paulb@22 | 405 | return boolean() |
paulb@22 | 406 | else: |
paulb@55 | 407 | raise TypeError |
paulb@22 | 408 | |
paulb@22 | 409 | def __ge__(self, other): |
paulb@22 | 410 | if isinstance(other, int): |
paulb@22 | 411 | return boolean() |
paulb@22 | 412 | elif isinstance(other, long): |
paulb@22 | 413 | return boolean() |
paulb@22 | 414 | elif isinstance(other, float): |
paulb@22 | 415 | return boolean() |
paulb@22 | 416 | else: |
paulb@55 | 417 | raise TypeError |
paulb@22 | 418 | |
paulb@22 | 419 | def __eq__(self, other): |
paulb@22 | 420 | if isinstance(other, int): |
paulb@22 | 421 | return boolean() |
paulb@22 | 422 | elif isinstance(other, long): |
paulb@22 | 423 | return boolean() |
paulb@22 | 424 | elif isinstance(other, float): |
paulb@22 | 425 | return boolean() |
paulb@22 | 426 | else: |
paulb@55 | 427 | raise TypeError |
paulb@22 | 428 | |
paulb@22 | 429 | def __ne__(self, other): |
paulb@22 | 430 | if isinstance(other, int): |
paulb@22 | 431 | return boolean() |
paulb@22 | 432 | elif isinstance(other, long): |
paulb@22 | 433 | return boolean() |
paulb@22 | 434 | elif isinstance(other, float): |
paulb@22 | 435 | return boolean() |
paulb@22 | 436 | else: |
paulb@55 | 437 | raise TypeError |
paulb@22 | 438 | |
paulb@22 | 439 | def __neg__(self): |
paulb@22 | 440 | return float() |
paulb@22 | 441 | |
paulb@22 | 442 | def __pos__(self): |
paulb@22 | 443 | return self |
paulb@22 | 444 | |
paulb@22 | 445 | def __str__(self): |
paulb@22 | 446 | return string() |
paulb@22 | 447 | |
paulb@22 | 448 | def __true__(self): |
paulb@22 | 449 | return self != 0 |
paulb@22 | 450 | |
paulb@22 | 451 | class string: |
paulb@22 | 452 | def __add__(self, other): |
paulb@22 | 453 | if isinstance(other, string): |
paulb@22 | 454 | return string() |
paulb@22 | 455 | else: |
paulb@55 | 456 | raise TypeError |
paulb@22 | 457 | |
paulb@22 | 458 | def __radd__(self, other): |
paulb@22 | 459 | if isinstance(other, string): |
paulb@22 | 460 | return string() |
paulb@22 | 461 | else: |
paulb@55 | 462 | raise TypeError |
paulb@22 | 463 | |
paulb@22 | 464 | def __len__(self): |
paulb@22 | 465 | return int() |
paulb@22 | 466 | |
paulb@22 | 467 | def __str__(self): |
paulb@22 | 468 | return self |
paulb@22 | 469 | |
paulb@22 | 470 | def __true__(self): |
paulb@22 | 471 | return self.__len__() != 0 |
paulb@22 | 472 | |
paulb@22 | 473 | def join(self, l): |
paulb@22 | 474 | total = 0 |
paulb@22 | 475 | first = 1 |
paulb@22 | 476 | self_len = self.__len__() |
paulb@22 | 477 | for i in l: |
paulb@22 | 478 | if not first: |
paulb@22 | 479 | total += self_len |
paulb@22 | 480 | total += len(str(i)) |
paulb@22 | 481 | first = 0 |
paulb@22 | 482 | b = buffer(total) |
paulb@22 | 483 | first = 1 |
paulb@22 | 484 | for i in l: |
paulb@22 | 485 | if not first: |
paulb@22 | 486 | b.append(self) |
paulb@22 | 487 | b.append(str(i)) |
paulb@22 | 488 | first = 0 |
paulb@22 | 489 | s = str(b) |
paulb@22 | 490 | return s |
paulb@22 | 491 | |
paulb@22 | 492 | class buffer: |
paulb@22 | 493 | def __init__(self, size): |
paulb@22 | 494 | if not isinstance(size, int): |
paulb@55 | 495 | raise TypeError |
paulb@22 | 496 | |
paulb@22 | 497 | def append(self, s): |
paulb@22 | 498 | if not isinstance(s, string): |
paulb@55 | 499 | raise TypeError |
paulb@22 | 500 | pass |
paulb@22 | 501 | |
paulb@22 | 502 | def __str__(self): |
paulb@22 | 503 | return string() |
paulb@22 | 504 | |
paulb@22 | 505 | class list: |
paulb@89 | 506 | def __init__(self, *args): |
paulb@22 | 507 | self.next = None |
paulb@22 | 508 | self.last = self |
paulb@89 | 509 | for arg in args: |
paulb@89 | 510 | self.append(arg) |
paulb@22 | 511 | |
paulb@22 | 512 | def __getitem__(self, index): |
paulb@22 | 513 | i = 0 |
paulb@22 | 514 | n = self |
paulb@22 | 515 | # NOTE: Support negative indices using last. |
paulb@22 | 516 | while i < index and n.next is not None: |
paulb@22 | 517 | n = n.next |
paulb@22 | 518 | i += 1 |
paulb@22 | 519 | if n.next is not None: |
paulb@22 | 520 | return n.value |
paulb@22 | 521 | else: |
paulb@22 | 522 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 523 | |
paulb@22 | 524 | def __setitem__(self, index, value): |
paulb@22 | 525 | i = 0 |
paulb@22 | 526 | n = self |
paulb@22 | 527 | # NOTE: Support negative indices using last. |
paulb@22 | 528 | while i < index and n.next is not None: |
paulb@22 | 529 | n = n.next |
paulb@22 | 530 | i += 1 |
paulb@22 | 531 | if n.next is not None: |
paulb@22 | 532 | n.value = value |
paulb@22 | 533 | else: |
paulb@22 | 534 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 535 | |
paulb@22 | 536 | def __getslice__(self, start, end=None): |
paulb@22 | 537 | slice = [] |
paulb@22 | 538 | i = 0 |
paulb@22 | 539 | n = self |
paulb@22 | 540 | # NOTE: Support negative indices using last. |
paulb@22 | 541 | while (end is None or i < end) and n.next is not None: |
paulb@22 | 542 | if i >= start: |
paulb@22 | 543 | slice.append(n.value) |
paulb@22 | 544 | n = n.next |
paulb@22 | 545 | i += 1 |
paulb@22 | 546 | return slice |
paulb@22 | 547 | |
paulb@22 | 548 | def __setslice__(self, start, end, slice): |
paulb@22 | 549 | i = 0 |
paulb@22 | 550 | n = self |
paulb@22 | 551 | j = 0 |
paulb@22 | 552 | p = slice |
paulb@22 | 553 | # NOTE: Support negative indices using last. |
paulb@22 | 554 | # NOTE: Support appending when start >= len(self). |
paulb@22 | 555 | while i < end and n is not None and p is not None: |
paulb@22 | 556 | if i >= start: |
paulb@22 | 557 | n.value = p.value |
paulb@22 | 558 | p = p.next |
paulb@22 | 559 | j += 1 |
paulb@22 | 560 | n = n.next |
paulb@22 | 561 | i += 1 |
paulb@22 | 562 | |
paulb@22 | 563 | def append(self, value): |
paulb@22 | 564 | n = self.last |
paulb@22 | 565 | n.value = value |
paulb@22 | 566 | n.next = self.__class__() |
paulb@22 | 567 | self.last = n.next |
paulb@22 | 568 | |
paulb@22 | 569 | def __len__(self): |
paulb@22 | 570 | i = 0 |
paulb@22 | 571 | n = self |
paulb@22 | 572 | while n.next is not None: |
paulb@22 | 573 | n = n.next |
paulb@22 | 574 | i += 1 |
paulb@22 | 575 | return i |
paulb@22 | 576 | |
paulb@22 | 577 | def __add__(self, other): |
paulb@22 | 578 | result = self.__class__() |
paulb@22 | 579 | for value in self: |
paulb@22 | 580 | result.append(value) |
paulb@22 | 581 | for value in other: |
paulb@22 | 582 | result.append(value) |
paulb@22 | 583 | return result |
paulb@22 | 584 | |
paulb@97 | 585 | def __iadd__(self, other): |
paulb@97 | 586 | for value in other: |
paulb@97 | 587 | self.append(value) |
paulb@97 | 588 | return self |
paulb@97 | 589 | |
paulb@22 | 590 | def __str__(self): |
paulb@22 | 591 | output = ["["] |
paulb@22 | 592 | n = self |
paulb@22 | 593 | first = 1 |
paulb@22 | 594 | while n.next is not None: |
paulb@22 | 595 | if not first: |
paulb@22 | 596 | output.append(", ") |
paulb@22 | 597 | else: |
paulb@22 | 598 | first = 0 |
paulb@22 | 599 | output.append(str(n.value)) |
paulb@22 | 600 | n = n.next |
paulb@22 | 601 | output.append("]") |
paulb@22 | 602 | return "".join(output) |
paulb@22 | 603 | |
paulb@22 | 604 | def __iter__(self): |
paulb@22 | 605 | return listiterator(self) |
paulb@22 | 606 | |
paulb@22 | 607 | def __true__(self): |
paulb@22 | 608 | return self.__len__() != 0 |
paulb@22 | 609 | |
paulb@22 | 610 | class listiterator: |
paulb@22 | 611 | def __init__(self, l): |
paulb@22 | 612 | self.l = l |
paulb@22 | 613 | |
paulb@22 | 614 | def next(self): |
paulb@22 | 615 | l = self.l |
paulb@22 | 616 | next = l.next |
paulb@22 | 617 | if next is not None: |
paulb@22 | 618 | self.l = next |
paulb@22 | 619 | return l.value |
paulb@22 | 620 | else: |
paulb@55 | 621 | raise StopIteration |
paulb@22 | 622 | |
paulb@22 | 623 | def __true__(self): |
paulb@22 | 624 | return boolean() |
paulb@22 | 625 | |
paulb@22 | 626 | class tuple: |
paulb@66 | 627 | def __init__(self, *args): |
paulb@22 | 628 | self.next = None |
paulb@22 | 629 | self.last = self |
paulb@66 | 630 | for arg in args: |
paulb@66 | 631 | self.append(arg) |
paulb@22 | 632 | |
paulb@22 | 633 | def __getitem__(self, index): |
paulb@22 | 634 | i = 0 |
paulb@22 | 635 | n = self |
paulb@22 | 636 | # NOTE: Support negative indices using last. |
paulb@22 | 637 | while i < index and n.next is not None: |
paulb@22 | 638 | n = n.next |
paulb@22 | 639 | i += 1 |
paulb@22 | 640 | if n.next is not None: |
paulb@22 | 641 | return n.value |
paulb@22 | 642 | else: |
paulb@22 | 643 | raise IndexError() # NOTE: Make this compliant with Python! |
paulb@22 | 644 | |
paulb@22 | 645 | def __getslice__(self, start, end=None): |
paulb@22 | 646 | # NOTE: Should probably return a tuple. |
paulb@22 | 647 | slice = [] |
paulb@22 | 648 | i = 0 |
paulb@22 | 649 | n = self |
paulb@22 | 650 | # NOTE: Support negative indices using last. |
paulb@22 | 651 | while (end is None or i < end) and n.next is not None: |
paulb@22 | 652 | if i >= start: |
paulb@22 | 653 | slice.append(n.value) |
paulb@22 | 654 | n = n.next |
paulb@22 | 655 | i += 1 |
paulb@22 | 656 | return slice |
paulb@22 | 657 | |
paulb@22 | 658 | # NOTE: The append method should be internal at most. |
paulb@22 | 659 | |
paulb@22 | 660 | def append(self, value): |
paulb@22 | 661 | n = self.last |
paulb@22 | 662 | n.value = value |
paulb@22 | 663 | n.next = self.__class__() |
paulb@22 | 664 | self.last = n.next |
paulb@22 | 665 | |
paulb@22 | 666 | def __len__(self): |
paulb@22 | 667 | i = 0 |
paulb@22 | 668 | n = self |
paulb@22 | 669 | while n.next is not None: |
paulb@22 | 670 | n = n.next |
paulb@22 | 671 | i += 1 |
paulb@22 | 672 | return i |
paulb@22 | 673 | |
paulb@22 | 674 | def __add__(self, other): |
paulb@22 | 675 | result = self.__class__() |
paulb@22 | 676 | for value in self: |
paulb@22 | 677 | result.append(value) |
paulb@22 | 678 | for value in other: |
paulb@22 | 679 | result.append(value) |
paulb@22 | 680 | return result |
paulb@22 | 681 | |
paulb@22 | 682 | def __str__(self): |
paulb@22 | 683 | output = ["("] |
paulb@22 | 684 | n = self |
paulb@22 | 685 | first = 1 |
paulb@22 | 686 | while n.next is not None: |
paulb@22 | 687 | if not first: |
paulb@22 | 688 | output.append(", ") |
paulb@22 | 689 | else: |
paulb@22 | 690 | first = 0 |
paulb@22 | 691 | output.append(str(n.value)) |
paulb@22 | 692 | n = n.next |
paulb@22 | 693 | output.append(")") |
paulb@22 | 694 | return "".join(output) |
paulb@22 | 695 | |
paulb@22 | 696 | def __iter__(self): |
paulb@22 | 697 | return tupleiterator(self) |
paulb@22 | 698 | |
paulb@22 | 699 | def __true__(self): |
paulb@22 | 700 | return self.__len__() != 0 |
paulb@22 | 701 | |
paulb@22 | 702 | class tupleiterator: |
paulb@22 | 703 | def __init__(self, l): |
paulb@22 | 704 | self.l = l |
paulb@22 | 705 | |
paulb@22 | 706 | def next(self): |
paulb@22 | 707 | l = self.l |
paulb@22 | 708 | next = l.next |
paulb@22 | 709 | if next is not None: |
paulb@22 | 710 | self.l = next |
paulb@22 | 711 | return l.value |
paulb@22 | 712 | else: |
paulb@55 | 713 | raise StopIteration |
paulb@22 | 714 | |
paulb@22 | 715 | def __true__(self): |
paulb@22 | 716 | return boolean() |
paulb@22 | 717 | |
paulb@54 | 718 | class dict: |
paulb@54 | 719 | pass |
paulb@54 | 720 | |
paulb@22 | 721 | class Exception: |
paulb@22 | 722 | pass |
paulb@22 | 723 | |
paulb@22 | 724 | class StopIteration(Exception): |
paulb@22 | 725 | pass |
paulb@22 | 726 | |
paulb@22 | 727 | class IndexError(Exception): |
paulb@22 | 728 | pass |
paulb@22 | 729 | |
paulb@22 | 730 | class AttributeError(Exception): |
paulb@22 | 731 | pass |
paulb@22 | 732 | |
paulb@22 | 733 | class TypeError(Exception): |
paulb@22 | 734 | pass |
paulb@22 | 735 | |
paulb@22 | 736 | class none: |
paulb@22 | 737 | def __true__(self): |
paulb@22 | 738 | return False |
paulb@22 | 739 | |
paulb@22 | 740 | def __str__(self): |
paulb@22 | 741 | return "None" |
paulb@22 | 742 | |
paulb@22 | 743 | class undefined: |
paulb@22 | 744 | pass |
paulb@22 | 745 | |
paulb@105 | 746 | class xrange: |
paulb@105 | 747 | def __init__(self, start, end, step=1): |
paulb@105 | 748 | self.start = start |
paulb@105 | 749 | self.end = end |
paulb@105 | 750 | self.step = step |
paulb@105 | 751 | self.current = start |
paulb@105 | 752 | |
paulb@105 | 753 | def __iter__(self): |
paulb@105 | 754 | return self |
paulb@105 | 755 | |
paulb@105 | 756 | def next(self): |
paulb@105 | 757 | if self.current >= self.end: |
paulb@105 | 758 | raise StopIteration |
paulb@105 | 759 | current = self.current |
paulb@105 | 760 | self.current += self.step |
paulb@105 | 761 | return current |
paulb@105 | 762 | |
paulb@105 | 763 | # General functions. |
paulb@105 | 764 | |
paulb@22 | 765 | def isinstance(obj, cls): |
paulb@22 | 766 | return boolean() |
paulb@22 | 767 | |
paulb@22 | 768 | def issubclass(cls1, cls2): |
paulb@22 | 769 | return boolean() |
paulb@22 | 770 | |
paulb@22 | 771 | def len(x): |
paulb@22 | 772 | return x.__len__() |
paulb@22 | 773 | |
paulb@22 | 774 | def max(*l): |
paulb@22 | 775 | max_so_far = l[0] |
paulb@22 | 776 | for i in l[1:]: |
paulb@22 | 777 | if i > max_so_far: |
paulb@22 | 778 | max_so_far = i |
paulb@22 | 779 | return max_so_far |
paulb@22 | 780 | |
paulb@105 | 781 | def range(start, end, step=None): |
paulb@105 | 782 | if start == end: |
paulb@105 | 783 | return [] |
paulb@105 | 784 | elif start > end: |
paulb@105 | 785 | step = step or 1 |
paulb@105 | 786 | i = start |
paulb@105 | 787 | result = [] |
paulb@105 | 788 | while i < end: |
paulb@105 | 789 | result.append(i) |
paulb@105 | 790 | i += 1 |
paulb@105 | 791 | return result |
paulb@105 | 792 | else: |
paulb@105 | 793 | step = step or -1 |
paulb@105 | 794 | i = start |
paulb@105 | 795 | result = [] |
paulb@105 | 796 | while i > end: |
paulb@105 | 797 | result.append(i) |
paulb@105 | 798 | i -= 1 |
paulb@105 | 799 | return result |
paulb@105 | 800 | |
paulb@22 | 801 | def str(x): |
paulb@22 | 802 | return x.__str__() |
paulb@22 | 803 | |
paulb@22 | 804 | # Special values. None of these definitions should be generated by the compiler. |
paulb@22 | 805 | # All such definitions should be made in the underlying implementation. |
paulb@22 | 806 | |
paulb@22 | 807 | True = boolean() |
paulb@22 | 808 | False = boolean() |
paulb@22 | 809 | None = none() |
paulb@22 | 810 | Undefined = undefined() |
paulb@22 | 811 | |
paulb@22 | 812 | # Special functions. These all operate on references at run-time. |
paulb@22 | 813 | |
paulb@22 | 814 | def __is__(a, b): |
paulb@22 | 815 | return boolean() |
paulb@22 | 816 | |
paulb@22 | 817 | def __is_not__(a, b): |
paulb@22 | 818 | return boolean() |
paulb@22 | 819 | |
paulb@22 | 820 | def __not__(a): |
paulb@22 | 821 | return boolean() |
paulb@22 | 822 | |
paulb@22 | 823 | # vim: tabstop=4 expandtab shiftwidth=4 |