18 KiB
18 KiB
In [1]:
from joy.utils.stack import iter_stack, list_to_stackIn [2]:
list_to_stack([1, 2, 3])Out [2]:
(1, (2, (3, ())))
In [3]:
list(iter_stack((1, (2, (3, ())))))Out [3]:
[1, 2, 3]
In [4]:
stack = ()
for n in (1, 2, 3):
stack = n, stack
print(stack)
print(list(iter_stack(stack)))(3, (2, (1, ()))) [3, 2, 1]
In [5]:
from joy.parser import text_to_expressionIn [6]:
text_to_expression('1 2 3 4 5')Out [6]:
(1, (2, (3, (4, (5, ())))))
In [7]:
text_to_expression('[1 2 3] 4 5')Out [7]:
((1, (2, (3, ()))), (4, (5, ())))
In [8]:
text_to_expression('1 23 ["four" [-5.0] cons] 8888')Out [8]:
(1, (23, (("four", ((-5.0, ()), (cons, ()))), (8888, ()))))In [9]:
text_to_expression('[][][][][]')Out [9]:
((), ((), ((), ((), ((), ())))))
In [10]:
text_to_expression('[[[[[]]]]]')Out [10]:
((((((), ()), ()), ()), ()), ())
In [11]:
import joy.library
print(' '.join(sorted(joy.library.initialize())))!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken div divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd gcd2 ge genrec getitem gt help i id ifte ii infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons tailrec take ternary third times truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
In [12]:
import inspectIn [13]:
print(inspect.getdoc(joy.library.dip))The dip combinator expects a quoted program on the stack and below it
some item, it hoists the item into the expression and runs the program
on the rest of the stack.
::
... x [Q] dip
-------------------
... Q x
In [14]:
def dip(stack, expression, dictionary):
try:
(quote, (x, stack)) = stack
except ValueError:
raise StackUnderflowError('Not enough values on stack.')
return stack, concat(quote, (x, expression)), dictionaryIn [15]:
print(joy.library.definitions)? == dup truthy *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons *fraction0 == concat [[swap] dip * [*] dip] infra anamorphism == [pop []] swap [dip swons] genrec average == [sum 1.0 *] [size] cleave / binary == nullary [popop] dip cleave == fork [popd] dip codireco == cons dip rest cons dinfrirst == dip infra first unstack == ? [uncons ?] loop pop down_to_zero == [0 >] [dup --] while dupdipd == dup dipd enstacken == stack [clear] dip flatten == [] swap [concat] step fork == [i] app2 gcd == 1 [tuck modulus dup 0 >] loop pop ifte == [nullary not] dipd branch ii == [dip] dupdip i least_fraction == dup [gcd] infra [div] concat map make_generator == [codireco] ccons nullary == [stack] dinfrirst of == swap at pam == [i] map tailrec == [i] genrec product == 1 swap [*] step quoted == [unit] dip range == [0 <=] [1 - dup] anamorphism range_to_zero == unit [down_to_zero] infra run == [] swap infra size == 0 swap [pop ++] step sqr == dup mul step_zero == 0 roll> step swoncat == swap concat tailrec == [i] genrec ternary == unary [popop] dip unary == nullary popd unquoted == [i] dip while == swap [nullary] cons dup dipd concat loop