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