# HG changeset patch # User Paul Boddie # Date 1335913435 -7200 # Node ID 9787cf0491a8b5e0fec3e3c3e7bec87468ab08d1 # Parent ed94ff8a2e78b2b2e4df8f3472dbe80814f17a37 Changed the visitor dispatch to occur through visit methods on the AST nodes. (This requires a modified version of the compiler package.) Added report output support for yield statements and conditional expressions. diff -r ed94ff8a2e78 -r 9787cf0491a8 micropython/ast.py --- a/micropython/ast.py Wed May 02 01:01:58 2012 +0200 +++ b/micropython/ast.py Wed May 02 01:03:55 2012 +0200 @@ -52,7 +52,6 @@ container. """ - ASTVisitor.__init__(self) Assembler.__init__(self, program) self.visitor = self self.module = module diff -r ed94ff8a2e78 -r 9787cf0491a8 micropython/common.py --- a/micropython/common.py Wed May 02 01:01:58 2012 +0200 +++ b/micropython/common.py Wed May 02 01:03:55 2012 +0200 @@ -19,7 +19,6 @@ this program. If not, see . """ -import compiler.visitor import operator try: @@ -263,16 +262,25 @@ # Visitors and activities related to node annotations. -class ASTVisitor(compiler.visitor.ASTVisitor): +class ASTVisitor: "A base class for visitors." + def default(self, node, *args): + for n in node.getChildNodes(): + self.dispatch(n) + def dispatch(self, node, *args): "Dispatch using 'node', annotating any raised exceptions." + # Dispatch via a generic visit method. + try: - return compiler.visitor.ASTVisitor.dispatch(self, node, *args) + return node.visit(self, *args) + + # Annotate the exception in case of failure. + except NodeProcessingError, exc: if exc.astnode is None: exc.astnode = node diff -r ed94ff8a2e78 -r 9787cf0491a8 micropython/inspect.py --- a/micropython/inspect.py Wed May 02 01:01:58 2012 +0200 +++ b/micropython/inspect.py Wed May 02 01:03:55 2012 +0200 @@ -92,7 +92,6 @@ used to provide access to other modules when required. """ - ASTVisitor.__init__(self) Module.__init__(self, name, importer) self.visitor = self diff -r ed94ff8a2e78 -r 9787cf0491a8 micropython/report.py --- a/micropython/report.py Wed May 02 01:01:58 2012 +0200 +++ b/micropython/report.py Wed May 02 01:03:55 2012 +0200 @@ -502,7 +502,6 @@ "A module source code browser." def __init__(self, module, program): - ASTVisitor.__init__(self) self.visitor = self self.module = module self.program = program @@ -864,6 +863,12 @@ self.stream.write("\n") self.stream.write("\n") + def visitYield(self, node): + self.stream.write("
\n") + self._keyword("yield") + self.dispatch(node.value) + self.stream.write("
\n") + # Expression-related helper methods. def _visitBitBinary(self, node, name, symbol): @@ -1009,6 +1014,15 @@ self._span_end() self._span_end() + def visitIfExp(self, node): + self._span_start("ifexp") + self.dispatch(node.then) + self._keyword("if") + self.dispatch(node.test) + self._keyword("else") + self.dispatch(node.else_) + self._span_end() + def visitKeyword(self, node): self._span_start("keyword-arg") self.stream.write(node.name)