Exemple #1
0
	def generateCodeBlock(self, current_ast_node, argumentList=[]):
		# Enter start of code block
		self.currentDepth += 1
		self.current_symboltable = self.current_symboltable.getCurrentChild()

		nonVoidFunc = True
		voidMain = False
		# check if every non-void function has a return
		if current_ast_node.parent.name == "function":
			if current_ast_node.parent.type != "void":
				if len(current_ast_node.children) == 0:
					raise SymbolTable.NoReturnException(current_ast_node.parent.value)
				if current_ast_node.getChild(-1).name != "return":
				 	raise SymbolTable.NoReturnException(current_ast_node.parent.value)
			else:
				nonVoidFunc = False
				if current_ast_node.parent.value == "main":
					voidMain = True

		if current_ast_node.parent.name != "if" and current_ast_node.parent.name != "while":
			self.filestring += "{\n"

		for i in range(len(argumentList)):
			self.generateDeclaration(argumentList[i])
			self.filestring += "store {} %{}, {}* %{}\n".format(self.getType(argumentList[i].type), str(i),
						self.getType(argumentList[i].type), argumentList[i].value) 


		for child in current_ast_node.children:
			if child.name == "code block":
				self.generateCodeBlock(child)
			# Declaration without definition
			elif len(child.children) == 0 and child.type is not None:
				if child.name != "array decl":
					self.generateDeclaration(child)
				else:
					raise SymbolTable.SemanticException("Size of array {} was not declared".format(child.value))
			elif child.name == "=":
				# If left child has no type -> declaration
				if child.type == "decl":
					self.generateDefinition(child)
				# Else -> assignment
				elif child.type == "ass":
					self.generateAssignment(child)
			elif child.name == "array decl":
				self.generateArrayDeclaration(child)
			elif child.name == "f call":
				# the only rvalue that can be called on its own is a function call
				# all the others are essentially useless and are thus not generated
				self.generateExpression(child)
			elif child.name == "if":
				self.generateIfStatement(child)
			elif child.name == "while":
				self.generateWhileStatement(child)
			elif child.name == "return":
				if nonVoidFunc:	
					self.generateReturn(child)
					break
				else:
					self.filestring += "ret void\n"
					break

		if current_ast_node.getChild(-1).name != "return":
			if voidMain:
				self.filestring += "ret i32 0\n"
			elif current_ast_node.parent.type == "void":
				self.filestring += "ret void\n"

		# Ending code block
		self.current_symboltable = self.current_symboltable.parent
		self.current_symboltable.incrementChildIndex()

		if current_ast_node.parent.name != "if" and current_ast_node.parent.name != "while":
			self.filestring += "}\n"

		self.currentDepth -= 1