# Path: joy/parser.py
# def text_to_expression(text):
#   '''
#   Convert a text to a Joy expression.
#   '''
#   return _parse(_tokenize(text))
# 
# class Symbol(str):
#   __repr__ = str.__str__
# 
# Path: joy/utils/stack.py
# def list_to_stack(el, stack=()):
#   '''Convert a list (or other sequence) to a stack.
# 
#   [1, 2, 3] -> (1, (2, (3, ())))
# 
#   '''
#   for item in reversed(el):
#     stack = item, stack
#   return stack
# 
# def iter_stack(stack):
#   '''Iterate through the items on the stack.'''
#   while stack:
#     item, stack = stack
#     yield item
# 
# def pick(s, n):
#   '''
#   Find the nth item on the stack. (Pick with zero is the same as "dup".)
#   '''
#   if n < 0:
#     raise ValueError
#   while True:
#     try:
#       item, s = s
#     except ValueError:
#       raise IndexError
#     n -= 1
#     if n < 0:
#       break
#   return item
# 
# def pushback(quote, expression):
#   '''Concatinate quote onto expression.
# 
#   In joy [1 2] [3 4] would become [1 2 3 4].
#   '''
#   return list_to_stack(list(iter_stack(quote)), expression)


from inspect import getdoc
from .parser import text_to_expression, Symbol
from .utils.stack import list_to_stack, iter_stack, pick, pushback
import operator, math
#     print('\n'.join((
#       boxline,
#       '| %s |' % (name,),
#       boxline,
#       d if d else '   ...',
#       '',
#       '--' * 40,
#       '',
#       )))
#   return stack


def help_(S, expression, dictionary):
  '''Accepts a quoted symbol on the top of the stack and prints its docs.'''
  ((symbol, _), stack) = S
  word = dictionary[symbol]
  print(getdoc(word))
  return stack, expression, dictionary


#
# § Combinators
#


# Several combinators depend on other words in their definitions,
# we use symbols to prevent hard-coding these, so in theory, you
# could change the word in the dictionary to use different semantics.
S_choice = Symbol('choice')
S_first = Symbol('first')

# Path: joy/parser.py
# def text_to_expression(text):
#   '''
#   Convert a text to a Joy expression.
#   '''
#   return _parse(_tokenize(text))
# 
# class Symbol(str):
#   __repr__ = str.__str__
# 
# Path: joy/utils/stack.py
# def list_to_stack(el, stack=()):
#   '''Convert a list (or other sequence) to a stack.
# 
#   [1, 2, 3] -> (1, (2, (3, ())))
# 
#   '''
#   for item in reversed(el):
#     stack = item, stack
#   return stack
# 
# def iter_stack(stack):
#   '''Iterate through the items on the stack.'''
#   while stack:
#     item, stack = stack
#     yield item
# 
# def pick(s, n):
#   '''
#   Find the nth item on the stack. (Pick with zero is the same as "dup".)
#   '''
#   if n < 0:
#     raise ValueError
#   while True:
#     try:
#       item, s = s
#     except ValueError:
#       raise IndexError
#     n -= 1
#     if n < 0:
#       break
#   return item
# 
# def pushback(quote, expression):
#   '''Concatinate quote onto expression.
# 
#   In joy [1 2] [3 4] would become [1 2 3 4].
#   '''
#   return list_to_stack(list(iter_stack(quote)), expression)


# Path: joy/library.py
from inspect import getdoc
from .parser import text_to_expression, Symbol
from .utils.stack import list_to_stack, iter_stack, pick, pushback
import operator, math

#     print('\n'.join((
#       boxline,
#       '| %s |' % (name,),
#       boxline,
#       d if d else '   ...',
#       '',
#       '--' * 40,
#       '',
#       )))
#   return stack


def help_(S, expression, dictionary):
  '''Accepts a quoted symbol on the top of the stack and prints its docs.'''
  ((symbol, _), stack) = S
  word = dictionary[symbol]
  print(getdoc(word))
  return stack, expression, dictionary


#
# § Combinators
#


# Several combinators depend on other words in their definitions,
# we use symbols to prevent hard-coding these, so in theory, you
# could change the word in the dictionary to use different semantics.
S_choice = Symbol('choice')
S_first = Symbol('first')

S_getitem = Symbol('getitem')