generate_IEC_std.py
changeset 284 6cf858411d3a
parent 167 893ff98ab3be
child 291 e580c945c62a
equal deleted inserted replaced
283:c4199b88cf60 284:6cf858411d3a
    38  
    38  
    39 """
    39 """
    40 
    40 
    41 # Get definitions
    41 # Get definitions
    42 from plcopen.structures import *
    42 from plcopen.structures import *
       
    43 
       
    44 if len(sys.argv) != 2 :
       
    45     print "Usage: " + sys.argv[0] + " path_name\n -> create files in path_name"
       
    46     sys.exit(0)
    43 
    47 
    44 #import pprint
    48 #import pprint
    45 #pp = pprint.PrettyPrinter(indent=4)
    49 #pp = pprint.PrettyPrinter(indent=4)
    46 
    50 
    47 matiec_header = """/*
    51 matiec_header = """/*
   152     if (%(input_name)s_param_value == NULL)
   156     if (%(input_name)s_param_value == NULL)
   153       %(input_name)s_param_value = function_call_param_iterator.next();
   157       %(input_name)s_param_value = function_call_param_iterator.next();
   154     symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value);
   158     symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value);
   155     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 ;
   159     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 ;
   156 """%{"input_name":Paramname}
   160 """%{"input_name":Paramname}
   157             
   161                                 
   158             for ParamType,NextParamDecl in ParamTypes.iteritems():
   162             for ParamType,NextParamDecl in ParamTypes.iteritems():
   159             
   163             
   160                 res += """    
   164                 res += """    
   161     %(type_test)s
   165     %(type_test)s
   162     {
   166     {
   165 """%{
   169 """%{
   166     "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), 
   170     "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), 
   167     "if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n    ')}
   171     "if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n    ')}
   168 
   172 
   169             res += """    
   173             res += """    
       
   174     
   170     ERROR;
   175     ERROR;
   171 }
   176 }
   172 """
   177 """
   173         
   178         
   174         return res.replace('\n','\n'+indent)
   179         return res.replace('\n','\n'+indent)
   175     else:
   180     else:
   176         res = "\n"
   181         res = "\n"
   177         fdecl=fdecls[0]
   182         fdecl=fdecls[0]
       
   183         
       
   184         if not do_type_search_only:
       
   185             code_gen = eval(fdecl["python_eval_c_code_format"])
       
   186             
       
   187             if code_gen[1] is not None:
       
   188                 res += "function_name = (symbol_c*)(new pragma_c(\"%s\"));\n"%code_gen[1]
       
   189             if fdecl["extensible"]:
       
   190                 res += """
       
   191 char nb_param_str[10];
       
   192 sprintf(nb_param_str, "%d", nb_param);
       
   193 ADD_PARAM_LIST((symbol_c*)(new integer_c(nb_param_str)), (symbol_c*)(new int_type_name_c()), function_param_iterator_c::direction_in)
       
   194 """
       
   195             for paramname,paramtype,unused in fdecl["inputs"]:
       
   196                 res += "ADD_PARAM_LIST(%(input_name)s_param_value, %(input_name)s_type_symbol, function_param_iterator_c::direction_in)\n"%{"input_name" : paramname}
       
   197 
       
   198             if fdecl["extensible"]:
       
   199                 res += """
       
   200 int base_num = %d;
       
   201 symbol_c *param_value = NULL;
       
   202 do{
       
   203     char my_name[10];
       
   204     sprintf(my_name, "IN%%d", base_num++);
       
   205     identifier_c param_name(my_name);
       
   206     
       
   207     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   208     param_value = function_call_param_iterator.search(&param_name);
       
   209     
       
   210     /* Get the value from a foo(<param_value>) style call */
       
   211     if (param_value == NULL)
       
   212       param_value = function_call_param_iterator.next();
       
   213     if (param_value != NULL){
       
   214         symbol_c *current_type_symbol = search_expression_type->get_type(param_value);
       
   215         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 ;
       
   216     
       
   217         /*Function specific CODE */
       
   218         ADD_PARAM_LIST(param_value, current_type_symbol, function_param_iterator_c::direction_in)
       
   219     }
       
   220     
       
   221 }while(param_value != NULL);
       
   222 """%(fdecl["baseinputnumber"] + 2)
   178         
   223         
   179         result_type_rule = fdecl["return_type_rule"]
   224         result_type_rule = fdecl["return_type_rule"]
   180         res += {
   225         res += {
   181             "copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n",
   226             "copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n",
   182             "defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(),
   227             "defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(),
   183             }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule)
   228             }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule)
   184         
   229         
   185         if not do_type_search_only:
   230         if not do_type_search_only:
   186             code_gen = eval(fdecl["python_eval_c_code_format"])
   231             if code_gen[0] is not None:
   187 
   232                 res += "function_type_prefix = %s;\n"%{"return_type" : "return_type_symbol"}.get(code_gen[0], "(symbol_c*)(new pragma_c(\"%s\"))"%code_gen[0])
   188             code_gen_dic_decl = {}
   233             if code_gen[2] is not None:
   189             for paramname,paramtype,unused in fdecl["inputs"]:
   234                 res += "function_type_suffix = %s_symbol;\n"%{"common_type" : "last_type"}.get(code_gen[2], code_gen[2])
   190                 code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname)
   235             res += "break;\n"
   191                 code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname)
       
   192             code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("'
       
   193             code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("'
       
   194             code_gen_dic_decl["common_type"] = '");\nlast_type_symbol->accept(*this);\ns4o.print("'
       
   195             code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n  s4o.print("('
       
   196             code_gen_dic_decl["end_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol)) {\n  s4o.print("&1");\n  s4o.print(")");\n}\ns4o.print("'
       
   197             
       
   198             if type(code_gen) == type(tuple()):
       
   199                 res += 's4o.indent_right();\n'
       
   200                 res += 's4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[0]%code_gen_dic_decl)
       
   201                 static_param_accept_list = []
       
   202                 for paramname,paramtype,unused in fdecl["inputs"]:
       
   203                     static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname))
       
   204                 res += ('s4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[1])).join(static_param_accept_list)
       
   205                 code = 's4o.print("%s\\n" + s4o.indent_spaces);\nparam_value->accept(*this);\n'%(code_gen[1])
       
   206                 end_code = 's4o.print("%s");\ns4o.indent_left();\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl)
       
   207             else:
       
   208                 code = ''
       
   209                 end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','')
       
   210 
       
   211             if fdecl["extensible"]:
       
   212                 res += ("""
       
   213 int base_num = %d;
       
   214 symbol_c *param_value = NULL;
       
   215 do{
       
   216     char my_name[10];
       
   217     sprintf(my_name, "IN%%d", base_num++);
       
   218     identifier_c param_name(my_name);
       
   219     
       
   220     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   221     param_value = function_call_param_iterator.search(&param_name);
       
   222     
       
   223     /* Get the value from a foo(<param_value>) style call */
       
   224     if (param_value == NULL)
       
   225       param_value = function_call_param_iterator.next();
       
   226     if (param_value != NULL){
       
   227         symbol_c *current_type_symbol = search_expression_type->get_type(param_value);
       
   228         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 ;
       
   229     
       
   230         /*Function specific CODE */
       
   231         %s
       
   232     }
       
   233     
       
   234 }while(param_value != NULL);
       
   235 %s
       
   236 """%(fdecl["baseinputnumber"]+2, code.replace('\n','\n        '), end_code))
       
   237             else:
       
   238                 #res += code + end_code
       
   239                 res += end_code
       
   240         else:
   236         else:
   241             res += "return return_type_symbol;\n"
   237             res += "return return_type_symbol;\n"
   242         
   238         
   243                 
       
   244         return res.replace('\n','\n'+indent)
   239         return res.replace('\n','\n'+indent)
   245 
   240 
   246 ###################################################################
   241 ###################################################################
   247 ###                                                             ###
   242 ###                                                             ###
   248 ###                           MAIN                              ###
   243 ###                           MAIN                              ###
   334 """    %(fname.lower())
   329 """    %(fname.lower())
   335 st_code_gen +=  """
   330 st_code_gen +=  """
   336     case function_none :
   331     case function_none :
   337     ERROR;
   332     ERROR;
   338 }
   333 }
   339 return NULL;
       
   340 """
   334 """
   341 
   335 
   342 ###################################################################
   336 ###################################################################
   343 """
   337 """
   344 Generate the part of generate_c_il_c::visit(il_function_call)
   338 Generate the part of generate_c_il_c::visit(il_function_call)
   367 """    %(fname.lower())
   361 """    %(fname.lower())
   368 il_code_gen +=  """
   362 il_code_gen +=  """
   369     case function_none :
   363     case function_none :
   370     ERROR;
   364     ERROR;
   371 }
   365 }
   372 return NULL;
       
   373 """
   366 """
   374 
   367 
   375 ###################################################################
   368 ###################################################################
   376 """
   369 """
   377 Generate the part of search_expression_type_c::visit(function_invocation)
   370 Generate the part of search_expression_type_c::visit(function_invocation)
   378 that is responsible of returning type symbol for function invocation.
   371 that is responsible of returning type symbol for function invocation.
   379 """
   372 """
   380 search_type_code = matiec_header + """
   373 search_type_code = matiec_header + """
   381 
   374 
   382 void *compute_standard_function_st(function_invocation_c *symbol) {
   375 void *compute_standard_function_default(function_invocation_c *st_symbol = NULL, il_formal_funct_call_c *il_symbol = NULL) {
   383 
   376   function_type_t current_function_type;
   384   function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
   377   function_call_param_iterator_c *tmp_function_call_param_iterator;
   385   function_call_param_iterator_c function_call_param_iterator(symbol);
   378   if (st_symbol != NULL && il_symbol == NULL) {
       
   379     current_function_type = get_function_type((identifier_c *)st_symbol->function_name);
       
   380     tmp_function_call_param_iterator = new function_call_param_iterator_c(st_symbol);
       
   381   }
       
   382   else if (st_symbol == NULL && il_symbol != NULL) {
       
   383     current_function_type = get_function_type((identifier_c *)il_symbol->function_name);
       
   384     tmp_function_call_param_iterator = new function_call_param_iterator_c(il_symbol);
       
   385   }
       
   386   else
       
   387     ERROR;
       
   388   function_call_param_iterator_c function_call_param_iterator(*tmp_function_call_param_iterator);
   386   search_expression_type_c* search_expression_type = this;
   389   search_expression_type_c* search_expression_type = this;
   387 
   390 
   388   switch(current_function_type){
   391   switch(current_function_type){
   389 """
   392 """
   390 
   393 
   484                 else:
   487                 else:
   485                     iec_std_lib_generated +=  " DO(" + typename2 + ")"
   488                     iec_std_lib_generated +=  " DO(" + typename2 + ")"
   486         iec_std_lib_generated +=  "\n"
   489         iec_std_lib_generated +=  "\n"
   487     else:
   490     else:
   488         break
   491         break
   489 
       
   490 if len(sys.argv) != 2 :
       
   491     print "Usage: " + sys.argv[0] + "path_name\n -> create files in path_name"
       
   492     sys.exit(0)
       
   493 
   492 
   494 # Now, print that out, or write to files from sys.argv
   493 # Now, print that out, or write to files from sys.argv
   495 for path, name, ext in [
   494 for path, name, ext in [
   496         ('stage4/generate_c','function_type_decl','h'),
   495         ('stage4/generate_c','function_type_decl','h'),
   497         ('stage4/generate_c','get_function_type_decl','c'),
   496         ('stage4/generate_c','get_function_type_decl','c'),