import ast

from .common import _convert_to_expression
from .repr import ast_repr

def _exec(node, ns):
    """
    A kind of catch all exec/eval. It will try to do an eval if possible.

    Fall back to exec
    """
    node = ast.fix_missing_locations(node)

    mode = 'exec'
    if not isinstance(node, ast.Module):
        module = ast.Module()
        module.body = [node]

    # try expression eval
    expr = _convert_to_expression(node)
    if expr:
        module = expr
        mode = 'eval'

    print('eval', ast_repr(node), mode)
    code = compile(module, '<dale>', mode)
    res = eval(code, ns)
    return res

def _eval(node, ns):
    """
    Will eval an ast Node within a namespace.
    """
    expr = _convert_to_expression(node)
    if expr is None:
        raise Exception("{0} cannot be evaled".format(repr(node)))
    return _exec(node, ns)

@@ -3,16 +3,31 @@
 port ast
+%0Aimport logging
 %0A%0Afrom .
@@ -221,24 +221,368 @@
 xec%0A    %22%22%22%0A
+    code = _compile(node)%0A    res = eval(code, ns)%0A    return res%0A%0Adef _eval(node, ns):%0A    %22%22%22%0A    Will eval an ast Node within a namespace.%0A    %22%22%22%0A    expr = _convert_to_expression(node)%0A    if expr is None:%0A        raise Exception(%22%7B0%7D cannot be evaled%22.format(repr(node)))%0A    return _exec(node, ns)%0A%0Adef _compile(node, force_eval=False):%0A
     node = a
@@ -860,48 +860,8 @@
 l'%0A%0A
-    print('eval', ast_repr(node), mode)%0A
     
@@ -909,277 +909,14 @@
   re
-s = eval(code, ns)%0A    return res%0A%0Adef _eval(node, ns):%0A    %22%22%22%0A    Will eval an ast Node within a namespace.%0A    %22%22%22%0A    expr = _convert_to_expression(node)%0A    if expr is None:%0A        raise Exception(%22%7B0%7D cannot be evaled%22.format(repr(node)))%0A    return _exec(node, ns)
+turn code
 %0A

split up eval into compile as well