# Path: joy/joy.py
# def run(text, stack, dictionary, viewer=None):
#   '''
#   Return the stack resulting from running the Joy code text on the stack.
#   '''
#   try:
#     expression = text_to_expression(text)
#   except ParseError as err:
#     print('Err:', err.message)
#     return stack, (), dictionary
#   return joy(stack, expression, dictionary, viewer)
# 
# Path: joy/library.py
# def initialize(dictionary=None):
#   if dictionary is None:
#     dictionary = {}
#   dictionary.update((F.name, F) for F in builtins)
#   dictionary.update((F.name, F) for F in combinators)
#   dictionary.update((F.name, F) for F in primitives)
#   add_aliases(dictionary)
#   DefinitionWrapper.add_definitions(definitions, dictionary)
#   return dictionary
# 
# class DefinitionWrapper(FunctionWrapper):
#   '''
#   Provide implementation of defined functions, and some helper methods.
#   '''
# 
#   def __init__(self, name, body_text, doc=None):
#     self.name = self.__name__ = name
#     self.body = text_to_expression(body_text)
#     self._body = tuple(iter_stack(self.body))
#     self.__doc__ = doc or body_text
# 
#   def __call__(self, stack, expression, dictionary):
#     expression = list_to_stack(self._body, expression)
#     return stack, expression, dictionary
# 
#   @classmethod
#   def parse_definition(class_, defi):
#     '''
#     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
# 
# Path: joy/utils/stack.py
# def stack_to_string(stack):
#   '''
#   Return a "pretty print" string for a stack.
# 
#   The items are written right-to-left:
# 
#   (top, (second, ...)) -> '... second top'
#   '''
#   f = lambda stack: reversed(list(iter_stack(stack)))
#   return _to_string(stack, f)
# 
# Path: joy/utils/pretty_print.py
# class TracePrinter(object):
# 
#   def __init__(self):
#     self.history = []
# 
#   def viewer(self, stack, expression):
#     '''Pass this method as the viewer to joy() function.'''
#     self.history.append((stack, expression))
# 
#   def __str__(self):
#     return '\n'.join(self.go())
# 
#   def go(self):
#     max_stack_length = 0
#     lines = []
#     for stack, expression in self.history:
#       stack = stack_to_string(stack)
#       expression = expression_to_string(expression)
#       n = len(stack)
#       if n > max_stack_length:
#         max_stack_length = n
#       lines.append((n, '%s . %s' % (stack, expression)))
#     return [  # Prefix spaces to line up '.'s.
#       (' ' * (max_stack_length - length) + line)
#       for length, line in lines
#       ]
# 
#   def print_(self):
#     try:
#       print(self)
#     except:
#       print_exc()
#       print('Exception while printing viewer.')


from traceback import print_exc, format_exc
from joy.joy import run
from joy.library import initialize, DefinitionWrapper
from joy.utils.stack import stack_to_string
from joy.utils.pretty_print import TracePrinter
#    Copyright © 2017 Simon Forman
#
#    This file is part of Joypy
#
#    Joypy is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Joypy is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Joypy.  If not see <http://www.gnu.org/licenses/>.
#


D = initialize()
S = ()


def J(text, stack=S, dictionary=D):
    print stack_to_string(run(text, stack, dictionary)[0])


def V(text, stack=S, dictionary=D):
    tp = TracePrinter()
    try:

# Path: joy/joy.py
# def run(text, stack, dictionary, viewer=None):
#   '''
#   Return the stack resulting from running the Joy code text on the stack.
#   '''
#   try:
#     expression = text_to_expression(text)
#   except ParseError as err:
#     print('Err:', err.message)
#     return stack, (), dictionary
#   return joy(stack, expression, dictionary, viewer)
# 
# Path: joy/library.py
# def initialize(dictionary=None):
#   if dictionary is None:
#     dictionary = {}
#   dictionary.update((F.name, F) for F in builtins)
#   dictionary.update((F.name, F) for F in combinators)
#   dictionary.update((F.name, F) for F in primitives)
#   add_aliases(dictionary)
#   DefinitionWrapper.add_definitions(definitions, dictionary)
#   return dictionary
# 
# class DefinitionWrapper(FunctionWrapper):
#   '''
#   Provide implementation of defined functions, and some helper methods.
#   '''
# 
#   def __init__(self, name, body_text, doc=None):
#     self.name = self.__name__ = name
#     self.body = text_to_expression(body_text)
#     self._body = tuple(iter_stack(self.body))
#     self.__doc__ = doc or body_text
# 
#   def __call__(self, stack, expression, dictionary):
#     expression = list_to_stack(self._body, expression)
#     return stack, expression, dictionary
# 
#   @classmethod
#   def parse_definition(class_, defi):
#     '''
#     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
# 
# Path: joy/utils/stack.py
# def stack_to_string(stack):
#   '''
#   Return a "pretty print" string for a stack.
# 
#   The items are written right-to-left:
# 
#   (top, (second, ...)) -> '... second top'
#   '''
#   f = lambda stack: reversed(list(iter_stack(stack)))
#   return _to_string(stack, f)
# 
# Path: joy/utils/pretty_print.py
# class TracePrinter(object):
# 
#   def __init__(self):
#     self.history = []
# 
#   def viewer(self, stack, expression):
#     '''Pass this method as the viewer to joy() function.'''
#     self.history.append((stack, expression))
# 
#   def __str__(self):
#     return '\n'.join(self.go())
# 
#   def go(self):
#     max_stack_length = 0
#     lines = []
#     for stack, expression in self.history:
#       stack = stack_to_string(stack)
#       expression = expression_to_string(expression)
#       n = len(stack)
#       if n > max_stack_length:
#         max_stack_length = n
#       lines.append((n, '%s . %s' % (stack, expression)))
#     return [  # Prefix spaces to line up '.'s.
#       (' ' * (max_stack_length - length) + line)
#       for length, line in lines
#       ]
# 
#   def print_(self):
#     try:
#       print(self)
#     except:
#       print_exc()
#       print('Exception while printing viewer.')


# Path: docs/notebook_preamble.py
from traceback import print_exc, format_exc
from joy.joy import run
from joy.library import initialize, DefinitionWrapper
from joy.utils.stack import stack_to_string
from joy.utils.pretty_print import TracePrinter

#    Copyright © 2017 Simon Forman
#
#    This file is part of Joypy
#
#    Joypy is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Joypy is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Joypy.  If not see <http://www.gnu.org/licenses/>.
#


D = initialize()
S = ()


def J(text, stack=S, dictionary=D):
    print stack_to_string(run(text, stack, dictionary)[0])


def V(text, stack=S, dictionary=D):
    tp = TracePrinter()
    try:

        run(text, stack, dictionary, tp.viewer)