import types


def compile_expression(expr: str) -> types.CodeType:
    """Compiles strings to code objects.
    Strings with one or more newlines are assumed to contain whole
    statements already.
    Strings without newlines are treated as simple expressions and
    "result = " is prepended to them before compilation."""

    if "\n" not in expr:
        expr = "result = {}".format(expr)
    return compile(expr, "expr", "exec")