19 KiB
19 KiB
In [1]:
import inspect
import joy.utils.stack
print inspect.getdoc(joy.utils.stack)In [2]:
joy.utils.stack.list_to_stack([1, 2, 3])In [3]:
list(joy.utils.stack.iter_stack((1, (2, (3, ())))))In [4]:
stack = ()
for n in [1, 2, 3]:
stack = n, stack
print stack
print list(joy.utils.stack.iter_stack(stack))In [5]:
import joy.joy
print inspect.getsource(joy.joy.joy)In [6]:
import joy.parser
print inspect.getdoc(joy.parser)This module exports a single function for converting text to a joy
expression as well as a single Symbol class and a single Exception type.
The Symbol string class is used by the interpreter to recognize literals
by the fact that they are not Symbol objects.
A crude grammar::
joy = term*
term = int | float | string | '[' joy ']' | symbol
A Joy expression is a sequence of zero or more terms. A term is a
literal value (integer, float, string, or Joy expression) or a function
symbol. Function symbols are unquoted strings and cannot contain square
brackets. Terms must be separated by blanks, which can be omitted
around square brackets.
In [7]:
print inspect.getsource(joy.parser._parse)def _parse(tokens):
'''
Return a stack/list expression of the tokens.
'''
frame = []
stack = []
for tok in tokens:
if tok == '[':
stack.append(frame)
frame = []
stack[-1].append(frame)
elif tok == ']':
try:
frame = stack.pop()
except IndexError:
raise ParseError('Extra closing bracket.')
frame[-1] = list_to_stack(frame[-1])
else:
frame.append(tok)
if stack:
raise ParseError('Unclosed bracket.')
return list_to_stack(frame)
In [8]:
joy.parser.text_to_expression('1 2 3 4 5') # A simple sequence.Out [8]:
(1, (2, (3, (4, (5, ())))))
In [9]:
joy.parser.text_to_expression('[1 2 3] 4 5') # Three items, the first is a list with three itemsOut [9]:
((1, (2, (3, ()))), (4, (5, ())))
In [10]:
joy.parser.text_to_expression('1 23 ["four" [-5.0] cons] 8888') # A mixed bag. cons is
# a Symbol, no lookup at
# parse-time. Haiku docs.Out [10]:
(1, (23, (('four', ((-5.0, ()), (cons, ()))), (8888, ()))))In [11]:
joy.parser.text_to_expression('[][][][][]') # Five empty lists.Out [11]:
((), ((), ((), ((), ((), ())))))
In [12]:
joy.parser.text_to_expression('[[[[[]]]]]') # Five nested lists.Out [12]:
((((((), ()), ()), ()), ()), ())
In [13]:
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 divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infer 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 take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
In [14]:
print inspect.getsource(joy.library.dip)@inscribe @combinator_effect(_COMB_NUMS(), a1, s1) @FunctionWrapper def dip(stack, expression, dictionary): ''' 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 ''' (quote, (x, stack)) = stack expression = (x, expression) return stack, concat(quote, expression), dictionary
In [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 primrec == [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 ternary == unary [popop] dip unary == nullary popd unquoted == [i] dip while == swap [nullary] cons dup dipd concat loop
In [ ]: