plcopen/structures.py
author Laurent Bessard
Wed, 09 Oct 2013 10:57:20 +0200
changeset 1347 533741e5075c
parent 1330 96b242e4c59d
child 1390 0f4d32a033e0
permissions -rw-r--r--
Fixed pou variables information loading stylesheet
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     5
#based on the plcopen standard. 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    19
#General Public License for more details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
965
308e51832711 Fixed test identifier function
Laurent Bessard
parents: 949
diff changeset
    25
import string, os, sys, re
1313
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    26
from plcopen import LoadProject
1320
bb04c41cbee9 Used OrderedDict for keeping of standard function blocks library defined
Laurent Bessard
parents: 1313
diff changeset
    27
from collections import OrderedDict
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    28
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    29
LANGUAGES = ["IL","ST","FBD","LD","SFC"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    30
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    31
LOCATIONDATATYPES = {"X" : ["BOOL"],
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    32
                     "B" : ["SINT", "USINT", "BYTE", "STRING"],
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    33
                     "W" : ["INT", "UINT", "WORD", "WSTRING"],
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    34
                     "D" : ["DINT", "UDINT", "REAL", "DWORD"],
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    35
                     "L" : ["LINT", "ULINT", "LREAL", "LWORD"]} 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    36
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    37
_ = lambda x:x
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    38
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    39
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    40
#                        Function Block Types definitions
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    41
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    42
1313
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    43
ScriptDirectory = os.path.split(os.path.realpath(__file__))[0]
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    44
1330
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1320
diff changeset
    45
StdBlockLibrary, error = LoadProject(
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1320
diff changeset
    46
    os.path.join(ScriptDirectory, "Standard_Function_Blocks.xml"))
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1320
diff changeset
    47
AddnlBlockLibrary, error = LoadProject(
96b242e4c59d Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
Laurent Bessard
parents: 1320
diff changeset
    48
    os.path.join(ScriptDirectory, "Additional_Function_Blocks.xml"))
1313
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    49
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    50
StdBlockComments = {
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    51
    "SR": _("SR bistable\nThe SR bistable is a latch where the Set dominates."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    52
    "RS": _("RS bistable\nThe RS bistable is a latch where the Reset dominates."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    53
    "SEMA": _("Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    54
    "R_TRIG": _("Rising edge detector\nThe output produces a single pulse when a rising edge is detected."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    55
    "F_TRIG": _("Falling edge detector\nThe output produces a single pulse when a falling edge is detected."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    56
    "CTU": _("Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    57
    "CTD": _("Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    58
    "CTUD": _("Up-down counter\nThe up-down counter has two inputs CU and CD. It can be used to both count up on one input and down on the other."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    59
    "TP": _("Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    60
    "TON": _("On-delay timer\nThe on-delay timer can be used to delay setting an output true, for fixed period after an input becomes true."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    61
    "TOF": _("Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    62
    "RTC": _("Real time clock\nThe real time clock has many uses including time stamping, setting dates and times of day in batch reports, in alarm messages and so on."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    63
    "INTEGRAL": _("Integral\nThe integral function block integrates the value of input XIN over time."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    64
    "DERIVATIVE": _("Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    65
    "PID": _("PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    66
    "RAMP": _("Ramp\nThe RAMP function block is modelled on example given in the standard."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    67
    "HYSTERESIS": _("Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    68
}
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    69
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    70
for block_type in ["CTU", "CTD", "CTUD"]:
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    71
    for return_type in ["DINT", "LINT", "UDINT", "ULINT"]:
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    72
        StdBlockComments["%s_%s" % (block_type, return_type)] = StdBlockComments[block_type]
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    73
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    74
def GetBlockInfos(pou):
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    75
    infos = pou.getblockInfos()
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    76
    infos["comment"] = StdBlockComments[infos["name"]]
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    77
    infos["inputs"] = [
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    78
        (var_name, var_type, "rising")
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    79
        if var_name in ["CU", "CD"]
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    80
        else (var_name, var_type, var_modifier)
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    81
        for var_name, var_type, var_modifier in infos["inputs"]]
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
    82
    return infos
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
Ordored list of common Function Blocks defined in the IEC 61131-3
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
Each block have this attributes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
    - "name" : The block name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
    - "type" : The block type. It can be "function", "functionBlock" or "program"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    89
    - "extensible" : Boolean that define if the block is extensible
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    90
    - "inputs" : List of the block inputs
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    91
    - "outputs" : List of the block outputs
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
    - "comment" : Comment that will be displayed in the block popup
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
    - "generate" : Method that generator will call for generating ST block code
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    94
Inputs and outputs are a tuple of characteristics that are in order:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    95
    - The name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
    - The data type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
    - The default modifier which can be "none", "negated", "rising" or "falling"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
1283
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   100
StdBlckLst = [{"name" : _("Standard function blocks"), "list":
1313
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   101
               [GetBlockInfos(pou) for pou in StdBlockLibrary.getpous()]},
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
              {"name" : _("Additional function blocks"), "list":
1313
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   103
               [GetBlockInfos(pou) for pou in AddnlBlockLibrary.getpous()]},
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
             ]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   105
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   106
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   107
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   108
#                           Data Types definitions
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   109
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   110
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   111
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   112
Ordored list of common data types defined in the IEC 61131-3
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   113
Each type is associated to his direct parent type. It defines then a hierarchy
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   114
between type that permits to make a comparison of two types
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   115
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   116
TypeHierarchy_list = [
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   117
    ("ANY", None),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   118
    ("ANY_DERIVED", "ANY"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   119
    ("ANY_ELEMENTARY", "ANY"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   120
    ("ANY_MAGNITUDE", "ANY_ELEMENTARY"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   121
    ("ANY_BIT", "ANY_ELEMENTARY"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
    ("ANY_NBIT", "ANY_BIT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   123
    ("ANY_STRING", "ANY_ELEMENTARY"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   124
    ("ANY_DATE", "ANY_ELEMENTARY"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   125
    ("ANY_NUM", "ANY_MAGNITUDE"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   126
    ("ANY_REAL", "ANY_NUM"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   127
    ("ANY_INT", "ANY_NUM"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   128
    ("ANY_SINT", "ANY_INT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   129
    ("ANY_UINT", "ANY_INT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   130
    ("BOOL", "ANY_BIT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   131
    ("SINT", "ANY_SINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   132
    ("INT", "ANY_SINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
    ("DINT", "ANY_SINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   134
    ("LINT", "ANY_SINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   135
    ("USINT", "ANY_UINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   136
    ("UINT", "ANY_UINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
    ("UDINT", "ANY_UINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   138
    ("ULINT", "ANY_UINT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   139
    ("REAL", "ANY_REAL"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
    ("LREAL", "ANY_REAL"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
    ("TIME", "ANY_MAGNITUDE"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   142
    ("DATE", "ANY_DATE"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
    ("TOD", "ANY_DATE"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
    ("DT", "ANY_DATE"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   145
    ("STRING", "ANY_STRING"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   146
    ("BYTE", "ANY_NBIT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
    ("WORD", "ANY_NBIT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
    ("DWORD", "ANY_NBIT"),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
    ("LWORD", "ANY_NBIT")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
    #("WSTRING", "ANY_STRING") # TODO
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   151
]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   152
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   153
TypeHierarchy = dict(TypeHierarchy_list)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   154
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   155
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   156
returns true if the given data type is the same that "reference" meta-type or one of its types.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   157
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   158
def IsOfType(type, reference):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   159
    if reference is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   160
        return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
    elif type == reference:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   162
        return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   163
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   164
        parent_type = TypeHierarchy[type]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   165
        if parent_type is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   166
            return IsOfType(parent_type, reference)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   167
    return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   168
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   169
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   170
returns list of all types that correspont to the ANY* meta type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   171
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   172
def GetSubTypes(type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   173
    return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   174
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   175
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   176
DataTypeRange_list = [
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   177
    ("SINT", (-2**7, 2**7 - 1)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   178
    ("INT", (-2**15, 2**15 - 1)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   179
    ("DINT", (-2**31, 2**31 - 1)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   180
    ("LINT", (-2**31, 2**31 - 1)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   181
    ("USINT", (0, 2**8 - 1)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   182
    ("UINT", (0, 2**16 - 1)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   183
    ("UDINT", (0, 2**31 - 1)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   184
    ("ULINT", (0, 2**31 - 1))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   185
]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   186
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   187
DataTypeRange = dict(DataTypeRange_list)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   188
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   189
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   190
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   191
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   192
#                             Test identifier
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   193
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
965
308e51832711 Fixed test identifier function
Laurent Bessard
parents: 949
diff changeset
   195
IDENTIFIER_MODEL = re.compile(
308e51832711 Fixed test identifier function
Laurent Bessard
parents: 949
diff changeset
   196
    "(?:%(letter)s|_(?:%(letter)s|%(digit)s))(?:_?(?:%(letter)s|%(digit)s))*$" %
308e51832711 Fixed test identifier function
Laurent Bessard
parents: 949
diff changeset
   197
    {"letter": "[a-zA-Z]", "digit": "[0-9]"})
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   198
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   199
# Test if identifier is valid
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   200
def TestIdentifier(identifier):
965
308e51832711 Fixed test identifier function
Laurent Bessard
parents: 949
diff changeset
   201
     return IDENTIFIER_MODEL.match(identifier) is not None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   202
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   203
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   204
#                        Standard functions list generation
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   205
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   206
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   207
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   208
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   209
take a .csv file and translate it it a "csv_table"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   210
"""            
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   211
def csv_file_to_table(file):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   212
    return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   213
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   214
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   215
seek into the csv table to a section ( section_name match 1st field )
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   216
return the matching row without first field
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   217
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   218
def find_section(section_name, table):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   219
    fields = [None]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   220
    while(fields[0] != section_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   221
        fields = table.pop(0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   222
    return fields[1:]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   223
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   224
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   225
extract the standard functions standard parameter names and types...
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   226
return a { ParameterName: Type, ...}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   227
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   228
def get_standard_funtions_input_variables(table):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   229
    variables = find_section("Standard_functions_variables_types", table)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   230
    standard_funtions_input_variables = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   231
    fields = [True,True]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   232
    while(fields[1]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   233
        fields = table.pop(0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   234
        variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!=''])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   235
        standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   236
    return standard_funtions_input_variables
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   237
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   238
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   239
translate .csv file input declaration into PLCOpenEditor interessting values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   240
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   241
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   242
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   243
def csv_input_translate(str_decl, variables, base):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   244
    decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   245
    params = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   246
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   247
    len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   248
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   249
    for param_type in decl:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   250
        if param_type in variables.keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   251
            param_name = param_type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   252
            param_type = variables[param_type]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   253
        elif len_of_not_predifined_variable > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   254
            param_name = "IN%d"%base
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   255
            base += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   256
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   257
            param_name = "IN"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   258
        params.append((param_name, param_type, "none"))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   259
    return params
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   260
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   261
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   262
ANY_TO_ANY_LIST=[
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   263
        # simple type conv are let as C cast
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   264
        (("ANY_INT","ANY_BIT"),("ANY_NUM","ANY_BIT"), ("return_type", "__move_", "IN_type")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   265
        (("ANY_REAL",),("ANY_REAL",), ("return_type", "__move_", "IN_type")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   266
        # REAL_TO_INT
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   267
        (("ANY_REAL",),("ANY_SINT",), ("return_type", "__real_to_sint", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   268
        (("ANY_REAL",),("ANY_UINT",), ("return_type", "__real_to_uint", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   269
        (("ANY_REAL",),("ANY_BIT",), ("return_type", "__real_to_bit", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   270
        # TO_TIME
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   271
        (("ANY_INT","ANY_BIT"),("ANY_DATE","TIME"), ("return_type", "__int_to_time", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   272
        (("ANY_REAL",),("ANY_DATE","TIME"), ("return_type", "__real_to_time", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   273
        (("ANY_STRING",), ("ANY_DATE","TIME"), ("return_type", "__string_to_time", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   274
        # FROM_TIME
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   275
        (("ANY_DATE","TIME"), ("ANY_REAL",), ("return_type", "__time_to_real", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   276
        (("ANY_DATE","TIME"), ("ANY_INT","ANY_NBIT"), ("return_type", "__time_to_int", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   277
        (("TIME",), ("ANY_STRING",), ("return_type", "__time_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   278
        (("DATE",), ("ANY_STRING",), ("return_type", "__date_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   279
        (("TOD",), ("ANY_STRING",), ("return_type", "__tod_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   280
        (("DT",), ("ANY_STRING",), ("return_type", "__dt_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   281
        # TO_STRING
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   282
        (("BOOL",), ("ANY_STRING",), ("return_type", "__bool_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   283
        (("ANY_BIT",), ("ANY_STRING",), ("return_type", "__bit_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   284
        (("ANY_REAL",), ("ANY_STRING",), ("return_type", "__real_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   285
        (("ANY_SINT",), ("ANY_STRING",), ("return_type", "__sint_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   286
        (("ANY_UINT",), ("ANY_STRING",), ("return_type", "__uint_to_string", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   287
        # FROM_STRING
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   288
        (("ANY_STRING",), ("BOOL",), ("return_type", "__string_to_bool", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   289
        (("ANY_STRING",), ("ANY_BIT",), ("return_type", "__string_to_bit", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   290
        (("ANY_STRING",), ("ANY_SINT",), ("return_type", "__string_to_sint", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   291
        (("ANY_STRING",), ("ANY_UINT",), ("return_type", "__string_to_uint", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   292
        (("ANY_STRING",), ("ANY_REAL",), ("return_type", "__string_to_real", None))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   293
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   294
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   295
BCD_TO_ANY_LIST=[
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   296
        (("BYTE",),("USINT",), ("return_type", "__bcd_to_uint", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   297
        (("WORD",),("UINT",), ("return_type", "__bcd_to_uint", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   298
        (("DWORD",),("UDINT",), ("return_type", "__bcd_to_uint", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   299
        (("LWORD",),("ULINT",), ("return_type", "__bcd_to_uint", None))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   300
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   301
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   302
ANY_TO_BCD_LIST=[
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   303
        (("USINT",),("BYTE",), ("return_type", "__uint_to_bcd", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   304
        (("UINT",),("WORD",), ("return_type", "__uint_to_bcd", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   305
        (("UDINT",),("DWORD",), ("return_type", "__uint_to_bcd", None)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   306
        (("ULINT",),("LWORD",), ("return_type", "__uint_to_bcd", None))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   307
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   308
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   309
def ANY_TO_ANY_FORMAT_GEN(any_to_any_list, fdecl):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   310
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   311
    for (InTypes, OutTypes, Format) in any_to_any_list:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   312
        outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   313
        inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   314
        if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   315
             return Format
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   316
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   317
    return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   318
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   319
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   320
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   321
Returns this kind of declaration for all standard functions
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   322
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   323
            [{"name" : "Numerical", 'list': [   {   
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   324
                'baseinputnumber': 1,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   325
                'comment': 'Addition',
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   326
                'extensible': True,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   327
                'inputs': [   ('IN1', 'ANY_NUM', 'none'),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   328
                              ('IN2', 'ANY_NUM', 'none')],
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   329
                'name': 'ADD',
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   330
                'outputs': [('OUT', 'ANY_NUM', 'none')],
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   331
                'type': 'function'}, ...... ] },.....]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   332
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   333
def get_standard_funtions(table):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   334
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   335
    variables = get_standard_funtions_input_variables(table)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   336
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   337
    fonctions = find_section("Standard_functions_type",table)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   338
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   339
    Standard_Functions_Decl = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   340
    Current_section = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   341
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   342
    translate = {
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   343
            "extensible" : lambda x: {"yes":True, "no":False}[x],
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   344
            "inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   345
            "outputs":lambda x:[("OUT",x,"none")]}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   346
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   347
    for fields in table:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   348
        if fields[1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   349
            # If function section name given
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   350
            if fields[0]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   351
                words = fields[0].split('"')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   352
                if len(words) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   353
                    section_name = words[1]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   354
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   355
                    section_name = fields[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   356
                Current_section = {"name" : section_name, "list" : []}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   357
                Standard_Functions_Decl.append(Current_section)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   358
                Function_decl_list = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   359
            if Current_section:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   360
                Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   361
                baseinputnumber = int(Function_decl.get("baseinputnumber",1))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   362
                Function_decl["baseinputnumber"] = baseinputnumber
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   363
                for param, value in Function_decl.iteritems():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   364
                    if param in translate:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   365
                        Function_decl[param] = translate[param](value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   366
                Function_decl["type"] = "function"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   367
                
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   368
                if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') :
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   369
                    input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   370
                    output_types = GetSubTypes(Function_decl["outputs"][0][1])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   371
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   372
                    input_ovrloading_types = [None]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   373
                    output_types = [None]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   374
                
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   375
                funcdeclname_orig = Function_decl["name"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   376
                funcdeclname = Function_decl["name"].strip('*_')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   377
                fdc = Function_decl["inputs"][:]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   378
                for intype in input_ovrloading_types:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   379
                    if intype != None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   380
                        Function_decl["inputs"] = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   381
                        for decl_tpl in fdc:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   382
                            if IsOfType(intype, decl_tpl[1]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   383
                                Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   384
                            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   385
                                Function_decl["inputs"] += [(decl_tpl)]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   386
                            
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   387
                            if funcdeclname_orig.startswith('*'):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   388
                                funcdeclin = intype + '_' + funcdeclname 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   389
                            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   390
                                funcdeclin = funcdeclname
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   391
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   392
                        funcdeclin = funcdeclname
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   393
                        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   394
                    for outype in output_types:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   395
                        if outype != None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   396
                            decl_tpl = Function_decl["outputs"][0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   397
                            Function_decl["outputs"] = [ (decl_tpl[0] , outype,  decl_tpl[2])]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   398
                            if funcdeclname_orig.endswith('*'):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   399
                                funcdeclout =  funcdeclin + '_' + outype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   400
                            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   401
                                funcdeclout =  funcdeclin
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   402
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   403
                            funcdeclout =  funcdeclin
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   404
                        Function_decl["name"] = funcdeclout
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   405
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   406
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   407
                        fdecl = Function_decl
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   408
                        res = eval(Function_decl["python_eval_c_code_format"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   409
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   410
                        if res != None :
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   411
                            # create the copy of decl dict to be appended to section
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   412
                            Function_decl_copy = Function_decl.copy()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   413
                            Current_section["list"].append(Function_decl_copy)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   414
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   415
                raise "First function must be in a category"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   416
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   417
    return Standard_Functions_Decl
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   418
1313
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   419
std_decl = get_standard_funtions(csv_file_to_table(open(os.path.join(ScriptDirectory,"iec_std.csv"))))#, True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   420
1283
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   421
StdBlckLst.extend(std_decl)
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   422
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   423
# Dictionary to speedup block type fetching by name
1320
bb04c41cbee9 Used OrderedDict for keeping of standard function blocks library defined
Laurent Bessard
parents: 1313
diff changeset
   424
StdBlckDct = OrderedDict()
1283
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   425
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   426
for section in StdBlckLst:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   427
    for desc in section["list"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   428
        words = desc["comment"].split('"')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   429
        if len(words) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   430
            desc["comment"] = words[1]
1313
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   431
        desc["usage"] = ("\n (%s) => (%s)" % 
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   432
            (", ".join(["%s:%s" % (input[1], input[0]) 
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   433
                        for input in desc["inputs"]]),
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   434
             ", ".join(["%s:%s" % (output[1], output[0]) 
85c167bfff93 Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents: 1310
diff changeset
   435
                        for output in desc["outputs"]])))
1283
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   436
        BlkLst = StdBlckDct.setdefault(desc["name"],[])
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   437
        BlkLst.append((section["name"], desc))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   438
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   439
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   440
#                            Languages Keywords
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   441
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   442
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   443
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   444
# Keywords for Pou Declaration
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   445
POU_BLOCK_START_KEYWORDS = ["FUNCTION", "FUNCTION_BLOCK", "PROGRAM"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   446
POU_BLOCK_END_KEYWORDS = ["END_FUNCTION", "END_FUNCTION_BLOCK", "END_PROGRAM"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   447
POU_KEYWORDS = ["EN", "ENO", "F_EDGE", "R_EDGE"] + POU_BLOCK_START_KEYWORDS + POU_BLOCK_END_KEYWORDS
1283
f3cfe1ff917e More optimization attemps in type handling
Edouard Tisserant
parents: 1239
diff changeset
   448
for category in StdBlckLst:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   449
    for block in category["list"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   450
        if block["name"] not in POU_KEYWORDS:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   451
            POU_KEYWORDS.append(block["name"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   452
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   453
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   454
# Keywords for Type Declaration
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   455
TYPE_BLOCK_START_KEYWORDS = ["TYPE", "STRUCT"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   456
TYPE_BLOCK_END_KEYWORDS = ["END_TYPE", "END_STRUCT"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   457
TYPE_KEYWORDS = ["ARRAY", "OF", "T", "D", "TIME_OF_DAY", "DATE_AND_TIME"] + TYPE_BLOCK_START_KEYWORDS + TYPE_BLOCK_END_KEYWORDS
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   458
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   459
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   460
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   461
# Keywords for Variable Declaration
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   462
VAR_BLOCK_START_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", "VAR_EXTERNAL"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   463
VAR_BLOCK_END_KEYWORDS = ["END_VAR"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   464
VAR_KEYWORDS = ["AT", "CONSTANT", "RETAIN", "NON_RETAIN"] + VAR_BLOCK_START_KEYWORDS + VAR_BLOCK_END_KEYWORDS
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   465
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   466
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   467
# Keywords for Configuration Declaration
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   468
CONFIG_BLOCK_START_KEYWORDS = ["CONFIGURATION", "RESOURCE", "VAR_ACCESS", "VAR_CONFIG", "VAR_GLOBAL"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   469
CONFIG_BLOCK_END_KEYWORDS = ["END_CONFIGURATION", "END_RESOURCE", "END_VAR"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   470
CONFIG_KEYWORDS = ["ON", "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK"] + CONFIG_BLOCK_START_KEYWORDS + CONFIG_BLOCK_END_KEYWORDS
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   471
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   472
# Keywords for Structured Function Chart
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   473
SFC_BLOCK_START_KEYWORDS = ["ACTION", "INITIAL_STEP", "STEP", "TRANSITION"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   474
SFC_BLOCK_END_KEYWORDS = ["END_ACTION", "END_STEP", "END_TRANSITION"]
949
aa24cf3b7009 Fixed SFC keyword definition
Laurent Bessard
parents: 814
diff changeset
   475
SFC_KEYWORDS = ["FROM", "TO"] + SFC_BLOCK_START_KEYWORDS + SFC_BLOCK_END_KEYWORDS
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   476
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   477
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   478
# Keywords for Instruction List
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   479
IL_KEYWORDS = ["TRUE", "FALSE", "LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   480
 "XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   481
 "LE", "LT", "JMP", "JMPC", "JMPCN", "CAL", "CALC", "CALCN", "RET", "RETC", "RETCN"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   482
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   483
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   484
# Keywords for Structured Text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   485
ST_BLOCK_START_KEYWORDS = ["IF", "ELSIF", "ELSE", "CASE", "FOR", "WHILE", "REPEAT"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   486
ST_BLOCK_END_KEYWORDS = ["END_IF", "END_CASE", "END_FOR", "END_WHILE", "END_REPEAT"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   487
ST_KEYWORDS = ["TRUE", "FALSE", "THEN", "OF", "TO", "BY", "DO", "DO", "UNTIL", "EXIT", 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   488
 "RETURN", "NOT", "MOD", "AND", "XOR", "OR"] + ST_BLOCK_START_KEYWORDS + ST_BLOCK_END_KEYWORDS
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   489
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   490
# All the keywords of IEC
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   491
IEC_BLOCK_START_KEYWORDS = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   492
IEC_BLOCK_END_KEYWORDS = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   493
IEC_KEYWORDS = ["E", "TRUE", "FALSE"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   494
for all_keywords, keywords_list in [(IEC_BLOCK_START_KEYWORDS, [POU_BLOCK_START_KEYWORDS, TYPE_BLOCK_START_KEYWORDS,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   495
                                                                VAR_BLOCK_START_KEYWORDS, CONFIG_BLOCK_START_KEYWORDS,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   496
                                                                SFC_BLOCK_START_KEYWORDS, ST_BLOCK_START_KEYWORDS]),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   497
                                    (IEC_BLOCK_END_KEYWORDS, [POU_BLOCK_END_KEYWORDS, TYPE_BLOCK_END_KEYWORDS,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   498
                                                              VAR_BLOCK_END_KEYWORDS, CONFIG_BLOCK_END_KEYWORDS,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   499
                                                              SFC_BLOCK_END_KEYWORDS, ST_BLOCK_END_KEYWORDS]),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   500
                                    (IEC_KEYWORDS, [POU_KEYWORDS, TYPE_KEYWORDS, VAR_KEYWORDS, CONFIG_KEYWORDS,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   501
                                                    SFC_KEYWORDS, IL_KEYWORDS, ST_KEYWORDS])]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   502
    for keywords in keywords_list:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   503
        all_keywords.extend([keyword for keyword in keywords if keyword not in all_keywords])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   504