1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/builtins.py Sun Jul 23 23:55:46 2006 +0200
1.3 @@ -0,0 +1,1115 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Simple built-in classes and functions.
1.8 +
1.9 +Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This software is free software; you can redistribute it and/or
1.12 +modify it under the terms of the GNU General Public License as
1.13 +published by the Free Software Foundation; either version 2 of
1.14 +the License, or (at your option) any later version.
1.15 +
1.16 +This software is distributed in the hope that it will be useful,
1.17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 +GNU General Public License for more details.
1.20 +
1.21 +You should have received a copy of the GNU General Public
1.22 +License along with this library; see the file LICENCE.txt
1.23 +If not, write to the Free Software Foundation, Inc.,
1.24 +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1.25 +
1.26 +--------
1.27 +
1.28 +The docstring annotations have the following meanings:
1.29 +
1.30 + NAME Indicates a "stable" name used by callers of a function
1.31 + instead of a generated name which would distinguish
1.32 + different specialisations.
1.33 +
1.34 + NATIVE Means that the class or function body details are not
1.35 + accurate representations of the actual code and should not
1.36 + be generated by a compiler.
1.37 +
1.38 + INTERCHANGEABLE Indicates that instances of a class are not sufficiently or
1.39 + meaningfully distinguishable from each other with regard to
1.40 + instantiation.
1.41 +
1.42 + SPECIAL Indicates that the compiler should try and optimise calls to
1.43 + the annotated function.
1.44 +
1.45 + ATOMIC Indicates that the class and its instance(s) cannot store
1.46 + attributes, although some may be predefined (such as the
1.47 + __true__ method).
1.48 +
1.49 +--------
1.50 +NATIVE
1.51 +"""
1.52 +
1.53 +class boolean:
1.54 + """
1.55 + NATIVE
1.56 + INTERCHANGEABLE
1.57 + """
1.58 + def __true__(self):
1.59 + return self
1.60 +
1.61 + def __str__(self):
1.62 + if self:
1.63 + return "True"
1.64 + else:
1.65 + return "False"
1.66 +
1.67 +class int:
1.68 + """
1.69 + NATIVE
1.70 + INTERCHANGEABLE
1.71 + """
1.72 + def __iadd__(self, other):
1.73 + """
1.74 + NAME: IMPL.builtins.int.__iadd__
1.75 + NATIVE
1.76 + """
1.77 + if isinstance(other, int):
1.78 + return int()
1.79 + else:
1.80 + TypeConstraintError
1.81 +
1.82 + def __isub__(self, other):
1.83 + """
1.84 + NAME: IMPL.builtins.int.__isub__
1.85 + NATIVE
1.86 + """
1.87 + if isinstance(other, int):
1.88 + return int()
1.89 + else:
1.90 + TypeConstraintError
1.91 +
1.92 + def __add__(self, other):
1.93 + """
1.94 + NAME: IMPL.builtins.int.__add__
1.95 + NATIVE
1.96 + """
1.97 + if isinstance(other, int):
1.98 + return int()
1.99 + else:
1.100 + TypeConstraintError
1.101 +
1.102 + def __radd__(self, other):
1.103 + """
1.104 + NAME: IMPL.builtins.int.__radd__
1.105 + NATIVE
1.106 + """
1.107 + if isinstance(other, int):
1.108 + return int()
1.109 + else:
1.110 + TypeConstraintError
1.111 +
1.112 + def __sub__(self, other):
1.113 + """
1.114 + NAME: IMPL.builtins.int.__sub__
1.115 + NATIVE
1.116 + """
1.117 + if isinstance(other, int):
1.118 + return int()
1.119 + else:
1.120 + TypeConstraintError
1.121 +
1.122 + def __rsub__(self, other):
1.123 + """
1.124 + NAME: IMPL.builtins.int.__rsub__
1.125 + NATIVE
1.126 + """
1.127 + if isinstance(other, int):
1.128 + return int()
1.129 + else:
1.130 + TypeConstraintError
1.131 +
1.132 + def __mul__(self, other):
1.133 + """
1.134 + NAME: IMPL.builtins.int.__mul__
1.135 + NATIVE
1.136 + """
1.137 + if isinstance(other, int):
1.138 + return int()
1.139 + else:
1.140 + TypeConstraintError
1.141 +
1.142 + def __rmul__(self, other):
1.143 + """
1.144 + NAME: IMPL.builtins.int.__rmul__
1.145 + NATIVE
1.146 + """
1.147 + if isinstance(other, int):
1.148 + return int()
1.149 + else:
1.150 + TypeConstraintError
1.151 +
1.152 + def __div__(self, other):
1.153 + """
1.154 + NAME: IMPL.builtins.int.__div__
1.155 + NATIVE
1.156 + """
1.157 + if isinstance(other, int):
1.158 + return int()
1.159 + else:
1.160 + TypeConstraintError
1.161 +
1.162 + def __rdiv__(self, other):
1.163 + """
1.164 + NAME: IMPL.builtins.int.__rdiv__
1.165 + NATIVE
1.166 + """
1.167 + if isinstance(other, int):
1.168 + return int()
1.169 + else:
1.170 + TypeConstraintError
1.171 +
1.172 + def __pow__(self, other):
1.173 + """
1.174 + NAME: IMPL.builtins.int.__pow__
1.175 + NATIVE
1.176 + """
1.177 + if isinstance(other, int):
1.178 + return int()
1.179 + else:
1.180 + TypeConstraintError
1.181 +
1.182 + def __lt__(self, other):
1.183 + """
1.184 + NAME: IMPL.builtins.int.__lt__
1.185 + NATIVE
1.186 + """
1.187 + if isinstance(other, int):
1.188 + return boolean()
1.189 + else:
1.190 + TypeConstraintError
1.191 +
1.192 + def __gt__(self, other):
1.193 + """
1.194 + NAME: IMPL.builtins.int.__gt__
1.195 + NATIVE
1.196 + """
1.197 + if isinstance(other, int):
1.198 + return boolean()
1.199 + else:
1.200 + TypeConstraintError
1.201 +
1.202 + def __le__(self, other):
1.203 + """
1.204 + NAME: IMPL.builtins.int.__le__
1.205 + NATIVE
1.206 + """
1.207 + if isinstance(other, int):
1.208 + return boolean()
1.209 + else:
1.210 + TypeConstraintError
1.211 +
1.212 + def __ge__(self, other):
1.213 + """
1.214 + NAME: IMPL.builtins.int.__ge__
1.215 + NATIVE
1.216 + """
1.217 + if isinstance(other, int):
1.218 + return boolean()
1.219 + else:
1.220 + TypeConstraintError
1.221 +
1.222 + def __eq__(self, other):
1.223 + """
1.224 + NAME: IMPL.builtins.int.__eq__
1.225 + NATIVE
1.226 + """
1.227 + if isinstance(other, int):
1.228 + return boolean()
1.229 + else:
1.230 + TypeConstraintError
1.231 +
1.232 + def __ne__(self, other):
1.233 + """
1.234 + NAME: IMPL.builtins.int.__ne__
1.235 + NATIVE
1.236 + """
1.237 + if isinstance(other, int):
1.238 + return boolean()
1.239 + else:
1.240 + TypeConstraintError
1.241 +
1.242 + def __neg__(self):
1.243 + """
1.244 + NAME: IMPL.builtins.int.__neg__
1.245 + NATIVE
1.246 + """
1.247 + return int()
1.248 +
1.249 + def __pos__(self):
1.250 + return self
1.251 +
1.252 + def __str__(self):
1.253 + """
1.254 + NAME: IMPL.builtins.int.__str__
1.255 + NATIVE
1.256 + """
1.257 + return string()
1.258 +
1.259 + def __true__(self):
1.260 + return self != 0
1.261 +
1.262 +class long:
1.263 + """
1.264 + NATIVE
1.265 + INTERCHANGEABLE
1.266 + """
1.267 + def __iadd__(self, other):
1.268 + """
1.269 + NAME: IMPL.builtins.long.__iadd__
1.270 + NATIVE
1.271 + """
1.272 + if isinstance(other, int):
1.273 + return long()
1.274 + elif isinstance(other, long):
1.275 + return long()
1.276 + else:
1.277 + TypeConstraintError
1.278 +
1.279 + def __isub__(self, other):
1.280 + """
1.281 + NAME: IMPL.builtins.long.__isub__
1.282 + NATIVE
1.283 + """
1.284 + if isinstance(other, int):
1.285 + return long()
1.286 + elif isinstance(other, long):
1.287 + return long()
1.288 + else:
1.289 + TypeConstraintError
1.290 +
1.291 + def __add__(self, other):
1.292 + """
1.293 + NAME: IMPL.builtins.long.__add__
1.294 + NATIVE
1.295 + """
1.296 + if isinstance(other, int):
1.297 + return long()
1.298 + elif isinstance(other, long):
1.299 + return long()
1.300 + else:
1.301 + TypeConstraintError
1.302 +
1.303 + def __radd__(self, other):
1.304 + """
1.305 + NAME: IMPL.builtins.long.__radd__
1.306 + NATIVE
1.307 + """
1.308 + if isinstance(other, int):
1.309 + return long()
1.310 + elif isinstance(other, long):
1.311 + return long()
1.312 + else:
1.313 + TypeConstraintError
1.314 +
1.315 + def __sub__(self, other):
1.316 + """
1.317 + NAME: IMPL.builtins.long.__sub__
1.318 + NATIVE
1.319 + """
1.320 + if isinstance(other, int):
1.321 + return long()
1.322 + elif isinstance(other, long):
1.323 + return long()
1.324 + else:
1.325 + TypeConstraintError
1.326 +
1.327 + def __rsub__(self, other):
1.328 + """
1.329 + NAME: IMPL.builtins.long.__rsub__
1.330 + NATIVE
1.331 + """
1.332 + if isinstance(other, int):
1.333 + return long()
1.334 + elif isinstance(other, long):
1.335 + return long()
1.336 + else:
1.337 + TypeConstraintError
1.338 +
1.339 + def __lt__(self, other):
1.340 + """
1.341 + NAME: IMPL.builtins.long.__lt__
1.342 + NATIVE
1.343 + """
1.344 + if isinstance(other, int):
1.345 + return boolean()
1.346 + elif isinstance(other, long):
1.347 + return boolean()
1.348 + else:
1.349 + TypeConstraintError
1.350 +
1.351 + def __gt__(self, other):
1.352 + """
1.353 + NAME: IMPL.builtins.long.__gt__
1.354 + NATIVE
1.355 + """
1.356 + if isinstance(other, int):
1.357 + return boolean()
1.358 + elif isinstance(other, long):
1.359 + return boolean()
1.360 + else:
1.361 + TypeConstraintError
1.362 +
1.363 + def __le__(self, other):
1.364 + """
1.365 + NAME: IMPL.builtins.long.__le__
1.366 + NATIVE
1.367 + """
1.368 + if isinstance(other, int):
1.369 + return boolean()
1.370 + elif isinstance(other, long):
1.371 + return boolean()
1.372 + else:
1.373 + TypeConstraintError
1.374 +
1.375 + def __ge__(self, other):
1.376 + """
1.377 + NAME: IMPL.builtins.long.__ge__
1.378 + NATIVE
1.379 + """
1.380 + if isinstance(other, int):
1.381 + return boolean()
1.382 + elif isinstance(other, long):
1.383 + return boolean()
1.384 + else:
1.385 + TypeConstraintError
1.386 +
1.387 + def __eq__(self, other):
1.388 + """
1.389 + NAME: IMPL.builtins.long.__eq__
1.390 + NATIVE
1.391 + """
1.392 + if isinstance(other, int):
1.393 + return boolean()
1.394 + elif isinstance(other, long):
1.395 + return boolean()
1.396 + else:
1.397 + TypeConstraintError
1.398 +
1.399 + def __ne__(self, other):
1.400 + """
1.401 + NAME: IMPL.builtins.long.__ne__
1.402 + NATIVE
1.403 + """
1.404 + if isinstance(other, int):
1.405 + return boolean()
1.406 + elif isinstance(other, long):
1.407 + return boolean()
1.408 + else:
1.409 + TypeConstraintError
1.410 +
1.411 + def __neg__(self):
1.412 + """
1.413 + NAME: IMPL.builtins.long.__neg__
1.414 + NATIVE
1.415 + """
1.416 + return long()
1.417 +
1.418 + def __pos__(self):
1.419 + return self
1.420 +
1.421 + def __str__(self):
1.422 + """
1.423 + NAME: IMPL.builtins.long.__str__
1.424 + NATIVE
1.425 + """
1.426 + return string()
1.427 +
1.428 + def __true__(self):
1.429 + return self != 0
1.430 +
1.431 +class float:
1.432 + """
1.433 + NATIVE
1.434 + INTERCHANGEABLE
1.435 + """
1.436 + def __iadd__(self, other):
1.437 + """
1.438 + NAME: IMPL.builtins.float.__iadd__
1.439 + NATIVE
1.440 + """
1.441 + if isinstance(other, int):
1.442 + return float()
1.443 + elif isinstance(other, long):
1.444 + return float()
1.445 + elif isinstance(other, float):
1.446 + return float()
1.447 + else:
1.448 + TypeConstraintError
1.449 +
1.450 + def __isub__(self, other):
1.451 + """
1.452 + NAME: IMPL.builtins.float.__isub__
1.453 + NATIVE
1.454 + """
1.455 + if isinstance(other, int):
1.456 + return float()
1.457 + elif isinstance(other, long):
1.458 + return float()
1.459 + elif isinstance(other, float):
1.460 + return float()
1.461 + else:
1.462 + TypeConstraintError
1.463 +
1.464 + def __add__(self, other):
1.465 + """
1.466 + NAME: IMPL.builtins.float.__add__
1.467 + NATIVE
1.468 + """
1.469 + if isinstance(other, int):
1.470 + return float()
1.471 + elif isinstance(other, long):
1.472 + return float()
1.473 + elif isinstance(other, float):
1.474 + return float()
1.475 + else:
1.476 + TypeConstraintError
1.477 +
1.478 + def __radd__(self, other):
1.479 + """
1.480 + NAME: IMPL.builtins.float.__radd__
1.481 + NATIVE
1.482 + """
1.483 + if isinstance(other, int):
1.484 + return float()
1.485 + elif isinstance(other, long):
1.486 + return float()
1.487 + elif isinstance(other, float):
1.488 + return float()
1.489 + else:
1.490 + TypeConstraintError
1.491 +
1.492 + def __sub__(self, other):
1.493 + """
1.494 + NAME: IMPL.builtins.float.__sub__
1.495 + NATIVE
1.496 + """
1.497 + if isinstance(other, int):
1.498 + return float()
1.499 + elif isinstance(other, long):
1.500 + return float()
1.501 + elif isinstance(other, float):
1.502 + return float()
1.503 + else:
1.504 + TypeConstraintError
1.505 +
1.506 + def __rsub__(self, other):
1.507 + """
1.508 + NAME: IMPL.builtins.float.__rsub__
1.509 + NATIVE
1.510 + """
1.511 + if isinstance(other, int):
1.512 + return float()
1.513 + elif isinstance(other, long):
1.514 + return float()
1.515 + elif isinstance(other, float):
1.516 + return float()
1.517 + else:
1.518 + TypeConstraintError
1.519 +
1.520 + def __mul__(self, other):
1.521 + """
1.522 + NAME: IMPL.builtins.float.__mul__
1.523 + NATIVE
1.524 + """
1.525 + if isinstance(other, int):
1.526 + return float()
1.527 + elif isinstance(other, long):
1.528 + return float()
1.529 + elif isinstance(other, float):
1.530 + return float()
1.531 + else:
1.532 + TypeConstraintError
1.533 +
1.534 + def __rmul__(self, other):
1.535 + """
1.536 + NAME: IMPL.builtins.float.__rmul__
1.537 + NATIVE
1.538 + """
1.539 + if isinstance(other, int):
1.540 + return float()
1.541 + elif isinstance(other, long):
1.542 + return float()
1.543 + elif isinstance(other, float):
1.544 + return float()
1.545 + else:
1.546 + TypeConstraintError
1.547 +
1.548 + def __div__(self, other):
1.549 + """
1.550 + NAME: IMPL.builtins.float.__div__
1.551 + NATIVE
1.552 + """
1.553 + if isinstance(other, int):
1.554 + return float()
1.555 + elif isinstance(other, long):
1.556 + return float()
1.557 + elif isinstance(other, float):
1.558 + return float()
1.559 + else:
1.560 + TypeConstraintError
1.561 +
1.562 + def __rdiv__(self, other):
1.563 + """
1.564 + NAME: IMPL.builtins.float.__rdiv__
1.565 + NATIVE
1.566 + """
1.567 + if isinstance(other, int):
1.568 + return float()
1.569 + elif isinstance(other, long):
1.570 + return float()
1.571 + elif isinstance(other, float):
1.572 + return float()
1.573 + else:
1.574 + TypeConstraintError
1.575 +
1.576 + def __pow__(self, other):
1.577 + """
1.578 + NAME: IMPL.builtins.float.__pow__
1.579 + NATIVE
1.580 + """
1.581 + if isinstance(other, int):
1.582 + return float()
1.583 + elif isinstance(other, long):
1.584 + return float()
1.585 + elif isinstance(other, float):
1.586 + return float()
1.587 + else:
1.588 + TypeConstraintError
1.589 +
1.590 + def __rpow__(self, other):
1.591 + """
1.592 + NAME: IMPL.builtins.float.__rpow__
1.593 + NATIVE
1.594 + """
1.595 + if isinstance(other, int):
1.596 + return float()
1.597 + elif isinstance(other, long):
1.598 + return float()
1.599 + elif isinstance(other, float):
1.600 + return float()
1.601 + else:
1.602 + TypeConstraintError
1.603 +
1.604 + def __lt__(self, other):
1.605 + """
1.606 + NAME: IMPL.builtins.float.__lt__
1.607 + NATIVE
1.608 + """
1.609 + if isinstance(other, int):
1.610 + return boolean()
1.611 + elif isinstance(other, long):
1.612 + return boolean()
1.613 + elif isinstance(other, float):
1.614 + return boolean()
1.615 + else:
1.616 + TypeConstraintError
1.617 +
1.618 + def __gt__(self, other):
1.619 + """
1.620 + NAME: IMPL.builtins.float.__gt__
1.621 + NATIVE
1.622 + """
1.623 + if isinstance(other, int):
1.624 + return boolean()
1.625 + elif isinstance(other, long):
1.626 + return boolean()
1.627 + elif isinstance(other, float):
1.628 + return boolean()
1.629 + else:
1.630 + TypeConstraintError
1.631 +
1.632 + def __le__(self, other):
1.633 + """
1.634 + NAME: IMPL.builtins.float.__le__
1.635 + NATIVE
1.636 + """
1.637 + if isinstance(other, int):
1.638 + return boolean()
1.639 + elif isinstance(other, long):
1.640 + return boolean()
1.641 + elif isinstance(other, float):
1.642 + return boolean()
1.643 + else:
1.644 + TypeConstraintError
1.645 +
1.646 + def __ge__(self, other):
1.647 + """
1.648 + NAME: IMPL.builtins.float.__ge__
1.649 + NATIVE
1.650 + """
1.651 + if isinstance(other, int):
1.652 + return boolean()
1.653 + elif isinstance(other, long):
1.654 + return boolean()
1.655 + elif isinstance(other, float):
1.656 + return boolean()
1.657 + else:
1.658 + TypeConstraintError
1.659 +
1.660 + def __eq__(self, other):
1.661 + """
1.662 + NAME: IMPL.builtins.float.__eq__
1.663 + NATIVE
1.664 + """
1.665 + if isinstance(other, int):
1.666 + return boolean()
1.667 + elif isinstance(other, long):
1.668 + return boolean()
1.669 + elif isinstance(other, float):
1.670 + return boolean()
1.671 + else:
1.672 + TypeConstraintError
1.673 +
1.674 + def __ne__(self, other):
1.675 + """
1.676 + NAME: IMPL.builtins.float.__ne__
1.677 + NATIVE
1.678 + """
1.679 + if isinstance(other, int):
1.680 + return boolean()
1.681 + elif isinstance(other, long):
1.682 + return boolean()
1.683 + elif isinstance(other, float):
1.684 + return boolean()
1.685 + else:
1.686 + TypeConstraintError
1.687 +
1.688 + def __neg__(self):
1.689 + """
1.690 + NAME: IMPL.builtins.float.__neg__
1.691 + NATIVE
1.692 + """
1.693 + return float()
1.694 +
1.695 + def __pos__(self):
1.696 + return self
1.697 +
1.698 + def __str__(self):
1.699 + """
1.700 + NAME: IMPL.builtins.float.__str__
1.701 + NATIVE
1.702 + """
1.703 + return string()
1.704 +
1.705 + def __true__(self):
1.706 + return self != 0
1.707 +
1.708 +class string:
1.709 + """
1.710 + NATIVE
1.711 + INTERCHANGEABLE
1.712 + """
1.713 + def __add__(self, other):
1.714 + """
1.715 + NAME: IMPL.builtins.string.__add__
1.716 + NATIVE
1.717 + """
1.718 + if isinstance(other, string):
1.719 + return string()
1.720 + else:
1.721 + TypeConstraintError
1.722 +
1.723 + def __radd__(self, other):
1.724 + """
1.725 + NAME: IMPL.builtins.string.__radd__
1.726 + NATIVE
1.727 + """
1.728 + if isinstance(other, string):
1.729 + return string()
1.730 + else:
1.731 + TypeConstraintError
1.732 +
1.733 + def __len__(self):
1.734 + """
1.735 + NAME: IMPL.builtins.string.__len__
1.736 + NATIVE
1.737 + """
1.738 + return int()
1.739 +
1.740 + def __str__(self):
1.741 + return self
1.742 +
1.743 + def __true__(self):
1.744 + return self.__len__() != 0
1.745 +
1.746 + def join(self, l):
1.747 + total = 0
1.748 + first = 1
1.749 + self_len = self.__len__()
1.750 + for i in l:
1.751 + if not first:
1.752 + total += self_len
1.753 + total += len(str(i))
1.754 + first = 0
1.755 + b = buffer(total)
1.756 + first = 1
1.757 + for i in l:
1.758 + if not first:
1.759 + b.append(self)
1.760 + b.append(str(i))
1.761 + first = 0
1.762 + s = str(b)
1.763 + return s
1.764 +
1.765 +class buffer:
1.766 + """
1.767 + NATIVE
1.768 + INTERCHANGEABLE
1.769 + """
1.770 + def __init__(self, size):
1.771 + """
1.772 + NAME: IMPL.builtins.buffer.__init__
1.773 + NATIVE
1.774 + """
1.775 + if not isinstance(size, int):
1.776 + TypeConstraintError
1.777 +
1.778 + def append(self, s):
1.779 + """
1.780 + NAME: IMPL.builtins.buffer.append
1.781 + NATIVE
1.782 + """
1.783 + if not isinstance(s, string):
1.784 + TypeConstraintError
1.785 + pass
1.786 +
1.787 + def __str__(self):
1.788 + """
1.789 + NAME: IMPL.builtins.buffer.__str__
1.790 + NATIVE
1.791 + """
1.792 + return string()
1.793 +
1.794 +class list:
1.795 + def __init__(self):
1.796 + self.next = None
1.797 + self.last = self
1.798 +
1.799 + def __getitem__(self, index):
1.800 + i = 0
1.801 + n = self
1.802 + # NOTE: Support negative indices using last.
1.803 + while i < index and n.next is not None:
1.804 + n = n.next
1.805 + i += 1
1.806 + if n.next is not None:
1.807 + return n.value
1.808 + else:
1.809 + raise IndexError() # NOTE: Make this compliant with Python!
1.810 +
1.811 + def __setitem__(self, index, value):
1.812 + i = 0
1.813 + n = self
1.814 + # NOTE: Support negative indices using last.
1.815 + while i < index and n.next is not None:
1.816 + n = n.next
1.817 + i += 1
1.818 + if n.next is not None:
1.819 + n.value = value
1.820 + else:
1.821 + raise IndexError() # NOTE: Make this compliant with Python!
1.822 +
1.823 + def __getslice__(self, start, end=None):
1.824 + slice = []
1.825 + i = 0
1.826 + n = self
1.827 + # NOTE: Support negative indices using last.
1.828 + while (end is None or i < end) and n.next is not None:
1.829 + if i >= start:
1.830 + slice.append(n.value)
1.831 + n = n.next
1.832 + i += 1
1.833 + return slice
1.834 +
1.835 + def __setslice__(self, start, end, slice):
1.836 + i = 0
1.837 + n = self
1.838 + j = 0
1.839 + p = slice
1.840 + # NOTE: Support negative indices using last.
1.841 + # NOTE: Support appending when start >= len(self).
1.842 + while i < end and n is not None and p is not None:
1.843 + if i >= start:
1.844 + n.value = p.value
1.845 + p = p.next
1.846 + j += 1
1.847 + n = n.next
1.848 + i += 1
1.849 +
1.850 + def append(self, value):
1.851 + n = self.last
1.852 + n.value = value
1.853 + n.next = self.__class__()
1.854 + self.last = n.next
1.855 +
1.856 + def __len__(self):
1.857 + i = 0
1.858 + n = self
1.859 + while n.next is not None:
1.860 + n = n.next
1.861 + i += 1
1.862 + return i
1.863 +
1.864 + def __add__(self, other):
1.865 + result = self.__class__()
1.866 + for value in self:
1.867 + result.append(value)
1.868 + for value in other:
1.869 + result.append(value)
1.870 + return result
1.871 +
1.872 + def __str__(self):
1.873 + output = ["["]
1.874 + n = self
1.875 + first = 1
1.876 + while n.next is not None:
1.877 + if not first:
1.878 + output.append(", ")
1.879 + else:
1.880 + first = 0
1.881 + output.append(str(n.value))
1.882 + n = n.next
1.883 + output.append("]")
1.884 + return "".join(output)
1.885 +
1.886 + def __iter__(self):
1.887 + return listiterator(self)
1.888 +
1.889 + def __true__(self):
1.890 + return self.__len__() != 0
1.891 +
1.892 +class listiterator:
1.893 + def __init__(self, l):
1.894 + self.l = l
1.895 +
1.896 + def next(self):
1.897 + l = self.l
1.898 + next = l.next
1.899 + if next is not None:
1.900 + self.l = next
1.901 + return l.value
1.902 + else:
1.903 + raise StopIteration() # NOTE: Make this compliant with Python!
1.904 +
1.905 + def __true__(self):
1.906 + """
1.907 + NAME: IMPL.builtins.int.__true__
1.908 + NATIVE
1.909 + """
1.910 + return boolean()
1.911 +
1.912 +class tuple:
1.913 + def __init__(self):
1.914 + self.next = None
1.915 + self.last = self
1.916 +
1.917 + def __getitem__(self, index):
1.918 + i = 0
1.919 + n = self
1.920 + # NOTE: Support negative indices using last.
1.921 + while i < index and n.next is not None:
1.922 + n = n.next
1.923 + i += 1
1.924 + if n.next is not None:
1.925 + return n.value
1.926 + else:
1.927 + raise IndexError() # NOTE: Make this compliant with Python!
1.928 +
1.929 + def __getslice__(self, start, end=None):
1.930 + # NOTE: Should probably return a tuple.
1.931 + slice = []
1.932 + i = 0
1.933 + n = self
1.934 + # NOTE: Support negative indices using last.
1.935 + while (end is None or i < end) and n.next is not None:
1.936 + if i >= start:
1.937 + slice.append(n.value)
1.938 + n = n.next
1.939 + i += 1
1.940 + return slice
1.941 +
1.942 + # NOTE: The append method should be internal at most.
1.943 +
1.944 + def append(self, value):
1.945 + n = self.last
1.946 + n.value = value
1.947 + n.next = self.__class__()
1.948 + self.last = n.next
1.949 +
1.950 + def __len__(self):
1.951 + i = 0
1.952 + n = self
1.953 + while n.next is not None:
1.954 + n = n.next
1.955 + i += 1
1.956 + return i
1.957 +
1.958 + def __add__(self, other):
1.959 + result = self.__class__()
1.960 + for value in self:
1.961 + result.append(value)
1.962 + for value in other:
1.963 + result.append(value)
1.964 + return result
1.965 +
1.966 + def __str__(self):
1.967 + output = ["("]
1.968 + n = self
1.969 + first = 1
1.970 + while n.next is not None:
1.971 + if not first:
1.972 + output.append(", ")
1.973 + else:
1.974 + first = 0
1.975 + output.append(str(n.value))
1.976 + n = n.next
1.977 + output.append(")")
1.978 + return "".join(output)
1.979 +
1.980 + def __iter__(self):
1.981 + return tupleiterator(self)
1.982 +
1.983 + def __true__(self):
1.984 + return self.__len__() != 0
1.985 +
1.986 +class tupleiterator:
1.987 + def __init__(self, l):
1.988 + self.l = l
1.989 +
1.990 + def next(self):
1.991 + l = self.l
1.992 + next = l.next
1.993 + if next is not None:
1.994 + self.l = next
1.995 + return l.value
1.996 + else:
1.997 + raise StopIteration() # NOTE: Make this compliant with Python!
1.998 +
1.999 + def __true__(self):
1.1000 + """
1.1001 + NAME: IMPL.builtins.int.__true__
1.1002 + NATIVE
1.1003 + """
1.1004 + return boolean()
1.1005 +
1.1006 +class Exception:
1.1007 + pass
1.1008 +
1.1009 +class StopIteration(Exception):
1.1010 + pass
1.1011 +
1.1012 +class IndexError(Exception):
1.1013 + pass
1.1014 +
1.1015 +class AttributeError(Exception):
1.1016 + pass
1.1017 +
1.1018 +class TypeError(Exception):
1.1019 + pass
1.1020 +
1.1021 +class none:
1.1022 + """
1.1023 + NATIVE
1.1024 + INTERCHANGEABLE
1.1025 + ATOMIC
1.1026 + """
1.1027 + def __true__(self):
1.1028 + return False
1.1029 +
1.1030 + def __str__(self):
1.1031 + return "None"
1.1032 +
1.1033 +class undefined:
1.1034 + """
1.1035 + NATIVE
1.1036 + INTERCHANGEABLE
1.1037 + ATOMIC
1.1038 + """
1.1039 + pass
1.1040 +
1.1041 +def isinstance(obj, cls):
1.1042 + """
1.1043 + NAME: IMPL.builtins.isinstance
1.1044 + NATIVE
1.1045 + SPECIAL
1.1046 + """
1.1047 + return boolean()
1.1048 +
1.1049 +def issubclass(cls1, cls2):
1.1050 + """
1.1051 + NAME: IMPL.builtins.isinstance
1.1052 + NATIVE
1.1053 + SPECIAL
1.1054 + """
1.1055 + return boolean()
1.1056 +
1.1057 +def len(x):
1.1058 + return x.__len__()
1.1059 +
1.1060 +def max(*l):
1.1061 + max_so_far = l[0]
1.1062 + for i in l[1:]:
1.1063 + if i > max_so_far:
1.1064 + max_so_far = i
1.1065 + return max_so_far
1.1066 +
1.1067 +def str(x):
1.1068 + return x.__str__()
1.1069 +
1.1070 +class xrange:
1.1071 + def __init__(self, start, end, step=1):
1.1072 + self.start = start
1.1073 + self.end = end
1.1074 + self.step = step
1.1075 + self.current = start
1.1076 +
1.1077 + def __iter__(self):
1.1078 + return self
1.1079 +
1.1080 + def next(self):
1.1081 + if self.current >= self.end:
1.1082 + raise StopIteration()
1.1083 + current = self.current
1.1084 + self.current += self.step
1.1085 + return current
1.1086 +
1.1087 +# Special values. None of these definitions should be generated by the compiler.
1.1088 +# All such definitions should be made in the underlying implementation.
1.1089 +
1.1090 +True = boolean()
1.1091 +False = boolean()
1.1092 +None = none()
1.1093 +Undefined = undefined()
1.1094 +
1.1095 +# Special functions. These all operate on references at run-time.
1.1096 +
1.1097 +def __is__(a, b):
1.1098 + """
1.1099 + NAME: IMPL.builtins.__is__
1.1100 + NATIVE
1.1101 + """
1.1102 + return boolean()
1.1103 +
1.1104 +def __is_not__(a, b):
1.1105 + """
1.1106 + NAME: IMPL.builtins.__is_not__
1.1107 + NATIVE
1.1108 + """
1.1109 + return boolean()
1.1110 +
1.1111 +def __not__(a):
1.1112 + """
1.1113 + NAME: IMPL.builtins.__not__
1.1114 + NATIVE
1.1115 + """
1.1116 + return boolean()
1.1117 +
1.1118 +# vim: tabstop=4 expandtab shiftwidth=4