# HG changeset patch # User etisserant # Date 1181835573 -7200 # Node ID ee18a387e80a7375daf7955d1c07bee8c256a8c4 # Parent faa048f4172e0b351b04f6ce8a8b7171e9773e95 Enhanced standard funcrtion declaration from .csv file. diff -r faa048f4172e -r ee18a387e80a plcopen/iec_std.csv --- a/plcopen/iec_std.csv Thu Jun 14 08:59:43 2007 +0200 +++ b/plcopen/iec_std.csv Thu Jun 14 17:39:33 2007 +0200 @@ -1,84 +1,80 @@ -Standard_functions_variables_types;name;comment;type;;;; -;N;Number of bits to be shifted;UINT;;;; -;L;Left position within character string;UINT;;;; -;P;Position within character string;UINT;;;; -;G;Selection out of 2 inputs (gate);BOOL;;;; -;K;Selection out of n inputs;ANY_INT;;;; -;MN;Minimum value for limitation;ANY;;;; -;MX;Maximum value for limitation;ANY;;;; -;;;;;;; -;;;;;;; -;;;;;;; -;;;;;;; -;;;;;;; -;;;;;;; -Standard_functions_type;name;baseinputnumber;inputs;outputs;comment;overloaded;extensible -Type conversion;*_TO_**;1;(ANY);ANY;Data type conversion;yes;no -;TRUNC;1;(ANY_REAL);ANY_INT;Rounding up/down;yes;no -;BCD_TO_**;1;(ANY_BIT);ANY;Conversion from BCD;yes;no -;*_TO_BCD;1;(ANY_INT);ANY_BIT;Conversion to BCD;yes;no -;DATE_AND_TIME_TO_TIME_OF_DAY;1;(DT);TOD;Conversion to time-of-day;no;no -;DATE_AND_TIME_TO_DATE;1;(DT);DATE;Conversion to date;no;no -Numerical;ABS;1;(ANY_NUM);ANY_NUM;Absolute number;yes;no -;SQRT;1;(ANY_REAL);ANY_REAL;Square root (base 2);yes;no -;LN;1;(ANY_REAL);ANY_REAL;Natural logarithm;yes;no -;LOG;1;(ANY_REAL);ANY_REAL;Logarithm to base 10;yes;no -;EXP;1;(ANY_REAL);ANY_REAL;Exponentiation;yes;no -;SIN;1;(ANY_REAL);ANY_REAL;Sine;yes;no -;COS;1;(ANY_REAL);ANY_REAL;Cosine;yes;no -;TAN;1;(ANY_REAL);ANY_REAL;Tangent;yes;no -;ASIN;1;(ANY_REAL);ANY_REAL;Arc sine;yes;no -;ACOS;1;(ANY_REAL);ANY_REAL;Arc cosine;yes;no -;ATAN;1;(ANY_REAL);ANY_REAL;Arc tangent;yes;no -Arithmetic;ADD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Addition;yes;yes -;ADD;1;(TIME, TIME);TIME;Time addition;yes;no -;ADD;1;(TOD, TIME);TOD;Time-of-day addition;yes;no -;ADD;1;(DT, TIME);DT;Date addition;yes;no -;MUL;1;(ANY_NUM, ANY_NUM);ANY_NUM;Multiplication;yes;yes -;MUL;1;(TIME, ANY_NUM);TIME;Time multiplication;yes;no -;SUB;1;(ANY_NUM, ANY_NUM);ANY_NUM;Subtraction;yes;no -;SUB;1;(TIME, TIME);TIME;Time subtraction;yes;no -;SUB;1;(DATE, DATE);TIME;Date subtraction;yes;no -;SUB;1;(TOD, TIME);TOD;Time-of-day subtraction;yes;no -;SUB;1;(TOD, TOD);TIME;Time-of-day subtraction;yes;no -;SUB;1;(DT, TIME);DT;Date and time subtraction;yes;no -;SUB;1;(DT, DT);TIME;Date and time subtraction;yes;no -;DIV;1;(ANY_NUM, ANY_NUM);ANY_NUM;Division;yes;no -;DIV;1;(TIME, ANY_NUM);TIME;Time division;yes;no -;MOD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Remainder (modulo);yes;no -;EXPT;1;(ANY_NUM, ANY_NUM);ANY_NUM;Exponent;yes;no -;MOVE;1;(ANY_NUM, ANY_NUM);ANY_NUM;Assignment;yes;no -Bit-shift;SHL;1;(ANY_BIT, N);ANY_BIT;Shift left;yes;no -;SHR;1;(ANY_BIT, N);ANY_BIT;Shift right;yes;no -;ROR;1;(ANY_BIT, N);ANY_BIT;Rotate right;yes;no -;ROL;1;(ANY_BIT, N);ANY_BIT;Rotate left;yes;no -Bitwise;AND;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise AND;yes;yes -;OR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise OR;yes;yes -;XOR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise EXOR;yes;yes -;NOT;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise inverting;yes;no -Selection;SEL;0;(G, ANY, ANY);ANY;Binary selection (1 of 2);yes;no -;SEL;0;(G, ENUM, ENUM);ENUM;Binary selection (1 of 2);no;no -;MAX;1;(ANY, ANY);ANY;Maximum;yes;yes -;MIN;1;(ANY, ANY);ANY;Minimum;yes;yes -;LIMIT;1;(MN, ANY, MX);ANY;Limitation;yes;no -;MUX;0;(K, ANY, ANY);ANY;Multiplexer (select 1 of N);yes;yes -;MUX;0;(K, ENUM, ENUM);ENUM;Multiplexer (select 1 of N);no;no -Comparison;GT;1;(ANY, ANY);BOOL;Greater than;yes;yes -;GE;1;(ANY, ANY);BOOL;Greater than or equal to;yes;yes -;EQ;1;(ANY, ANY);BOOL;Equal to;yes;yes -;EQ;1;(ENUM, ENUM);BOOL;Equal to;no;no -;LT;1;(ANY, ANY);BOOL;Less than;yes;yes -;LE;1;(ANY, ANY);BOOL;Less than or equal to;yes;yes -;NE;1;(ANY, ANY);BOOL;Not equal to;yes;no -;NE;1;(ENUM, ENUM);BOOL;Not equal to;no;no -Character string;LEN;1;(STRING);INT;Length of string;no;no -;LEFT;1;(STRING, L);STRING;string left of;yes;no -;RIGHT;1;(STRING, L);STRING;string right of;yes;no -;MID;1;(STRING, L, P);STRING;string from the middle;yes;no -;CONCAT;1;(STRING, STRING);STRING;Concatenation;no;yes -;CONCAT;1;(DATE, TOD);DT;Time concatenation;no;no -;INSERT;1;(STRING, STRING, P);STRING;Insertion (into);yes;no -;DELETE;1;(STRING, L, P);STRING;Deletion (within);yes;no -;REPLACE;1;(STRING, STRING, L, P);STRING;Replacement (within);yes;no -;FIND;1;(STRING, STRING);INT;Find position;yes;no -;;;;;;; +Standard_functions_variables_types;name;comment;type;;;;;;;;; +;N;Number of bits to be shifted;UINT;;;;;;;;; +;L;Left position within character string;UINT;;;;;;;;; +;P;Position within character string;UINT;;;;;;;;; +;G;Selection out of 2 inputs (gate);BOOL;;;;;;;;; +;K;Selection out of n inputs;ANY_INT;;;;;;;;; +;MN;Minimum value for limitation;ANY;;;;;;;;; +;MX;Maximum value for limitation;ANY;;;;;;;;; +;;;;;;;;;;;; +;;;;;;;;;;;; +;;;;;;;;;;;; +;;;;;;;;;;;; +;;;;;;;;;;;; +;;;;;;;;;;;; +Standard_functions_type;name;baseinputnumber;inputs;outputs;comment;extensible;lib_typed_decl;python_eval_c_code_format;return_type_rule;;; +Type conversion;*_TO_**;1;(ANY);ANY;Data type conversion;no;;ANY_TO_ANY_FORMAT_GEN(fdecl);defined;;;0=defined +;TRUNC;1;(ANY_REAL);ANY_INT;Rounding up/down;no;;"(int)%(IN_value)s";&constant_int_type_name_c;;; +;BCD_TO_**;1;(ANY_BIT);ANY_INT;Conversion from BCD;no;;"__bcd_to_something(sizeof(%(IN_type)s),&%(IN_value)s)";defined;;; +;*_TO_BCD;1;(ANY_INT);ANY_BIT;Conversion to BCD;no;;"__something_to_bcd(sizeof(%(IN_type)s),&%(IN_value)s)";&constant_int_type_name_c;;; +;DATE_AND_TIME_TO_TIME_OF_DAY;1;(DT);TOD;Conversion to time-of-day;no;;"__date_and_time_to_time_of_day(&%(IN_value)s)";defined;;; +;DATE_AND_TIME_TO_DATE;1;(DT);DATE;Conversion to date;no;;"__date_and_time_to_time_of_day(&%(IN_value)s)";defined;;; +Numerical;ABS;1;(ANY_NUM);ANY_NUM;Absolute number;no;"__abs_%(return_type)s(%(return_type)s IN){ return IN > 0 ? IN : -IN {sc}}";"__abs_%(IN_type)s(%(IN_value)s)";IN_type_symbol;;; +;SQRT;1;(ANY_REAL);ANY_REAL;Square root (base 2);no;;"sqrt(%(IN_value)s)";IN_type_symbol;;; +;LN;1;(ANY_REAL);ANY_REAL;Natural logarithm;no;;"ln(%(IN_value)s)";IN_type_symbol;;; +;LOG;1;(ANY_REAL);ANY_REAL;Logarithm to base 10;no;;"log(%(IN_value)s)";IN_type_symbol;;; +;EXP;1;(ANY_REAL);ANY_REAL;Exponentiation;no;;"exp(%(IN_value)s)";IN_type_symbol;;; +;SIN;1;(ANY_REAL);ANY_REAL;Sine;no;;"sin(%(IN_value)s)";IN_type_symbol;;; +;COS;1;(ANY_REAL);ANY_REAL;Cosine;no;;"cos(%(IN_value)s)";IN_type_symbol;;; +;TAN;1;(ANY_REAL);ANY_REAL;Tangent;no;;"tan(%(IN_value)s)";IN_type_symbol;;; +;ASIN;1;(ANY_REAL);ANY_REAL;Arc sine;no;;"asin(%(IN_value)s)";IN_type_symbol;;; +;ACOS;1;(ANY_REAL);ANY_REAL;Arc cosine;no;;"acos(%(IN_value)s)";IN_type_symbol;;; +;ATAN;1;(ANY_REAL);ANY_REAL;Arc tangent;no;;"atan(%(IN_value)s)";IN_type_symbol;;; +Arithmetic;ADD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Addition;yes;;("(","+",")");copy_input;;; +;ADD;1;(TIME, TIME);TIME;Time addition;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;ADD;1;(TOD, TIME);TOD;Time-of-day addition;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;ADD;1;(DT, TIME);DT;Date addition;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;MUL;1;(ANY_NUM, ANY_NUM);ANY_NUM;Multiplication;yes;;("(","*",")");copy_input;;; +;MUL;1;(TIME, ANY_NUM);TIME;Time multiplication;no;;"__time_mul(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;SUB;1;(ANY_NUM, ANY_NUM);ANY_NUM;Subtraction;no;;("(","-",")");copy_input;;; +;SUB;1;(TIME, TIME);TIME;Time subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;SUB;1;(DATE, DATE);TIME;Date subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;SUB;1;(TOD, TIME);TOD;Time-of-day subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;SUB;1;(TOD, TOD);TIME;Time-of-day subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;SUB;1;(DT, TIME);DT;Date and time subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;SUB;1;(DT, DT);TIME;Date and time subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;DIV;1;(ANY_NUM, ANY_NUM);ANY_NUM;Division;no;;("(","/",")");copy_input;;; +;DIV;1;(TIME, ANY_NUM);TIME;Time division;no;;"__time_div(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;MOD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Remainder (modulo);no;;("(","%",")");copy_input;;; +;EXPT;1;(ANY_NUM, ANY_NUM);ANY_NUM;Exponent;no;;"pow(&%(IN1_value)s, &%(IN2_value)s)";copy_input;;; +;MOVE;1;(ANY_NUM);ANY_NUM;Assignment;no;;"%(IN_value)s";copy_input;;; +Bit-shift;SHL;1;(ANY_BIT, N);ANY_BIT;Shift left;no;;"%(IN_value)s<<%(N_value)s";IN_type_symbol;;; +;SHR;1;(ANY_BIT, N);ANY_BIT;Shift right;no;;"%(IN_value)s>>%(N_value)s";IN_type_symbol;;; +;ROR;1;(ANY_BIT, N);ANY_BIT;Rotate right;no;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}";"__ror(sizeof(%(IN_type)s), &%(IN_value)s, %(N_value)s)";IN_type_symbol;;; +;ROL;1;(ANY_BIT, N);ANY_BIT;Rotate left;no;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}";"__rol(sizeof(%(IN_type)s), &%(IN_value)s, %(N_value)s)";IN_type_symbol;;; +Bitwise;AND;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise AND;yes;;("(","&",")");copy_input;;; +;OR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise OR;yes;;("(","|",")");copy_input;;; +;XOR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise EXOR;yes;;("(","^",")");copy_input;;; +;NOT;1;(ANY_BIT);ANY_BIT;Bitwise inverting;no;;"~%(IN_value)s";IN_type_symbol;;; +Selection;SEL;0;(G, ANY, ANY);ANY;Binary selection (1 of 2);no;;"%(G_value)s ? %(IN1_value)s : %(IN0_value)s";copy_input;;; +;MAX;1;(ANY, ANY);ANY;Maximum;yes;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}";("__max_%(return_type)s(%(param_count)s,",",",")");copy_input;;; +;MIN;1;(ANY, ANY);ANY;Minimum;yes;"__min_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 < IN2 ? IN1 : IN2 {sc}}";("__min_%(return_type)s(%(param_count)s,",",",")");copy_input;;; +;LIMIT;1;(MN, ANY, MX);ANY;Limitation;no;;"__limit_%(IN_type)s(%(MN_value)s, %(IN_value)s, %(MX_value)s)";IN_type_symbol;;; +;MUX;0;(K, ANY, ANY);ANY;Multiplexer (select 1 of N);yes;;("__mux_%(return_type)s(%(param_count)s,",",",")");copy_input;;; +Comparison;GT;1;(ANY, ANY);BOOL;Greater than;yes;"__gt_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 0 ? IN : -IN {sc}}";("__gt_%(return_type)s(%(param_count)s,",",",")");defined;;; +;GE;1;(ANY, ANY);BOOL;Greater than or equal to;yes;;("__ge_%(return_type)s(%(param_count)s,",",",")");defined;;; +;EQ;1;(ANY, ANY);BOOL;Equal to;yes;;("__eq_%(return_type)s(%(param_count)s,",",",")");defined;;; +;LT;1;(ANY, ANY);BOOL;Less than;yes;;("__lt_%(return_type)s(%(param_count)s,",",",")");defined;;; +;LE;1;(ANY, ANY);BOOL;Less than or equal to;yes;;("__le_%(return_type)s(%(param_count)s,",",",")");defined;;; +;NE;1;(ANY, ANY);BOOL;Not equal to;yes;;("__ne_%(return_type)s(%(param_count)s,",",",")");defined;;; +Character string;LEN;1;(STRING);INT;Length of string;no;;"__len(&%(IN_value)s)";defined;;; +;LEFT;1;(STRING, L);STRING;string left of;no;;"__left(&%(IN_value)s, %(L_value)s)";defined;;; +;RIGHT;1;(STRING, L);STRING;string right of;no;;"__right(&%(IN_value)s, %(L_value)s)";defined;;; +;MID;1;(STRING, L, P);STRING;string from the middle;no;;"__mid(&%(IN_value)s, %(L_value)s, %(P_value)s)";defined;;; +;CONCAT;1;(STRING, STRING);STRING;Concatenation;yes;;("__concat(%(param_count)s,",",&",")");defined;;; +;CONCAT;1;(DATE, TOD);DT;Time concatenation;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;INSERT;1;(STRING, STRING, P);STRING;Insertion (into);no;;"__insert(&%(IN1_value)s, &%(IN2_value)s, %(P_value)s)";defined;;; +;DELETE;1;(STRING, L, P);STRING;Deletion (within);no;;"__delete(&%(IN_value)s, %(L_value)s, %(P_value)s)";defined;;; +;REPLACE;1;(STRING, STRING, L, P);STRING;Replacement (within);no;;"__replace(&%(IN1_value)s, &%(IN2_value)s, %(L_value)s, %(P_value)s)";defined;;; +;FIND;1;(STRING, STRING);INT;Find position;no;;"__find(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +;;;;;;;;;defined;;; diff -r faa048f4172e -r ee18a387e80a plcopen/structures.py --- 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( = ) style call */ + symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(¶m_name); + + /* Get the value from a foo() 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( = ) style call */ + symbol_c *param_value = function_call_param_iterator.search(¶m_name); + + /* Get the value from a foo() 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")))))