-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from dflook/constants
Add simple constant folding
- Loading branch information
Showing
20 changed files
with
454 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM fedora:38 AS fuzz | ||
|
||
RUN dnf install -y \ | ||
python3 \ | ||
python3-pip \ | ||
&& dnf clean all && rm -rf /var/cache/dnf/* | ||
|
||
RUN pip install hypothesis[cli] hypofuzz | ||
|
||
COPY fuzz.sh /fuzz.sh | ||
|
||
WORKDIR /tmp/work | ||
ENTRYPOINT ["/fuzz.sh"] | ||
|
||
EXPOSE 9999/tcp | ||
VOLUME /tmp/work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
pip install . | ||
|
||
exec hypothesis fuzz hypo_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SECONDS_IN_A_DAY = 60 * 60 * 24 | ||
SECONDS_IN_A_WEEK = SECONDS_IN_A_DAY * 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Constant Folding | ||
================ | ||
|
||
This transform evaluates constant expressions with literal operands when minifying and replaces the expression with the resulting value, if the value is shorter than the expression. | ||
|
||
There are some limitations, notably the division and power operators are not evaluated. | ||
|
||
This will be most effective with numeric literals. | ||
|
||
This transform is always safe and enabled by default. Disable by passing the ``constant_folding=False`` argument to the :func:`python_minifier.minify` function, | ||
or passing ``--no-constant-folding`` to the pyminify command. | ||
|
||
Example | ||
------- | ||
|
||
Input | ||
~~~~~ | ||
|
||
.. literalinclude:: constant_folding.py | ||
|
||
Output | ||
~~~~~~ | ||
|
||
.. literalinclude:: constant_folding.min.py | ||
:language: python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from hypothesis import assume | ||
from hypothesis.strategies import integers, lists, binary, sampled_from, recursive, dictionaries, booleans, SearchStrategy, text, composite, one_of, floats, complex_numbers, characters, none | ||
import ast | ||
|
||
from expressions import NameConstant, Num | ||
|
||
leaves = NameConstant() | Num() | ||
|
||
@composite | ||
def BinOp(draw, expression) -> ast.BinOp: | ||
op = draw(sampled_from([ | ||
ast.Add(), | ||
ast.Sub(), | ||
ast.Mult(), | ||
ast.Div(), | ||
ast.FloorDiv(), | ||
ast.Mod(), | ||
ast.Pow(), | ||
ast.LShift(), | ||
ast.RShift(), | ||
ast.BitOr(), | ||
ast.BitXor(), | ||
ast.BitAnd(), | ||
ast.MatMult() | ||
])) | ||
|
||
le = draw(lists(expression, min_size=2, max_size=2)) | ||
|
||
return ast.BinOp(le[0], op, le[1]) | ||
|
||
|
||
def expression() -> SearchStrategy: | ||
return recursive( | ||
leaves, | ||
lambda expression: | ||
BinOp(expression), | ||
max_leaves=150 | ||
) | ||
|
||
@composite | ||
def FoldableExpression(draw) -> ast.Expression: | ||
""" An eval expression """ | ||
e = draw(expression()) | ||
return ast.Expression(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import ast | ||
|
||
from datetime import timedelta | ||
from hypothesis import given, settings, Verbosity, note, HealthCheck | ||
|
||
from folding import FoldableExpression | ||
from patterns import Pattern | ||
from python_minifier import ModulePrinter | ||
from python_minifier.ast_compare import compare_ast | ||
from python_minifier.ast_printer import print_ast | ||
from python_minifier.expression_printer import ExpressionPrinter | ||
from expressions import Expression | ||
from module import Module | ||
from python_minifier.rename.mapper import add_parent | ||
from python_minifier.transforms.constant_folding import FoldConstants | ||
|
||
|
||
@given(node=Expression()) | ||
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose | ||
def test_expression(node): | ||
assert isinstance(node, ast.AST) | ||
|
||
note(ast.dump(node)) | ||
printer = ExpressionPrinter() | ||
code = printer(node) | ||
note(code) | ||
compare_ast(node, ast.parse(code, 'test_expression', 'eval')) | ||
|
||
|
||
@given(node=Module()) | ||
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose | ||
def test_module(node): | ||
assert isinstance(node, ast.Module) | ||
|
||
note(ast.dump(node)) | ||
printer = ModulePrinter() | ||
code = printer(node) | ||
note(code) | ||
compare_ast(node, ast.parse(code, 'test_module')) | ||
|
||
|
||
@given(node=Pattern()) | ||
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=2), max_examples=100, verbosity=Verbosity.verbose) | ||
def test_pattern(node): | ||
|
||
module = ast.Module( | ||
body=[ast.Match(subject=ast.Constant(value=None), | ||
cases=[ | ||
ast.match_case( | ||
pattern=node, | ||
guard=None, | ||
body=[ast.Pass()] | ||
) | ||
])], | ||
type_ignores=[] | ||
) | ||
|
||
printer = ModulePrinter() | ||
code = printer(module) | ||
note(code) | ||
compare_ast(module, ast.parse(code, 'test_pattern')) | ||
|
||
@given(node=FoldableExpression()) | ||
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100000, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose | ||
def test_folding(node): | ||
assert isinstance(node, ast.AST) | ||
note(print_ast(node)) | ||
|
||
add_parent(node) | ||
|
||
constant_folder = FoldConstants() | ||
|
||
# The constant folder asserts the value is correct | ||
constant_folder(node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ sphinxcontrib-programoutput | |
sphinx_rtd_theme | ||
pyyaml | ||
sh | ||
hypothesis | ||
hypothesis | ||
hypofuzz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.