Permalink
Browse files

Standardized teminology: using the term `tree` instead of `node` to r…

…efer to ASTs throughout the project
  • Loading branch information...
1 parent 2a41ac9 commit cace9fd699f4bb1a401270d3d7d0c6bdc3e2bd02 @lihaoyi committed May 8, 2013
View
@@ -2,9 +2,9 @@
macros = Macros()
-def u(node):
+def u(tree):
"""Stub to make the IDE happy"""
-def name(node):
+def name(tree):
"""Stub to make the IDE happy"""
@@ -19,32 +19,32 @@ def __repr__(self):
@Walker
-def _unquote_search(node):
- if isinstance(node, BinOp) and type(node.left) is Name and type(node.op) is Mod:
- if 'u' == node.left.id:
+def _unquote_search(tree):
+ if isinstance(tree, BinOp) and type(tree.left) is Name and type(tree.op) is Mod:
+ if 'u' == tree.left.id:
x = parse_expr("ast_repr(x)")
- x.args[0] = node.right
+ x.args[0] = tree.right
return Literal(x)
- if 'name' == node.left.id:
+ if 'name' == tree.left.id:
x = parse_expr("Name(id = x)")
- x.keywords[0].value = node.right
+ x.keywords[0].value = tree.right
return Literal(x)
- if 'ast' == node.left.id:
- return Literal(node.right)
- if 'ast_list' == node.left.id:
+ if 'ast' == tree.left.id:
+ return Literal(tree.right)
+ if 'ast_list' == tree.left.id:
x = parse_expr("List(elts = x)")
- x.keywords[0].value = node.right
+ x.keywords[0].value = tree.right
return Literal(x)
- return node
+ return tree
@macros.expr
-def q(node):
- node = _unquote_search.recurse(node)
- return parse_expr(real_repr(node))
+def q(tree):
+ tree = _unquote_search.recurse(tree)
+ return parse_expr(real_repr(tree))
@macros.block
-def q(node):
- body = _unquote_search.recurse(node.body)
- return parse_stmt(node.optional_vars.id + " = " + real_repr(body))
+def q(tree):
+ body = _unquote_search.recurse(tree.body)
+ return parse_stmt(tree.optional_vars.id + " = " + real_repr(body))
View
@@ -28,35 +28,35 @@ def __init__(self, func, autorecurse=True):
self.func = func
self.autorecurse = autorecurse
- def walk_children(self, node):
- for field, old_value in list(iter_fields(node)):
- old_value = getattr(node, field, None)
+ def walk_children(self, tree):
+ for field, old_value in list(iter_fields(tree)):
+ old_value = getattr(tree, field, None)
new_value = self.recurse(old_value)
- setattr(node, field, new_value)
+ setattr(tree, field, new_value)
- def recurse(self, node):
- if isinstance(node, list):
+ def recurse(self, tree):
+ if isinstance(tree, list):
return flatten([
self.recurse(x)
- for x in node
+ for x in tree
])
- elif isinstance(node, comprehension):
- self.walk_children(node)
- return node
- elif isinstance(node, AST):
- node = self.func(node)
+ elif isinstance(tree, comprehension):
+ self.walk_children(tree)
+ return tree
+ elif isinstance(tree, AST):
+ tree = self.func(tree)
if self.autorecurse:
- if type(node) is list:
- return self.recurse(node)
+ if type(tree) is list:
+ return self.recurse(tree)
else:
- self.walk_children(node)
- return node
+ self.walk_children(tree)
+ return tree
else:
- return node
+ return tree
else:
- return node
+ return tree
class _MacroLoader(object):
@@ -88,9 +88,9 @@ def load_module(self, fullname):
return mod
-def _detect_macros(node):
+def _detect_macros(tree):
required_pkgs = []
- for stmt in node.body:
+ for stmt in tree.body:
if (isinstance(stmt, ImportFrom)
and stmt.names[0].name == 'macros'
and stmt.names[0].asname is None):
@@ -99,49 +99,48 @@ def _detect_macros(node):
return required_pkgs
-def _expand_ast(node, modules):
- def macro_expand(node):
+def _expand_ast(tree, modules):
+ def macro_expand(tree):
for module in [m.macros for m in modules]:
- if (isinstance(node, With)):
- if (isinstance(node.context_expr, Name)
- and node.context_expr.id in module.block_registry):
- return module.block_registry[node.context_expr.id](node), True
+ if (isinstance(tree, With)):
+ if (isinstance(tree.context_expr, Name)
+ and tree.context_expr.id in module.block_registry):
+ return module.block_registry[tree.context_expr.id](tree), True
-# When passing arguments to a macro. TODO arity-checking?
- if (isinstance(node.context_expr, Call)
- and isinstance(node.context_expr.func, Name)
- and node.context_expr.func.id in module.block_registry):
- the_macro = module.block_registry[node.context_expr.func.id]
+ if (isinstance(tree.context_expr, Call)
+ and isinstance(tree.context_expr.func, Name)
+ and tree.context_expr.func.id in module.block_registry):
+ the_macro = module.block_registry[tree.context_expr.func.id]
- return the_macro(node, *(node.context_expr.args)), True
+ return the_macro(tree, *(tree.context_expr.args)), True
- if (isinstance(node, BinOp)
- and type(node.left) is Name
- and type(node.op) is Mod
- and node.left.id in module.expr_registry):
+ if (isinstance(tree, BinOp)
+ and type(tree.left) is Name
+ and type(tree.op) is Mod
+ and tree.left.id in module.expr_registry):
- return module.expr_registry[node.left.id](node.right), True
+ return module.expr_registry[tree.left.id](tree.right), True
- if (isinstance(node, ClassDef)
- and len(node.decorator_list) == 1
- and node.decorator_list[0]
- and type(node.decorator_list[0]) is Name
- and node.decorator_list[0].id in module.decorator_registry):
+ if (isinstance(tree, ClassDef)
+ and len(tree.decorator_list) == 1
+ and tree.decorator_list[0]
+ and type(tree.decorator_list[0]) is Name
+ and tree.decorator_list[0].id in module.decorator_registry):
- return module.decorator_registry[node.decorator_list[0].id](node), True
+ return module.decorator_registry[tree.decorator_list[0].id](tree), True
- return node, False
+ return tree, False
@Walker
- def macro_searcher(node):
+ def macro_searcher(tree):
modified = True
while modified:
- node, modified = macro_expand(node)
- return node
+ tree, modified = macro_expand(tree)
+ return tree
- node = macro_searcher.recurse(node)
- return node
+ tree = macro_searcher.recurse(tree)
+ return tree
@singleton
View
@@ -205,83 +205,83 @@ def match(self, matchee):
return updates
-def build_matcher(node, modified):
- if isinstance(node, Num):
- return q%(LiteralMatcher(u%(node.n)))
- if isinstance(node, Str):
- return q%(LiteralMatcher(u%(node.s)))
- if isinstance(node, Name):
- if node.id in ['True', 'False']:
- return q%(LiteralMatcher(ast%(node)))
- modified.add(node.id)
- return q%(NameMatcher(u%(node.id)))
- if isinstance(node, List):
+def build_matcher(tree, modified):
+ if isinstance(tree, Num):
+ return q%(LiteralMatcher(u%(tree.n)))
+ if isinstance(tree, Str):
+ return q%(LiteralMatcher(u%(tree.s)))
+ if isinstance(tree, Name):
+ if tree.id in ['True', 'False']:
+ return q%(LiteralMatcher(ast%(tree)))
+ modified.add(tree.id)
+ return q%(NameMatcher(u%(tree.id)))
+ if isinstance(tree, List):
sub_matchers = []
- for child in node.elts:
+ for child in tree.elts:
sub_matchers.append(build_matcher(child, modified))
return Call(Name('ListMatcher', Load()), sub_matchers, [], None, None)
- if isinstance(node, Tuple):
+ if isinstance(tree, Tuple):
sub_matchers = []
- for child in node.elts:
+ for child in tree.elts:
sub_matchers.append(build_matcher(child, modified))
return Call(Name('TupleMatcher', Load()), sub_matchers, [], None, None)
- if isinstance(node, Call):
+ if isinstance(tree, Call):
sub_matchers = []
- for child in node.args:
+ for child in tree.args:
sub_matchers.append(build_matcher(child, modified))
positional_matchers = List(sub_matchers, Load())
kw_matchers = []
- for kw in node.keywords:
+ for kw in tree.keywords:
kw_matchers.append(
keyword(kw.arg, build_matcher(kw.value, modified)))
- return Call(Name('ClassMatcher', Load()), [node.func,
+ return Call(Name('ClassMatcher', Load()), [tree.func,
positional_matchers], kw_matchers, None, None)
- if (isinstance(node, BinOp) and isinstance(node.op, BitAnd)):
- sub1 = build_matcher(node.left, modified)
- sub2 = build_matcher(node.right, modified)
+ if (isinstance(tree, BinOp) and isinstance(tree.op, BitAnd)):
+ sub1 = build_matcher(tree.left, modified)
+ sub2 = build_matcher(tree.right, modified)
return Call(Name('ParallelMatcher', Load()), [sub1, sub2], [], None,
None)
- raise Exception("Unrecognized node " + repr(node))
+ raise Exception("Unrecognized tree " + repr(tree))
-def _is_pattern_match_stmt(node):
- return (isinstance(node, Expr) and
- _is_pattern_match_expr(node.value))
+def _is_pattern_match_stmt(tree):
+ return (isinstance(tree, Expr) and
+ _is_pattern_match_expr(tree.value))
-def _is_pattern_match_expr(node):
- return (isinstance(node, BinOp) and
- isinstance(node.op, LShift))
+def _is_pattern_match_expr(tree):
+ return (isinstance(tree, BinOp) and
+ isinstance(tree.op, LShift))
@macros.block
-def _matching(node):
+def _matching(tree):
"""
This macro will enable non-refutable pattern matching. If a pattern match
fails, an exception will be thrown.
"""
@Walker
- def func(node):
- if _is_pattern_match_stmt(node):
+ def func(tree):
+ if _is_pattern_match_stmt(tree):
modified = set()
- matcher = build_matcher(node.value.left, modified)
+ matcher = build_matcher(tree.value.left, modified)
# lol random names for hax
with q as assignment:
xsfvdy = ast%(matcher)
statements = [assignment,
- Expr(q%(xsfvdy.match_value(ast%(node.value.right))))]
+ Expr(q%(xsfvdy.match_value(ast%(tree.value.right))))]
for var_name in modified:
statements.append(Assign([Name(var_name, Store())],
q%(xsfvdy.get_var(u%var_name))))
return statements
else:
- return node
- func.recurse(node)
- return node.body
+ return tree
+ func.recurse(tree)
+ return tree.body
-def _rewrite_if(node, var_name=None):
+def _rewrite_if(tree, var_name=None):
# with q as rewritten:
# try:
# with matching:
@@ -291,11 +291,11 @@ def _rewrite_if(node, var_name=None):
# u%(_maybe_rewrite_if(failBody))
# return rewritten
handler = ExceptHandler(Name('PatternMatchException',
- Load()), None, node.orelse)
- try_stmt = TryExcept(node.body, [handler], [])
+ Load()), None, tree.orelse)
+ try_stmt = TryExcept(tree.body, [handler], [])
if var_name:
- node.test = BinOp(node.test, LShift(), Name(var_name, Load()))
- macroed_match = With(Name('_matching', Load()), None, Expr(node.test))
+ tree.test = BinOp(tree.test, LShift(), Name(var_name, Load()))
+ macroed_match = With(Name('_matching', Load()), None, Expr(tree.test))
try_stmt.body = [macroed_match] + try_stmt.body
if len(handler.body) == 1:
handler.body = [_maybe_rewrite_if(handler.body[0], var_name)]
@@ -311,7 +311,7 @@ def _maybe_rewrite_if(stmt, var_name=None):
@macros.block
-def switch(node, arg):
+def switch(tree, arg):
"""
This only enables (refutable) pattern matching in top-level if statements.
The advantage of this is the limited reach ensures less interference with
@@ -321,23 +321,23 @@ def switch(node, arg):
import string
import random
new_id = util.gen_sym()
- for i in xrange(len(node.body)):
- node.body[i] = _maybe_rewrite_if(node.body[i], new_id)
- node.body = [Assign([Name(new_id, Store())], arg)] + node.body
- return node.body
+ for i in xrange(len(tree.body)):
+ tree.body[i] = _maybe_rewrite_if(tree.body[i], new_id)
+ tree.body = [Assign([Name(new_id, Store())], arg)] + tree.body
+ return tree.body
@macros.block
-def patterns(node):
+def patterns(tree):
"""
This enables patterns everywhere! NB if you use this macro, you will not be
able to use real left shifts anywhere.
"""
# First transform all if-matches, then wrap the whole thing in a "with
# _matching" block
@Walker
- def if_rewriter(node):
- return _maybe_rewrite_if(node)
- if_rewriter.recurse(node)
- node.context_expr = Name('_matching', Load())
- return node
+ def if_rewriter(tree):
+ return _maybe_rewrite_if(tree)
+ if_rewriter.recurse(tree)
+ tree.context_expr = Name('_matching', Load())
+ return tree
Oops, something went wrong.

0 comments on commit cace9fd

Please sign in to comment.