Add support for AND, OR, XOR for booleans
authorlbessard
Tue, 19 Jun 2007 08:43:41 +0200
changeset 21 e619d7bea692
parent 20 1ac28118c322
child 22 a765fae3b361
Add support for AND, OR, XOR for booleans
plcopen/iec_std.csv
plcopen/structures.py
--- a/plcopen/iec_std.csv	Mon Jun 18 15:34:44 2007 +0200
+++ b/plcopen/iec_std.csv	Tue Jun 19 08:43:41 2007 +0200
@@ -52,9 +52,9 @@
 ;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;;;
+Bitwise;AND;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise AND;yes;;("(%(start_bool_filter)s","&",")%(end_bool_filter)s");copy_input;;;
+;OR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise OR;yes;;("(%(start_bool_filter)s","|",")%(end_bool_filter)s");copy_input;;;
+;XOR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise EXOR;yes;;("(%(start_bool_filter)s","^",")%(end_bool_filter)s");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;;;
--- a/plcopen/structures.py	Mon Jun 18 15:34:44 2007 +0200
+++ b/plcopen/structures.py	Tue Jun 19 08:43:41 2007 +0200
@@ -27,10 +27,12 @@
 
 LANGUAGES = ["IL","ST","FBD","LD","SFC"]
 
+
 #-------------------------------------------------------------------------------
 #                        Function Block Types definitions
 #-------------------------------------------------------------------------------
 
+
 """
 Ordored list of common Function Blocks defined in the IEC 61131-3
 Each block have this attributes:
@@ -144,37 +146,37 @@
 between type that permits to make a comparison of two types
 """
 TypeHierarchy_list = [
-	("ANY" , None),
-	("ANY_DERIVED" , "ANY"),
-	("ANY_ELEMENTARY" , "ANY"),
+	("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")]
+	("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)
 
@@ -196,8 +198,83 @@
     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)
+#-------------------------------------------------------------------------------
+#                             Test identifier
+#-------------------------------------------------------------------------------
+
+
+
+# Test if identifier is valid
+def TestIdentifier(identifier):
+     if identifier[0].isdigit():
+        return False
+     words = identifier.split('_')
+     for i, word in enumerate(words):
+         if len(word) == 0 and i != 0:
+             return False
+         if len(word) != 0 and not word.isalnum():
+             return False
+     return True
+
+
+#-------------------------------------------------------------------------------
+#                            Languages Keywords
+#-------------------------------------------------------------------------------
+
+
+# Keywords for Pou Declaration
+POU_KEYWORDS = ["FUNCTION", "END_FUNCTION", "FUNCTION_BLOCK", "END_FUNCTION_BLOCK",
+ "PROGRAM", "END_PROGRAM", "EN", "ENO", "F_EDGE", "R_EDGE"]
+for category in BlockTypes:
+    for block in category["list"]:
+        if block["name"] not in POU_KEYWORDS:
+            POU_KEYWORDS.append(block["name"])
+
+
+# Keywords for Type Declaration
+TYPE_KEYWORDS = ["TYPE", "END_TYPE", "STRUCT", "END_STRUCT", "ARRAY", "OF", "T",
+ "D", "TIME_OF_DAY", "DATE_AND_TIME"]
+TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS])
+
+
+# Keywords for Variable Declaration
+VAR_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", 
+ "VAR_EXTERNAL", "END_VAR", "AT", "CONSTANT", "RETAIN", "NON_RETAIN"]
+
+
+# Keywords for Configuration Declaration
+CONFIG_KEYWORDS = ["CONFIGURATION", "END_CONFIGURATION", "RESOURCE", "ON", "END_RESOURCE",
+ "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK", "VAR_ACCESS", "VAR_CONFIG", 
+ "VAR_GLOBAL", "END_VAR"]
+
+
+# Keywords for Structured Function Chart
+SFC_KEYWORDS = ["ACTION", "END_ACTION", "INITIAL_STEP", "STEP", "END_STEP", "TRANSITION",
+ "FROM", "TO", "END_TRANSITION"]
+
+
+# Keywords for Instruction List
+IL_KEYWORDS = ["LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN",
+ "XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE",
+ "LE", "LT", "JMP", "JMPC", "JMPNC", "CAL", "CALC", "CALNC", "RET", "RETC", "RETNC"]
+
+
+# Keywords for Instruction List and Structured Text
+ST_KEYWORDS = ["IF", "THEN", "ELSIF", "ELSE", "END_IF", "CASE", "OF", "END_CASE", 
+ "FOR", "TO", "BY", "DO", "END_FOR", "WHILE", "DO", "END_WHILE", "REPEAT", "UNTIL", 
+ "END_REPEAT", "EXIT", "RETURN", "NOT", "MOD", "AND", "XOR", "OR"]
+
+ 
+# All the keywords of IEC
+IEC_KEYWORDS = ["E", "TRUE", "FALSE"]
+IEC_KEYWORDS.extend([keyword for keyword in POU_KEYWORDS if keyword not in IEC_KEYWORDS])
+IEC_KEYWORDS.extend([keyword for keyword in TYPE_KEYWORDS if keyword not in IEC_KEYWORDS])
+IEC_KEYWORDS.extend([keyword for keyword in VAR_KEYWORDS if keyword not in IEC_KEYWORDS])
+IEC_KEYWORDS.extend([keyword for keyword in CONFIG_KEYWORDS if keyword not in IEC_KEYWORDS])
+IEC_KEYWORDS.extend([keyword for keyword in SFC_KEYWORDS if keyword not in IEC_KEYWORDS])
+IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS])
+IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS])
+
 
 
 """
@@ -411,82 +488,6 @@
 	
 	return Standard_Functions_Decl
 
-#-------------------------------------------------------------------------------
-#                             Test identifier
-#-------------------------------------------------------------------------------
-
-
-
-# Test if identifier is valid
-def TestIdentifier(identifier):
-     if identifier[0].isdigit():
-        return False
-     words = identifier.split('_')
-     for i, word in enumerate(words):
-         if len(word) == 0 and i != 0:
-             return False
-         if len(word) != 0 and not word.isalnum():
-             return False
-     return True
-
-#-------------------------------------------------------------------------------
-#                            Languages Keywords
-#-------------------------------------------------------------------------------
-
-
-
-# Keywords for Pou Declaration
-POU_KEYWORDS = ["FUNCTION", "END_FUNCTION", "FUNCTION_BLOCK", "END_FUNCTION_BLOCK",
- "PROGRAM", "END_PROGRAM", "EN", "ENO", "F_EDGE", "R_EDGE"]
-for category in BlockTypes:
-    for block in category["list"]:
-        if block["name"] not in POU_KEYWORDS:
-            POU_KEYWORDS.append(block["name"])
-
-
-# Keywords for Type Declaration
-TYPE_KEYWORDS = ["TYPE", "END_TYPE", "STRUCT", "END_STRUCT", "ARRAY", "OF", "T",
- "D", "TIME_OF_DAY", "DATE_AND_TIME"]
-TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS])
-
-
-# Keywords for Variable Declaration
-VAR_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", 
- "VAR_EXTERNAL", "END_VAR", "AT", "CONSTANT", "RETAIN", "NON_RETAIN"]
-
-
-# Keywords for Configuration Declaration
-CONFIG_KEYWORDS = ["CONFIGURATION", "END_CONFIGURATION", "RESOURCE", "ON", "END_RESOURCE",
- "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK", "VAR_ACCESS", "VAR_CONFIG", 
- "VAR_GLOBAL", "END_VAR"]
-
-
-# Keywords for Structured Function Chart
-SFC_KEYWORDS = ["ACTION", "END_ACTION", "INITIAL_STEP", "STEP", "END_STEP", "TRANSITION",
- "FROM", "TO", "END_TRANSITION"]
-
-
-# Keywords for Instruction List
-IL_KEYWORDS = ["LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN",
- "XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE",
- "LE", "LT", "JMP", "JMPC", "JMPNC", "CAL", "CALC", "CALNC", "RET", "RETC", "RETNC"]
-
-
-# Keywords for Instruction List and Structured Text
-ST_KEYWORDS = ["IF", "THEN", "ELSIF", "ELSE", "END_IF", "CASE", "OF", "END_CASE", 
- "FOR", "TO", "BY", "DO", "END_FOR", "WHILE", "DO", "END_WHILE", "REPEAT", "UNTIL", 
- "END_REPEAT", "EXIT", "RETURN", "NOT", "MOD", "AND", "XOR", "OR"]
-
- 
-# All the keywords of IEC
-IEC_KEYWORDS = ["E", "TRUE", "FALSE"]
-IEC_KEYWORDS.extend([keyword for keyword in POU_KEYWORDS if keyword not in IEC_KEYWORDS])
-IEC_KEYWORDS.extend([keyword for keyword in TYPE_KEYWORDS if keyword not in IEC_KEYWORDS])
-IEC_KEYWORDS.extend([keyword for keyword in VAR_KEYWORDS if keyword not in IEC_KEYWORDS])
-IEC_KEYWORDS.extend([keyword for keyword in CONFIG_KEYWORDS if keyword not in IEC_KEYWORDS])
-IEC_KEYWORDS.extend([keyword for keyword in SFC_KEYWORDS if keyword not in IEC_KEYWORDS])
-IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS])
-IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS])
 
 if __name__ == '__main__':
 	
@@ -495,10 +496,10 @@
 
 	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))"
+		"ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_type_symbol))",
+		"ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))",
+		"ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))",
+		"ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))"
 		}.get(typename,
 			"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{
 					"paramname" : paramname, "typename": typename.lower()}
@@ -559,7 +560,9 @@
 					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("'
-	
+				code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n  s4o.print("('
+				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("'
+                
 				if type(code_gen) == type(tuple()):
 					res += 's4o.print("%s");\n'%(code_gen[0]%code_gen_dic_decl)
 					static_param_accept_list = []
@@ -567,7 +570,7 @@
 						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])
+					end_code = 's4o.print("%s");\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl)
 				else:
 					code = ''
 					end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','')