author | lbessard |
Thu, 21 Jun 2007 10:23:26 +0200 | |
changeset 23 | cce8d5662738 |
parent 22 | a765fae3b361 |
child 25 | 8dc68e669d99 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
4 |
import string, os, sys |
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
5 |
|
0 | 6 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
7 |
#based on the plcopen standard. |
|
8 |
# |
|
9 |
#Copyright (C): Edouard TISSERANT and Laurent BESSARD |
|
10 |
# |
|
11 |
#See COPYING file for copyrights details. |
|
12 |
# |
|
13 |
#This library is free software; you can redistribute it and/or |
|
5 | 14 |
#modify it under the terms of the GNU General Public |
0 | 15 |
#License as published by the Free Software Foundation; either |
16 |
#version 2.1 of the License, or (at your option) any later version. |
|
17 |
# |
|
18 |
#This library is distributed in the hope that it will be useful, |
|
19 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
21 |
#Lesser General Public License for more details. |
|
22 |
# |
|
5 | 23 |
#You should have received a copy of the GNU General Public |
0 | 24 |
#License along with this library; if not, write to the Free Software |
25 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
26 |
||
27 |
||
9 | 28 |
LANGUAGES = ["IL","ST","FBD","LD","SFC"] |
29 |
||
21 | 30 |
|
0 | 31 |
#------------------------------------------------------------------------------- |
32 |
# Function Block Types definitions |
|
33 |
#------------------------------------------------------------------------------- |
|
34 |
||
21 | 35 |
|
0 | 36 |
""" |
37 |
Ordored list of common Function Blocks defined in the IEC 61131-3 |
|
38 |
Each block have this attributes: |
|
39 |
- "name" : The block name |
|
40 |
- "type" : The block type. It can be "function", "functionBlock" or "program" |
|
41 |
- "extensible" : Boolean that define if the block is extensible |
|
42 |
- "inputs" : List of the block inputs |
|
43 |
- "outputs" : List of the block outputs |
|
44 |
- "comment" : Comment that will be displayed in the block popup |
|
45 |
Inputs and outputs are a tuple of characteristics that are in order: |
|
46 |
- The name |
|
47 |
- The data type |
|
48 |
- The default modifier which can be "none", "negated", "rising" or "falling" |
|
49 |
""" |
|
50 |
||
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
51 |
BlockTypes = [{"name" : "Standard function blocks", "list": |
0 | 52 |
[{"name" : "SR", "type" : "functionBlock", "extensible" : False, |
53 |
"inputs" : [("S1","BOOL","none"),("R","BOOL","none")], |
|
54 |
"outputs" : [("Q1","BOOL","none")], |
|
55 |
"comment" : "SR bistable\nThe SR bistable is a latch where the Set dominates."}, |
|
56 |
{"name" : "RS", "type" : "functionBlock", "extensible" : False, |
|
57 |
"inputs" : [("S","BOOL","none"),("R1","BOOL","none")], |
|
58 |
"outputs" : [("Q1","BOOL","none")], |
|
59 |
"comment" : "RS bistable\nThe RS bistable is a latch where the Reset dominates."}, |
|
60 |
{"name" : "SEMA", "type" : "functionBlock", "extensible" : False, |
|
61 |
"inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], |
|
62 |
"outputs" : [("BUSY","BOOL","none")], |
|
63 |
"comment" : "Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."}, |
|
64 |
{"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, |
|
65 |
"inputs" : [("CLK","BOOL","none")], |
|
66 |
"outputs" : [("Q","BOOL","none")], |
|
67 |
"comment" : "Rising edge detector\nThe output produces a single pulse when a rising edge is detected."}, |
|
68 |
{"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, |
|
69 |
"inputs" : [("CLK","BOOL","none")], |
|
70 |
"outputs" : [("Q","BOOL","none")], |
|
71 |
"comment" : "Falling edge detector\nThe output produces a single pulse when a falling edge is detected."}, |
|
72 |
{"name" : "CTU", "type" : "functionBlock", "extensible" : False, |
|
73 |
"inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], |
|
74 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
75 |
"comment" : "Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."}, |
|
76 |
{"name" : "CTD", "type" : "functionBlock", "extensible" : False, |
|
77 |
"inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], |
|
78 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
79 |
"comment" : "Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."}, |
|
80 |
{"name" : "CTUD", "type" : "functionBlock", "extensible" : False, |
|
81 |
"inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], |
|
82 |
"outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")], |
|
83 |
"comment" : "Up-down counter\nThe up-down counter has two inputs CU and CD. It can be used to both count up on one input ans down on the other."}, |
|
84 |
{"name" : "TP", "type" : "functionBlock", "extensible" : False, |
|
85 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
86 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
87 |
"comment" : "Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."}, |
|
88 |
{"name" : "TOF", "type" : "functionBlock", "extensible" : False, |
|
89 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
90 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
91 |
"comment" : "On-delay timer\nThe on-delay timer can be used to delay setting an output true, for fixed period after an input becomes true."}, |
|
92 |
{"name" : "TON", "type" : "functionBlock", "extensible" : False, |
|
93 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
94 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
95 |
"comment" : "Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."}, |
|
96 |
{"name" : "RTC", "type" : "functionBlock", "extensible" : False, |
|
97 |
"inputs" : [("EN","BOOL","none"),("PDT","DATE_AND_TIME","none")], |
|
98 |
"outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")], |
|
99 |
"comment" : "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."}, |
|
100 |
{"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, |
|
101 |
"inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], |
|
102 |
"outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")], |
|
103 |
"comment" : "Integral\nThe integral function block integrates the value of input XIN over time."}, |
|
104 |
{"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, |
|
105 |
"inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], |
|
106 |
"outputs" : [("XOUT","REAL","none")], |
|
107 |
"comment" : "Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."}, |
|
108 |
{"name" : "PID", "type" : "functionBlock", "extensible" : False, |
|
109 |
"inputs" : [("AUTO","BOOL","none"),("PV","REAL","none"),("SP","REAL","none"),("X0","REAL","none"),("KP","REAL","none"),("TR","REAL","none"),("TD","REAL","none"),("CYCLE","TIME","none")], |
|
110 |
"outputs" : [("XOUT","REAL","none")], |
|
111 |
"comment" : "PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."}, |
|
112 |
{"name" : "RAMP", "type" : "functionBlock", "extensible" : False, |
|
113 |
"inputs" : [("RUN","BOOL","none"),("X0","REAL","none"),("X1","REAL","none"),("TR","TIME","none"),("CYCLE","TIME","none"),("HOLDBACK","BOOL","none"),("ERROR","REAL","none"),("PV","REAL","none")], |
|
114 |
"outputs" : [("RAMP","BOOL","none"),("XOUT","REAL","none")], |
|
115 |
"comment" : "Ramp\nThe RAMP function block is modelled on example given in the standard but with the addition of a 'Holdback' feature."}, |
|
116 |
{"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, |
|
117 |
"inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], |
|
118 |
"outputs" : [("Q","BOOL","none")], |
|
119 |
"comment" : "Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."}, |
|
120 |
{"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, |
|
121 |
"inputs" : [("PV1","REAL","none"),("PV2","REAL","none"),("RATIO","REAL","none"),("TIMON","TIME","none"),("TIMOFF","TIME","none"),("TOLERANCE","BOOL","none"),("RESET","BOOL","none"),("CYCLE","TIME","none")], |
|
122 |
"outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")], |
|
123 |
"comment" : "Ratio monitor\nThe ratio_monitor function block checks that one process value PV1 is always a given ratio (defined by input RATIO) of a second process value PV2."}, |
|
124 |
]} |
|
125 |
] |
|
126 |
||
127 |
""" |
|
128 |
Function that returns the block definition associated to the block type given |
|
129 |
""" |
|
130 |
||
131 |
def GetBlockType(type): |
|
132 |
for category in BlockTypes: |
|
133 |
for blocktype in category["list"]: |
|
134 |
if blocktype["name"] == type: |
|
135 |
return blocktype |
|
136 |
return None |
|
137 |
||
138 |
||
139 |
#------------------------------------------------------------------------------- |
|
140 |
# Data Types definitions |
|
141 |
#------------------------------------------------------------------------------- |
|
142 |
||
143 |
""" |
|
144 |
Ordored list of common data types defined in the IEC 61131-3 |
|
145 |
Each type is associated to his direct parent type. It defines then a hierarchy |
|
146 |
between type that permits to make a comparison of two types |
|
147 |
""" |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
148 |
TypeHierarchy_list = [ |
22 | 149 |
("ANY", None), |
150 |
("ANY_DERIVED", "ANY"), |
|
151 |
("ANY_ELEMENTARY", "ANY"), |
|
152 |
("ANY_MAGNITUDE", "ANY_ELEMENTARY"), |
|
153 |
("ANY_BIT", "ANY_ELEMENTARY"), |
|
154 |
("ANY_STRING", "ANY_ELEMENTARY"), |
|
155 |
("ANY_DATE", "ANY_ELEMENTARY"), |
|
156 |
("ANY_NUM", "ANY_MAGNITUDE"), |
|
157 |
("ANY_REAL", "ANY_NUM"), |
|
158 |
("ANY_INT", "ANY_NUM"), |
|
159 |
("REAL", "ANY_REAL"), |
|
160 |
("LREAL", "ANY_REAL"), |
|
161 |
("SINT", "ANY_INT"), |
|
162 |
("INT", "ANY_INT"), |
|
163 |
("DINT", "ANY_INT"), |
|
164 |
("LINT", "ANY_INT"), |
|
165 |
("USINT", "ANY_INT"), |
|
166 |
("UINT", "ANY_INT"), |
|
167 |
("UDINT", "ANY_INT"), |
|
168 |
("ULINT", "ANY_INT"), |
|
169 |
("TIME", "ANY_MAGNITUDE"), |
|
170 |
("BOOL", "ANY_BIT"), |
|
171 |
("BYTE", "ANY_BIT"), |
|
172 |
("WORD", "ANY_BIT"), |
|
173 |
("DWORD", "ANY_BIT"), |
|
174 |
("LWORD", "ANY_BIT"), |
|
175 |
("STRING", "ANY_STRING"), |
|
176 |
("WSTRING", "ANY_STRING"), |
|
177 |
("DATE", "ANY_DATE"), |
|
178 |
("TOD", "ANY_DATE"), |
|
179 |
("DT", "ANY_DATE")] |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
180 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
181 |
TypeHierarchy = dict(TypeHierarchy_list) |
0 | 182 |
|
183 |
""" |
|
15 | 184 |
returns true if the given data type is the same that "reference" meta-type or one of its types. |
0 | 185 |
""" |
186 |
||
187 |
def IsOfType(test, reference): |
|
188 |
while test != None: |
|
189 |
if test == reference: |
|
190 |
return True |
|
191 |
test = TypeHierarchy[test] |
|
192 |
return False |
|
193 |
||
15 | 194 |
""" |
195 |
returns list of all types that correspont to the ANY* meta type |
|
196 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
197 |
def GetSubTypes(reference): |
22 | 198 |
return [ typename for typename, parenttype in TypeHierarchy_list if typename[:3] != "ANY" and IsOfType(typename, reference)] |
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
199 |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
200 |
|
21 | 201 |
#------------------------------------------------------------------------------- |
202 |
# Test identifier |
|
203 |
#------------------------------------------------------------------------------- |
|
204 |
||
205 |
||
206 |
||
207 |
# Test if identifier is valid |
|
208 |
def TestIdentifier(identifier): |
|
209 |
if identifier[0].isdigit(): |
|
210 |
return False |
|
211 |
words = identifier.split('_') |
|
212 |
for i, word in enumerate(words): |
|
213 |
if len(word) == 0 and i != 0: |
|
214 |
return False |
|
215 |
if len(word) != 0 and not word.isalnum(): |
|
216 |
return False |
|
217 |
return True |
|
218 |
||
219 |
||
220 |
#------------------------------------------------------------------------------- |
|
221 |
# Languages Keywords |
|
222 |
#------------------------------------------------------------------------------- |
|
223 |
||
224 |
||
225 |
# Keywords for Pou Declaration |
|
226 |
POU_KEYWORDS = ["FUNCTION", "END_FUNCTION", "FUNCTION_BLOCK", "END_FUNCTION_BLOCK", |
|
227 |
"PROGRAM", "END_PROGRAM", "EN", "ENO", "F_EDGE", "R_EDGE"] |
|
228 |
for category in BlockTypes: |
|
229 |
for block in category["list"]: |
|
230 |
if block["name"] not in POU_KEYWORDS: |
|
231 |
POU_KEYWORDS.append(block["name"]) |
|
232 |
||
233 |
||
234 |
# Keywords for Type Declaration |
|
235 |
TYPE_KEYWORDS = ["TYPE", "END_TYPE", "STRUCT", "END_STRUCT", "ARRAY", "OF", "T", |
|
236 |
"D", "TIME_OF_DAY", "DATE_AND_TIME"] |
|
237 |
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS]) |
|
238 |
||
239 |
||
240 |
# Keywords for Variable Declaration |
|
241 |
VAR_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", |
|
242 |
"VAR_EXTERNAL", "END_VAR", "AT", "CONSTANT", "RETAIN", "NON_RETAIN"] |
|
243 |
||
244 |
||
245 |
# Keywords for Configuration Declaration |
|
246 |
CONFIG_KEYWORDS = ["CONFIGURATION", "END_CONFIGURATION", "RESOURCE", "ON", "END_RESOURCE", |
|
247 |
"PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK", "VAR_ACCESS", "VAR_CONFIG", |
|
248 |
"VAR_GLOBAL", "END_VAR"] |
|
249 |
||
250 |
||
251 |
# Keywords for Structured Function Chart |
|
252 |
SFC_KEYWORDS = ["ACTION", "END_ACTION", "INITIAL_STEP", "STEP", "END_STEP", "TRANSITION", |
|
253 |
"FROM", "TO", "END_TRANSITION"] |
|
254 |
||
255 |
||
256 |
# Keywords for Instruction List |
|
257 |
IL_KEYWORDS = ["LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN", |
|
258 |
"XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE", |
|
259 |
"LE", "LT", "JMP", "JMPC", "JMPNC", "CAL", "CALC", "CALNC", "RET", "RETC", "RETNC"] |
|
260 |
||
261 |
||
262 |
# Keywords for Instruction List and Structured Text |
|
263 |
ST_KEYWORDS = ["IF", "THEN", "ELSIF", "ELSE", "END_IF", "CASE", "OF", "END_CASE", |
|
264 |
"FOR", "TO", "BY", "DO", "END_FOR", "WHILE", "DO", "END_WHILE", "REPEAT", "UNTIL", |
|
265 |
"END_REPEAT", "EXIT", "RETURN", "NOT", "MOD", "AND", "XOR", "OR"] |
|
266 |
||
267 |
||
268 |
# All the keywords of IEC |
|
269 |
IEC_KEYWORDS = ["E", "TRUE", "FALSE"] |
|
270 |
IEC_KEYWORDS.extend([keyword for keyword in POU_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
271 |
IEC_KEYWORDS.extend([keyword for keyword in TYPE_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
272 |
IEC_KEYWORDS.extend([keyword for keyword in VAR_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
273 |
IEC_KEYWORDS.extend([keyword for keyword in CONFIG_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
274 |
IEC_KEYWORDS.extend([keyword for keyword in SFC_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
275 |
IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
276 |
IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
277 |
||
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
278 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
279 |
|
15 | 280 |
""" |
281 |
take a .csv file and translate it it a "csv_table" |
|
282 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
283 |
def csv_file_to_table(file): |
22 | 284 |
return [ map(string.strip,line.split(';')) for line in file.xreadlines()] |
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
285 |
|
15 | 286 |
""" |
287 |
seek into the csv table to a section ( section_name match 1st field ) |
|
288 |
return the matching row without first field |
|
289 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
290 |
def find_section(section_name, table): |
22 | 291 |
fields = [None] |
292 |
while(fields[0] != section_name): |
|
293 |
fields = table.pop(0) |
|
294 |
return fields[1:] |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
295 |
|
15 | 296 |
""" |
297 |
extract the standard functions standard parameter names and types... |
|
298 |
return a { ParameterName: Type, ...} |
|
299 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
300 |
def get_standard_funtions_input_variables(table): |
22 | 301 |
variables = find_section("Standard_functions_variables_types", table) |
302 |
standard_funtions_input_variables = {} |
|
303 |
fields = [True,True] |
|
304 |
while(fields[1]): |
|
305 |
fields = table.pop(0) |
|
306 |
variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!='']) |
|
307 |
standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] |
|
308 |
return standard_funtions_input_variables |
|
309 |
||
15 | 310 |
""" |
311 |
translate .csv file input declaration into PLCOpenEditor interessting values |
|
312 |
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} |
|
313 |
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] |
|
314 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
315 |
def csv_input_translate(str_decl, variables, base): |
22 | 316 |
decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',') |
317 |
params = [] |
|
318 |
||
319 |
len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables]) |
|
320 |
||
321 |
for param_type in decl: |
|
322 |
if param_type in variables.keys(): |
|
323 |
param_name = param_type |
|
324 |
param_type = variables[param_type] |
|
325 |
elif len_of_not_predifined_variable > 1: |
|
326 |
param_name = "IN%d"%base |
|
327 |
base += 1 |
|
328 |
else: |
|
329 |
param_name = "IN" |
|
330 |
params.append((param_name, param_type, "none")) |
|
331 |
return params |
|
332 |
||
333 |
||
334 |
ANY_T0_ANY_LIST=[ |
|
335 |
(("ANY_NUM","ANY_BIT"),("ANY_NUM","ANY_BIT"), "(%(return_type)s)%(IN_value)s"), |
|
336 |
(("ANY_NUM","ANY_BIT"),("ANY_DATE","TIME"), "(%(return_type)s)real_to_time(%(IN_value)s)"), |
|
337 |
(("ANY_DATE","TIME"), ("ANY_NUM","ANY_BIT"), "(%(return_type)s)time_to_real(%(IN_value)s)"), |
|
338 |
(("ANY_DATE","TIME"), ("ANY_STRING",), "(%(return_type)s)time_to_string(%(IN_value)s)"), |
|
339 |
(("ANY_STRING",), ("ANY_DATE","TIME"), "(%(return_type)s)string_to_time(%(IN_value)s)"), |
|
340 |
(("ANY_BIT",), ("ANY_STRING",), "(%(return_type)s)int_to_string(%(IN_value)s, 16)"), |
|
341 |
(("ANY_NUM",), ("ANY_STRING",), "(%(return_type)s)int_to_string(%(IN_value)s, 10)"), |
|
342 |
(("ANY_STRING",), ("ANY_BIT",), "(%(return_type)s)string_to_int(%(IN_value)s, 16)"), |
|
343 |
(("ANY_STRING",), ("ANY_NUM",), "(%(return_type)s)string_to_int(%(IN_value)s, 10)")] |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
344 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
345 |
def ANY_TO_ANY_FORMAT_GEN(fdecl): |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
346 |
|
22 | 347 |
for (InTypes, OutTypes, Format) in ANY_T0_ANY_LIST: |
348 |
outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes)) |
|
349 |
inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes)) |
|
350 |
if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]: |
|
351 |
return Format |
|
352 |
||
353 |
return None |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
354 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
355 |
|
15 | 356 |
""" |
357 |
Returns this kind of declaration for all standard functions |
|
358 |
||
359 |
[{"name" : "Numerical", 'list': [ { |
|
360 |
'baseinputnumber': 1, |
|
361 |
'comment': 'Addition', |
|
362 |
'extensible': True, |
|
363 |
'inputs': [ ('IN1', 'ANY_NUM', 'none'), |
|
364 |
('IN2', 'ANY_NUM', 'none')], |
|
365 |
'name': 'ADD', |
|
366 |
'outputs': [('OUT', 'ANY_NUM', 'none')], |
|
367 |
'type': 'function'}, ...... ] },.....] |
|
368 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
369 |
def get_standard_funtions(table): |
22 | 370 |
|
371 |
variables = get_standard_funtions_input_variables(table) |
|
372 |
||
373 |
fonctions = find_section("Standard_functions_type",table) |
|
374 |
||
375 |
Standard_Functions_Decl = [] |
|
376 |
Current_section = None |
|
377 |
||
378 |
translate = { |
|
379 |
"extensible" : lambda x: {"yes":True, "no":False}[x], |
|
380 |
"inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber), |
|
381 |
"outputs":lambda x:[("OUT",x,"none")]} |
|
382 |
||
383 |
for fields in table: |
|
384 |
if fields[1]: |
|
385 |
# If function section name given |
|
386 |
if fields[0]: |
|
387 |
Current_section = {"name" : fields[0], "list" : []} |
|
388 |
Standard_Functions_Decl.append(Current_section) |
|
389 |
Function_decl_list = [] |
|
390 |
if Current_section: |
|
391 |
Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ]) |
|
392 |
baseinputnumber = int(Function_decl.get("baseinputnumber",1)) |
|
393 |
Function_decl["baseinputnumber"] = baseinputnumber |
|
394 |
for param, value in Function_decl.iteritems(): |
|
395 |
if param in translate: |
|
396 |
Function_decl[param] = translate[param](value) |
|
397 |
Function_decl["type"] = "function" |
|
398 |
||
399 |
if Function_decl["name"].startswith('*') : |
|
400 |
input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) |
|
401 |
else: |
|
402 |
input_ovrloading_types = [None] |
|
403 |
||
404 |
if Function_decl["name"].endswith('*') : |
|
405 |
output_types = GetSubTypes(Function_decl["outputs"][0][1]) |
|
406 |
else: |
|
407 |
output_types = [None] |
|
408 |
||
409 |
funcdeclname_orig = Function_decl["name"] |
|
410 |
funcdeclname = Function_decl["name"].strip('*_') |
|
411 |
fdc = Function_decl["inputs"][:] |
|
412 |
for intype in input_ovrloading_types: |
|
413 |
if intype != None: |
|
414 |
Function_decl["inputs"] = [] |
|
415 |
for decl_tpl in fdc: |
|
416 |
if IsOfType(intype, decl_tpl[1]): |
|
417 |
Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])] |
|
418 |
else: |
|
419 |
Function_decl["inputs"] += [(decl_tpl)] |
|
420 |
||
421 |
if funcdeclname_orig.startswith('*'): |
|
422 |
funcdeclin = intype + '_' + funcdeclname |
|
423 |
else: |
|
424 |
funcdeclin = funcdeclname |
|
425 |
else: |
|
426 |
funcdeclin = funcdeclname |
|
427 |
||
428 |
for outype in output_types: |
|
429 |
if outype != None: |
|
430 |
decl_tpl = Function_decl["outputs"][0] |
|
431 |
Function_decl["outputs"] = [ (decl_tpl[0] , outype, decl_tpl[2])] |
|
432 |
if funcdeclname_orig.endswith('*'): |
|
433 |
funcdeclout = funcdeclin + '_' + outype |
|
434 |
else: |
|
435 |
funcdeclout = funcdeclin |
|
436 |
else: |
|
437 |
funcdeclout = funcdeclin |
|
438 |
Function_decl["name"] = funcdeclout |
|
439 |
||
440 |
||
441 |
fdecl = Function_decl |
|
442 |
res = eval(Function_decl["python_eval_c_code_format"]) |
|
443 |
||
444 |
if res != None : |
|
445 |
# create the copy of decl dict to be appended to section |
|
446 |
Function_decl_copy = Function_decl.copy() |
|
447 |
# Have to generate type description in comment with freshly redefined types |
|
448 |
Function_decl_copy["comment"] += ( |
|
449 |
"\n (" + |
|
450 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') + |
|
451 |
" ) => (" + |
|
452 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') + |
|
453 |
" )") |
|
454 |
Current_section["list"].append(Function_decl_copy) |
|
455 |
#pp.pprint(Function_decl_copy) |
|
456 |
else: |
|
457 |
raise "First function must be in a category" |
|
458 |
||
459 |
return Standard_Functions_Decl |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
460 |
|
0 | 461 |
|
15 | 462 |
if __name__ == '__main__': |
22 | 463 |
|
464 |
import pprint |
|
465 |
pp = pprint.PrettyPrinter(indent=4) |
|
466 |
||
467 |
def ANY_to_compiler_test_type_GEN(typename, paramname): |
|
468 |
return {"ANY" : "", |
|
469 |
"ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_type_symbol))", |
|
470 |
"ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))", |
|
471 |
"ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))", |
|
472 |
"ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))" |
|
473 |
}.get(typename, |
|
474 |
"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{ |
|
475 |
"paramname" : paramname, "typename": typename.lower()} |
|
476 |
||
477 |
def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False): |
|
478 |
if type(fdecls) != type(tuple()): |
|
479 |
res = "" |
|
480 |
for Paramname, ParamTypes in fdecls.iteritems(): |
|
481 |
if do_il: |
|
482 |
res += """ |
|
23 | 483 |
{""" |
484 |
if not do_type_search_only: |
|
485 |
res += """ |
|
22 | 486 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
487 |
symbol_c *%(input_name)s_param_value = &this->default_variable_name; |
|
23 | 488 |
"""%{"input_name":Paramname} |
489 |
res += """ |
|
490 |
symbol_c *%(input_name)s_type_symbol = param_data_type; |
|
22 | 491 |
last_type_symbol = param_data_type; |
492 |
"""%{"input_name":Paramname} |
|
493 |
else: |
|
494 |
res += """ |
|
495 |
{ |
|
496 |
identifier_c param_name("%(input_name)s"); |
|
497 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
498 |
symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(¶m_name); |
|
499 |
||
500 |
/* Get the value from a foo(<param_value>) style call */ |
|
501 |
if (%(input_name)s_param_value == NULL) |
|
502 |
%(input_name)s_param_value = function_call_param_iterator.next(); |
|
503 |
symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value); |
|
504 |
last_type_symbol = last_type_symbol && search_expression_type->is_same_type(%(input_name)s_type_symbol, last_type_symbol) ? search_expression_type->common_type(%(input_name)s_type_symbol, last_type_symbol) : %(input_name)s_type_symbol ; |
|
505 |
"""%{"input_name":Paramname} |
|
506 |
||
507 |
for ParamType,NextParamDecl in ParamTypes.iteritems(): |
|
508 |
||
509 |
res += """ |
|
510 |
%(type_test)s |
|
511 |
{ |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
512 |
%(if_good_type_code)s |
22 | 513 |
} |
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
514 |
"""%{ |
22 | 515 |
"type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), |
516 |
"if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n ')} |
|
517 |
||
518 |
res += """ |
|
519 |
ERROR; |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
520 |
} |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
521 |
""" |
22 | 522 |
|
523 |
return res.replace('\n','\n'+indent) |
|
524 |
else: |
|
525 |
res = "\n" |
|
526 |
fdecl=fdecls[0] |
|
527 |
||
528 |
result_type_rule = fdecl["return_type_rule"] |
|
529 |
res += { |
|
530 |
"copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n", |
|
531 |
"defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(), |
|
532 |
}.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule) |
|
533 |
||
534 |
if not do_type_search_only: |
|
535 |
code_gen = eval(fdecl["python_eval_c_code_format"]) |
|
536 |
||
537 |
code_gen_dic_decl = {} |
|
538 |
for paramname,paramtype,unused in fdecl["inputs"]: |
|
539 |
code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname) |
|
540 |
code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname) |
|
541 |
code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("' |
|
542 |
code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("' |
|
543 |
code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n s4o.print("(' |
|
544 |
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("' |
|
21 | 545 |
|
22 | 546 |
if type(code_gen) == type(tuple()): |
547 |
res += 's4o.print("%s");\n'%(code_gen[0]%code_gen_dic_decl) |
|
548 |
static_param_accept_list = [] |
|
549 |
for paramname,paramtype,unused in fdecl["inputs"]: |
|
550 |
static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname)) |
|
551 |
res += ('s4o.print("%s");\n'%(code_gen[1])).join(static_param_accept_list) |
|
552 |
code = 's4o.print("%s");\nparam_value->accept(*this);\n'%(code_gen[1]) |
|
553 |
end_code = 's4o.print("%s");\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl) |
|
554 |
else: |
|
555 |
code = '' |
|
556 |
end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','') |
|
557 |
||
558 |
if fdecl["extensible"]: |
|
559 |
res += (""" |
|
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
560 |
int base_num = %d; |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
561 |
symbol_c *param_value = NULL; |
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
562 |
do{ |
22 | 563 |
char my_name[10]; |
564 |
sprintf(my_name, "IN%%d", base_num++); |
|
565 |
identifier_c param_name(my_name); |
|
566 |
||
567 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
568 |
param_value = function_call_param_iterator.search(¶m_name); |
|
569 |
||
570 |
/* Get the value from a foo(<param_value>) style call */ |
|
571 |
if (param_value == NULL) |
|
572 |
param_value = function_call_param_iterator.next(); |
|
573 |
if (param_value != NULL){ |
|
574 |
symbol_c *current_type_symbol = search_expression_type->get_type(param_value); |
|
575 |
last_type_symbol = last_type_symbol && search_expression_type->is_same_type(current_type_symbol, last_type_symbol) ? search_expression_type->common_type(current_type_symbol, last_type_symbol) : current_type_symbol ; |
|
576 |
||
577 |
/*Function specific CODE */ |
|
578 |
%s |
|
579 |
} |
|
580 |
||
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
581 |
}while(param_value != NULL); |
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
582 |
%s |
22 | 583 |
"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n '), end_code)) |
584 |
else: |
|
585 |
#res += code + end_code |
|
586 |
res += end_code |
|
587 |
else: |
|
588 |
res += "return return_type_symbol;\n" |
|
589 |
||
590 |
||
591 |
return res.replace('\n','\n'+indent) |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
592 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
593 |
################################################################### |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
594 |
### ### |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
595 |
### MAIN ### |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
596 |
### ### |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
597 |
################################################################### |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
598 |
|
22 | 599 |
# Get definitions |
600 |
std_decl = get_standard_funtions(csv_file_to_table(open("iec_std.csv")))#, True) |
|
601 |
||
602 |
# Reorganize into a dict of dict, according |
|
603 |
# fname : paramname : paramtype : paraname : paramtype... |
|
604 |
# Keep ptrack of original order in a separated list |
|
605 |
std_fdecls = {} |
|
606 |
official_order = [] |
|
607 |
for section in std_decl: |
|
608 |
for fdecl in section["list"]: |
|
609 |
if len(official_order)==0 or official_order[-1] != fdecl["name"]: |
|
610 |
official_order.append(fdecl["name"]) |
|
611 |
# store all func by name in a dict |
|
612 |
std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {}) |
|
613 |
current = std_fdecls_fdecl_name |
|
614 |
for i in fdecl["inputs"]: |
|
615 |
current[i[0]] = current.get(i[0], {}) |
|
616 |
current = current[i[0]] |
|
617 |
last = current |
|
618 |
current[i[1]] = current.get(i[1], {}) |
|
619 |
current = current[i[1]] |
|
620 |
last[i[1]]=(fdecl,) |
|
621 |
std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name |
|
622 |
||
623 |
# Generate the long enumeration of std function types |
|
624 |
function_type_decl = """ |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
625 |
/**** |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
626 |
* IEC 61131-3 standard function lib |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
627 |
* generated code, do not edit by hand |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
628 |
*/ |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
629 |
typedef enum { |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
630 |
""" |
22 | 631 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
632 |
function_type_decl += " function_"+fname.lower()+",\n" |
|
633 |
||
634 |
function_type_decl += """ function_none |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
635 |
} function_type_t; |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
636 |
""" |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
637 |
|
22 | 638 |
# Generate the funct thaat return enumerated according function name |
639 |
get_function_type_decl = """ |
|
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
640 |
/**** |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
641 |
* IEC 61131-3 standard function lib |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
642 |
* generated code, do not edit by hand |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
643 |
*/ |
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
644 |
function_type_t get_function_type(identifier_c *function_name) { |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
645 |
""" |
22 | 646 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
647 |
get_function_type_decl += """ |
|
648 |
if (!strcasecmp(function_name->value, "%s")) |
|
649 |
return function_%s; |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
650 |
"""%(fname,fname.lower()) |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
651 |
|
22 | 652 |
get_function_type_decl += """ |
653 |
else return function_none; |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
654 |
} |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
655 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
656 |
""" |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
657 |
|
22 | 658 |
# Generate the part of generate_cc_st_c::visit(function_invocation) |
659 |
# that is responsible to generate C code for std lib calls. |
|
660 |
st_code_gen = """ |
|
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
661 |
/**** |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
662 |
* IEC 61131-3 standard function lib |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
663 |
* generated code, do not edit by hand |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
664 |
*/ |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
665 |
switch(current_function_type){ |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
666 |
""" |
22 | 667 |
|
668 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
669 |
st_code_gen += """ |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
670 |
/**** |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
671 |
*%s |
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
672 |
*/ |
22 | 673 |
case function_%s : |
674 |
{ |
|
675 |
symbol_c *last_type_symbol = NULL; |
|
676 |
""" %(fname, fname.lower()) |
|
677 |
indent = " " |
|
678 |
||
679 |
st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n ') |
|
680 |
||
681 |
st_code_gen += """ |
|
682 |
}/*function_%s*/ |
|
683 |
break; |
|
684 |
""" %(fname.lower()) |
|
685 |
st_code_gen += """ |
|
686 |
case function_none : |
|
687 |
ERROR; |
|
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
688 |
} |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
689 |
return NULL; |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
690 |
""" |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
691 |
|
22 | 692 |
# Generate the part of generate_cc_il_c::visit(il_function_call) |
693 |
# that is responsible to generate C code for std lib calls. |
|
694 |
il_code_gen = """ |
|
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
695 |
/**** |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
696 |
* IEC 61131-3 standard function lib |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
697 |
* generated code, do not edit by hand |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
698 |
*/ |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
699 |
switch(current_function_type){ |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
700 |
""" |
22 | 701 |
|
702 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
703 |
il_code_gen += """ |
|
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
704 |
/**** |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
705 |
*%s |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
706 |
*/ |
22 | 707 |
case function_%s : |
708 |
{ |
|
709 |
symbol_c *last_type_symbol = NULL; |
|
710 |
""" %(fname, fname.lower()) |
|
711 |
indent = " " |
|
712 |
||
23 | 713 |
il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n ') |
22 | 714 |
|
715 |
il_code_gen += """ |
|
716 |
}/*function_%s*/ |
|
717 |
break; |
|
718 |
""" %(fname.lower()) |
|
23 | 719 |
il_code_gen += """ |
22 | 720 |
case function_none : |
721 |
ERROR; |
|
19
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
722 |
} |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
723 |
return NULL; |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
724 |
""" |
0b499416ebd7
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
18
diff
changeset
|
725 |
|
22 | 726 |
# Generate the part of search_expression_type_c::visit(function_invocation) |
727 |
# that is responsible of returning type symbol for function invocation. |
|
728 |
search_type_code = """ |
|
729 |
/**** |
|
730 |
* IEC 61131-3 standard function lib |
|
731 |
* generated code, do not edit by hand |
|
732 |
*/ |
|
23 | 733 |
|
734 |
void *compute_standard_function_st(function_invocation_c *symbol) { |
|
735 |
||
736 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
737 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
738 |
search_expression_type_c* search_expression_type = this; |
|
739 |
||
740 |
switch(current_function_type){ |
|
22 | 741 |
""" |
742 |
||
743 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
744 |
search_type_code += """ |
|
745 |
/**** |
|
746 |
*%s |
|
747 |
*/ |
|
748 |
case function_%s : |
|
749 |
{ |
|
750 |
symbol_c *last_type_symbol = NULL; |
|
751 |
""" %(fname, fname.lower()) |
|
752 |
indent = " " |
|
753 |
||
754 |
search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n ') |
|
755 |
||
756 |
search_type_code += """ |
|
757 |
}/*function_%s*/ |
|
758 |
break; |
|
759 |
""" %(fname.lower()) |
|
760 |
search_type_code += """ |
|
761 |
case function_none : |
|
762 |
ERROR; |
|
23 | 763 |
} |
764 |
return NULL; |
|
22 | 765 |
} |
23 | 766 |
|
767 |
void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) { |
|
768 |
||
769 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
770 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
771 |
search_expression_type_c* search_expression_type = this; |
|
772 |
||
773 |
switch(current_function_type){ |
|
774 |
""" |
|
775 |
||
776 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
777 |
search_type_code += """ |
|
778 |
/**** |
|
779 |
*%s |
|
780 |
*/ |
|
781 |
case function_%s : |
|
782 |
{ |
|
783 |
symbol_c *last_type_symbol = NULL; |
|
784 |
""" %(fname, fname.lower()) |
|
785 |
indent = " " |
|
786 |
||
787 |
search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n ') |
|
788 |
||
789 |
search_type_code += """ |
|
790 |
}/*function_%s*/ |
|
791 |
break; |
|
792 |
""" %(fname.lower()) |
|
793 |
search_type_code += """ |
|
794 |
case function_none : |
|
795 |
ERROR; |
|
796 |
} |
|
797 |
return NULL; |
|
798 |
} |
|
22 | 799 |
""" |
800 |
||
801 |
||
802 |
# Now, print that out, or write to files from sys.argv |
|
803 |
for name, ext in [ |
|
804 |
('function_type_decl','h'), |
|
805 |
('get_function_type_decl','c'), |
|
806 |
('st_code_gen','c'), |
|
807 |
('il_code_gen','c'), |
|
808 |
('search_type_code','c')]: |
|
809 |
fd = open(os.path.join(sys.argv[1],name+'.'+ext),'w') |
|
810 |
fd.write(eval(name)) |
|
811 |
fd.close() |
|
15 | 812 |
else: |
22 | 813 |
# Put standard functions declaration in Bloktypes |
814 |
BlockTypes.extend(get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv"))))) |
|
815 |
||
816 |