1 # -*- coding: utf-8 -*-
3 # Copyright © 2014-2020 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 pkg_resources import resource_stream
27 from io import TextIOWrapper
28 from inspect import getdoc, getmembers, isfunction
29 from functools import wraps
30 from itertools import count
33 from . import __name__ as _joy_package_name
34 from .parser import text_to_expression, Symbol
35 from .utils import generated_library as genlib
36 from .utils.errors import (
41 from .utils.stack import (
50 def default_defs(dictionary):
51 def_stream = TextIOWrapper(
52 resource_stream(_joy_package_name, 'defs.txt'),
55 Def.load_definitions(def_stream, dictionary)
68 # This is the main dict we're building.
72 def inscribe(function, d=_dictionary):
73 '''A decorator to inscribe functions into the default dictionary.'''
74 d[function.name] = function
79 '''Return a dictionary of Joy functions for use with joy().'''
80 return _dictionary.copy()
88 ('floordiv', ['/floor', '//', '/', 'div']),
89 ('mod', ['%', 'rem', 'remainder', 'modulus']),
92 ('getitem', ['pick', 'at']),
103 ('rolldown', ['roll<']),
104 ('rollup', ['roll>']),
110 def add_aliases(D, A):
112 Given a dict and a iterable of (name, [alias, ...]) pairs, create
113 additional entries in the dict mapping each alias to the named function
114 if it's in the dict. Aliases for functions not in the dict are ignored.
116 for name, aliases in A:
121 for alias in aliases:
125 def FunctionWrapper(f):
126 '''Set name attribute.'''
128 raise ValueError('Function %s must have doc string.' % f.__name__)
129 f.name = f.__name__.rstrip('_') # Don't shadow builtins.
133 def SimpleFunctionWrapper(f):
135 Wrap functions that take and return just a stack.
139 def inner(stack, expression, dictionary):
140 return f(stack), expression, dictionary
144 def BinaryBuiltinWrapper(f):
146 Wrap functions that take two arguments and return a single result.
150 def inner(stack, expression, dictionary):
152 (a, (b, stack)) = stack
154 raise StackUnderflowError('Not enough values on stack.')
155 # Boolean predicates like "or" fail here. :(
156 ## if ( not isinstance(a, int)
157 ## or not isinstance(b, int)
158 ## or isinstance(a, bool) # Because bools are ints in Python.
159 ## or isinstance(b, bool)
161 ## raise NotAnIntError
163 return (result, stack), expression, dictionary
167 def UnaryBuiltinWrapper(f):
169 Wrap functions that take one argument and return a single result.
173 def inner(stack, expression, dictionary):
176 return (result, stack), expression, dictionary
182 Definitions created by inscribe.
185 def __init__(self, name, body):
188 self._body = tuple(iter_stack(body))
189 self.__doc__ = expression_to_string(body)
190 self._compiled = None
192 def __call__(self, stack, expression, dictionary):
194 return self._compiled(stack, expression, dictionary) # pylint: disable=E1102
195 expression = list_to_stack(self._body, expression)
196 return stack, expression, dictionary
199 def load_definitions(class_, stream, dictionary):
201 if line.lstrip().startswith('#'):
203 name, body = text_to_expression(line)
204 if name not in dictionary:
205 inscribe(class_(name, body), dictionary)
215 def inscribe_(stack, expression, dictionary):
217 Create a new Joy function definition in the Joy dictionary. A
218 definition is given as a quote with a name followed by a Joy
219 expression. for example:
221 [sqr dup mul] inscribe
224 (name, body), stack = stack
225 inscribe(Def(name, body), dictionary)
226 return stack, expression, dictionary
230 @SimpleFunctionWrapper
232 '''Parse the string on the stack to a Joy expression.'''
234 expression = text_to_expression(text)
235 return expression, stack
239 # @SimpleFunctionWrapper
241 # '''Attempt to infer the stack effect of a Joy expression.'''
243 # effects = infer_expression(E)
244 # e = list_to_stack([(fi, (fo, ())) for fi, fo in effects])
249 @SimpleFunctionWrapper
254 getitem == drop first
256 Expects an integer and a quote on the stack and returns the item at the
257 nth position in the quote counting from 0.
261 -------------------------
265 n, (Q, stack) = stack
266 return pick(Q, n), stack
270 @SimpleFunctionWrapper
277 Expects an integer and a quote on the stack and returns the quote with
278 n items removed off the top.
282 ----------------------
286 n, (Q, stack) = stack
297 @SimpleFunctionWrapper
300 Expects an integer and a quote on the stack and returns the quote with
301 just the top n items in reverse order (because that's easier and you can
302 use reverse if needed.)
306 ----------------------
310 n, (Q, stack) = stack
324 def gcd2(stack, expression, dictionary):
325 '''Compiled GCD function.'''
326 (v1, (v2, stack)) = stack
331 (v1, (v2, stack)) = (v3, (v1, stack))
332 return (v2, stack), expression, dictionary
336 @SimpleFunctionWrapper
339 Use a Boolean value to select one of two items.
343 ----------------------
348 ---------------------
351 Currently Python semantics are used to evaluate the "truthiness" of the
352 Boolean value (so empty string, zero, etc. are counted as false, etc.)
354 (if_, (then, (else_, stack))) = stack
355 return then if if_ else else_, stack
359 @SimpleFunctionWrapper
362 Use a Boolean value to select one of two items from a sequence.
366 ------------------------
371 -----------------------
374 The sequence can contain more than two items but not fewer.
375 Currently Python semantics are used to evaluate the "truthiness" of the
376 Boolean value (so empty string, zero, etc. are counted as false, etc.)
378 (flag, (choices, stack)) = stack
379 (else_, (then, _)) = choices
380 return then if flag else else_, stack
384 @SimpleFunctionWrapper
386 '''Given a list find the maximum.'''
388 return max(iter_stack(tos)), stack
392 @SimpleFunctionWrapper
394 '''Given a list find the minimum.'''
396 return min(iter_stack(tos)), stack
400 @SimpleFunctionWrapper
403 Given a quoted sequence of numbers return the sum.
406 sum == 0 swap [+] step
410 return sum(iter_stack(tos)), stack
414 @SimpleFunctionWrapper
417 Expects an item on the stack and a quote under it and removes that item
418 from the the quote. The item is only removed once.
422 ------------------------
426 (tos, (second, stack)) = S
427 l = list(iter_stack(second))
429 return list_to_stack(l), stack
433 @SimpleFunctionWrapper
435 '''Given a list remove duplicate items.'''
437 I = list(iter_stack(tos))
438 return list_to_stack(sorted(set(I), key=I.index)), stack
442 @SimpleFunctionWrapper
444 '''Given a list return it sorted.'''
446 return list_to_stack(sorted(iter_stack(tos))), stack
450 @SimpleFunctionWrapper
452 '''Clear everything from the stack.
455 clear == stack [pop stack] loop
465 @SimpleFunctionWrapper
466 def disenstacken(stack):
468 The disenstacken operator expects a list on top of the stack and makes that
469 the stack discarding the rest of the stack.
475 @SimpleFunctionWrapper
478 Reverse the list on the top of the stack.
481 reverse == [] swap shunt
485 for term in iter_stack(tos):
491 @SimpleFunctionWrapper
494 Concatinate the two lists on the top of the stack.
497 [a b c] [d e f] concat
498 ----------------------------
502 (tos, (second, stack)) = S
503 return concat(second, tos), stack
507 @SimpleFunctionWrapper
510 Like concat but reverses the top list into the second.
513 shunt == [swons] step == reverse swap concat
515 [a b c] [d e f] shunt
516 ---------------------------
520 (tos, (second, stack)) = stack
523 second = term, second
528 @SimpleFunctionWrapper
531 Replace the two lists on the top of the stack with a list of the pairs
532 from each list. The smallest list sets the length of the result list.
534 (tos, (second, stack)) = S
537 for a, b in zip(iter_stack(tos), iter_stack(second))
539 return list_to_stack(accumulator), stack
543 @SimpleFunctionWrapper
547 return tos + 1, stack
551 @SimpleFunctionWrapper
555 return tos - 1, stack
559 @SimpleFunctionWrapper
570 a, (b, stack) = stack
576 return int(math.floor(n))
578 floor.__doc__ = math.floor.__doc__
582 @SimpleFunctionWrapper
585 divmod(x, y) -> (quotient, remainder)
587 Return the tuple (x//y, x%y). Invariant: q * y + r == x.
596 Return the square root of the number a.
597 Negative numbers return complex roots.
602 assert a < 0, repr(a)
603 r = math.sqrt(-a) * 1j
609 # if isinstance(text, str):
610 # return run(text, stack)
615 @SimpleFunctionWrapper
617 '''The identity function.'''
622 @SimpleFunctionWrapper
624 '''True if the form on TOS is void otherwise False.'''
626 return _void(form), stack
630 return any(not _void(i) for i in iter_stack(form))
641 def words(stack, expression, dictionary):
642 '''Print all the words in alphabetical order.'''
643 print(' '.join(sorted(dictionary)))
644 return stack, expression, dictionary
649 def sharing(stack, expression, dictionary):
650 '''Print redistribution information.'''
651 print("You may convey verbatim copies of the Program's source code as"
652 ' you receive it, in any medium, provided that you conspicuously'
653 ' and appropriately publish on each copy an appropriate copyright'
654 ' notice; keep intact all notices stating that this License and'
655 ' any non-permissive terms added in accord with section 7 apply'
656 ' to the code; keep intact all notices of the absence of any'
657 ' warranty; and give all recipients a copy of this License along'
659 ' You should have received a copy of the GNU General Public License'
660 ' along with Thun. If not see <http://www.gnu.org/licenses/>.')
661 return stack, expression, dictionary
666 def warranty(stack, expression, dictionary):
667 '''Print warranty information.'''
668 print('THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY'
669 ' APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE'
670 ' COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM'
671 ' "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR'
672 ' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES'
673 ' OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE'
674 ' ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS'
675 ' WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE'
676 ' COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.')
677 return stack, expression, dictionary
680 # def simple_manual(stack):
682 # Print words and help for each word.
684 # for name, f in sorted(FUNCTIONS.items()):
686 # boxline = '+%s+' % ('-' * (len(name) + 2))
689 # '| %s |' % (name,),
691 # d if d else ' ...',
701 def help_(S, expression, dictionary):
702 '''Accepts a quoted symbol on the top of the stack and prints its docs.'''
703 ((symbol, _), stack) = S
704 word = dictionary[symbol]
705 print(HELP_TEMPLATE % (symbol, getdoc(word), symbol))
706 return stack, expression, dictionary
714 # Several combinators depend on other words in their definitions,
715 # we use symbols to prevent hard-coding these, so in theory, you
716 # could change the word in the dictionary to use different semantics.
717 S_choice = Symbol('choice')
718 S_first = Symbol('first')
719 S_genrec = Symbol('genrec')
720 S_getitem = Symbol('getitem')
722 S_ifte = Symbol('ifte')
723 S_infra = Symbol('infra')
724 S_loop = Symbol('loop')
725 S_pop = Symbol('pop')
726 S_primrec = Symbol('primrec')
727 S_step = Symbol('step')
728 S_swaack = Symbol('swaack')
729 S_times = Symbol('times')
734 def i(stack, expression, dictionary):
736 The i combinator expects a quoted program on the stack and unpacks it
737 onto the pending expression for evaluation.
748 raise StackUnderflowError('Not enough values on stack.')
749 return stack, concat(quote, expression), dictionary
754 def x(stack, expression, dictionary):
760 ... [Q] x = ... [Q] dup i
761 ... [Q] x = ... [Q] [Q] i
762 ... [Q] x = ... [Q] Q
766 return stack, concat(quote, expression), dictionary
771 def b(stack, expression, dictionary):
777 ... [P] [Q] b == ... [P] i [Q] i
778 ... [P] [Q] b == ... P Q
781 q, (p, (stack)) = stack
782 return stack, concat(p, concat(q, expression)), dictionary
787 def dupdip(stack, expression, dictionary):
791 [F] dupdip == dup [F] dip
801 return stack, concat(F, (a, expression)), dictionary
806 def infra(stack, expression, dictionary):
808 Accept a quoted program and a list on the stack and run the program
809 with the list as its stack. Does not affect the rest of the stack.
812 ... [a b c] [Q] . infra
813 -----------------------------
814 c b a . Q [...] swaack
817 (quote, (aggregate, stack)) = stack
818 return aggregate, concat(quote, (stack, (S_swaack, expression))), dictionary
823 def genrec(stack, expression, dictionary):
825 General Recursion Combinator.
828 [if] [then] [rec1] [rec2] genrec
829 ---------------------------------------------------------------------
830 [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
832 From "Recursion Theory and Joy" (j05cmp.html) by Manfred von Thun:
833 "The genrec combinator takes four program parameters in addition to
834 whatever data parameters it needs. Fourth from the top is an if-part,
835 followed by a then-part. If the if-part yields true, then the then-part
836 is executed and the combinator terminates. The other two parameters are
837 the rec1-part and the rec2-part. If the if-part yields false, the
838 rec1-part is executed. Following that the four program parameters and
839 the combinator are again pushed onto the stack bundled up in a quoted
840 form. Then the rec2-part is executed, where it will find the bundled
841 form. Typically it will then execute the bundled form, either with i or
842 with app2, or some other combinator."
844 The way to design one of these is to fix your base case [then] and the
845 test [if], and then treat rec1 and rec2 as an else-part "sandwiching"
846 a quotation of the whole function.
848 For example, given a (general recursive) function 'F':
851 F == [I] [T] [R1] [R2] genrec
853 If the [I] if-part fails you must derive R1 and R2 from:
858 Just set the stack arguments in front, and figure out what R1 and R2
859 have to do to apply the quoted [F] in the proper way. In effect, the
860 genrec combinator turns into an ifte combinator with a quoted copy of
861 the original definition in the else-part:
864 F == [I] [T] [R1] [R2] genrec
865 == [I] [T] [R1 [F] R2] ifte
867 Primitive recursive functions are those where R2 == i.
870 P == [I] [T] [R] tailrec
871 == [I] [T] [R [P] i] ifte
872 == [I] [T] [R P] ifte
875 (rec2, (rec1, stack)) = stack
876 (then, (if_, _)) = stack
877 F = (if_, (then, (rec1, (rec2, (S_genrec, ())))))
878 else_ = concat(rec1, (F, rec2))
879 return (else_, stack), (S_ifte, expression), dictionary
884 def map_(S, expression, dictionary):
886 Run the quoted program on TOS on the items in the list under it, push a
887 new list with the results in place of the program and original list.
889 # (quote, (aggregate, stack)) = S
890 # results = list_to_stack([
891 # joy((term, stack), quote, dictionary)[0][0]
892 # for term in iter_stack(aggregate)
894 # return (results, stack), expression, dictionary
895 (quote, (aggregate, stack)) = S
897 return (aggregate, stack), expression, dictionary
899 for term in iter_stack(aggregate):
901 batch = (s, (quote, (S_infra, (S_first, batch))))
902 stack = (batch, ((), stack))
903 return stack, (S_infra, expression), dictionary
908 def primrec(stack, expression, dictionary):
910 From the "Overview of the language JOY":
912 > The primrec combinator expects two quoted programs in addition to a
913 data parameter. For an integer data parameter it works like this: If
914 the data parameter is zero, then the first quotation has to produce
915 the value to be returned. If the data parameter is positive then the
916 second has to combine the data parameter with the result of applying
917 the function to its predecessor.::
921 > Then primrec tests whether the top element on the stack (initially
922 the 5) is equal to zero. If it is, it pops it off and executes one of
923 the quotations, the [1] which leaves 1 on the stack as the result.
924 Otherwise it pushes a decremented copy of the top element and
925 recurses. On the way back from the recursion it uses the other
926 quotation, [*], to multiply what is now a factorial on top of the
927 stack by the second element on the stack.::
929 n [Base] [Recur] primrec
931 0 [Base] [Recur] primrec
932 ------------------------------
935 n [Base] [Recur] primrec
936 ------------------------------------------ n > 0
937 n (n-1) [Base] [Recur] primrec Recur
940 recur, (base, (n, stack)) = stack
942 expression = concat(base, expression)
944 expression = S_primrec, concat(recur, expression)
945 stack = recur, (base, (n - 1, (n, stack)))
946 return stack, expression, dictionary
949 #def cleave(S, expression, dictionary):
951 # The cleave combinator expects two quotations, and below that an item X.
952 # It first executes [P], with X on top, and saves the top result element.
953 # Then it executes [Q], again with X, and saves the top result.
954 # Finally it restores the stack to what it was below X and pushes the two
955 # results P(X) and Q(X).
957 # (Q, (P, (x, stack))) = S
958 # p = joy((x, stack), P, dictionary)[0][0]
959 # q = joy((x, stack), Q, dictionary)[0][0]
960 # return (q, (p, stack)), expression, dictionary
965 def branch(stack, expression, dictionary):
967 Use a Boolean value to select one of two quoted programs to run.
971 branch == roll< choice i
976 --------------------------
980 -------------------------
984 (then, (else_, (flag, stack))) = stack
985 return stack, concat(then if flag else else_, expression), dictionary
990 ##def ifte(stack, expression, dictionary):
992 ## If-Then-Else Combinator
995 ## ... [if] [then] [else] ifte
996 ## ---------------------------------------------------
997 ## ... [[else] [then]] [...] [if] infra select i
1002 ## ... [if] [then] [else] ifte
1003 ## -------------------------------------------------------
1004 ## ... [else] [then] [...] [if] infra first choice i
1007 ## Has the effect of grabbing a copy of the stack on which to run the
1008 ## if-part using infra.
1010 ## (else_, (then, (if_, stack))) = stack
1011 ## expression = (S_infra, (S_first, (S_choice, (S_i, expression))))
1012 ## stack = (if_, (stack, (then, (else_, stack))))
1013 ## return stack, expression, dictionary
1018 def cond(stack, expression, dictionary):
1020 This combinator works like a case statement. It expects a single quote
1021 on the stack that must contain zero or more condition quotes and a
1022 default quote. Each condition clause should contain a quoted predicate
1023 followed by the function expression to run if that predicate returns
1024 true. If no predicates return true the default function runs.
1026 It works by rewriting into a chain of nested `ifte` expressions, e.g.::
1028 [[[B0] T0] [[B1] T1] [D]] cond
1029 -----------------------------------------
1030 [B0] [T0] [[B1] [T1] [D] ifte] ifte
1033 conditions, stack = stack
1035 expression = _cond(conditions, expression)
1037 # Attempt to preload the args to first ifte.
1038 (P, (T, (E, expression))) = expression
1040 # If, for any reason, the argument to cond should happen to contain
1041 # only the default clause then this optimization will fail.
1044 stack = (E, (T, (P, stack)))
1045 return stack, expression, dictionary
1048 def _cond(conditions, expression):
1049 (clause, rest) = conditions
1050 if not rest: # clause is [D]
1053 return (P, (T, (_cond(rest, ()), (S_ifte, expression))))
1058 def dip(stack, expression, dictionary):
1060 The dip combinator expects a quoted program on the stack and below it
1061 some item, it hoists the item into the expression and runs the program
1062 on the rest of the stack.
1071 (quote, (x, stack)) = stack
1073 raise StackUnderflowError('Not enough values on stack.')
1074 expression = (x, expression)
1075 return stack, concat(quote, expression), dictionary
1080 def dipd(S, expression, dictionary):
1082 Like dip but expects two items.
1086 ---------------------
1090 (quote, (x, (y, stack))) = S
1091 expression = (y, (x, expression))
1092 return stack, concat(quote, expression), dictionary
1097 def dipdd(S, expression, dictionary):
1099 Like dip but expects three items.
1103 -----------------------
1107 (quote, (x, (y, (z, stack)))) = S
1108 expression = (z, (y, (x, expression)))
1109 return stack, concat(quote, expression), dictionary
1114 def app1(S, expression, dictionary):
1116 Given a quoted program on TOS and anything as the second stack item run
1117 the program and replace the two args with the first result of the
1122 -----------------------------------
1123 ... [x ...] [Q] . infra first
1126 (quote, (x, stack)) = S
1127 stack = (quote, ((x, stack), stack))
1128 expression = (S_infra, (S_first, expression))
1129 return stack, expression, dictionary
1134 def app2(S, expression, dictionary):
1135 '''Like app1 with two items.
1139 -----------------------------------
1140 ... [y ...] [Q] . infra first
1141 [x ...] [Q] infra first
1144 (quote, (x, (y, stack))) = S
1145 expression = (S_infra, (S_first,
1146 ((x, stack), (quote, (S_infra, (S_first,
1148 stack = (quote, ((y, stack), stack))
1149 return stack, expression, dictionary
1154 def app3(S, expression, dictionary):
1155 '''Like app1 with three items.
1158 ... z y x [Q] . app3
1159 -----------------------------------
1160 ... [z ...] [Q] . infra first
1161 [y ...] [Q] infra first
1162 [x ...] [Q] infra first
1165 (quote, (x, (y, (z, stack)))) = S
1166 expression = (S_infra, (S_first,
1167 ((y, stack), (quote, (S_infra, (S_first,
1168 ((x, stack), (quote, (S_infra, (S_first,
1169 expression))))))))))
1170 stack = (quote, ((z, stack), stack))
1171 return stack, expression, dictionary
1176 def step(S, expression, dictionary):
1178 Run a quoted program on each item in a sequence.
1182 -----------------------
1187 ------------------------
1191 ... [a b c] [Q] . step
1192 ----------------------------------------
1193 ... a . Q [b c] [Q] step
1195 The step combinator executes the quotation on each member of the list
1196 on top of the stack.
1198 (quote, (aggregate, stack)) = S
1200 return stack, expression, dictionary
1201 head, tail = aggregate
1202 stack = quote, (head, stack)
1204 expression = tail, (quote, (S_step, expression))
1205 expression = S_i, expression
1206 return stack, expression, dictionary
1211 def times(stack, expression, dictionary):
1213 times == [-- dip] cons [swap] infra [0 >] swap while pop
1217 --------------------- w/ n <= 0
1222 -----------------------
1227 ------------------------------------- w/ n > 1
1228 ... . Q (n - 1) [Q] times
1231 # times == [-- dip] cons [swap] infra [0 >] swap while pop
1232 (quote, (n, stack)) = stack
1234 return stack, expression, dictionary
1237 expression = n, (quote, (S_times, expression))
1238 expression = concat(quote, expression)
1239 return stack, expression, dictionary
1242 # The current definition above works like this:
1245 # --------------------------------------
1246 # [P] nullary [Q [P] nullary] loop
1248 # while == [pop i not] [popop] [dudipd] tailrec
1250 #def while_(S, expression, dictionary):
1251 # '''[if] [body] while'''
1252 # (body, (if_, stack)) = S
1253 # while joy(stack, if_, dictionary)[0][0]:
1254 # stack = joy(stack, body, dictionary)[0]
1255 # return stack, expression, dictionary
1260 def loop(stack, expression, dictionary):
1262 Basic loop combinator.
1266 -----------------------
1270 ------------------------
1275 quote, stack = stack
1277 raise StackUnderflowError('Not enough values on stack.')
1278 if not isinstance(quote, tuple):
1279 raise NotAListError('Loop body not a list.')
1281 (flag, stack) = stack
1283 raise StackUnderflowError('Not enough values on stack.')
1285 expression = concat(quote, (quote, (S_loop, expression)))
1286 return stack, expression, dictionary
1291 def cmp_(stack, expression, dictionary):
1293 cmp takes two values and three quoted programs on the stack and runs
1294 one of the three depending on the results of comparing the two values:
1298 ------------------------- a > b
1302 ------------------------- a = b
1306 ------------------------- a < b
1309 L, (E, (G, (b, (a, stack)))) = stack
1310 expression = concat(G if a > b else L if a < b else E, expression)
1311 return stack, expression, dictionary
1314 # FunctionWrapper(cleave),
1315 # FunctionWrapper(while_),
1320 #divmod_ = pm = __(n2, n1), __(n4, n3)
1322 BinaryBuiltinWrapper(operator.eq),
1323 BinaryBuiltinWrapper(operator.ge),
1324 BinaryBuiltinWrapper(operator.gt),
1325 BinaryBuiltinWrapper(operator.le),
1326 BinaryBuiltinWrapper(operator.lt),
1327 BinaryBuiltinWrapper(operator.ne),
1329 BinaryBuiltinWrapper(operator.xor),
1330 BinaryBuiltinWrapper(operator.lshift),
1331 BinaryBuiltinWrapper(operator.rshift),
1333 BinaryBuiltinWrapper(operator.and_),
1334 BinaryBuiltinWrapper(operator.or_),
1336 BinaryBuiltinWrapper(operator.add),
1337 BinaryBuiltinWrapper(operator.floordiv),
1338 BinaryBuiltinWrapper(operator.mod),
1339 BinaryBuiltinWrapper(operator.mul),
1340 BinaryBuiltinWrapper(operator.pow),
1341 BinaryBuiltinWrapper(operator.sub),
1342 ## BinaryBuiltinWrapper(operator.truediv),
1344 UnaryBuiltinWrapper(bool),
1345 UnaryBuiltinWrapper(operator.not_),
1347 UnaryBuiltinWrapper(abs),
1348 UnaryBuiltinWrapper(operator.neg),
1349 UnaryBuiltinWrapper(sqrt),
1351 UnaryBuiltinWrapper(floor),
1352 UnaryBuiltinWrapper(round),
1355 del F # Otherwise Sphinx autodoc will pick it up.
1358 for name, primitive in getmembers(genlib, isfunction):
1359 inscribe(SimpleFunctionWrapper(primitive))
1362 add_aliases(_dictionary, ALIASES)