#!/usr/bin/env python
# vim:fileencoding=utf8

# Copyright (c) 2014 Florian Brucker
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
A Snowball to Python compiler.
"""

import sbl2py.ast
import sbl2py.grammar


__version__ = '0.1.0-2'


def translate_file(infile, *args, **kwargs):
    """
    Translate a Snowball file to Python.

    ``infile`` is an open readable file containing the Snowball source
    code. The return value is a string containing the translated Python
    code.

    See ``translate_string`` for additional arguments.
    """
    return translate_string(infile.read(), *args, **kwargs)

def translate_string(code, header=None, debug=False):
    """
    Translate a Snowball code string to Python.

    ``header`` is an optional string that is inserted at the beginning
    of the generated code. It is automatically prefixed with comment
    markers.

    If ``debug`` is ``True`` then the external Snowball routines return
    both the original ``_String`` object and the ``_Program`` instance
    that created it. This is useful for checking that variables have
    been computed correctly.
    """
    node = sbl2py.grammar.parse_string(code)
    env = sbl2py.ast.Environment(header=header, debug=debug)
    return node.generate_code(env)

