--- a/plcopen/structures.py Tue Aug 12 18:15:07 2008 +0200
+++ b/plcopen/structures.py Tue Aug 12 18:15:35 2008 +0200
@@ -33,15 +33,20 @@
"D" : ["DINT", "UDINT", "REAL", "DWORD"],
"L" : ["LINT", "ULINT", "LREAL", "LWORD"]}
+# Helper for emulate join on element list
+def JoinList(separator, list):
+ return reduce(lambda x, y: x + separator + y, list)
+
def generate_block(generator, block, body, link, order=False):
body_type = body.getcontent()["name"]
name = block.getinstanceName()
type = block.gettypeName()
executionOrderId = block.getexecutionOrderId()
- block_infos = GetBlockType(type)
+ block_infos = generator.GetBlockType(type)
if block_infos["type"] == "function":
output_variable = block.outputVariables.getvariable()[0]
output_name = "%s%d_OUT"%(type, block.getlocalId())
+ output_info = (generator.TagName, "block", block.getlocalId(), "output", 0)
if not generator.ComputedBlocks.get(block, False) and not order:
generator.ComputedBlocks[block] = True
if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] or generator.Interface[-1][2] or generator.Interface[-1][3]:
@@ -51,7 +56,8 @@
else:
generator.Interface[-1][4].append(("ANY", output_name, None, None))
vars = []
- for variable in block.inputVariables.getvariable():
+ for i, variable in enumerate(block.inputVariables.getvariable()):
+ input_info = (generator.TagName, "block", block.getlocalId(), "input", i)
connections = variable.connectionPointIn.getconnections()
if connections and len(connections) == 1:
if body_type == "FBD" or body_type == "SFC":
@@ -63,14 +69,21 @@
else:
paths = paths[0]
value = generator.ComputeLDExpression(paths, True)
- vars.append(generator.ExtractModifier(variable, value))
- generator.Program += " %s := %s(%s);\n"%(output_name, type, ", ".join(vars))
- return generator.ExtractModifier(output_variable, output_name)
+ vars.append(generator.ExtractModifier(variable, value, input_info))
+ generator.Program += [(generator.CurrentIndent, ()),
+ (output_name, output_info),
+ (" := ", ()),
+ (type, (generator.TagName, "block", block.getlocalId(), "type")),
+ ("(", ())]
+ generator.Program += JoinList([(", ", ())], vars)
+ generator.Program += [(");\n", ())]
+ return generator.ExtractModifier(output_variable, [(output_name, output_info)], output_info)
elif block_infos["type"] == "functionBlock":
if not generator.ComputedBlocks.get(block, False) and not order:
generator.ComputedBlocks[block] = True
vars = []
- for variable in block.inputVariables.getvariable():
+ for i, variable in enumerate(block.inputVariables.getvariable()):
+ input_info = (generator.TagName, "block", block.getlocalId(), "input", i)
connections = variable.connectionPointIn.getconnections()
if connections and len(connections) == 1:
parameter = variable.getformalParameter()
@@ -83,16 +96,22 @@
else:
paths = paths[0]
value = generator.ComputeLDExpression(paths, True)
- vars.append("%s := %s"%(parameter, generator.ExtractModifier(variable, value)))
- generator.Program += " %s(%s);\n"%(name, ", ".join(vars))
+ vars.append([(parameter, input_info),
+ (" := ", ())] + generator.ExtractModifier(variable, value, input_info))
+ generator.Program += [(generator.CurrentIndent, ()),
+ (name, (generator.TagName, "block", block.getlocalId(), "name")),
+ ("(", ())]
+ generator.Program += JoinList([(", ", ())], vars)
+ generator.Program += [(");\n", ())]
if link:
connectionPoint = link.getposition()[-1]
else:
connectionPoint = None
- for variable in block.outputVariables.getvariable():
+ for i, variable in enumerate(block.outputVariables.getvariable()):
blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY()
+ output_info = (generator.TagName, "block", block.getlocalId(), "output", i)
if not connectionPoint or block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety():
- return generator.ExtractModifier(variable, "%s.%s"%(name, variable.getformalParameter()))
+ return generator.ExtractModifier(variable, [("%s.%s"%(name, variable.getformalParameter()), output_info)], output_info)
raise ValueError, "No output variable found"
def initialise_block(type, name, block = None):
@@ -212,35 +231,7 @@
"generate" : generate_block, "initialise" : initialise_block}
]},
]
-
-PluginTypes = []
-
-"""
-Function that returns the block definition associated to the block type given
-"""
-
-def GetBlockType(type, inputs = None):
- for category in BlockTypes + PluginTypes:
- for blocktype in category["list"]:
- if inputs:
- block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
- same_inputs = inputs == block_inputs
- else:
- same_inputs = True
- if blocktype["name"] == type and same_inputs:
- return blocktype
- return None
-
-"""
-Function that add a new plugin to the plugin list
-"""
-
-def AddPluginBlockList(blocklist):
- PluginTypes.extend(blocklist)
-
-def ClearPluginTypes():
- for i in xrange(len(PluginTypes)):
- PluginTypes.pop(0)
+
#-------------------------------------------------------------------------------
# Data Types definitions
@@ -265,7 +256,7 @@
("ANY_INT", "ANY_NUM"),
("ANY_SINT", "ANY_INT"),
("ANY_UINT", "ANY_INT"),
- ("BOOL", "ANY_BIT"),
+ ("BOOL", "ANY_BIT"),
("SINT", "ANY_SINT"),
("INT", "ANY_SINT"),
("DINT", "ANY_SINT"),
@@ -290,11 +281,26 @@
TypeHierarchy = dict(TypeHierarchy_list)
-def ResetTypeHierarchy():
- TypeHierarchy = dict(TypeHierarchy_list)
-
-def AddDataTypeHierarchy(name, reference):
- TypeHierarchy[name] = reference
+"""
+returns true if the given data type is the same that "reference" meta-type or one of its types.
+"""
+def IsOfType(type, reference):
+ if reference is None:
+ return True
+ elif type == reference:
+ return True
+ else:
+ parent_type = TypeHierarchy[type]
+ if parent_type is not None:
+ return IsOfType(parent_type, reference)
+ return False
+
+"""
+returns list of all types that correspont to the ANY* meta type
+"""
+def GetSubTypes(type):
+ return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)]
+
DataTypeRange_list = [
("SINT", (-2**7, 2**7 - 1)),
@@ -309,60 +315,7 @@
DataTypeRange = dict(DataTypeRange_list)
-def ResetDataTypeRange():
- DataTypeRange = dict(DataTypeRange_list)
-
-def AddDataTypeRange(name, range):
- DataTypeRange[name] = range
-
-"""
-returns true if the given data type is the same that "reference" meta-type or one of its types.
-"""
-
-def IsOfType(test, reference):
- if reference is None:
- return True
- while test is not None:
- if test == reference:
- return True
- test = TypeHierarchy[test]
- return False
-
-def IsEndType(reference):
- if reference is not None:
- return not reference.startswith("ANY")
- else:
- return True
-
-def GetBaseType(type):
- parent_type = TypeHierarchy[type]
- if parent_type.startswith("ANY"):
- return type
- else:
- return GetBaseType(parent_type)
-
-def GetDataTypeRange(reference):
- while reference is not None:
- if reference in DataTypeRange:
- return DataTypeRange[reference]
- else:
- reference = TypeHierarchy[reference]
- return None
-
-"""
-returns list of all types that correspont to the ANY* meta type
-"""
-def GetSubTypes(reference):
- return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, reference)]
-
-
-EnumeratedDataValues = []
-
-def ResetEnumeratedDataValues():
- EnumeratedDataValues = []
-
-def AddEnumeratedDataValues(values):
- EnumeratedDataValues.extend(values)
+
#-------------------------------------------------------------------------------
# Test identifier