Demo entry 6737306

code

   

Submitted by anonymous on Apr 30, 2018 at 11:03
Language: Python 3. Code size: 1.8 kB.

def checkAndFind(taci, operation=None):
	opcode = taci.getAttribute("opcode")
	
	inst = {}
	if operation == "LABEL" and opcode != "LABEL":
		return inst,  opcode
	
	if opcode not in semantics.keys():
		error(5,opcode + " is unknown")
	
	l_occur = []
	# operand = {}  # {kind: "variable" | "literal", type: "int" | "string", data: __}
	for child in taci.childNodes:
		if child.nodeName not in semantics[opcode] or child.nodeName != "#text":
			error(5,child.nodeName + "is not in the semantics")
		if child.nodeName in l_occur:
			error(5,child.nodeName + " appears 2 times")
			
		elif child.nodeName != "#text":
			l_occur.append(child.nodeName)
			
			if child.childNodes[0].data == "":
				error(5,"The data of the node is empty")
				
			operand = {"kind":"variable", "type":"string", "data":child.childNodes[0].data}
			
			# check if the attribute are correctly set
			if not all([e in ["kind", "type"] for e in child.attributes.keys()]):
				error(5, "Error with a attribute")
			else:
				if child.hasAttribute("kind"):
					if child.getAttribute("kind") in ["variable", "literal"]:
						operand["kind"] = child.getAttribute("kind")
						if child.getAttribute("kind") == "literal":
							operand["type"] = "int"
					else:
						error(5,child.getAttribute("kind") + " is unknown attribute for kind")
				if child.hasAttribute("type"):
					if child.getAttribute("type") in ["int", "string"]:
						operand["type"] = child.getAttribute("type")
					else:
						error(5, child.hasAttribute("type") + " is unknown attribute for type ")
			
			# data coherent with his type
			if operand["type"] == "int":
				try:
					operand["data"] = int(operand["data"])
				except ValueError:
					error(5, "The data is not a int")
			
			inst[child.nodeName] = operand
	
	return inst, opcode

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).