<commit_before>from .ast import *
from .invoke import arepr

# TODO:
# from bytecode to AST???

__all__ = ['ast_to_source']

BINOP_STRENGTH = {'|': 1, '&': 2, '+': 4, '-': 4, '*': 5, '/': 5, '%': 6, '^': 8}
UNOP_STRENGTH = 7

def binop_strength(op):
    if op in {'==', '!=', '<', '>', '>=', '<='}:
        return 3
    return BINOP_STRENGTH[op[0]]

def ast_to_source(t):
    return ''.join(to_source_body(t, 0, 0, end='', allowsingleline=False))

def to_source_body(body, indentation, strength, do=False, end='end', allowsingleline=True):
    if do:
        yield 'do'
    if len(body) < 2 and allowsingleline:
        if body:
            yield ' '
            yield from body[0].to_source(indentation, 0)
        yield ' '
        yield end
        return
    yield '\n'
    for line in body:
        yield '    ' * indentation
        yield from line.to_source(indentation + 1, 0)
        yield '\n'
    if end:
        yield '    ' * (indentation - 1)
        yield end

@ReturnValue._method
def to_source(self, indentation, strength):
    if isinstance(self.value, Nil):
        yield 'return'
    else:
        yield 'return '
        yield from self.value.to_source(indentation, 0)

@Assign._method
def to_source(self, indentation, strength):
    yield from self.name.to_source(indentation, strength)
    yield ' '
    yield self.assign
    yield ' '
    yield from self.value.to_source(indentation, strength)

@For._method
def to_source(self, indentation, strength):
    yield 'for'
    for i, name in enumerate(self.namelist):
        if i:
            yield ','
        yield ' '
        yield from Name(name).to_source(indentation, strength)
    if self.namelist:
        yield ' in'
    yield ' '
    yield from self.iterable.to_source(indentation, strength)
    yield from to_source_body(self.body, indentation, strength)

@Do._method
def to_source(self, indentation, strength):
    yield from to_source_body(self.body, indentation, strength, do=True)

@If._method
def to_source(self, indentation, strength):
    yield 'if '
    yield from self.cond.to_source(indentation, strength)
    elsif = self.elsebody and len(self.elsebody) == 1 and isinstance(self.elsebody[0], If)
    yield from to_source_body(self.thenbody, indentation, strength, end='els' if elsif else 'else' if self.elsebody else 'end')
    if elsif:
        yield from self.elsebody[0].to_source(indentation, strength)
    elif self.elsebody:
        yield from to_source_body(self.elsebody, indentation, strength)<commit_msg>Superficial changes for the reverse AST visitor<commit_after>from .ast import *
from .invoke import arepr

__all__ = ['ast_to_source']

BINOP_STRENGTH = {'|': 1, '&': 2, '+': 4, '-': 4, '*': 5, '/': 5, '%': 6, '^': 8}
UNOP_STRENGTH = 7

def binop_strength(op):
    if op in {'==', '!=', '<', '>', '>=', '<='}:
        return 3
    return BINOP_STRENGTH[op[0]]

def ast_to_source(t):
    return ''.join(to_source_body(t, 0, 0, end='', allowsingleline=False))

def to_source_body(body, indentation, strength, end='end', allowsingleline=True):
    if len(body) < 2 and allowsingleline:
        if body:
            yield ' '
            yield from body[0].to_source(indentation, 0)
        yield ' '
        yield end
        return
    yield '\n'
    for line in body:
        yield '    ' * indentation
        yield from line.to_source(indentation + 1, 0)
        yield '\n'
    if end:
        yield '    ' * (indentation - 1)
        yield end

@ReturnValue._method
def to_source(self, indentation, strength):
    if isinstance(self.value, Nil):
        yield 'return'
    else:
        yield 'return '
        yield from self.value.to_source(indentation, 0)

@Assign._method
def to_source(self, indentation, strength):
    yield from self.name.to_source(indentation, strength)
    yield ' '
    yield self.assign
    yield ' '
    yield from self.value.to_source(indentation, strength)

@For._method
def to_source(self, indentation, strength):
    yield 'for'
    for i, name in enumerate(self.namelist):
        if i:
            yield ','
        yield ' '
        yield from Name(name).to_source(indentation, strength)
    if self.namelist:
        yield ' in'
    yield ' '
    yield from self.iterable.to_source(indentation, strength)
    yield from to_source_body(self.body, indentation, strength)

@Do._method
def to_source(self, indentation, strength):
    yield 'do'
    yield from to_source_body(self.body, indentation, strength)

@If._method
def to_source(self, indentation, strength):
    yield 'if '
    yield from self.cond.to_source(indentation, strength)
    elsif = self.elsebody and len(self.elsebody) == 1 and isinstance(self.elsebody[0], If)
    yield from to_source_body(self.thenbody, indentation, strength, end='els' if elsif else 'else' if self.elsebody else 'end')
    if elsif:
        yield from self.elsebody[0].to_source(indentation, strength)
    elif self.elsebody:
        yield from to_source_body(self.elsebody, indentation, strength)
{'fraction_non_alphanumeric': 0.1279887482419128, 'fraction_numerical': 0.009242515571629495, 'mean_word_length': 3.004827031375704, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3657150', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1623, 'n_words': 428}