|
1 /* |
|
2 * (c) 2007 Mario de Sousa, Laurent Bessard |
|
3 * |
|
4 * Offered to the public under the terms of the GNU General Public License |
|
5 * as published by the Free Software Foundation; either version 2 of the |
|
6 * License, or (at your option) any later version. |
|
7 * |
|
8 * This program is distributed in the hope that it will be useful, but |
|
9 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 * Public License for more details. |
|
12 * |
|
13 * This code is made available on the understanding that it will not be |
|
14 * used in safety-critical situations without a full and competent review. |
|
15 */ |
|
16 |
|
17 /* |
|
18 * An IEC 61131-3 IL and ST compiler. |
|
19 * |
|
20 * Based on the |
|
21 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 * |
|
23 */ |
|
24 |
|
25 |
|
26 /* |
|
27 * Conversion of sfc networks (i.e. SFC code). |
|
28 * |
|
29 * This is part of the 4th stage that generates |
|
30 * a c++ source program equivalent to the SFC, IL and ST |
|
31 * code. |
|
32 */ |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 /***********************************************************************/ |
|
38 /***********************************************************************/ |
|
39 /***********************************************************************/ |
|
40 /***********************************************************************/ |
|
41 |
|
42 class generate_cc_sfc_stepdecl_c: public generate_cc_base_c { |
|
43 |
|
44 private: |
|
45 char step_number; |
|
46 |
|
47 public: |
|
48 generate_cc_sfc_stepdecl_c(stage4out_c *s4o_ptr): generate_cc_base_c(s4o_ptr) {} |
|
49 ~generate_cc_sfc_stepdecl_c(void) {} |
|
50 |
|
51 void reset_step_number(void) {step_number = 0;} |
|
52 char get_step_number(void) {return step_number;} |
|
53 void increment_step_number(void) {step_number++;} |
|
54 void print_step_number(void) { |
|
55 char str[10]; |
|
56 sprintf(str, "%d", step_number); |
|
57 s4o.print(str); |
|
58 } |
|
59 |
|
60 /*********************************************/ |
|
61 /* B.1.6 Sequential function chart elements */ |
|
62 /*********************************************/ |
|
63 |
|
64 void *visit(initial_step_c *symbol) { |
|
65 s4o.print("#define "); |
|
66 symbol->step_name->accept(*this); |
|
67 s4o.print(" "); |
|
68 print_step_number(); |
|
69 s4o.print("\n"); |
|
70 increment_step_number(); |
|
71 return NULL; |
|
72 } |
|
73 |
|
74 void *visit(step_c *symbol) { |
|
75 s4o.print("#define "); |
|
76 symbol->step_name->accept(*this); |
|
77 s4o.print(" "); |
|
78 print_step_number(); |
|
79 s4o.print("\n"); |
|
80 increment_step_number(); |
|
81 return NULL; |
|
82 } |
|
83 |
|
84 void *visit(transition_c *symbol) {return NULL;} |
|
85 |
|
86 void *visit(action_c *symbol) {return NULL;} |
|
87 |
|
88 }; /* generate_cc_sfc_stepdecl_c */ |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 /***********************************************************************/ |
|
94 /***********************************************************************/ |
|
95 /***********************************************************************/ |
|
96 /***********************************************************************/ |
|
97 |
|
98 class generate_cc_sfc_actiondecl_c: public generate_cc_base_c { |
|
99 |
|
100 private: |
|
101 char action_number; |
|
102 |
|
103 public: |
|
104 generate_cc_sfc_actiondecl_c(stage4out_c *s4o_ptr): generate_cc_base_c(s4o_ptr) {} |
|
105 ~generate_cc_sfc_actiondecl_c(void) {} |
|
106 |
|
107 void reset_action_number(void) {action_number = 0;} |
|
108 char get_action_number(void) {return action_number;} |
|
109 void increment_action_number(void) {action_number++;} |
|
110 void print_action_number(void) { |
|
111 char str[10]; |
|
112 sprintf(str, "%d", action_number); |
|
113 s4o.print(str); |
|
114 } |
|
115 |
|
116 /*********************************************/ |
|
117 /* B.1.6 Sequential function chart elements */ |
|
118 /*********************************************/ |
|
119 |
|
120 void *visit(initial_step_c *symbol) {return NULL;} |
|
121 |
|
122 void *visit(step_c *symbol) {return NULL;} |
|
123 |
|
124 void *visit(transition_c *symbol) {return NULL;} |
|
125 |
|
126 void *visit(action_c *symbol) { |
|
127 s4o.print("#define "); |
|
128 symbol->action_name->accept(*this); |
|
129 s4o.print(" "); |
|
130 print_action_number(); |
|
131 s4o.print("\n"); |
|
132 increment_action_number(); |
|
133 return NULL; |
|
134 } |
|
135 |
|
136 }; /* generate_cc_sfc_actiondecl_c */ |
|
137 |
|
138 |
|
139 |
|
140 /***********************************************************************/ |
|
141 /***********************************************************************/ |
|
142 /***********************************************************************/ |
|
143 /***********************************************************************/ |
|
144 |
|
145 class generate_cc_sfcdecl_c: public iterator_visitor_c { |
|
146 |
|
147 protected: |
|
148 stage4out_c &s4o; |
|
149 |
|
150 private: |
|
151 generate_cc_sfc_stepdecl_c *generate_cc_sfc_stepdecl; |
|
152 generate_cc_sfc_actiondecl_c *generate_cc_sfc_actiondecl; |
|
153 |
|
154 public: |
|
155 generate_cc_sfcdecl_c(stage4out_c *s4o_ptr) : s4o(*s4o_ptr) { |
|
156 generate_cc_sfc_stepdecl = new generate_cc_sfc_stepdecl_c(s4o_ptr); |
|
157 generate_cc_sfc_actiondecl = new generate_cc_sfc_actiondecl_c(s4o_ptr); |
|
158 } |
|
159 |
|
160 virtual ~generate_cc_sfcdecl_c(void) { |
|
161 delete generate_cc_sfc_stepdecl; |
|
162 delete generate_cc_sfc_actiondecl; |
|
163 } |
|
164 |
|
165 public: |
|
166 |
|
167 /*********************************************/ |
|
168 /* B.1.6 Sequential function chart elements */ |
|
169 /*********************************************/ |
|
170 |
|
171 /*| sequential_function_chart sfc_network*/ |
|
172 void *visit(sfc_network_c *symbol) { |
|
173 char i; |
|
174 |
|
175 /* generate step number definitions */ |
|
176 s4o.print(s4o.indent_spaces + "// Steps declaration\n"); |
|
177 generate_cc_sfc_stepdecl->reset_step_number(); |
|
178 symbol->accept(*generate_cc_sfc_stepdecl); |
|
179 s4o.print("\n" + s4o.indent_spaces + "nb_steps = "); |
|
180 generate_cc_sfc_stepdecl->print_step_number(); |
|
181 s4o.print(";\n" + s4o.indent_spaces + "STEP step_list["); |
|
182 generate_cc_sfc_stepdecl->print_step_number(); |
|
183 s4o.print("] = {\n"); |
|
184 s4o.indent_right(); |
|
185 for (i = 0; i < generate_cc_sfc_stepdecl->get_step_number(); i++) { |
|
186 if (i == 0) { |
|
187 s4o.print(s4o.indent_spaces + "{1, 0, 0}"); |
|
188 } |
|
189 else { |
|
190 s4o.print(",\n" + s4o.indent_spaces + "{0, 0, 0}"); |
|
191 } |
|
192 } |
|
193 s4o.indent_left(); |
|
194 s4o.print("\n" + s4o.indent_spaces + "};\n\n"); |
|
195 |
|
196 /* generate action number definitions */ |
|
197 s4o.print(s4o.indent_spaces + "// Actions declaration\n"); |
|
198 generate_cc_sfc_actiondecl->reset_action_number(); |
|
199 symbol->accept(*generate_cc_sfc_actiondecl); |
|
200 s4o.print("\n" + s4o.indent_spaces + "nb_actions = "); |
|
201 generate_cc_sfc_actiondecl->print_action_number(); |
|
202 s4o.print(";\n" + s4o.indent_spaces + "STEP step_list["); |
|
203 generate_cc_sfc_actiondecl->print_action_number(); |
|
204 s4o.print("] = {\n"); |
|
205 s4o.indent_right(); |
|
206 for (i = 0; i < generate_cc_sfc_actiondecl->get_action_number(); i++) { |
|
207 if (i == 0) { |
|
208 s4o.print(s4o.indent_spaces + "{0, 0, 0, 0, 0, 0}"); |
|
209 } |
|
210 else { |
|
211 s4o.print(",\n" + s4o.indent_spaces + "{0, 0, 0, 0, 0, 0}"); |
|
212 } |
|
213 } |
|
214 s4o.indent_left(); |
|
215 s4o.print("\n" + s4o.indent_spaces + "};\n\n"); |
|
216 |
|
217 return NULL; |
|
218 } |
|
219 |
|
220 /***********************************/ |
|
221 /* B 2.1 Instructions and Operands */ |
|
222 /***********************************/ |
|
223 /*| instruction_list il_instruction */ |
|
224 void *visit(instruction_list_c *symbol) {return NULL;} |
|
225 |
|
226 /***************************************/ |
|
227 /* B.3 - Language ST (Structured Text) */ |
|
228 /***************************************/ |
|
229 /********************/ |
|
230 /* B 3.2 Statements */ |
|
231 /********************/ |
|
232 void *visit(statement_list_c *symbol) {return NULL;} |
|
233 |
|
234 /* Remainder implemented in generate_cc_sfcdecl_c... */ |
|
235 }; |