1 # -*- coding: utf-8 -*-
3 # Copyright © 2014, 2015, 2017, 2018 Simon Forman
5 # This file is part of Thun
7 # Thun is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # Thun is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Thun. If not see <http://www.gnu.org/licenses/>.
21 This module contains the Joy function infrastructure and a library of
22 functions. Its main export is a Python function initialize() that
23 returns a dictionary of Joy functions suitable for use with the joy()
26 from inspect import getdoc
27 from functools import wraps
28 from inspect import getmembers, isfunction
31 from .parser import text_to_expression, Symbol
32 from .utils.stack import list_to_stack, iter_stack, pick, concat
33 from .utils.brutal_hackery import rename_code_object
35 from .utils import generated_library as genlib
41 def inscribe(function):
42 '''A decorator to inscribe functions into the default dictionary.'''
43 _dictionary[function.name] = function
48 '''Return a dictionary of Joy functions for use with joy().'''
49 return _dictionary.copy()
58 ('mod', ['%', 'rem', 'remainder', 'modulus']),
61 ('getitem', ['pick', 'at']),
72 ('rolldown', ['roll<']),
73 ('rollup', ['roll>']),
79 def add_aliases(D, A):
81 Given a dict and a iterable of (name, [alias, ...]) pairs, create
82 additional entries in the dict mapping each alias to the named function
83 if it's in the dict. Aliases for functions not in the dict are ignored.
85 for name, aliases in A:
96 product == 1 swap [*] step
97 flatten == [] swap [concat] step
100 enstacken == stack [clear] dip
101 disenstacken == ? [uncons ?] loop pop
103 dinfrirst == dip infra first
104 nullary == [stack] dinfrirst
105 unary == nullary popd
106 binary == nullary [popop] dip
107 ternary == unary [popop] dip
111 size == 0 swap [pop ++] step
113 cleave == fork [popd] dip
114 average == [sum 1.0 *] [size] cleave /
115 gcd == 1 [tuck modulus dup 0 >] loop pop
116 least_fraction == dup [gcd] infra [div] concat map
117 *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
118 *fraction0 == concat [[swap] dip * [*] dip] infra
119 down_to_zero == [0 >] [dup --] while
120 range_to_zero == unit [down_to_zero] infra
121 anamorphism == [pop []] swap [dip swons] genrec
122 range == [0 <=] [1 - dup] anamorphism
123 while == swap [nullary] cons dup dipd concat loop
125 primrec == [i] genrec
126 step_zero == 0 roll> step
127 codireco == cons dip rest cons
128 make_generator == [codireco] ccons
129 ifte == [nullary not] dipd branch
132 # ifte == [nullary] dipd swap branch
133 # genrec == [[genrec] cons cons cons cons] nullary swons concat ifte
135 # Another definition for while. FWIW
136 # while == over [[i] dip nullary] ccons [nullary] dip loop
140 ##second == rest first
141 ##third == rest rest first
143 ##swoncat == swap concat
146 ##z-down == [] swap uncons swap
147 ##z-up == swons swap shunt
148 ##z-right == [swons] cons dip uncons swap
149 ##z-left == swons [uncons swap] dip swap
152 ##divisor == popop 2 *
154 ##radical == swap dup * rollup * 4 * - sqrt
157 ##q0 == [[divisor] [minusb] [radical]] pam
158 ##q1 == [[root1] [root2]] pam
159 ##quadratic == [q0] ternary i [q1] ternary
163 ##PE1.1 == + dup [+] dip
164 ##PE1.2 == dup [3 & PE1.1] dip 2 >>
165 ##PE1.3 == 14811 swap [PE1.2] times pop
166 ##PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop
168 #PE1.2 == [PE1.1] step
169 #PE1 == 0 0 66 [[3 2 1 3 1 2 3] PE1.2] times [3 2 1 3] PE1.2 pop
173 def FunctionWrapper(f):
174 '''Set name attribute.'''
176 raise ValueError('Function %s must have doc string.' % f.__name__)
177 f.name = f.__name__.rstrip('_') # Don't shadow builtins.
181 def SimpleFunctionWrapper(f):
183 Wrap functions that take and return just a stack.
187 @rename_code_object(f.__name__)
188 def inner(stack, expression, dictionary):
189 return f(stack), expression, dictionary
193 def BinaryBuiltinWrapper(f):
195 Wrap functions that take two arguments and return a single result.
199 @rename_code_object(f.__name__)
200 def inner(stack, expression, dictionary):
201 (a, (b, stack)) = stack
203 return (result, stack), expression, dictionary
207 def UnaryBuiltinWrapper(f):
209 Wrap functions that take one argument and return a single result.
213 @rename_code_object(f.__name__)
214 def inner(stack, expression, dictionary):
217 return (result, stack), expression, dictionary
221 class DefinitionWrapper(object):
223 Provide implementation of defined functions, and some helper methods.
226 def __init__(self, name, body_text, doc=None):
227 self.name = self.__name__ = name
228 self.body = text_to_expression(body_text)
229 self._body = tuple(iter_stack(self.body))
230 self.__doc__ = doc or body_text
231 self._compiled = None
233 def __call__(self, stack, expression, dictionary):
235 return self._compiled(stack, expression, dictionary)
236 expression = list_to_stack(self._body, expression)
237 return stack, expression, dictionary
240 def parse_definition(class_, defi):
242 Given some text describing a Joy function definition parse it and
243 return a DefinitionWrapper.
245 name, proper, body_text = (n.strip() for n in defi.partition('=='))
247 raise ValueError('Definition %r failed' % (defi,))
248 return class_(name, body_text)
251 def add_definitions(class_, defs, dictionary):
253 Scan multi-line string defs for definitions and add them to the
256 for definition in _text_to_defs(defs):
257 class_.add_def(definition, dictionary)
260 def add_def(class_, definition, dictionary):
262 Add the definition to the dictionary.
264 F = class_.parse_definition(definition)
265 dictionary[F.name] = F
268 def _text_to_defs(text):
269 return (line.strip() for line in text.splitlines() if '==' in line)
277 # Load the auto-generated primitives into the dictionary.
278 for name, primitive in getmembers(genlib, isfunction):
279 inscribe(SimpleFunctionWrapper(primitive))
283 @SimpleFunctionWrapper
285 '''Parse the string on the stack to a Joy expression.'''
287 expression = text_to_expression(text)
288 return expression, stack
292 @SimpleFunctionWrapper
297 getitem == drop first
299 Expects an integer and a quote on the stack and returns the item at the
300 nth position in the quote counting from 0.
304 -------------------------
308 n, (Q, stack) = stack
309 return pick(Q, n), stack
313 @SimpleFunctionWrapper
320 Expects an integer and a quote on the stack and returns the quote with
321 n items removed off the top.
325 ----------------------
329 n, (Q, stack) = stack
340 @SimpleFunctionWrapper
343 Expects an integer and a quote on the stack and returns the quote with
344 just the top n items in reverse order (because that's easier and you can
345 use reverse if needed.)
349 ----------------------
353 n, (Q, stack) = stack
366 @SimpleFunctionWrapper
369 Use a Boolean value to select one of two items.
373 ----------------------
378 ---------------------
381 Currently Python semantics are used to evaluate the "truthiness" of the
382 Boolean value (so empty string, zero, etc. are counted as false, etc.)
384 (if_, (then, (else_, stack))) = stack
385 return then if if_ else else_, stack
389 @SimpleFunctionWrapper
392 Use a Boolean value to select one of two items from a sequence.
396 ------------------------
401 -----------------------
404 The sequence can contain more than two items but not fewer.
405 Currently Python semantics are used to evaluate the "truthiness" of the
406 Boolean value (so empty string, zero, etc. are counted as false, etc.)
408 (flag, (choices, stack)) = stack
409 (else_, (then, _)) = choices
410 return then if flag else else_, stack
414 @SimpleFunctionWrapper
416 '''Given a list find the maximum.'''
418 return max(iter_stack(tos)), stack
422 @SimpleFunctionWrapper
424 '''Given a list find the minimum.'''
426 return min(iter_stack(tos)), stack
430 @SimpleFunctionWrapper
432 '''Given a quoted sequence of numbers return the sum.
434 sum == 0 swap [+] step
437 return sum(iter_stack(tos)), stack
441 @SimpleFunctionWrapper
444 Expects an item on the stack and a quote under it and removes that item
445 from the the quote. The item is only removed once.
449 ------------------------
453 (tos, (second, stack)) = S
454 l = list(iter_stack(second))
456 return list_to_stack(l), stack
460 @SimpleFunctionWrapper
462 '''Given a list remove duplicate items.'''
464 I = list(iter_stack(tos))
465 list_to_stack(sorted(set(I), key=I.index))
466 return list_to_stack(sorted(set(I), key=I.index)), stack
470 @SimpleFunctionWrapper
472 '''Given a list return it sorted.'''
474 return list_to_stack(sorted(iter_stack(tos))), stack
478 @SimpleFunctionWrapper
480 '''Clear everything from the stack.
483 clear == stack [pop stack] loop
493 @SimpleFunctionWrapper
496 The unstack operator expects a list on top of the stack and makes that
497 the stack discarding the rest of the stack.
503 @SimpleFunctionWrapper
505 '''Reverse the list on the top of the stack.
508 reverse == [] swap shunt
512 for term in iter_stack(tos):
518 @SimpleFunctionWrapper
520 '''Concatinate the two lists on the top of the stack.
523 [a b c] [d e f] concat
524 ----------------------------
528 (tos, (second, stack)) = S
529 return concat(second, tos), stack
533 @SimpleFunctionWrapper
535 '''Like concat but reverses the top list into the second.
538 shunt == [swons] step == reverse swap concat
540 [a b c] [d e f] shunt
541 ---------------------------
545 (tos, (second, stack)) = stack
548 second = term, second
553 @SimpleFunctionWrapper
556 Replace the two lists on the top of the stack with a list of the pairs
557 from each list. The smallest list sets the length of the result list.
559 (tos, (second, stack)) = S
562 for a, b in zip(iter_stack(tos), iter_stack(second))
564 return list_to_stack(accumulator), stack
568 @SimpleFunctionWrapper
572 return tos + 1, stack
576 @SimpleFunctionWrapper
580 return tos - 1, stack
584 @SimpleFunctionWrapper
595 a, (b, stack) = stack
601 return int(math.floor(n))
603 floor.__doc__ = math.floor.__doc__
607 @SimpleFunctionWrapper
610 divmod(x, y) -> (quotient, remainder)
612 Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
621 Return the square root of the number a.
622 Negative numbers return complex roots.
627 assert a < 0, repr(a)
628 r = math.sqrt(-a) * 1j
634 # if isinstance(text, str):
635 # return run(text, stack)
640 @SimpleFunctionWrapper
642 '''The identity function.'''
647 @SimpleFunctionWrapper
649 '''True if the form on TOS is void otherwise False.'''
651 return _void(form), stack
655 return any(not _void(i) for i in iter_stack(form))
666 def words(stack, expression, dictionary):
667 '''Print all the words in alphabetical order.'''
668 print(' '.join(sorted(dictionary)))
669 return stack, expression, dictionary
674 def sharing(stack, expression, dictionary):
675 '''Print redistribution information.'''
676 print("You may convey verbatim copies of the Program's source code as"
677 ' you receive it, in any medium, provided that you conspicuously'
678 ' and appropriately publish on each copy an appropriate copyright'
679 ' notice; keep intact all notices stating that this License and'
680 ' any non-permissive terms added in accord with section 7 apply'
681 ' to the code; keep intact all notices of the absence of any'
682 ' warranty; and give all recipients a copy of this License along'
684 ' You should have received a copy of the GNU General Public License'
685 ' along with Thun. If not see <http://www.gnu.org/licenses/>.')
686 return stack, expression, dictionary
691 def warranty(stack, expression, dictionary):
692 '''Print warranty information.'''
693 print('THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY'
694 ' APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE'
695 ' COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM'
696 ' "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR'
697 ' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES'
698 ' OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE'
699 ' ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS'
700 ' WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE'
701 ' COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.')
702 return stack, expression, dictionary
705 # def simple_manual(stack):
707 # Print words and help for each word.
709 # for name, f in sorted(FUNCTIONS.items()):
711 # boxline = '+%s+' % ('-' * (len(name) + 2))
714 # '| %s |' % (name,),
716 # d if d else ' ...',
726 def help_(S, expression, dictionary):
727 '''Accepts a quoted symbol on the top of the stack and prints its docs.'''
728 ((symbol, _), stack) = S
729 word = dictionary[symbol]
731 return stack, expression, dictionary
739 # Several combinators depend on other words in their definitions,
740 # we use symbols to prevent hard-coding these, so in theory, you
741 # could change the word in the dictionary to use different semantics.
742 S_choice = Symbol('choice')
743 S_first = Symbol('first')
744 S_getitem = Symbol('getitem')
745 S_genrec = Symbol('genrec')
746 S_loop = Symbol('loop')
748 S_ifte = Symbol('ifte')
749 S_infra = Symbol('infra')
750 S_step = Symbol('step')
751 S_times = Symbol('times')
752 S_swaack = Symbol('swaack')
753 S_truthy = Symbol('truthy')
758 def i(stack, expression, dictionary):
760 The i combinator expects a quoted program on the stack and unpacks it
761 onto the pending expression for evaluation.
770 return stack, concat(quote, expression), dictionary
775 def x(stack, expression, dictionary):
781 ... [Q] x = ... [Q] dup i
782 ... [Q] x = ... [Q] [Q] i
783 ... [Q] x = ... [Q] Q
787 return stack, concat(quote, expression), dictionary
792 def b(stack, expression, dictionary):
798 ... [P] [Q] b == ... [P] i [Q] i
799 ... [P] [Q] b == ... P Q
802 q, (p, (stack)) = stack
803 return stack, concat(p, concat(q, expression)), dictionary
808 def dupdip(stack, expression, dictionary):
812 [F] dupdip == dup [F] dip
822 return stack, concat(F, (a, expression)), dictionary
827 def infra(stack, expression, dictionary):
829 Accept a quoted program and a list on the stack and run the program
830 with the list as its stack.
833 ... [a b c] [Q] . infra
834 -----------------------------
835 c b a . Q [...] swaack
838 (quote, (aggregate, stack)) = stack
839 return aggregate, concat(quote, (stack, (S_swaack, expression))), dictionary
844 def genrec(stack, expression, dictionary):
846 General Recursion Combinator.
849 [if] [then] [rec1] [rec2] genrec
850 ---------------------------------------------------------------------
851 [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
853 From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun:
854 "The genrec combinator takes four program parameters in addition to
855 whatever data parameters it needs. Fourth from the top is an if-part,
856 followed by a then-part. If the if-part yields true, then the then-part
857 is executed and the combinator terminates. The other two parameters are
858 the rec1-part and the rec2-part. If the if-part yields false, the
859 rec1-part is executed. Following that the four program parameters and
860 the combinator are again pushed onto the stack bundled up in a quoted
861 form. Then the rec2-part is executed, where it will find the bundled
862 form. Typically it will then execute the bundled form, either with i or
863 with app2, or some other combinator."
865 The way to design one of these is to fix your base case [then] and the
866 test [if], and then treat rec1 and rec2 as an else-part "sandwiching"
867 a quotation of the whole function.
869 For example, given a (general recursive) function 'F':
872 F == [I] [T] [R1] [R2] genrec
874 If the [I] if-part fails you must derive R1 and R2 from:
879 Just set the stack arguments in front, and figure out what R1 and R2
880 have to do to apply the quoted [F] in the proper way. In effect, the
881 genrec combinator turns into an ifte combinator with a quoted copy of
882 the original definition in the else-part:
885 F == [I] [T] [R1] [R2] genrec
886 == [I] [T] [R1 [F] R2] ifte
888 Primitive recursive functions are those where R2 == i.
891 P == [I] [T] [R] primrec
892 == [I] [T] [R [P] i] ifte
893 == [I] [T] [R P] ifte
896 (rec2, (rec1, stack)) = stack
897 (then, (if_, _)) = stack
898 F = (if_, (then, (rec1, (rec2, (S_genrec, ())))))
899 else_ = concat(rec1, (F, rec2))
900 return (else_, stack), (S_ifte, expression), dictionary
905 def map_(S, expression, dictionary):
907 Run the quoted program on TOS on the items in the list under it, push a
908 new list with the results (in place of the program and original list.
910 # (quote, (aggregate, stack)) = S
911 # results = list_to_stack([
912 # joy((term, stack), quote, dictionary)[0][0]
913 # for term in iter_stack(aggregate)
915 # return (results, stack), expression, dictionary
916 (quote, (aggregate, stack)) = S
918 return (aggregate, stack), expression, dictionary
920 for term in iter_stack(aggregate):
922 batch = (s, (quote, (S_infra, (S_first, batch))))
923 stack = (batch, ((), stack))
924 return stack, (S_infra, expression), dictionary
927 #def cleave(S, expression, dictionary):
929 # The cleave combinator expects two quotations, and below that an item X.
930 # It first executes [P], with X on top, and saves the top result element.
931 # Then it executes [Q], again with X, and saves the top result.
932 # Finally it restores the stack to what it was below X and pushes the two
933 # results P(X) and Q(X).
935 # (Q, (P, (x, stack))) = S
936 # p = joy((x, stack), P, dictionary)[0][0]
937 # q = joy((x, stack), Q, dictionary)[0][0]
938 # return (q, (p, stack)), expression, dictionary
943 def branch(stack, expression, dictionary):
945 Use a Boolean value to select one of two quoted programs to run.
949 branch == roll< choice i
954 --------------------------
958 -------------------------
962 (then, (else_, (flag, stack))) = stack
963 return stack, concat(then if flag else else_, expression), dictionary
968 ##def ifte(stack, expression, dictionary):
970 ## If-Then-Else Combinator
973 ## ... [if] [then] [else] ifte
974 ## ---------------------------------------------------
975 ## ... [[else] [then]] [...] [if] infra select i
980 ## ... [if] [then] [else] ifte
981 ## -------------------------------------------------------
982 ## ... [else] [then] [...] [if] infra first choice i
985 ## Has the effect of grabbing a copy of the stack on which to run the
986 ## if-part using infra.
988 ## (else_, (then, (if_, stack))) = stack
989 ## expression = (S_infra, (S_first, (S_choice, (S_i, expression))))
990 ## stack = (if_, (stack, (then, (else_, stack))))
991 ## return stack, expression, dictionary
996 def cond(stack, expression, dictionary):
998 This combinator works like a case statement. It expects a single quote
999 on the stack that must contain zero or more condition quotes and a
1000 default quote. Each condition clause should contain a quoted predicate
1001 followed by the function expression to run if that predicate returns
1002 true. If no predicates return true the default function runs.
1004 It works by rewriting into a chain of nested `ifte` expressions, e.g.::
1006 [[[B0] T0] [[B1] T1] [D]] cond
1007 -----------------------------------------
1008 [B0] [T0] [[B1] [T1] [D] ifte] ifte
1011 conditions, stack = stack
1013 expression = _cond(conditions, expression)
1015 # Attempt to preload the args to first ifte.
1016 (P, (T, (E, expression))) = expression
1018 # If, for any reason, the argument to cond should happen to contain
1019 # only the default clause then this optimization will fail.
1022 stack = (E, (T, (P, stack)))
1023 return stack, expression, dictionary
1026 def _cond(conditions, expression):
1027 (clause, rest) = conditions
1028 if not rest: # clause is [D]
1031 return (P, (T, (_cond(rest, ()), (S_ifte, expression))))
1036 def dip(stack, expression, dictionary):
1038 The dip combinator expects a quoted program on the stack and below it
1039 some item, it hoists the item into the expression and runs the program
1040 on the rest of the stack.
1048 (quote, (x, stack)) = stack
1049 expression = (x, expression)
1050 return stack, concat(quote, expression), dictionary
1055 def dipd(S, expression, dictionary):
1057 Like dip but expects two items.
1061 ---------------------
1065 (quote, (x, (y, stack))) = S
1066 expression = (y, (x, expression))
1067 return stack, concat(quote, expression), dictionary
1072 def dipdd(S, expression, dictionary):
1074 Like dip but expects three items.
1078 -----------------------
1082 (quote, (x, (y, (z, stack)))) = S
1083 expression = (z, (y, (x, expression)))
1084 return stack, concat(quote, expression), dictionary
1089 def app1(S, expression, dictionary):
1091 Given a quoted program on TOS and anything as the second stack item run
1092 the program and replace the two args with the first result of the
1097 -----------------------------------
1098 ... [x ...] [Q] . infra first
1100 (quote, (x, stack)) = S
1101 stack = (quote, ((x, stack), stack))
1102 expression = (S_infra, (S_first, expression))
1103 return stack, expression, dictionary
1108 def app2(S, expression, dictionary):
1109 '''Like app1 with two items.
1113 -----------------------------------
1114 ... [y ...] [Q] . infra first
1115 [x ...] [Q] infra first
1118 (quote, (x, (y, stack))) = S
1119 expression = (S_infra, (S_first,
1120 ((x, stack), (quote, (S_infra, (S_first,
1122 stack = (quote, ((y, stack), stack))
1123 return stack, expression, dictionary
1128 def app3(S, expression, dictionary):
1129 '''Like app1 with three items.
1132 ... z y x [Q] . app3
1133 -----------------------------------
1134 ... [z ...] [Q] . infra first
1135 [y ...] [Q] infra first
1136 [x ...] [Q] infra first
1139 (quote, (x, (y, (z, stack)))) = S
1140 expression = (S_infra, (S_first,
1141 ((y, stack), (quote, (S_infra, (S_first,
1142 ((x, stack), (quote, (S_infra, (S_first,
1143 expression))))))))))
1144 stack = (quote, ((z, stack), stack))
1145 return stack, expression, dictionary
1150 def step(S, expression, dictionary):
1152 Run a quoted program on each item in a sequence.
1156 -----------------------
1161 ------------------------
1165 ... [a b c] [Q] . step
1166 ----------------------------------------
1167 ... a . Q [b c] [Q] step
1169 The step combinator executes the quotation on each member of the list
1170 on top of the stack.
1172 (quote, (aggregate, stack)) = S
1174 return stack, expression, dictionary
1175 head, tail = aggregate
1176 stack = quote, (head, stack)
1178 expression = tail, (quote, (S_step, expression))
1179 expression = S_i, expression
1180 return stack, expression, dictionary
1185 def times(stack, expression, dictionary):
1187 times == [-- dip] cons [swap] infra [0 >] swap while pop
1191 --------------------- w/ n <= 0
1196 ---------------------------------
1201 --------------------------------- w/ n > 1
1202 ... . Q (n - 1) [Q] times
1205 # times == [-- dip] cons [swap] infra [0 >] swap while pop
1206 (quote, (n, stack)) = stack
1208 return stack, expression, dictionary
1211 expression = n, (quote, (S_times, expression))
1212 expression = concat(quote, expression)
1213 return stack, expression, dictionary
1216 # The current definition above works like this:
1219 # --------------------------------------
1220 # [P] nullary [Q [P] nullary] loop
1222 # while == [pop i not] [popop] [dudipd] primrec
1224 #def while_(S, expression, dictionary):
1225 # '''[if] [body] while'''
1226 # (body, (if_, stack)) = S
1227 # while joy(stack, if_, dictionary)[0][0]:
1228 # stack = joy(stack, body, dictionary)[0]
1229 # return stack, expression, dictionary
1234 def loop(stack, expression, dictionary):
1236 Basic loop combinator.
1240 -----------------------
1244 ------------------------
1248 quote, (flag, stack) = stack
1250 expression = concat(quote, (quote, (S_loop, expression)))
1251 return stack, expression, dictionary
1256 def cmp_(stack, expression, dictionary):
1258 cmp takes two values and three quoted programs on the stack and runs
1259 one of the three depending on the results of comparing the two values:
1263 ------------------------- a > b
1267 ------------------------- a = b
1271 ------------------------- a < b
1274 L, (E, (G, (b, (a, stack)))) = stack
1275 expression = concat(G if a > b else L if a < b else E, expression)
1276 return stack, expression, dictionary
1279 # FunctionWrapper(cleave),
1280 # FunctionWrapper(while_),
1284 BinaryBuiltinWrapper(operator.add),
1285 BinaryBuiltinWrapper(operator.and_),
1286 BinaryBuiltinWrapper(operator.div),
1287 BinaryBuiltinWrapper(operator.eq),
1288 BinaryBuiltinWrapper(operator.floordiv),
1289 BinaryBuiltinWrapper(operator.ge),
1290 BinaryBuiltinWrapper(operator.gt),
1291 BinaryBuiltinWrapper(operator.le),
1292 BinaryBuiltinWrapper(operator.lshift),
1293 BinaryBuiltinWrapper(operator.lt),
1294 BinaryBuiltinWrapper(operator.mod),
1295 BinaryBuiltinWrapper(operator.mul),
1296 BinaryBuiltinWrapper(operator.ne),
1297 BinaryBuiltinWrapper(operator.or_),
1298 BinaryBuiltinWrapper(operator.pow),
1299 BinaryBuiltinWrapper(operator.rshift),
1300 BinaryBuiltinWrapper(operator.sub),
1301 BinaryBuiltinWrapper(operator.truediv),
1302 BinaryBuiltinWrapper(operator.xor),
1304 UnaryBuiltinWrapper(abs),
1305 UnaryBuiltinWrapper(bool),
1306 UnaryBuiltinWrapper(floor),
1307 UnaryBuiltinWrapper(operator.neg),
1308 UnaryBuiltinWrapper(operator.not_),
1309 UnaryBuiltinWrapper(sqrt),
1312 del F # Otherwise Sphinx autodoc will pick it up.
1315 add_aliases(_dictionary, ALIASES)
1318 DefinitionWrapper.add_definitions(definitions, _dictionary)