1.1 --- a/micropython/ast.py Thu Sep 04 19:44:39 2008 +0200
1.2 +++ b/micropython/ast.py Fri Sep 05 00:04:20 2008 +0200
1.3 @@ -87,7 +87,7 @@
1.4
1.5 # The temporary storage used by the current assignment expression.
1.6
1.7 - self.expr_temp = None
1.8 + self.expr_temp = []
1.9
1.10 # Wiring within the code.
1.11
1.12 @@ -108,9 +108,12 @@
1.13 def __repr__(self):
1.14 return "Translation(%r)" % self.module
1.15
1.16 - def get_module_code(self):
1.17 -
1.18 - "Return the top-level module code."
1.19 + def get_module_code(self, final=0):
1.20 +
1.21 + """
1.22 + Return the top-level module code including finalising code if 'final' is
1.23 + set to a true value.
1.24 + """
1.25
1.26 self.unit = self.module
1.27 self.code = []
1.28 @@ -120,6 +123,11 @@
1.29 if self.module.module is not None:
1.30 self.dispatch(self.module.module)
1.31
1.32 + # Finish off the translated program if appropriate.
1.33 +
1.34 + if final:
1.35 + self.new_op(Return())
1.36 +
1.37 self.unit.temp_usage = self.max_temp_position + 1
1.38 return self.code
1.39
1.40 @@ -268,17 +276,16 @@
1.41 # Assignment expression values.
1.42
1.43 def record_value(self):
1.44 - self.expr_temp = self._optimise_temp_storage()
1.45 + self.expr_temp.append(self.get_temp())
1.46 self.active_source = self.active
1.47
1.48 def discard_value(self):
1.49 - self.discard_temp(self.expr_temp)
1.50 - self.expr_temp = None
1.51 + self.discard_temp(self.expr_temp.pop())
1.52 self.active_source = None
1.53
1.54 def set_source(self):
1.55 if self.active is not None:
1.56 - self.active.source = self.expr_temp
1.57 + self.active.source = self.expr_temp[-1]
1.58
1.59 # Optimise away constant storage if appropriate.
1.60
1.61 @@ -301,7 +308,7 @@
1.62 if not self.temp_positions:
1.63 temp_position = 0
1.64 else:
1.65 - temp_position = max(self.temp_positions)
1.66 + temp_position = max(self.temp_positions) + 1
1.67 self.temp_positions.add(temp_position)
1.68 self.max_temp_position = max(self.max_temp_position, temp_position)
1.69 return self.unit.all_local_usage + temp_position # position in frame
1.70 @@ -1509,23 +1516,37 @@
1.71 self.record_value()
1.72
1.73 for n in node.nodes:
1.74 - self.dispatch(n)
1.75 + self.dispatch(n, 1)
1.76
1.77 self.discard_value()
1.78
1.79 - def visitAssAttr(self, node):
1.80 + def visitAssAttr(self, node, top_level=0):
1.81 self._visitAttr(node, self.attribute_store_instructions)
1.82 self.set_source()
1.83
1.84 - def visitAssList(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "AssList")
1.85 -
1.86 - def visitAssName(self, node):
1.87 + def visitAssList(self, node, top_level=1):
1.88 + for i, n in enumerate(node.nodes):
1.89 + self._startCallFunc()
1.90 + self.new_op(self.expr_temp[-1])
1.91 + self._generateAttr(node, "__getitem__", self.attribute_load_instructions)
1.92 + temp, target = self._generateCallFunc([compiler.ast.Const(i)], node)
1.93 + self._doCallFunc(temp, target)
1.94 + self._endCallFunc(temp, target)
1.95 +
1.96 + # Provide a different source value.
1.97 +
1.98 + self.record_value()
1.99 + self.dispatch(n, 0)
1.100 + self.discard_value()
1.101 +
1.102 + def visitAssName(self, node, top_level=1):
1.103
1.104 # Optimise away intermediate source storage.
1.105
1.106 - no_source = self._optimise_source_storage()
1.107 + if top_level:
1.108 + no_source = self._optimise_source_storage()
1.109 self._visitName(node, self.name_store_instructions)
1.110 - if not no_source:
1.111 + if not top_level or not no_source:
1.112 self.set_source()
1.113
1.114 visitAssTuple = visitAssList
1.115 @@ -1727,6 +1748,10 @@
1.116 self._doCallFunc(temp, target)
1.117 self._endCallFunc(temp, target)
1.118
1.119 + # Record the value to be assigned.
1.120 +
1.121 + self.record_value()
1.122 +
1.123 # Test for StopIteration.
1.124
1.125 self.load_builtin("StopIteration", node)
1.126 @@ -1739,6 +1764,7 @@
1.127 # Assign to the target.
1.128
1.129 self.dispatch(node.assign)
1.130 + self.discard_value()
1.131
1.132 # Process the body with the current next and exit points.
1.133