Enhanced standard funcrtion declaration from .csv file.
authoretisserant
Mon, 18 Jun 2007 14:45:29 +0200
changeset 19 0b499416ebd7
parent 18 ee18a387e80a
child 20 1ac28118c322
Enhanced standard funcrtion declaration from .csv file.
plcopen/structures.py
--- a/plcopen/structures.py	Thu Jun 14 17:39:33 2007 +0200
+++ b/plcopen/structures.py	Mon Jun 18 14:45:29 2007 +0200
@@ -503,13 +503,13 @@
 			"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{
 					"paramname" : paramname, "typename": typename.lower()}
 	
-	def recurse_and_indent(fdecls, indent):
+	def recurse_and_indent(fdecls, indent, do_type_search_only = False):
 		if type(fdecls) != type(tuple()):
 			res = ""
 			for Paramname, ParamTypes in fdecls.iteritems():
 				res += ("""
 {
-	indentifier_c param_name("%(input_name)s");
+	identifier_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);
 	
@@ -529,7 +529,7 @@
 	}
 """%{
 	"type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), 
-	"if_good_type_code":recurse_and_indent(NextParamDecl,indent).replace('\n','\n	')})
+	"if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n	')})
 
 				res += """	
 	ERROR;
@@ -544,44 +544,45 @@
 			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(),
+				"defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\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 = []
+			if not do_type_search_only:
+				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"]:
-					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
+					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;
+symbol_c *param_value = NULL;
 do{
 	char my_name[10];
 	sprintf(my_name, "IN%%d", base_num++);
-	indentifier_c param_name(my_name);
+	identifier_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);
+	param_value = function_call_param_iterator.search(&param_name);
 	
 	/* Get the value from a foo(<param_value>) style call */
 	if (param_value == NULL)
@@ -594,13 +595,16 @@
 		%s
 	}
 	
-}while(param_value != NULL)
+}while(param_value != NULL);
 %s
 """%(fdecl["baseinputnumber"]+2, code.replace('\n','\n		'), end_code))
+				else:
+					#res += code + end_code
+					res += end_code
 			else:
-				#res += code + end_code
-				res += end_code
-				
+				res += "return return_type_symbol;\n"
+			
+					
 			return res.replace('\n','\n'+indent)
 
 ###################################################################
@@ -619,7 +623,7 @@
 	official_order = []
 	for section in std_decl:
 		for fdecl in section["list"]:
-			if len(official_order)==0 or official_order[-1] != official_order:
+			if len(official_order)==0 or official_order[-1] != fdecl["name"]:
 				official_order.append(fdecl["name"])
 			# store all func by name in a dict
 			std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {})
@@ -650,6 +654,10 @@
 
 	# Generate the funct thaat return enumerated according function name
 	get_function_type_decl = """
+/****
+ * IEC 61131-3 standard function lib
+ * generated code, do not edit by hand
+ */
 function_type_t get_function_type(identifier_c *function_name) {
 """
 	for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
@@ -666,7 +674,13 @@
 
 	# 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"	
+	st_code_gen = """
+/****
+ * IEC 61131-3 standard function lib
+ * generated code, do not edit by hand
+ */
+switch(current_function_type){
+"""
 	
 	for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
 		st_code_gen += """
@@ -685,13 +699,58 @@
 	}/*function_%s*/
 	break;
 """	%(fname.lower())
-	st_code_gen += "}"
+	st_code_gen +=  """
+	case function_none :
+	ERROR;
+}
+return NULL;
+"""
+
+
+	# Generate the part of search_expression_type_c::visit(function_invocation)
+	# that is responsible of returning type symbol for function invocation.
+	search_type_code =  """
+/****
+ * IEC 61131-3 standard function lib
+ * generated code, do not edit by hand
+ */
+switch(current_function_type){
+"""
+	
+	for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
+		search_type_code += """
+/****
+ *%s
+ */
+	case function_%s :
+	{
+		symbol_c *last_type_symbol = NULL;
+"""	%(fname, fname.lower())
+		indent =  "	"
+
+		search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n	')
+		
+		search_type_code += """
+	}/*function_%s*/
+	break;
+"""	%(fname.lower())
+	search_type_code += """
+	case function_none :
+	ERROR;
+}
+return NULL;
+"""
+
 
 	# 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")
-	
+	for name, ext in [
+			('function_type_decl','h'),
+			('get_function_type_decl','c'),
+			('st_code_gen','c'),
+			('search_type_code','c')]:
+		fd = open(os.path.join(sys.argv[1],name+'.'+ext),'w')
+		fd.write(eval(name))
+		fd.close()
 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")))))