plcopen/structures.py
changeset 18 ee18a387e80a
parent 15 fc897b7bfa7b
child 19 0b499416ebd7
--- a/plcopen/structures.py	Thu Jun 14 08:59:43 2007 +0200
+++ b/plcopen/structures.py	Thu Jun 14 17:39:33 2007 +0200
@@ -143,39 +143,40 @@
 Each type is associated to his direct parent type. It defines then a hierarchy
 between type that permits to make a comparison of two types
 """
-
-TypeHierarchy = {"ANY" : None,
-                 "ANY_DERIVED" : "ANY",
-                 "ANY_ELEMENTARY" : "ANY",
-                 "ANY_MAGNITUDE": "ANY_ELEMENTARY",
-                 "ANY_BIT" : "ANY_ELEMENTARY",
-                 "ANY_STRING" : "ANY_ELEMENTARY",
-                 "ANY_DATE" : "ANY_ELEMENTARY",
-                 "ANY_NUM" : "ANY_MAGNITUDE",
-                 "ANY_REAL" : "ANY_NUM",
-                 "ANY_INT" : "ANY_NUM",
-                 "REAL" : "ANY_REAL",
-                 "LREAL" : "ANY_REAL",
-                 "SINT" : "ANY_INT",
-                 "INT" : "ANY_INT",
-                 "DINT" : "ANY_INT",
-                 "LINT" : "ANY_INT",
-                 "USINT" : "ANY_INT",
-                 "UINT" : "ANY_INT",
-                 "UDINT" : "ANY_INT",
-                 "ULINT" : "ANY_INT",
-                 "TIME" : "ANY_MAGNITUDE",
-                 "BOOL" : "ANY_BIT",
-                 "BYTE" : "ANY_BIT",
-                 "WORD" : "ANY_BIT",
-                 "DWORD" : "ANY_BIT",
-                 "LWORD" : "ANY_BIT",
-                 "STRING" : "ANY_STRING",
-                 "WSTRING" : "ANY_STRING",
-                 "DATE" : "ANY_DATE",
-                 "TOD" : "ANY_DATE",
-                 "DT" : "ANY_DATE"
-}
+TypeHierarchy_list = [
+	("ANY" , None),
+	("ANY_DERIVED" , "ANY"),
+	("ANY_ELEMENTARY" , "ANY"),
+	("ANY_MAGNITUDE", "ANY_ELEMENTARY"),
+	("ANY_BIT" , "ANY_ELEMENTARY"),
+	("ANY_STRING" , "ANY_ELEMENTARY"),
+	("ANY_DATE" , "ANY_ELEMENTARY"),
+	("ANY_NUM" , "ANY_MAGNITUDE"),
+	("ANY_REAL" , "ANY_NUM"),
+	("ANY_INT" , "ANY_NUM"),
+	("REAL" , "ANY_REAL"),
+	("LREAL" , "ANY_REAL"),
+	("SINT" , "ANY_INT"),
+	("INT" , "ANY_INT"),
+	("DINT" , "ANY_INT"),
+	("LINT" , "ANY_INT"),
+	("USINT" , "ANY_INT"),
+	("UINT" , "ANY_INT"),
+	("UDINT" , "ANY_INT"),
+	("ULINT" , "ANY_INT"),
+	("TIME" , "ANY_MAGNITUDE"),
+	("BOOL" , "ANY_BIT"),
+	("BYTE" , "ANY_BIT"),
+	("WORD" , "ANY_BIT"),
+	("DWORD" , "ANY_BIT"),
+	("LWORD" , "ANY_BIT"),
+	("STRING" , "ANY_STRING"),
+	("WSTRING" , "ANY_STRING"),
+	("DATE" , "ANY_DATE"),
+	("TOD" , "ANY_DATE"),
+	("DT" , "ANY_DATE")]
+
+TypeHierarchy = dict(TypeHierarchy_list)
 
 """
 returns true if the given data type is the same that "reference" meta-type or one of its types.
@@ -194,6 +195,11 @@
 def GetSubTypes(reference):
     return [ typename for typename in TypeHierarchy.iterkeys() if typename[:3] != "ANY" and IsOfType(typename, reference)]
 
+
+def IsATime(iectype):
+	return IsOfType(iectype, TIME) or IsOfType(iectype, ANY_DATE)
+
+
 """
 take a .csv file and translate it it a "csv_table"
 """            
@@ -220,7 +226,7 @@
 	fields = [True,True]
 	while(fields[1]):
 		fields = table.pop(0)
-		variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:])])
+		variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!=''])
 		standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
 	return standard_funtions_input_variables
 	
@@ -234,7 +240,14 @@
 	param_types = []
 	param_names = []
 	modifiers = []
-	if len(decl)>1 : suffix = str(base)
+
+	len_of_not_predifined_variable = 0
+	for param_type in decl:
+		predifined_variable_param_type = variables.get(param_type,None)
+		if not predifined_variable_param_type :
+			len_of_not_predifined_variable += 1
+
+	if len_of_not_predifined_variable>1 : suffix = str(base)
 	else: suffix = ''
 	
 	for param_type in decl:
@@ -265,10 +278,35 @@
 			"inputs" : lambda x:csv_input_translate(x,variables,Function_decl.get("baseinputnumber",1)),
 			"outputs":lambda x:[("OUT",x,"none")]}
 		Function_decl[field] = translate.get(field,lambda x:x)(val)
-	#Function_decl.pop("baseinputnumber")
-	Function_decl.pop("overloaded")
 	return Function_decl
 
+
+def ANY_TO_ANY_FORMAT_GEN(fdecl):
+
+	ANY_T0_ANY_LIST=[
+		(("ANY_NUM","ANY_BIT"),("ANY_NUM","ANY_BIT"), "(%(return_type)s)%(IN_value)s"),
+		(("ANY_NUM","ANY_BIT"),("ANY_DATE","TIME"), "(%(return_type)s)real_to_time(%(IN_value)s)"), 
+		(("ANY_DATE","TIME"), ("ANY_NUM","ANY_BIT"), "(%(return_type)s)time_to_real(%(IN_value)s)"), 
+		(("ANY_DATE","TIME"), ("ANY_STRING",), "(%(return_type)s)time_to_string(%(IN_value)s)"),
+		(("ANY_STRING",), ("ANY_DATE","TIME"), "(%(return_type)s)string_to_time(%(IN_value)s)"),
+		(("ANY_BIT",), ("ANY_STRING",), "(%(return_type)s)int_to_string(%(IN_value)s, 16)"),
+		(("ANY_NUM",), ("ANY_STRING",), "(%(return_type)s)int_to_string(%(IN_value)s, 10)"),
+		(("ANY_STRING",), ("ANY_BIT",), "(%(return_type)s)string_to_int(%(IN_value)s, 16)"),
+		(("ANY_STRING",), ("ANY_NUM",), "(%(return_type)s)string_to_int(%(IN_value)s, 10)")]
+	
+	for (InTypes, OutTypes, Format) in ANY_T0_ANY_LIST:
+		inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), InTypes))
+		#print "IN     ",inps , fdecl["outputs"][0][1], InTypes
+		outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), OutTypes))
+		#print "OUT    ",outs , fdecl["inputs"][0][1], OutTypes
+		if (inps and outs ):
+		 	return Format
+
+	#print "IN:", fdecl["outputs"][0][1], "    OUT:", fdecl["inputs"][0][1]
+	
+	return "#error %s_TO_%s not implemented!"%(fdecl["inputs"][0][1],fdecl["outputs"][0][1])
+
+
 """
 Returns this kind of declaration for all standard functions
 
@@ -299,42 +337,77 @@
 				Current_section = { "name" : fields[0], "list" : [] }
 				Function_decl_list = []
 		
-			dico_from_table = zip(fonctions, fields[1:])
+			dico_from_table = [ (champ,val) for champ,val in zip(fonctions, fields[1:]) if champ ]
 			Function_decl = decl_function(dico_from_table,variables)
-			
-			if Function_decl["name"].startswith("*"):
-				input_types = GetSubTypes(Function_decl["inputs"][0][1])
+			if Function_decl["name"].startswith('*') :
+				input_types = [ GetSubTypes(inpdecl[1]) for inpdecl in Function_decl["inputs"] ]
+				input_ovrloading_types = input_types[map(len,input_types).index(max(map(len,input_types)))]
 			else:
-				input_types = [None]
-			if Function_decl["name"].endswith("**"):
+				input_ovrloading_types = [None]
+				
+			if Function_decl["name"].endswith('*') :
 				output_types = GetSubTypes(Function_decl["outputs"][0][1])
 			else:
 				output_types = [None]
 				
-			funcdecl = Function_decl["name"]
-			for intype in input_types:
+			funcdeclname_orig = Function_decl["name"]
+			funcdeclname = Function_decl["name"].strip('*_')
+			fdc = Function_decl["inputs"][:]
+			for intype in input_ovrloading_types:
 				if intype != None:
-					decl_tpl = Function_decl["inputs"][0]
-					Function_decl["inputs"] = [decl_tpl[:1] + (intype,) + decl_tpl[2:]] + Function_decl["inputs"][1:]
-					funcdeclin = funcdecl.replace("*_", intype + '_')
-					Function_decl["name"] = funcdeclin
+					Function_decl["inputs"] = []
+					for decl_tpl in fdc:
+						if IsOfType(intype, decl_tpl[1]):
+							Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])]
+						else:
+							Function_decl["inputs"] += [(decl_tpl)]
+						
+						if funcdeclname_orig.startswith('*'):
+							funcdeclin = intype + '_' + funcdeclname 
+						else:
+							funcdeclin = funcdeclname
+				else:
+					funcdeclin = funcdeclname
+							
+
+				if Function_decl["return_type_rule"] == "copy_input":
+					output_types = [intype]
+				elif Function_decl["return_type_rule"] == "defined":
+					pass
 
 				for outype in output_types:
 					if outype != None:
 						decl_tpl = Function_decl["outputs"][0]
-						Function_decl["outputs"] = [decl_tpl[:1] + (outype,) + decl_tpl[2:]] + Function_decl["outputs"][1:]
-						funcdeclout = funcdeclin.replace("_**", '_' + outype)
-						Function_decl["name"] = funcdeclout
-					
-					# create the copy of decl dict to be appended to section
-					Function_decl_copy = Function_decl.copy()
-					# Have to generate type description in comment with freshly redefined types
-					Function_decl_copy["comment"] += ("\n (" +
-						str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') +
-						" ) => (" +
-						str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') +
-						" )")
-					Current_section["list"].append(Function_decl_copy)
+						Function_decl["outputs"] = [ (decl_tpl[0] , outype,  decl_tpl[2])]
+						if funcdeclname_orig.endswith('*'):
+							funcdeclout =  funcdeclin + '_' + outype
+						else:
+							funcdeclout =  funcdeclin
+					else:
+						funcdeclout =  funcdeclin
+					Function_decl["name"] = funcdeclout
+
+
+					try:
+						fdecl = Function_decl
+						res = eval(Function_decl["python_eval_c_code_format"])
+					except Exception,e:
+						res = None
+
+					if res != None :
+						# create the copy of decl dict to be appended to section
+						Function_decl_copy = Function_decl.copy()
+						# Have to generate type description in comment with freshly redefined types
+						Function_decl_copy["comment"] += (
+							"\n (" +
+							str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') +
+							" ) => (" +
+							str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') +
+							" )")
+						Current_section["list"].append(Function_decl_copy)
+						#pp.pprint(Function_decl_copy)
+						
+	Standard_Functions_Decl.append(Current_section)
 	
 	return Standard_Functions_Decl
 
@@ -416,10 +489,209 @@
 IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS])
 
 if __name__ == '__main__':
+	
 	import pprint
 	pp = pprint.PrettyPrinter(indent=4)
-	std_decl = get_standard_funtions(csv_file_to_table(open("iec_std.csv")))
-	pp.pprint(std_decl)
+
+	def ANY_to_compiler_test_type_GEN(typename, paramname):
+		return {"ANY" : "",
+		"ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_param_value))",
+		"ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_param_value))",
+		"ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_param_value))",
+		"ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_param_value))"
+		}.get(typename,
+			"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{
+					"paramname" : paramname, "typename": typename.lower()}
+	
+	def recurse_and_indent(fdecls, indent):
+		if type(fdecls) != type(tuple()):
+			res = ""
+			for Paramname, ParamTypes in fdecls.iteritems():
+				res += ("""
+{
+	indentifier_c param_name("%(input_name)s");
+	/* Get the value from a foo(<param_name> = <param_value>) style call */
+	symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(&param_name);
+	
+	/* Get the value from a foo(<param_value>) style call */
+	if (%(input_name)s_param_value == NULL)
+	  %(input_name)s_param_value = function_call_param_iterator.next();
+	symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value);
+	last_type_symbol = last_type_symbol && search_expression_type->is_same_type(%(input_name)s_type_symbol, last_type_symbol) ? search_expression_type->common_type(%(input_name)s_type_symbol, last_type_symbol) : %(input_name)s_type_symbol ;
+"""%{"input_name":Paramname})
+				
+				for ParamType,NextParamDecl in ParamTypes.iteritems():
+				
+					res += ("""	
+	%(type_test)s
+	{
+%(if_good_type_code)s
+	}
+"""%{
+	"type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), 
+	"if_good_type_code":recurse_and_indent(NextParamDecl,indent).replace('\n','\n	')})
+
+				res += """	
+	ERROR;
+}
+"""
+			
+			return res.replace('\n','\n'+indent)
+		else:
+			res = "\n"
+			fdecl=fdecls[0]
+			
+			result_type_rule = fdecl["return_type_rule"]
+			res += {
+				"copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n",
+				"defined" : "symbol_c * return_type_symbol = &%s_type_name_c;\n"%fdecl["outputs"][0][1].lower(),
+				}.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule)
+			
+			
+			try:
+				code_gen = eval(fdecl["python_eval_c_code_format"])
+			except Exception:
+				code_gen = "#error in eval of " + fdecl["name"]
+
+			code_gen_dic_decl = {}
+			for paramname,paramtype,unused in fdecl["inputs"]:
+				code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname)
+				code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname)
+			code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("'
+			code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("'
+
+			if type(code_gen) == type(tuple()):
+				res += 's4o.print("%s");\n'%(code_gen[0]%code_gen_dic_decl)
+				static_param_accept_list = []
+				for paramname,paramtype,unused in fdecl["inputs"]:
+					static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname))
+				res += ('s4o.print("%s");\n'%(code_gen[1])).join(static_param_accept_list)
+				code = 's4o.print("%s");\nparam_value->accept(*this);\n'%(code_gen[1])
+				end_code = 's4o.print("%s");\nreturn NULL;\n'%(code_gen[2])
+			else:
+				code = ''
+				end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','')
+
+			if fdecl["extensible"]:
+				res += ("""
+int base_num = %d
+do{
+	char my_name[10];
+	sprintf(my_name, "IN%%d", base_num++);
+	indentifier_c param_name(my_name);
+	
+	/* Get the value from a foo(<param_name> = <param_value>) style call */
+	symbol_c *param_value = function_call_param_iterator.search(&param_name);
+	
+	/* Get the value from a foo(<param_value>) style call */
+	if (param_value == NULL)
+	  param_value = function_call_param_iterator.next();
+	if (param_value != NULL){
+		symbol_c *current_type_symbol = search_expression_type->get_type(param_value);
+		last_type_symbol = last_type_symbol && search_expression_type->is_same_type(current_type_symbol, last_type_symbol) ? search_expression_type->common_type(current_type_symbol, last_type_symbol) : current_type_symbol ;
+	
+		/*Function specific CODE */
+		%s
+	}
+	
+}while(param_value != NULL)
+%s
+"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n		'), end_code))
+			else:
+				#res += code + end_code
+				res += end_code
+				
+			return res.replace('\n','\n'+indent)
+
+###################################################################
+###                                                             ###
+###                           MAIN                              ###
+###                                                             ###
+###################################################################
+
+	# Get definitions
+	std_decl = get_standard_funtions(csv_file_to_table(open("iec_std.csv")))#, True)
+	
+	# Reorganize into a dict of dict, according 
+	# fname : paramname : paramtype : paraname : paramtype...
+	# Keep ptrack of original order in a separated list
+	std_fdecls = {}
+	official_order = []
+	for section in std_decl:
+		for fdecl in section["list"]:
+			if len(official_order)==0 or official_order[-1] != official_order:
+				official_order.append(fdecl["name"])
+			# store all func by name in a dict
+			std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {})
+			current = std_fdecls_fdecl_name
+			for i in fdecl["inputs"]:
+				current[i[0]] = current.get(i[0], {})
+				current = current[i[0]]
+				last = current
+				current[i[1]] = current.get(i[1], {})
+				current = current[i[1]]
+			last[i[1]]=(fdecl,)
+			std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name
+
+	# Generate the long enumeration of std function types
+	function_type_decl =  """
+/****
+ * IEC 61131-3 standard function lib
+ * generated code, do not edit by hand
+ */
+typedef enum {
+"""
+	for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
+		function_type_decl += "	function_"+fname.lower()+",\n"
+
+	function_type_decl += """	function_none
+} function_type_t;
+"""
+
+	# Generate the funct thaat return enumerated according function name
+	get_function_type_decl = """
+function_type_t get_function_type(identifier_c *function_name) {
+"""
+	for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
+		get_function_type_decl += """
+	if (!strcasecmp(function_name->value, "%s"))
+		return function_%s;
+"""%(fname,fname.lower())
+
+	get_function_type_decl += """
+	else return function_none;
+}
+
+"""
+
+	# Generate the part of generate_cc_st_c::visit(function_invocation)
+	# that is responsible to generate C code for std lib calls.
+	st_code_gen = "switch(current_function_type){\n"	
+	
+	for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
+		st_code_gen += """
+/****
+ *%s
+ */
+	case function_%s :
+	{
+		symbol_c *last_type_symbol = NULL;
+"""	%(fname, fname.lower())
+		indent =  "	"
+
+		st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n	')
+		
+		st_code_gen += """
+	}/*function_%s*/
+	break;
+"""	%(fname.lower())
+	st_code_gen += "}"
+
+	# Now, print that out, or write to files from sys.argv
+	print function_type_decl
+	print get_function_type_decl
+	print st_code_gen #.replace("\n", "\\\n")
+	
 else:
 	# Put standard functions declaration in Bloktypes
 	BlockTypes.extend(get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv")))))