<commit_before>import logging
from tools import apply_effects, group
from tools import polygon, scale
from tools import default_colors, palette
from tools import osc_in
from pineal.parser import parse

log = logging.getLogger(__name__)


# TODO: check in namespace or in a symbol table
_primitives = {'polygon': polygon}
_effects = {'scale': scale}


def make_effect(branch, ns):
    name, leaf = branch
    arg = eval(leaf, ns)
    effect = _effects[name](arg)
    return effect


def make_group(tree, ns):
    entities = [make_entity(branch, ns)
                for branch in tree]
    return group(entities)


def make_entity(tree, ns):
    name, body = tree

    effects = [make_effect(branch, ns)
               for branch in body
               if branch[0] in _effects]

    body = [(key, value)
            for (key, value) in body
            if key not in _effects]

    if name in _primitives:
        kwargs = {key: eval(value, ns)
                  for (key, value) in body}

        entity = _primitives[name](**kwargs)

    elif name == 'group':
        # TODO: named groups
        entity = make_group(body, ns)

    else:
        # TODO: layers
        raise Exception('Invalid entity')

    return apply_effects(entity, effects)


def eval_draw(tree, ns):
    def draw():
        entities = [make_entity(branch, ns)
                    for branch in tree]

        for entity in entities:
            entity()

    ns['draw'] = draw


def eval_top_level(tree, ns):
    # TODO:
    # module
    # osc-in
    # eval_definitions(tree, ns)  # layer, group

    ns.update(default_colors)

    for head, body in tree:
        if head.startswith('source '):
            name = head.split()[1]
            ns.update({
                name: osc_in(eval(body, ns))
            })

        if head.startswith('palette '):
            name = head.split()[1]
            ns.update({
                name: palette(eval(body, ns))
            })

        elif head == 'draw':
            eval_draw(body, ns)


def pineal_eval(code, ns):
    tree = parse(code)
    log.debug(tree)

    eval_top_level(tree, ns)<commit_msg>Eval in a single point<commit_after>import logging
from tools import apply_effects, group
from tools import polygon, scale
from tools import default_colors, palette
from tools import osc_in
from pineal.parser import parse

log = logging.getLogger(__name__)


# TODO: check in namespace or in a symbol table
_primitives = {'polygon': polygon}
_effects = {'scale': scale}


def eval_leaf(leaf, ns):
    "Leaf should be a python expression"
    # TODO: return a funcion
    return eval(leaf, ns)


def make_effect(branch, ns):
    name, leaf = branch
    arg = eval_leaf(leaf, ns)
    effect = _effects[name](arg)
    return effect


def make_group(tree, ns):
    entities = [make_entity(branch, ns)
                for branch in tree]
    return group(entities)


def make_entity(tree, ns):
    name, body = tree

    effects = [make_effect(branch, ns)
               for branch in body
               if branch[0] in _effects]

    body = [(key, value)
            for (key, value) in body
            if key not in _effects]

    if name in _primitives:
        kwargs = {key: eval_leaf(value, ns)
                  for (key, value) in body}

        entity = _primitives[name](**kwargs)

    elif name == 'group':
        # TODO: named groups
        entity = make_group(body, ns)

    else:
        # TODO: layers
        raise Exception('Invalid entity')

    return apply_effects(entity, effects)


def eval_draw(tree, ns):
    def draw():
        entities = [make_entity(branch, ns)
                    for branch in tree]

        for entity in entities:
            entity()

    ns['draw'] = draw


def eval_top_level(tree, ns):
    # TODO:
    # module
    # osc-in
    # eval_definitions(tree, ns)  # layer, group

    ns.update(default_colors)

    for head, body in tree:
        if head.startswith('source '):
            name = head.split()[1]
            ns.update({
                name: osc_in(eval_leaf(body, ns))
            })

        if head.startswith('palette '):
            name = head.split()[1]
            ns.update({
                name: palette(eval_leaf(body, ns))
            })

        elif head == 'draw':
            eval_draw(body, ns)


def pineal_eval(code, ns):
    tree = parse(code)
    log.debug(tree)

    eval_top_level(tree, ns)
{'fraction_non_alphanumeric': 0.10340909090909091, 'fraction_numerical': 0.0013636363636363637, 'mean_word_length': 2.125710227272727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2303921', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1300, 'n_words': 407}