# 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
    Given some text describing a Joy function definition parse it and
    return a DefinitionWrapper.
    '''
    name, proper, body_text = (n.strip() for n in defi.partition('=='))
    if not proper:
      raise ValueError('Definition %r failed' % (defi,))
    return class_(name, body_text)

  @classmethod
  def add_definitions(class_, defs, dictionary):
    for definition in _text_to_defs(defs):
      class_.add_def(definition, dictionary)

  @classmethod
  def add_def(class_, definition, dictionary):
    F = class_.parse_definition(definition)
    dictionary[F.name] = F


def _text_to_defs(text):
  return filter(None, (line.strip() for line in text.splitlines()))


#
# Functions
#


def parse((text, stack)):
  '''Parse the string on the stack to a Joy expression.'''

# 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

    Given some text describing a Joy function definition parse it and
    return a DefinitionWrapper.
    '''
    name, proper, body_text = (n.strip() for n in defi.partition('=='))
    if not proper:
      raise ValueError('Definition %r failed' % (defi,))
    return class_(name, body_text)

  @classmethod
  def add_definitions(class_, defs, dictionary):
    for definition in _text_to_defs(defs):
      class_.add_def(definition, dictionary)

  @classmethod
  def add_def(class_, definition, dictionary):
    F = class_.parse_definition(definition)
    dictionary[F.name] = F


def _text_to_defs(text):
  return filter(None, (line.strip() for line in text.splitlines()))


#
# Functions
#


def parse((text, stack)):
  '''Parse the string on the stack to a Joy expression.'''

  expression = text_to_expression(text)