Skip to content
This repository
Newer
Older
100644 69 lines (56 sloc) 2.302 kb
9d99e47c »
2008-04-08 added optimizer
1 # -*- coding: utf-8 -*-
2 """
3 jinja2.optimizer
4 ~~~~~~~~~~~~~~~~
5
07a21ba6 »
2008-04-23 removed loop unrolling. for such optimizations we would need a separa…
6 The jinja optimizer is currently trying to constant fold a few expressions
7 and modify the AST in place so that it should be easier to evaluate it.
9d99e47c »
2008-04-08 added optimizer
8
07a21ba6 »
2008-04-23 removed loop unrolling. for such optimizations we would need a separa…
9 Because the AST does not contain all the scoping information and the
10 compiler has to find that out, we cannot do all the optimizations we
11 want. For example loop unrolling doesn't work because unrolled loops would
12 have a different scoping.
9d99e47c »
2008-04-08 added optimizer
13
07a21ba6 »
2008-04-23 removed loop unrolling. for such optimizations we would need a separa…
14 The solution would be a second syntax tree that has the scoping rules stored.
9d99e47c »
2008-04-08 added optimizer
15
55494e4b »
2010-01-22 It's a new year
16 :copyright: (c) 2010 by the Jinja Team.
d7764372 »
2008-07-15 fixed a few broken license declarations. should have been BSD not GPL
17 :license: BSD.
9d99e47c »
2008-04-08 added optimizer
18 """
19 from jinja2 import nodes
981cbf6e »
2008-05-13 removed unused imports
20 from jinja2.visitor import NodeTransformer
9d99e47c »
2008-04-08 added optimizer
21
22
981cbf6e »
2008-05-13 removed unused imports
23 def optimize(node, environment):
bcb7c533 »
2008-04-11 work on tha runtime
24 """The context hint can be used to perform an static optimization
25 based on the context given."""
26 optimizer = Optimizer(environment)
981cbf6e »
2008-05-13 removed unused imports
27 return optimizer.visit(node)
81b88170 »
2008-04-09 improved static optimizer
28
29
b40b8803 »
2008-04-08 used the new nodetransformer to make mitsuhiko happy
30 class Optimizer(NodeTransformer):
9d99e47c »
2008-04-08 added optimizer
31
81b88170 »
2008-04-09 improved static optimizer
32 def __init__(self, environment):
9d99e47c »
2008-04-08 added optimizer
33 self.environment = environment
34
981cbf6e »
2008-05-13 removed unused imports
35 def visit_If(self, node):
36 """Eliminate dead code."""
6df604ec »
2008-05-23 more unittests and updated documentation for extensions. Fixed bug in…
37 # do not optimize ifs that have a block inside so that it doesn't
38 # break super().
39 if node.find(nodes.Block) is not None:
40 return self.generic_visit(node)
81b88170 »
2008-04-09 improved static optimizer
41 try:
981cbf6e »
2008-05-13 removed unused imports
42 val = self.visit(node.test).as_const()
81b88170 »
2008-04-09 improved static optimizer
43 except nodes.Impossible:
981cbf6e »
2008-05-13 removed unused imports
44 return self.generic_visit(node)
81b88170 »
2008-04-09 improved static optimizer
45 if val:
c9705c24 »
2008-04-27 all unittests pass, the special and dependency lookups have their own…
46 body = node.body
47 else:
48 body = node.else_
49 result = []
50 for node in body:
981cbf6e »
2008-05-13 removed unused imports
51 result.extend(self.visit_list(node))
c9705c24 »
2008-04-27 all unittests pass, the special and dependency lookups have their own…
52 return result
81b88170 »
2008-04-09 improved static optimizer
53
981cbf6e »
2008-05-13 removed unused imports
54 def fold(self, node):
180a1bd1 »
2008-04-09 tuple unpacking at compile time is handled properly now
55 """Do constant folding."""
981cbf6e »
2008-05-13 removed unused imports
56 node = self.generic_visit(node)
180a1bd1 »
2008-04-09 tuple unpacking at compile time is handled properly now
57 try:
4dfc975c »
2008-04-09 simplified undefined behavior for better compile time processing
58 return nodes.Const.from_untrusted(node.as_const(),
d55ab53c »
2008-04-09 nodes have access to environment now
59 lineno=node.lineno,
60 environment=self.environment)
180a1bd1 »
2008-04-09 tuple unpacking at compile time is handled properly now
61 except nodes.Impossible:
62 return node
fed44b5f »
2008-04-13 added globals
63
180a1bd1 »
2008-04-09 tuple unpacking at compile time is handled properly now
64 visit_Add = visit_Sub = visit_Mul = visit_Div = visit_FloorDiv = \
65 visit_Pow = visit_Mod = visit_And = visit_Or = visit_Pos = visit_Neg = \
6dc6f291 »
2008-06-12 Improved attribute and item lookup by allowing template designers to …
66 visit_Not = visit_Compare = visit_Getitem = visit_Getattr = visit_Call = \
fed44b5f »
2008-04-13 added globals
67 visit_Filter = visit_Test = visit_CondExpr = fold
4dfc975c »
2008-04-09 simplified undefined behavior for better compile time processing
68 del fold
Something went wrong with that request. Please try again.