# Create a stack with a list containing our movies
my_stack = Stack([star_trek1, star_trek2, star_trek3])

# Iterate over stack
for i in my_stack:
    print(i.title + " was " + i.reviews[0])
print()

# Add a movie to the stack
my_stack.push(star_trek4)

# Print whats on top of the stack
print(my_stack.peek().title + " was on the top of my stack")

# pop() gets the item at the top of the stack and removes it from stack
some_movie = my_stack.pop()
print("The movie " + some_movie.title + " was removed from the stack")

# Check if something is in the stack
print("Star Trek IV is in the stack: " + str(my_stack.contains(star_trek4)))

# Can reverse the order of the stack with
my_stack.reverse()

# Emtpy/clear the stack
my_stack.clear_stack()

# Make sure it's empty
print("The stack is empty: " + str(my_stack.is_empty()))
Beispiel #2
0
class IfdefOperation(Operation):
	stack = None

	def __init__(self):
		self.stack = Stack()
		pass

	def apply(self, line, state):
		result = OperationResult(line, False)
		skip = False

		if not self.stack.isEmpty():
			#print(str(state.row) + " : " + str(self.stack.arr))
			if self.stack.contains(False):
				skip = True

		dirsearch = regex['directive'].search(line)
		if dirsearch:
			directive = dirsearch.group(1)
			identifier = dirsearch.group(2)

			if directive == "#ifdef" or directive == "#ifndef" and not skip:
				result.line = commentLine(result.line)

				if identifier == "":
					result.error = "Invalid " + directive
					return result

				if not identifier in state.macros:
					self.stack.push(directive == "#ifndef")
					result.line = handleCulledLine(result.line)
					return result

				self.stack.push(directive != "#ifndef")

			elif directive == "#else":
				result.line = commentLine(result.line)

				if self.stack.isEmpty():
					result.error = "Unexpected #else"
					return result
				
				if self.stack.top() == True:
					self.stack.pop()	
					self.stack.push(False)
				else:
					self.stack.pop()
					self.stack.push(True)

			elif directive == "#endif":
				result.line = commentLine(result.line)

				if self.stack.isEmpty():
					result.error = "Unexpected #endif"
					return result

				self.stack.pop()

		if skip:
			result.line = handleCulledLine(result.line)

		return result