1.1 --- a/micropython/ast.py Fri Feb 15 23:50:15 2008 +0100
1.2 +++ b/micropython/ast.py Sun Feb 17 02:38:04 2008 +0100
1.3 @@ -123,6 +123,9 @@
1.4 def new_op(self, op):
1.5 self.code.append(op)
1.6
1.7 + def replace_op(self, op):
1.8 + self.code[-1] = op
1.9 +
1.10 def last_op(self):
1.11 return self.code[-1]
1.12
1.13 @@ -134,6 +137,27 @@
1.14 def dispatch(self, node, *args):
1.15 return ASTVisitor.dispatch(self, node, *args)
1.16
1.17 + def _visitAttr(self, node, classes):
1.18 + AttrInstruction, AttrIndexInstruction = classes
1.19 + self.dispatch(node.expr)
1.20 +
1.21 + # NOTE: Only simple cases are used for optimisations.
1.22 +
1.23 + last = self.last_op()
1.24 + if isinstance(last, (LoadName, LoadAttr)) and last.attr.assignments == 1:
1.25 +
1.26 + # Get the details of the access.
1.27 +
1.28 + target = last.attr.value
1.29 + target_name = target.full_name()
1.30 + table_entry = self.objtable.table[target_name]
1.31 + pos = table_entry[node.attrname]
1.32 + self.replace_op(AttrInstruction(pos))
1.33 +
1.34 + else:
1.35 + index = self.objtable.get_index(node.attrname)
1.36 + self.new_op(AttrIndexInstruction(index))
1.37 +
1.38 def _visitName(self, node, classes):
1.39 name = node.name
1.40 scope = self.get_scope(name)
1.41 @@ -182,7 +206,8 @@
1.42 for n in node.nodes:
1.43 self.dispatch(n)
1.44
1.45 - def visitAssAttr(self, node): pass
1.46 + def visitAssAttr(self, node):
1.47 + self._visitAttr(node, (StoreAttr, StoreAttrIndex))
1.48
1.49 def visitAssList(self, node): pass
1.50
1.51 @@ -365,7 +390,8 @@
1.52
1.53 def visitGenExprInner(self, node): pass
1.54
1.55 - def visitGetattr(self, node): pass
1.56 + def visitGetattr(self, node):
1.57 + self._visitAttr(node, (LoadAttr, LoadAttrIndex))
1.58
1.59 def visitGlobal(self, node): pass
1.60