This repo is not current. Development has moved from Hg to Git. For the latest code use the "Source Code" tab above to go to the "Thun" git repo or navigate to:
https://osdn.net/projects/joypy/scm/git/Thun
Revision | 5bdb539d2d746316caea630d6d1cd126bbc9ee68 (tree) |
---|---|
Time | 2018-04-17 04:27:52 |
Author | Simon Forman <sforman@hush...> |
Commiter | Simon Forman |
Bringing over some changes.
I need to harmonize my local repo and the OSDN repo.
@@ -1,22 +1,28 @@ | ||
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # |
3 | -# Copyright © 2014, 2015, 2017 Simon Forman | |
3 | +# Copyright © 2014, 2015, 2017, 2018 Simon Forman | |
4 | 4 | # |
5 | -# This file is part of joy.py | |
5 | +# This file is part of Joypy | |
6 | 6 | # |
7 | -# joy.py is free software: you can redistribute it and/or modify | |
7 | +# Joypy is free software: you can redistribute it and/or modify | |
8 | 8 | # it under the terms of the GNU General Public License as published by |
9 | 9 | # the Free Software Foundation, either version 3 of the License, or |
10 | 10 | # (at your option) any later version. |
11 | 11 | # |
12 | -# joy.py is distributed in the hope that it will be useful, | |
12 | +# Joypy is distributed in the hope that it will be useful, | |
13 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | 15 | # GNU General Public License for more details. |
16 | 16 | # |
17 | 17 | # You should have received a copy of the GNU General Public License |
18 | -# along with joy.py. If not see <http://www.gnu.org/licenses/>. | |
18 | +# along with Joypy. If not see <http://www.gnu.org/licenses/>. | |
19 | 19 | # |
20 | +''' | |
21 | +This module contains the Joy function infrastructure and a library of | |
22 | +functions. It's main export is a Python function initialize() that | |
23 | +returns a dictionary of Joy functions suitable for use with the joy() | |
24 | +function. | |
25 | +''' | |
20 | 26 | from inspect import getdoc |
21 | 27 | import operator, math |
22 | 28 |
@@ -24,9 +30,24 @@ | ||
24 | 30 | from .utils.stack import list_to_stack, iter_stack, pick, pushback |
25 | 31 | |
26 | 32 | |
33 | +_dictionary = {} | |
34 | + | |
35 | + | |
36 | +def inscribe(function): | |
37 | + '''A decorator to inscribe functions in to the default dictionary.''' | |
38 | + _dictionary[function.name] = function | |
39 | + return function | |
40 | + | |
41 | + | |
42 | +def initialize(): | |
43 | + '''Return a dictionary of Joy functions for use with joy().''' | |
44 | + return _dictionary.copy() | |
45 | + | |
46 | + | |
27 | 47 | ALIASES = ( |
28 | 48 | ('add', ['+']), |
29 | 49 | ('and', ['&']), |
50 | + ('bool', ['truthy']), | |
30 | 51 | ('mul', ['*']), |
31 | 52 | ('truediv', ['/']), |
32 | 53 | ('mod', ['%', 'rem', 'remainder', 'modulus']), |
@@ -231,28 +252,30 @@ | ||
231 | 252 | # |
232 | 253 | |
233 | 254 | |
255 | +@inscribe | |
256 | +@SimpleFunctionWrapper | |
234 | 257 | def parse((text, stack)): |
235 | 258 | '''Parse the string on the stack to a Joy expression.''' |
236 | 259 | expression = text_to_expression(text) |
237 | 260 | return expression, stack |
238 | 261 | |
239 | 262 | |
263 | +@inscribe | |
264 | +@SimpleFunctionWrapper | |
240 | 265 | def first(((head, tail), stack)): |
241 | 266 | '''first == uncons pop''' |
242 | 267 | return head, stack |
243 | 268 | |
244 | 269 | |
270 | +@inscribe | |
271 | +@SimpleFunctionWrapper | |
245 | 272 | def rest(((head, tail), stack)): |
246 | 273 | '''rest == uncons popd''' |
247 | 274 | return tail, stack |
248 | 275 | |
249 | 276 | |
250 | -def truthy(stack): | |
251 | - '''Coerce the item on the top of the stack to its Boolean value.''' | |
252 | - n, stack = stack | |
253 | - return bool(n), stack | |
254 | - | |
255 | - | |
277 | +@inscribe | |
278 | +@SimpleFunctionWrapper | |
256 | 279 | def getitem(stack): |
257 | 280 | ''' |
258 | 281 | getitem == drop first |
@@ -269,6 +292,8 @@ | ||
269 | 292 | return pick(Q, n), stack |
270 | 293 | |
271 | 294 | |
295 | +@inscribe | |
296 | +@SimpleFunctionWrapper | |
272 | 297 | def drop(stack): |
273 | 298 | ''' |
274 | 299 | drop == [rest] times |
@@ -291,6 +316,8 @@ | ||
291 | 316 | return Q, stack |
292 | 317 | |
293 | 318 | |
319 | +@inscribe | |
320 | +@SimpleFunctionWrapper | |
294 | 321 | def take(stack): |
295 | 322 | ''' |
296 | 323 | Expects an integer and a quote on the stack and returns the quote with |
@@ -314,6 +341,8 @@ | ||
314 | 341 | return x, stack |
315 | 342 | |
316 | 343 | |
344 | +@inscribe | |
345 | +@SimpleFunctionWrapper | |
317 | 346 | def choice(stack): |
318 | 347 | ''' |
319 | 348 | Use a Boolean value to select one of two items. |
@@ -334,6 +363,8 @@ | ||
334 | 363 | return then if if_ else else_, stack |
335 | 364 | |
336 | 365 | |
366 | +@inscribe | |
367 | +@SimpleFunctionWrapper | |
337 | 368 | def select(stack): |
338 | 369 | ''' |
339 | 370 | Use a Boolean value to select one of two items from a sequence. |
@@ -356,18 +387,24 @@ | ||
356 | 387 | return then if flag else else_, stack |
357 | 388 | |
358 | 389 | |
390 | +@inscribe | |
391 | +@SimpleFunctionWrapper | |
359 | 392 | def max_(S): |
360 | 393 | '''Given a list find the maximum.''' |
361 | 394 | tos, stack = S |
362 | 395 | return max(iter_stack(tos)), stack |
363 | 396 | |
364 | 397 | |
398 | +@inscribe | |
399 | +@SimpleFunctionWrapper | |
365 | 400 | def min_(S): |
366 | 401 | '''Given a list find the minimum.''' |
367 | 402 | tos, stack = S |
368 | 403 | return min(iter_stack(tos)), stack |
369 | 404 | |
370 | 405 | |
406 | +@inscribe | |
407 | +@SimpleFunctionWrapper | |
371 | 408 | def sum_(S): |
372 | 409 | '''Given a quoted sequence of numbers return the sum. |
373 | 410 |
@@ -377,6 +414,8 @@ | ||
377 | 414 | return sum(iter_stack(tos)), stack |
378 | 415 | |
379 | 416 | |
417 | +@inscribe | |
418 | +@SimpleFunctionWrapper | |
380 | 419 | def remove(S): |
381 | 420 | ''' |
382 | 421 | Expects an item on the stack and a quote under it and removes that item |
@@ -393,6 +432,8 @@ | ||
393 | 432 | return list_to_stack(l), stack |
394 | 433 | |
395 | 434 | |
435 | +@inscribe | |
436 | +@SimpleFunctionWrapper | |
396 | 437 | def unique(S): |
397 | 438 | '''Given a list remove duplicate items.''' |
398 | 439 | tos, stack = S |
@@ -401,12 +442,16 @@ | ||
401 | 442 | return list_to_stack(sorted(set(I), key=I.index)), stack |
402 | 443 | |
403 | 444 | |
445 | +@inscribe | |
446 | +@SimpleFunctionWrapper | |
404 | 447 | def sort_(S): |
405 | 448 | '''Given a list return it sorted.''' |
406 | 449 | tos, stack = S |
407 | 450 | return list_to_stack(sorted(iter_stack(tos))), stack |
408 | 451 | |
409 | 452 | |
453 | +@inscribe | |
454 | +@SimpleFunctionWrapper | |
410 | 455 | def cons(S): |
411 | 456 | ''' |
412 | 457 | The cons operator expects a list on top of the stack and the potential |
@@ -417,6 +462,8 @@ | ||
417 | 462 | return (second, tos), stack |
418 | 463 | |
419 | 464 | |
465 | +@inscribe | |
466 | +@SimpleFunctionWrapper | |
420 | 467 | def uncons(S): |
421 | 468 | ''' |
422 | 469 | Inverse of cons, removes an item from the top of the list on the stack |
@@ -427,6 +474,8 @@ | ||
427 | 474 | return tos, (item, stack) |
428 | 475 | |
429 | 476 | |
477 | +@inscribe | |
478 | +@SimpleFunctionWrapper | |
430 | 479 | def clear(stack): |
431 | 480 | '''Clear everything from the stack. |
432 | 481 |
@@ -437,12 +486,16 @@ | ||
437 | 486 | return () |
438 | 487 | |
439 | 488 | |
489 | +@inscribe | |
490 | +@SimpleFunctionWrapper | |
440 | 491 | def dup(S): |
441 | 492 | '''Duplicate the top item on the stack.''' |
442 | 493 | (tos, stack) = S |
443 | 494 | return tos, (tos, stack) |
444 | 495 | |
445 | 496 | |
497 | +@inscribe | |
498 | +@SimpleFunctionWrapper | |
446 | 499 | def over(S): |
447 | 500 | ''' |
448 | 501 | Copy the second item down on the stack to the top of the stack. |
@@ -456,6 +509,8 @@ | ||
456 | 509 | return second, S |
457 | 510 | |
458 | 511 | |
512 | +@inscribe | |
513 | +@SimpleFunctionWrapper | |
459 | 514 | def tuck(S): |
460 | 515 | ''' |
461 | 516 | Copy the item at TOS under the second item of the stack. |
@@ -469,18 +524,24 @@ | ||
469 | 524 | return tos, (second, (tos, stack)) |
470 | 525 | |
471 | 526 | |
527 | +@inscribe | |
528 | +@SimpleFunctionWrapper | |
472 | 529 | def swap(S): |
473 | 530 | '''Swap the top two items on stack.''' |
474 | 531 | (tos, (second, stack)) = S |
475 | 532 | return second, (tos, stack) |
476 | 533 | |
477 | 534 | |
535 | +@inscribe | |
536 | +@SimpleFunctionWrapper | |
478 | 537 | def swaack(stack): |
479 | 538 | '''swap stack''' |
480 | 539 | old_stack, stack = stack |
481 | 540 | return stack, old_stack |
482 | 541 | |
483 | 542 | |
543 | +@inscribe | |
544 | +@SimpleFunctionWrapper | |
484 | 545 | def stack_(stack): |
485 | 546 | ''' |
486 | 547 | The stack operator pushes onto the stack a list containing all the |
@@ -489,45 +550,56 @@ | ||
489 | 550 | return stack, stack |
490 | 551 | |
491 | 552 | |
492 | -def unstack(S): | |
553 | +@inscribe | |
554 | +@SimpleFunctionWrapper | |
555 | +def unstack(stack): | |
493 | 556 | ''' |
494 | 557 | The unstack operator expects a list on top of the stack and makes that |
495 | 558 | the stack discarding the rest of the stack. |
496 | 559 | ''' |
497 | - (tos, stack) = S | |
498 | - return tos | |
560 | + return stack[0] | |
499 | 561 | |
500 | 562 | |
501 | -def pop(S): | |
563 | +@inscribe | |
564 | +@SimpleFunctionWrapper | |
565 | +def pop(stack): | |
502 | 566 | '''Pop and discard the top item from the stack.''' |
503 | - (tos, stack) = S | |
504 | - return stack | |
567 | + return stack[1] | |
505 | 568 | |
506 | 569 | |
507 | -def popd(S): | |
570 | +@inscribe | |
571 | +@SimpleFunctionWrapper | |
572 | +def popd(stack): | |
508 | 573 | '''Pop and discard the second item from the stack.''' |
509 | - (tos, (second, stack)) = S | |
574 | + (tos, (_, stack)) = stack | |
510 | 575 | return tos, stack |
511 | 576 | |
512 | 577 | |
513 | -def popdd(S): | |
578 | +@inscribe | |
579 | +@SimpleFunctionWrapper | |
580 | +def popdd(stack): | |
514 | 581 | '''Pop and discard the third item from the stack.''' |
515 | - (tos, (second, (third, stack))) = S | |
582 | + (tos, (second, (_, stack))) = stack | |
516 | 583 | return tos, (second, stack) |
517 | 584 | |
518 | 585 | |
519 | -def popop(S): | |
586 | +@inscribe | |
587 | +@SimpleFunctionWrapper | |
588 | +def popop(stack): | |
520 | 589 | '''Pop and discard the first and second items from the stack.''' |
521 | - (tos, (second, stack)) = S | |
522 | - return stack | |
590 | + return stack[1][1] | |
523 | 591 | |
524 | 592 | |
593 | +@inscribe | |
594 | +@SimpleFunctionWrapper | |
525 | 595 | def dupd(S): |
526 | 596 | '''Duplicate the second item on the stack.''' |
527 | 597 | (tos, (second, stack)) = S |
528 | 598 | return tos, (second, (second, stack)) |
529 | 599 | |
530 | 600 | |
601 | +@inscribe | |
602 | +@SimpleFunctionWrapper | |
531 | 603 | def reverse(S): |
532 | 604 | '''Reverse the list on the top of the stack. |
533 | 605 |
@@ -540,6 +612,8 @@ | ||
540 | 612 | return res, stack |
541 | 613 | |
542 | 614 | |
615 | +@inscribe | |
616 | +@SimpleFunctionWrapper | |
543 | 617 | def concat(S): |
544 | 618 | '''Concatinate the two lists on the top of the stack.''' |
545 | 619 | (tos, (second, stack)) = S |
@@ -548,6 +622,8 @@ | ||
548 | 622 | return tos, stack |
549 | 623 | |
550 | 624 | |
625 | +@inscribe | |
626 | +@SimpleFunctionWrapper | |
551 | 627 | def shunt((tos, (second, stack))): |
552 | 628 | ''' |
553 | 629 | shunt == [swons] step |
@@ -560,6 +636,8 @@ | ||
560 | 636 | return second, stack |
561 | 637 | |
562 | 638 | |
639 | +@inscribe | |
640 | +@SimpleFunctionWrapper | |
563 | 641 | def zip_(S): |
564 | 642 | ''' |
565 | 643 | Replace the two lists on the top of the stack with a list of the pairs |
@@ -573,18 +651,24 @@ | ||
573 | 651 | return list_to_stack(accumulator), stack |
574 | 652 | |
575 | 653 | |
654 | +@inscribe | |
655 | +@SimpleFunctionWrapper | |
576 | 656 | def succ(S): |
577 | 657 | '''Increment TOS.''' |
578 | 658 | (tos, stack) = S |
579 | 659 | return tos + 1, stack |
580 | 660 | |
581 | 661 | |
662 | +@inscribe | |
663 | +@SimpleFunctionWrapper | |
582 | 664 | def pred(S): |
583 | 665 | '''Decrement TOS.''' |
584 | 666 | (tos, stack) = S |
585 | 667 | return tos - 1, stack |
586 | 668 | |
587 | 669 | |
670 | +@inscribe | |
671 | +@SimpleFunctionWrapper | |
588 | 672 | def pm(stack): |
589 | 673 | ''' |
590 | 674 | Plus or minus |
@@ -605,6 +689,8 @@ | ||
605 | 689 | floor.__doc__ = math.floor.__doc__ |
606 | 690 | |
607 | 691 | |
692 | +@inscribe | |
693 | +@SimpleFunctionWrapper | |
608 | 694 | def divmod_(S): |
609 | 695 | a, (b, stack) = S |
610 | 696 | d, m = divmod(a, b) |
@@ -626,12 +712,16 @@ | ||
626 | 712 | return r |
627 | 713 | |
628 | 714 | |
715 | +@inscribe | |
716 | +@SimpleFunctionWrapper | |
629 | 717 | def rollup(S): |
630 | 718 | '''a b c -> b c a''' |
631 | 719 | (a, (b, (c, stack))) = S |
632 | 720 | return b, (c, (a, stack)) |
633 | 721 | |
634 | 722 | |
723 | +@inscribe | |
724 | +@SimpleFunctionWrapper | |
635 | 725 | def rolldown(S): |
636 | 726 | '''a b c -> c a b''' |
637 | 727 | (a, (b, (c, stack))) = S |
@@ -645,10 +735,14 @@ | ||
645 | 735 | # return stack |
646 | 736 | |
647 | 737 | |
738 | +@inscribe | |
739 | +@SimpleFunctionWrapper | |
648 | 740 | def id_(stack): |
649 | 741 | return stack |
650 | 742 | |
651 | 743 | |
744 | +@inscribe | |
745 | +@SimpleFunctionWrapper | |
652 | 746 | def void(stack): |
653 | 747 | form, stack = stack |
654 | 748 | return _void(form), stack |
@@ -664,12 +758,16 @@ | ||
664 | 758 | ## take |
665 | 759 | |
666 | 760 | |
761 | +@inscribe | |
762 | +@FunctionWrapper | |
667 | 763 | def words(stack, expression, dictionary): |
668 | 764 | '''Print all the words in alphabetical order.''' |
669 | 765 | print(' '.join(sorted(dictionary))) |
670 | 766 | return stack, expression, dictionary |
671 | 767 | |
672 | 768 | |
769 | +@inscribe | |
770 | +@FunctionWrapper | |
673 | 771 | def sharing(stack, expression, dictionary): |
674 | 772 | '''Print redistribution information.''' |
675 | 773 | print("You may convey verbatim copies of the Program's source code as" |
@@ -685,6 +783,8 @@ | ||
685 | 783 | return stack, expression, dictionary |
686 | 784 | |
687 | 785 | |
786 | +@inscribe | |
787 | +@FunctionWrapper | |
688 | 788 | def warranty(stack, expression, dictionary): |
689 | 789 | '''Print warranty information.''' |
690 | 790 | print('THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY' |
@@ -718,6 +818,8 @@ | ||
718 | 818 | # return stack |
719 | 819 | |
720 | 820 | |
821 | +@inscribe | |
822 | +@FunctionWrapper | |
721 | 823 | def help_(S, expression, dictionary): |
722 | 824 | '''Accepts a quoted symbol on the top of the stack and prints its docs.''' |
723 | 825 | ((symbol, _), stack) = S |
@@ -748,6 +850,8 @@ | ||
748 | 850 | S_truthy = Symbol('truthy') |
749 | 851 | |
750 | 852 | |
853 | +@inscribe | |
854 | +@FunctionWrapper | |
751 | 855 | def i(stack, expression, dictionary): |
752 | 856 | ''' |
753 | 857 | The i combinator expects a quoted program on the stack and unpacks it |
@@ -762,6 +866,8 @@ | ||
762 | 866 | return stack, pushback(quote, expression), dictionary |
763 | 867 | |
764 | 868 | |
869 | +@inscribe | |
870 | +@FunctionWrapper | |
765 | 871 | def x(stack, expression, dictionary): |
766 | 872 | ''' |
767 | 873 | x == dup i |
@@ -775,6 +881,8 @@ | ||
775 | 881 | return stack, pushback(quote, expression), dictionary |
776 | 882 | |
777 | 883 | |
884 | +@inscribe | |
885 | +@FunctionWrapper | |
778 | 886 | def b(stack, expression, dictionary): |
779 | 887 | ''' |
780 | 888 | b == [i] dip i |
@@ -787,6 +895,8 @@ | ||
787 | 895 | return stack, pushback(p, pushback(q, expression)), dictionary |
788 | 896 | |
789 | 897 | |
898 | +@inscribe | |
899 | +@FunctionWrapper | |
790 | 900 | def dupdip(stack, expression, dictionary): |
791 | 901 | ''' |
792 | 902 | [F] dupdip == dup [F] dip |
@@ -802,6 +912,8 @@ | ||
802 | 912 | return stack, pushback(F, (a, expression)), dictionary |
803 | 913 | |
804 | 914 | |
915 | +@inscribe | |
916 | +@FunctionWrapper | |
805 | 917 | def infra(stack, expression, dictionary): |
806 | 918 | ''' |
807 | 919 | Accept a quoted program and a list on the stack and run the program |
@@ -816,6 +928,8 @@ | ||
816 | 928 | return aggregate, pushback(quote, (stack, (S_swaack, expression))), dictionary |
817 | 929 | |
818 | 930 | |
931 | +@inscribe | |
932 | +@FunctionWrapper | |
819 | 933 | def genrec(stack, expression, dictionary): |
820 | 934 | ''' |
821 | 935 | General Recursion Combinator. |
@@ -870,6 +984,8 @@ | ||
870 | 984 | return (else_, stack), (S_ifte, expression), dictionary |
871 | 985 | |
872 | 986 | |
987 | +@inscribe | |
988 | +@FunctionWrapper | |
873 | 989 | def map_(S, expression, dictionary): |
874 | 990 | ''' |
875 | 991 | Run the quoted program on TOS on the items in the list under it, push a |
@@ -906,6 +1022,8 @@ | ||
906 | 1022 | # return (q, (p, stack)), expression, dictionary |
907 | 1023 | |
908 | 1024 | |
1025 | +@inscribe | |
1026 | +@FunctionWrapper | |
909 | 1027 | def branch(stack, expression, dictionary): |
910 | 1028 | ''' |
911 | 1029 | Use a Boolean value to select one of two quoted programs to run. |
@@ -926,6 +1044,8 @@ | ||
926 | 1044 | return stack, pushback(then if flag else else_, expression), dictionary |
927 | 1045 | |
928 | 1046 | |
1047 | +@inscribe | |
1048 | +@FunctionWrapper | |
929 | 1049 | def ifte(stack, expression, dictionary): |
930 | 1050 | ''' |
931 | 1051 | If-Then-Else Combinator |
@@ -951,6 +1071,8 @@ | ||
951 | 1071 | return stack, expression, dictionary |
952 | 1072 | |
953 | 1073 | |
1074 | +@inscribe | |
1075 | +@FunctionWrapper | |
954 | 1076 | def dip(stack, expression, dictionary): |
955 | 1077 | ''' |
956 | 1078 | The dip combinator expects a quoted program on the stack and below it |
@@ -967,6 +1089,8 @@ | ||
967 | 1089 | return stack, pushback(quote, expression), dictionary |
968 | 1090 | |
969 | 1091 | |
1092 | +@inscribe | |
1093 | +@FunctionWrapper | |
970 | 1094 | def dipd(S, expression, dictionary): |
971 | 1095 | ''' |
972 | 1096 | Like dip but expects two items. |
@@ -981,6 +1105,8 @@ | ||
981 | 1105 | return stack, pushback(quote, expression), dictionary |
982 | 1106 | |
983 | 1107 | |
1108 | +@inscribe | |
1109 | +@FunctionWrapper | |
984 | 1110 | def dipdd(S, expression, dictionary): |
985 | 1111 | ''' |
986 | 1112 | Like dip but expects three items. |
@@ -995,6 +1121,8 @@ | ||
995 | 1121 | return stack, pushback(quote, expression), dictionary |
996 | 1122 | |
997 | 1123 | |
1124 | +@inscribe | |
1125 | +@FunctionWrapper | |
998 | 1126 | def app1(S, expression, dictionary): |
999 | 1127 | ''' |
1000 | 1128 | Given a quoted program on TOS and anything as the second stack item run |
@@ -1011,6 +1139,8 @@ | ||
1011 | 1139 | return stack, expression, dictionary |
1012 | 1140 | |
1013 | 1141 | |
1142 | +@inscribe | |
1143 | +@FunctionWrapper | |
1014 | 1144 | def app2(S, expression, dictionary): |
1015 | 1145 | '''Like app1 with two items. |
1016 | 1146 |
@@ -1028,6 +1158,8 @@ | ||
1028 | 1158 | return stack, expression, dictionary |
1029 | 1159 | |
1030 | 1160 | |
1161 | +@inscribe | |
1162 | +@FunctionWrapper | |
1031 | 1163 | def app3(S, expression, dictionary): |
1032 | 1164 | '''Like app1 with three items. |
1033 | 1165 |
@@ -1047,6 +1179,8 @@ | ||
1047 | 1179 | return stack, expression, dictionary |
1048 | 1180 | |
1049 | 1181 | |
1182 | +@inscribe | |
1183 | +@FunctionWrapper | |
1050 | 1184 | def step(S, expression, dictionary): |
1051 | 1185 | ''' |
1052 | 1186 | Run a quoted program on each item in a sequence. |
@@ -1079,6 +1213,8 @@ | ||
1079 | 1213 | return stack, expression, dictionary |
1080 | 1214 | |
1081 | 1215 | |
1216 | +@inscribe | |
1217 | +@FunctionWrapper | |
1082 | 1218 | def times(stack, expression, dictionary): |
1083 | 1219 | ''' |
1084 | 1220 | times == [-- dip] cons [swap] infra [0 >] swap while pop |
@@ -1125,6 +1261,8 @@ | ||
1125 | 1261 | # return stack, expression, dictionary |
1126 | 1262 | |
1127 | 1263 | |
1264 | +@inscribe | |
1265 | +@FunctionWrapper | |
1128 | 1266 | def loop(stack, expression, dictionary): |
1129 | 1267 | ''' |
1130 | 1268 | Basic loop combinator. |
@@ -1175,7 +1313,15 @@ | ||
1175 | 1313 | # return (result[0], return_stack), expression, dictionary |
1176 | 1314 | |
1177 | 1315 | |
1178 | -builtins = ( | |
1316 | +# FunctionWrapper(binary), | |
1317 | +# FunctionWrapper(cleave), | |
1318 | +# FunctionWrapper(nullary), | |
1319 | +# FunctionWrapper(ternary), | |
1320 | +# FunctionWrapper(unary), | |
1321 | +# FunctionWrapper(while_), | |
1322 | + | |
1323 | + | |
1324 | +for F in ( | |
1179 | 1325 | BinaryBuiltinWrapper(operator.add), |
1180 | 1326 | BinaryBuiltinWrapper(operator.and_), |
1181 | 1327 | BinaryBuiltinWrapper(operator.div), |
@@ -1197,99 +1343,16 @@ | ||
1197 | 1343 | BinaryBuiltinWrapper(operator.xor), |
1198 | 1344 | |
1199 | 1345 | UnaryBuiltinWrapper(abs), |
1346 | + UnaryBuiltinWrapper(bool), | |
1200 | 1347 | UnaryBuiltinWrapper(floor), |
1201 | 1348 | UnaryBuiltinWrapper(operator.neg), |
1202 | 1349 | UnaryBuiltinWrapper(operator.not_), |
1203 | 1350 | UnaryBuiltinWrapper(sqrt), |
1204 | - ) | |
1205 | - | |
1206 | - | |
1207 | -combinators = ( | |
1208 | - FunctionWrapper(app1), | |
1209 | - FunctionWrapper(app2), | |
1210 | - FunctionWrapper(app3), | |
1211 | - FunctionWrapper(b), | |
1212 | - FunctionWrapper(branch), | |
1213 | -# FunctionWrapper(binary), | |
1214 | -# FunctionWrapper(cleave), | |
1215 | - FunctionWrapper(dip), | |
1216 | - FunctionWrapper(dipd), | |
1217 | - FunctionWrapper(dipdd), | |
1218 | - FunctionWrapper(dupdip), | |
1219 | - FunctionWrapper(genrec), | |
1220 | - FunctionWrapper(help_), | |
1221 | - FunctionWrapper(i), | |
1222 | - FunctionWrapper(ifte), | |
1223 | - FunctionWrapper(infra), | |
1224 | - FunctionWrapper(loop), | |
1225 | - FunctionWrapper(map_), | |
1226 | -# FunctionWrapper(nullary), | |
1227 | - FunctionWrapper(step), | |
1228 | - FunctionWrapper(times), | |
1229 | -# FunctionWrapper(ternary), | |
1230 | -# FunctionWrapper(unary), | |
1231 | -# FunctionWrapper(while_), | |
1232 | - FunctionWrapper(words), | |
1233 | - FunctionWrapper(x), | |
1234 | - ) | |
1351 | + ): | |
1352 | + inscribe(F) | |
1235 | 1353 | |
1236 | 1354 | |
1237 | -primitives = ( | |
1238 | - SimpleFunctionWrapper(choice), | |
1239 | - SimpleFunctionWrapper(clear), | |
1240 | - SimpleFunctionWrapper(concat), | |
1241 | - SimpleFunctionWrapper(cons), | |
1242 | - SimpleFunctionWrapper(divmod_), | |
1243 | - SimpleFunctionWrapper(drop), | |
1244 | - SimpleFunctionWrapper(dup), | |
1245 | - SimpleFunctionWrapper(dupd), | |
1246 | - SimpleFunctionWrapper(first), | |
1247 | - SimpleFunctionWrapper(getitem), | |
1248 | - SimpleFunctionWrapper(id_), | |
1249 | - SimpleFunctionWrapper(max_), | |
1250 | - SimpleFunctionWrapper(min_), | |
1251 | - SimpleFunctionWrapper(over), | |
1252 | - SimpleFunctionWrapper(parse), | |
1253 | - SimpleFunctionWrapper(pm), | |
1254 | - SimpleFunctionWrapper(pop), | |
1255 | - SimpleFunctionWrapper(popd), | |
1256 | - SimpleFunctionWrapper(popdd), | |
1257 | - SimpleFunctionWrapper(popop), | |
1258 | - SimpleFunctionWrapper(pred), | |
1259 | - SimpleFunctionWrapper(remove), | |
1260 | - SimpleFunctionWrapper(rest), | |
1261 | - SimpleFunctionWrapper(reverse), | |
1262 | - SimpleFunctionWrapper(rolldown), | |
1263 | - SimpleFunctionWrapper(rollup), | |
1264 | - SimpleFunctionWrapper(select), | |
1265 | - SimpleFunctionWrapper(shunt), | |
1266 | - SimpleFunctionWrapper(sort_), | |
1267 | - SimpleFunctionWrapper(stack_), | |
1268 | - SimpleFunctionWrapper(succ), | |
1269 | - SimpleFunctionWrapper(sum_), | |
1270 | - SimpleFunctionWrapper(swaack), | |
1271 | - SimpleFunctionWrapper(swap), | |
1272 | - SimpleFunctionWrapper(take), | |
1273 | - SimpleFunctionWrapper(truthy), | |
1274 | - SimpleFunctionWrapper(tuck), | |
1275 | - SimpleFunctionWrapper(uncons), | |
1276 | - SimpleFunctionWrapper(unique), | |
1277 | - SimpleFunctionWrapper(unstack), | |
1278 | - SimpleFunctionWrapper(unstack), | |
1279 | - SimpleFunctionWrapper(void), | |
1280 | - SimpleFunctionWrapper(zip_), | |
1281 | - | |
1282 | - FunctionWrapper(sharing), | |
1283 | - FunctionWrapper(warranty), | |
1284 | - ) | |
1355 | +add_aliases(_dictionary) | |
1285 | 1356 | |
1286 | 1357 | |
1287 | -def initialize(dictionary=None): | |
1288 | - if dictionary is None: | |
1289 | - dictionary = {} | |
1290 | - dictionary.update((F.name, F) for F in builtins) | |
1291 | - dictionary.update((F.name, F) for F in combinators) | |
1292 | - dictionary.update((F.name, F) for F in primitives) | |
1293 | - add_aliases(dictionary) | |
1294 | - DefinitionWrapper.add_definitions(definitions, dictionary) | |
1295 | - return dictionary | |
1358 | +DefinitionWrapper.add_definitions(definitions, _dictionary) |
@@ -34,7 +34,7 @@ | ||
34 | 34 | behaviour of the original version written in C.'''), |
35 | 35 | author='Simon Forman', |
36 | 36 | author_email='forman.simon@gmail.com', |
37 | - url='https://github.com/calroc/joypy', | |
37 | + url='https://osdn.net/projects/joypy', | |
38 | 38 | packages=['joy', 'joy.utils'], |
39 | 39 | classifiers=[ |
40 | 40 | 'Development Status :: 3 - Alpha', |