-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from textwrap import indent | ||
|
||
# Calls function for the descendants of node, then for node | ||
def apply_node_tree(node, func): | ||
if hasattr(node, "extra_children"): | ||
for child in reversed(node.extra_children): | ||
apply_node_tree(child, func) | ||
for child in reversed(node.children): | ||
apply_node_tree(child, func) | ||
func(node) | ||
|
||
def annotate_code(ctxt, source, root, fail=True): | ||
def comment_before_function(node, source=source): | ||
if node.type==NodeType.function: | ||
tokens, errors = tokenize(PicoSource("temp", source.text[node.idx:node.endidx]), ctxt) | ||
if fail and errors: | ||
raise Exception("\n".join(map(str, errors))) | ||
token_count = count_tokens(tokens) | ||
char_count = len(source.text[node.idx:node.endidx]) | ||
print("SOURCE:") | ||
print(source.text[node.idx:node.endidx]) | ||
rename_tokens(ctxt, node, True) | ||
minified_source, minified_tokens = minify_code(source.text[node.idx:node.endidx], tokens, node, minify=True) | ||
# always unchanged? | ||
# min_token_count = count_tokens(minified_tokens) | ||
min_char_count = len(minified_source) | ||
source.text = f'{source.text[:node.idx]}-- T:{token_count} C:{char_count} minC:{min_char_count}\n-- minified source:\n{indent(minified_source,"-- ")}\n{source.text[node.idx:]}' | ||
apply_node_tree(root, comment_before_function) | ||
|
||
from pico_tokenize import tokenize, count_tokens | ||
from pico_parse import NodeType | ||
from pico_process import PicoSource | ||
from pico_minify import minify_code | ||
from pico_rename import rename_tokens |
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