Esempio n. 1
0
	def nextInstruction(self):
		"""Execute the next instruction as specified by InstructionPointer"""

		code    = self.Code[self.InstructionPointer]
		pattern = re.compile("^Take (.*) Line to ([^#]*?)[\t ]*(#.*)?$")

		match       = pattern.match(code)
		line        = match.group(1)
		destination = match.group(2)

		if self.areStationsConnected(self.DataPointer, destination, line):
			if destination not in Stations.keys():
				raise RuntimeError("Station " + destination + " doesn't exist.")

			# Debug
			if self._verbose:
				pa = "\"%s\"" % self.Accumulator if isinstance(self.Accumulator, str) else str(self.Accumulator)
				ps = "\"%s\"" % self.StationValues[destination] if isinstance(self.StationValues[destination], str) else str(self.StationValues[destination])
				print ("[" + str(self.InstructionPointer) + "] " + code)
				print ("Before: %s (%s)" % (pa, ps))

			self.executeStation(destination)

			# Debug
			if self._verbose:
				pa = "\"%s\"" % self.Accumulator if isinstance(self.Accumulator, str) else str(self.Accumulator)
				ps = "\"%s\"" % self.StationValues[destination] if isinstance(self.StationValues[destination], str) else str(self.StationValues[destination])
				print ("After:  %s (%s)" % (pa, ps))
				print ("")

		else:
			raise RuntimeError("Stations " + self.DataPointer + " and " + destination + " are not connected through " + line + " Line.")

		self.InstructionPointer += 1
Esempio n. 2
0
	def __init__(self, code, acc, verbose = False):
		"""
		Initialize a new interpreter.

		Arguments:
			code -- the code to execute as a string
			acc -- initialization value for accumulator
		"""
		for line in iter(code.splitlines()):
			pattern = re.compile("^Take (.*) Line to ([^#]*?)[\t ]*(#.*)?$")

			# Add only valid Lines to the code list, ignoring the rest.
			if pattern.match(line):
				self.Code.append(line)

		self._verbose = verbose
		self.Accumulator = acc

		# Initialize Station Values to their names
		for station in Stations.keys():
			self.StationValues[station] = station