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>']),
78 def add_aliases(D, A):
80 Given a dict and a iterable of (name, [alias, ...]) pairs, create
81 additional entries in the dict mapping each alias to the named function
82 if it's in the dict. Aliases for functions not in the dict are ignored.
84 for name, aliases in A:
95 product == 1 swap [*] step
96 flatten == [] swap [concat] step
99 enstacken == stack [clear] dip
100 disenstacken == ? [uncons ?] loop pop
102 dinfrirst == dip infra first
103 nullary == [stack] dinfrirst
104 unary == nullary popd
105 binary == nullary [popop] dip
106 ternary == unary [popop] dip
110 size == 0 swap [pop ++] step
111 cleave == [i] app2 [popd] dip
112 average == [sum 1.0 *] [size] cleave /
113 gcd == 1 [tuck modulus dup 0 >] loop pop
114 least_fraction == dup [gcd] infra [div] concat map
115 *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
116 *fraction0 == concat [[swap] dip * [*] dip] infra
117 down_to_zero == [0 >] [dup --] while
118 range_to_zero == unit [down_to_zero] infra
119 anamorphism == [pop []] swap [dip swons] genrec
120 range == [0 <=] [1 - dup] anamorphism
121 while == swap [nullary] cons dup dipd concat loop
123 primrec == [i] genrec
124 step_zero == 0 roll> step
125 codireco == cons dip rest cons
126 make_generator == [codireco] ccons
128 # ifte == [nullary not] dipd branch
129 # ifte == [nullary] dipd swap branch
130 # genrec == [[genrec] cons cons cons cons] nullary swons concat ifte
132 # Another definition for while. FWIW
133 # while == over [[i] dip nullary] ccons [nullary] dip loop
137 ##second == rest first
138 ##third == rest rest first
140 ##swoncat == swap concat
143 ##z-down == [] swap uncons swap
144 ##z-up == swons swap shunt
145 ##z-right == [swons] cons dip uncons swap
146 ##z-left == swons [uncons swap] dip swap
149 ##divisor == popop 2 *
151 ##radical == swap dup * rollup * 4 * - sqrt
154 ##q0 == [[divisor] [minusb] [radical]] pam
155 ##q1 == [[root1] [root2]] pam
156 ##quadratic == [q0] ternary i [q1] ternary
160 ##PE1.1 == + dup [+] dip
161 ##PE1.2 == dup [3 & PE1.1] dip 2 >>
162 ##PE1.3 == 14811 swap [PE1.2] times pop
163 ##PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop
165 #PE1.2 == [PE1.1] step
166 #PE1 == 0 0 66 [[3 2 1 3 1 2 3] PE1.2] times [3 2 1 3] PE1.2 pop
170 def FunctionWrapper(f):
171 '''Set name attribute.'''
173 raise ValueError('Function %s must have doc string.' % f.__name__)
174 f.name = f.__name__.rstrip('_') # Don't shadow builtins.
178 def SimpleFunctionWrapper(f):
180 Wrap functions that take and return just a stack.
184 @rename_code_object(f.__name__)
185 def inner(stack, expression, dictionary):
186 return f(stack), expression, dictionary
190 def BinaryBuiltinWrapper(f):
192 Wrap functions that take two arguments and return a single result.
196 @rename_code_object(f.__name__)
197 def inner(stack, expression, dictionary):
198 (a, (b, stack)) = stack
200 return (result, stack), expression, dictionary
204 def UnaryBuiltinWrapper(f):
206 Wrap functions that take one argument and return a single result.
210 @rename_code_object(f.__name__)
211 def inner(stack, expression, dictionary):
214 return (result, stack), expression, dictionary
218 class DefinitionWrapper(object):
220 Provide implementation of defined functions, and some helper methods.
223 def __init__(self, name, body_text, doc=None):
224 self.name = self.__name__ = name
225 self.body = text_to_expression(body_text)
226 self._body = tuple(iter_stack(self.body))
227 self.__doc__ = doc or body_text
228 self._compiled = None
230 def __call__(self, stack, expression, dictionary):
232 return self._compiled(stack, expression, dictionary)
233 expression = list_to_stack(self._body, expression)
234 return stack, expression, dictionary
237 def parse_definition(class_, defi):
239 Given some text describing a Joy function definition parse it and
240 return a DefinitionWrapper.
242 name, proper, body_text = (n.strip() for n in defi.partition('=='))
244 raise ValueError('Definition %r failed' % (defi,))
245 return class_(name, body_text)
248 def add_definitions(class_, defs, dictionary):
250 Scan multi-line string defs for definitions and add them to the
253 for definition in _text_to_defs(defs):
254 class_.add_def(definition, dictionary)
257 def add_def(class_, definition, dictionary):
259 Add the definition to the dictionary.
261 F = class_.parse_definition(definition)
262 dictionary[F.name] = F
265 def _text_to_defs(text):
266 return (line.strip() for line in text.splitlines() if '==' in line)
274 # Load the auto-generated primitives into the dictionary.
275 for name, primitive in getmembers(genlib, isfunction):
276 inscribe(SimpleFunctionWrapper(primitive))
280 @SimpleFunctionWrapper
282 '''Parse the string on the stack to a Joy expression.'''
284 expression = text_to_expression(text)
285 return expression, stack
289 @SimpleFunctionWrapper
294 getitem == drop first
296 Expects an integer and a quote on the stack and returns the item at the
297 nth position in the quote counting from 0.
301 -------------------------
305 n, (Q, stack) = stack
306 return pick(Q, n), stack
310 @SimpleFunctionWrapper
317 Expects an integer and a quote on the stack and returns the quote with
318 n items removed off the top.
322 ----------------------
326 n, (Q, stack) = stack
337 @SimpleFunctionWrapper
340 Expects an integer and a quote on the stack and returns the quote with
341 just the top n items in reverse order (because that's easier and you can
342 use reverse if needed.)
346 ----------------------
350 n, (Q, stack) = stack
363 @SimpleFunctionWrapper
366 Use a Boolean value to select one of two items.
370 ----------------------
375 ---------------------
378 Currently Python semantics are used to evaluate the "truthiness" of the
379 Boolean value (so empty string, zero, etc. are counted as false, etc.)
381 (if_, (then, (else_, stack))) = stack
382 return then if if_ else else_, stack
386 @SimpleFunctionWrapper
389 Use a Boolean value to select one of two items from a sequence.
393 ------------------------
398 -----------------------
401 The sequence can contain more than two items but not fewer.
402 Currently Python semantics are used to evaluate the "truthiness" of the
403 Boolean value (so empty string, zero, etc. are counted as false, etc.)
405 (flag, (choices, stack)) = stack
406 (else_, (then, _)) = choices
407 return then if flag else else_, stack
411 @SimpleFunctionWrapper
413 '''Given a list find the maximum.'''
415 return max(iter_stack(tos)), stack
419 @SimpleFunctionWrapper
421 '''Given a list find the minimum.'''
423 return min(iter_stack(tos)), stack
427 @SimpleFunctionWrapper
429 '''Given a quoted sequence of numbers return the sum.
431 sum == 0 swap [+] step
434 return sum(iter_stack(tos)), stack
438 @SimpleFunctionWrapper
441 Expects an item on the stack and a quote under it and removes that item
442 from the the quote. The item is only removed once.
446 ------------------------
450 (tos, (second, stack)) = S
451 l = list(iter_stack(second))
453 return list_to_stack(l), stack
457 @SimpleFunctionWrapper
459 '''Given a list remove duplicate items.'''
461 I = list(iter_stack(tos))
462 list_to_stack(sorted(set(I), key=I.index))
463 return list_to_stack(sorted(set(I), key=I.index)), stack
467 @SimpleFunctionWrapper
469 '''Given a list return it sorted.'''
471 return list_to_stack(sorted(iter_stack(tos))), stack
475 @SimpleFunctionWrapper
477 '''Clear everything from the stack.
488 @SimpleFunctionWrapper
491 The unstack operator expects a list on top of the stack and makes that
492 the stack discarding the rest of the stack.
498 @SimpleFunctionWrapper
500 '''Reverse the list on the top of the stack.
503 reverse == [] swap shunt
507 for term in iter_stack(tos):
513 @SimpleFunctionWrapper
515 '''Concatinate the two lists on the top of the stack.
518 [a b c] [d e f] concat
519 ----------------------------
523 (tos, (second, stack)) = S
524 return concat(second, tos), stack
528 @SimpleFunctionWrapper
530 '''Like concat but reverses the top list into the second.
533 shunt == [swons] step == reverse swap concat
535 [a b c] [d e f] shunt
536 ---------------------------
540 (tos, (second, stack)) = stack
543 second = term, second
548 @SimpleFunctionWrapper
551 Replace the two lists on the top of the stack with a list of the pairs
552 from each list. The smallest list sets the length of the result list.
554 (tos, (second, stack)) = S
557 for a, b in zip(iter_stack(tos), iter_stack(second))
559 return list_to_stack(accumulator), stack
563 @SimpleFunctionWrapper
567 return tos + 1, stack
571 @SimpleFunctionWrapper
575 return tos - 1, stack
579 @SimpleFunctionWrapper
590 a, (b, stack) = stack
596 return int(math.floor(n))
598 floor.__doc__ = math.floor.__doc__
602 @SimpleFunctionWrapper
605 divmod(x, y) -> (quotient, remainder)
607 Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
616 Return the square root of the number a.
617 Negative numbers return complex roots.
622 assert a < 0, repr(a)
623 r = math.sqrt(-a) * 1j
629 # if isinstance(text, str):
630 # return run(text, stack)
635 @SimpleFunctionWrapper
637 '''The identity function.'''
642 @SimpleFunctionWrapper
644 '''True if the form on TOS is void otherwise False.'''
646 return _void(form), stack
650 return any(not _void(i) for i in iter_stack(form))
661 def words(stack, expression, dictionary):
662 '''Print all the words in alphabetical order.'''
663 print(' '.join(sorted(dictionary)))
664 return stack, expression, dictionary
669 def sharing(stack, expression, dictionary):
670 '''Print redistribution information.'''
671 print("You may convey verbatim copies of the Program's source code as"
672 ' you receive it, in any medium, provided that you conspicuously'
673 ' and appropriately publish on each copy an appropriate copyright'
674 ' notice; keep intact all notices stating that this License and'
675 ' any non-permissive terms added in accord with section 7 apply'
676 ' to the code; keep intact all notices of the absence of any'
677 ' warranty; and give all recipients a copy of this License along'
679 ' You should have received a copy of the GNU General Public License'
680 ' along with Thun. If not see <http://www.gnu.org/licenses/>.')
681 return stack, expression, dictionary
686 def warranty(stack, expression, dictionary):
687 '''Print warranty information.'''
688 print('THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY'
689 ' APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE'
690 ' COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM'
691 ' "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR'
692 ' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES'
693 ' OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE'
694 ' ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS'
695 ' WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE'
696 ' COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.')
697 return stack, expression, dictionary
700 # def simple_manual(stack):
702 # Print words and help for each word.
704 # for name, f in sorted(FUNCTIONS.items()):
706 # boxline = '+%s+' % ('-' * (len(name) + 2))
709 # '| %s |' % (name,),
711 # d if d else ' ...',
721 def help_(S, expression, dictionary):
722 '''Accepts a quoted symbol on the top of the stack and prints its docs.'''
723 ((symbol, _), stack) = S
724 word = dictionary[symbol]
726 return stack, expression, dictionary
734 # Several combinators depend on other words in their definitions,
735 # we use symbols to prevent hard-coding these, so in theory, you
736 # could change the word in the dictionary to use different semantics.
737 S_choice = Symbol('choice')
738 S_first = Symbol('first')
739 S_getitem = Symbol('getitem')
740 S_genrec = Symbol('genrec')
741 S_loop = Symbol('loop')
743 S_ifte = Symbol('ifte')
744 S_infra = Symbol('infra')
745 S_step = Symbol('step')
746 S_times = Symbol('times')
747 S_swaack = Symbol('swaack')
748 S_truthy = Symbol('truthy')
753 def i(stack, expression, dictionary):
755 The i combinator expects a quoted program on the stack and unpacks it
756 onto the pending expression for evaluation.
765 return stack, concat(quote, expression), dictionary
770 def x(stack, expression, dictionary):
776 ... [Q] x = ... [Q] dup i
777 ... [Q] x = ... [Q] [Q] i
778 ... [Q] x = ... [Q] Q
782 return stack, concat(quote, expression), dictionary
787 def b(stack, expression, dictionary):
793 ... [P] [Q] b == ... [P] i [Q] i
794 ... [P] [Q] b == ... P Q
797 q, (p, (stack)) = stack
798 return stack, concat(p, concat(q, expression)), dictionary
803 def dupdip(stack, expression, dictionary):
807 [F] dupdip == dup [F] dip
817 return stack, concat(F, (a, expression)), dictionary
822 def infra(stack, expression, dictionary):
824 Accept a quoted program and a list on the stack and run the program
825 with the list as its stack.
828 ... [a b c] [Q] . infra
829 -----------------------------
830 c b a . Q [...] swaack
833 (quote, (aggregate, stack)) = stack
834 return aggregate, concat(quote, (stack, (S_swaack, expression))), dictionary
839 def genrec(stack, expression, dictionary):
841 General Recursion Combinator.
844 [if] [then] [rec1] [rec2] genrec
845 ---------------------------------------------------------------------
846 [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
848 From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun:
849 "The genrec combinator takes four program parameters in addition to
850 whatever data parameters it needs. Fourth from the top is an if-part,
851 followed by a then-part. If the if-part yields true, then the then-part
852 is executed and the combinator terminates. The other two parameters are
853 the rec1-part and the rec2-part. If the if-part yields false, the
854 rec1-part is executed. Following that the four program parameters and
855 the combinator are again pushed onto the stack bundled up in a quoted
856 form. Then the rec2-part is executed, where it will find the bundled
857 form. Typically it will then execute the bundled form, either with i or
858 with app2, or some other combinator."
860 The way to design one of these is to fix your base case [then] and the
861 test [if], and then treat rec1 and rec2 as an else-part "sandwiching"
862 a quotation of the whole function.
864 For example, given a (general recursive) function 'F':
867 F == [I] [T] [R1] [R2] genrec
869 If the [I] if-part fails you must derive R1 and R2 from:
874 Just set the stack arguments in front, and figure out what R1 and R2
875 have to do to apply the quoted [F] in the proper way. In effect, the
876 genrec combinator turns into an ifte combinator with a quoted copy of
877 the original definition in the else-part:
880 F == [I] [T] [R1] [R2] genrec
881 == [I] [T] [R1 [F] R2] ifte
883 Primitive recursive functions are those where R2 == i.
886 P == [I] [T] [R] primrec
887 == [I] [T] [R [P] i] ifte
888 == [I] [T] [R P] ifte
891 (rec2, (rec1, stack)) = stack
892 (then, (if_, _)) = stack
893 F = (if_, (then, (rec1, (rec2, (S_genrec, ())))))
894 else_ = concat(rec1, (F, rec2))
895 return (else_, stack), (S_ifte, expression), dictionary
900 def map_(S, expression, dictionary):
902 Run the quoted program on TOS on the items in the list under it, push a
903 new list with the results (in place of the program and original list.
905 # (quote, (aggregate, stack)) = S
906 # results = list_to_stack([
907 # joy((term, stack), quote, dictionary)[0][0]
908 # for term in iter_stack(aggregate)
910 # return (results, stack), expression, dictionary
911 (quote, (aggregate, stack)) = S
913 return (aggregate, stack), expression, dictionary
915 for term in iter_stack(aggregate):
917 batch = (s, (quote, (S_infra, (S_first, batch))))
918 stack = (batch, ((), stack))
919 return stack, (S_infra, expression), dictionary
922 #def cleave(S, expression, dictionary):
924 # The cleave combinator expects two quotations, and below that an item X.
925 # It first executes [P], with X on top, and saves the top result element.
926 # Then it executes [Q], again with X, and saves the top result.
927 # Finally it restores the stack to what it was below X and pushes the two
928 # results P(X) and Q(X).
930 # (Q, (P, (x, stack))) = S
931 # p = joy((x, stack), P, dictionary)[0][0]
932 # q = joy((x, stack), Q, dictionary)[0][0]
933 # return (q, (p, stack)), expression, dictionary
938 def branch(stack, expression, dictionary):
940 Use a Boolean value to select one of two quoted programs to run.
944 branch == roll< choice i
949 --------------------------
953 -------------------------
957 (then, (else_, (flag, stack))) = stack
958 return stack, concat(then if flag else else_, expression), dictionary
963 def ifte(stack, expression, dictionary):
965 If-Then-Else Combinator
968 ... [if] [then] [else] ifte
969 ---------------------------------------------------
970 ... [[else] [then]] [...] [if] infra select i
975 ... [if] [then] [else] ifte
976 -------------------------------------------------------
977 ... [else] [then] [...] [if] infra first choice i
980 Has the effect of grabbing a copy of the stack on which to run the
983 (else_, (then, (if_, stack))) = stack
984 expression = (S_infra, (S_first, (S_choice, (S_i, expression))))
985 stack = (if_, (stack, (then, (else_, stack))))
986 return stack, expression, dictionary
991 def cond(stack, expression, dictionary):
993 This combinator works like a case statement. It expects a single quote
994 on the stack that must contain zero or more condition quotes and a
995 default quote. Each condition clause should contain a quoted predicate
996 followed by the function expression to run if that predicate returns
997 true. If no predicates return true the default function runs.
999 It works by rewriting into a chain of nested `ifte` expressions, e.g.::
1001 [[[B0] T0] [[B1] T1] [D]] cond
1002 -----------------------------------------
1003 [B0] [T0] [[B1] [T1] [D] ifte] ifte
1006 conditions, stack = stack
1008 expression = _cond(conditions, expression)
1010 # Attempt to preload the args to first ifte.
1011 (P, (T, (E, expression))) = expression
1013 # If, for any reason, the argument to cond should happen to contain
1014 # only the default clause then this optimization will fail.
1017 stack = (E, (T, (P, stack)))
1018 return stack, expression, dictionary
1021 def _cond(conditions, expression):
1022 (clause, rest) = conditions
1023 if not rest: # clause is [D]
1026 return (P, (T, (_cond(rest, ()), (S_ifte, expression))))
1031 def dip(stack, expression, dictionary):
1033 The dip combinator expects a quoted program on the stack and below it
1034 some item, it hoists the item into the expression and runs the program
1035 on the rest of the stack.
1043 (quote, (x, stack)) = stack
1044 expression = (x, expression)
1045 return stack, concat(quote, expression), dictionary
1050 def dipd(S, expression, dictionary):
1052 Like dip but expects two items.
1056 ---------------------
1060 (quote, (x, (y, stack))) = S
1061 expression = (y, (x, expression))
1062 return stack, concat(quote, expression), dictionary
1067 def dipdd(S, expression, dictionary):
1069 Like dip but expects three items.
1073 -----------------------
1077 (quote, (x, (y, (z, stack)))) = S
1078 expression = (z, (y, (x, expression)))
1079 return stack, concat(quote, expression), dictionary
1084 def app1(S, expression, dictionary):
1086 Given a quoted program on TOS and anything as the second stack item run
1087 the program and replace the two args with the first result of the
1092 -----------------------------------
1093 ... [x ...] [Q] . infra first
1095 (quote, (x, stack)) = S
1096 stack = (quote, ((x, stack), stack))
1097 expression = (S_infra, (S_first, expression))
1098 return stack, expression, dictionary
1103 def app2(S, expression, dictionary):
1104 '''Like app1 with two items.
1108 -----------------------------------
1109 ... [y ...] [Q] . infra first
1110 [x ...] [Q] infra first
1113 (quote, (x, (y, stack))) = S
1114 expression = (S_infra, (S_first,
1115 ((x, stack), (quote, (S_infra, (S_first,
1117 stack = (quote, ((y, stack), stack))
1118 return stack, expression, dictionary
1123 def app3(S, expression, dictionary):
1124 '''Like app1 with three items.
1127 ... z y x [Q] . app3
1128 -----------------------------------
1129 ... [z ...] [Q] . infra first
1130 [y ...] [Q] infra first
1131 [x ...] [Q] infra first
1134 (quote, (x, (y, (z, stack)))) = S
1135 expression = (S_infra, (S_first,
1136 ((y, stack), (quote, (S_infra, (S_first,
1137 ((x, stack), (quote, (S_infra, (S_first,
1138 expression))))))))))
1139 stack = (quote, ((z, stack), stack))
1140 return stack, expression, dictionary
1145 def step(S, expression, dictionary):
1147 Run a quoted program on each item in a sequence.
1151 -----------------------
1156 ------------------------
1160 ... [a b c] [Q] . step
1161 ----------------------------------------
1162 ... a . Q [b c] [Q] step
1164 The step combinator executes the quotation on each member of the list
1165 on top of the stack.
1167 (quote, (aggregate, stack)) = S
1169 return stack, expression, dictionary
1170 head, tail = aggregate
1171 stack = quote, (head, stack)
1173 expression = tail, (quote, (S_step, expression))
1174 expression = S_i, expression
1175 return stack, expression, dictionary
1180 def times(stack, expression, dictionary):
1182 times == [-- dip] cons [swap] infra [0 >] swap while pop
1186 --------------------- w/ n <= 0
1191 ---------------------------------
1196 --------------------------------- w/ n > 1
1197 ... . Q (n - 1) [Q] times
1200 # times == [-- dip] cons [swap] infra [0 >] swap while pop
1201 (quote, (n, stack)) = stack
1203 return stack, expression, dictionary
1206 expression = n, (quote, (S_times, expression))
1207 expression = concat(quote, expression)
1208 return stack, expression, dictionary
1211 # The current definition above works like this:
1214 # --------------------------------------
1215 # [P] nullary [Q [P] nullary] loop
1217 # while == [pop i not] [popop] [dudipd] primrec
1219 #def while_(S, expression, dictionary):
1220 # '''[if] [body] while'''
1221 # (body, (if_, stack)) = S
1222 # while joy(stack, if_, dictionary)[0][0]:
1223 # stack = joy(stack, body, dictionary)[0]
1224 # return stack, expression, dictionary
1229 def loop(stack, expression, dictionary):
1231 Basic loop combinator.
1235 -----------------------
1239 ------------------------
1243 quote, (flag, stack) = stack
1245 expression = concat(quote, (quote, (S_loop, expression)))
1246 return stack, expression, dictionary
1251 def cmp_(stack, expression, dictionary):
1253 cmp takes two values and three quoted programs on the stack and runs
1254 one of the three depending on the results of comparing the two values:
1258 ------------------------- a > b
1262 ------------------------- a = b
1266 ------------------------- a < b
1269 L, (E, (G, (b, (a, stack)))) = stack
1270 expression = concat(G if a > b else L if a < b else E, expression)
1271 return stack, expression, dictionary
1274 # FunctionWrapper(cleave),
1275 # FunctionWrapper(while_),
1279 BinaryBuiltinWrapper(operator.add),
1280 BinaryBuiltinWrapper(operator.and_),
1281 BinaryBuiltinWrapper(operator.div),
1282 BinaryBuiltinWrapper(operator.eq),
1283 BinaryBuiltinWrapper(operator.floordiv),
1284 BinaryBuiltinWrapper(operator.ge),
1285 BinaryBuiltinWrapper(operator.gt),
1286 BinaryBuiltinWrapper(operator.le),
1287 BinaryBuiltinWrapper(operator.lshift),
1288 BinaryBuiltinWrapper(operator.lt),
1289 BinaryBuiltinWrapper(operator.mod),
1290 BinaryBuiltinWrapper(operator.mul),
1291 BinaryBuiltinWrapper(operator.ne),
1292 BinaryBuiltinWrapper(operator.or_),
1293 BinaryBuiltinWrapper(operator.pow),
1294 BinaryBuiltinWrapper(operator.rshift),
1295 BinaryBuiltinWrapper(operator.sub),
1296 BinaryBuiltinWrapper(operator.truediv),
1297 BinaryBuiltinWrapper(operator.xor),
1299 UnaryBuiltinWrapper(abs),
1300 UnaryBuiltinWrapper(bool),
1301 UnaryBuiltinWrapper(floor),
1302 UnaryBuiltinWrapper(operator.neg),
1303 UnaryBuiltinWrapper(operator.not_),
1304 UnaryBuiltinWrapper(sqrt),
1307 del F # Otherwise Sphinx autodoc will pick it up.
1310 add_aliases(_dictionary, ALIASES)
1313 DefinitionWrapper.add_definitions(definitions, _dictionary)