1 /* |
|
2 * (c) 2003 Mario de Sousa |
|
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 * Seperation of type specification and default value constructs |
|
28 * (for e.g. simple_spec_init_c), into a type specificiation part, |
|
29 * and a default value part. |
|
30 */ |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 //#include <stdio.h> /* required for NULL */ |
|
36 //#include <string> |
|
37 //#include <iostream> |
|
38 |
|
39 //#include "../../util/symtable.hh" |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 class spec_init_sperator_c: public null_visitor_c { |
|
48 private: |
|
49 /* this is a singleton class... */ |
|
50 static spec_init_sperator_c *class_instance; |
|
51 static spec_init_sperator_c *get_class_instance(void) { |
|
52 if (NULL == class_instance) |
|
53 class_instance = new spec_init_sperator_c(); |
|
54 |
|
55 if (NULL == class_instance) |
|
56 ERROR; |
|
57 |
|
58 return class_instance; |
|
59 } |
|
60 |
|
61 private: |
|
62 typedef enum {search_spec, search_init} search_what_t; |
|
63 static search_what_t search_what; |
|
64 |
|
65 public: |
|
66 /* the only two public functions... */ |
|
67 static symbol_c *get_spec(symbol_c *spec_init) { |
|
68 search_what = search_spec; |
|
69 return (symbol_c *)spec_init->accept(*get_class_instance()); |
|
70 } |
|
71 |
|
72 static symbol_c *get_init(symbol_c *spec_init) { |
|
73 search_what = search_init; |
|
74 return (symbol_c *)spec_init->accept(*get_class_instance()); |
|
75 } |
|
76 |
|
77 // private: |
|
78 public: /* probably needs to be public so it may be visited... !! */ |
|
79 |
|
80 |
|
81 /*******************************************/ |
|
82 /* B 1.1 - Letters, digits and identifiers */ |
|
83 /*******************************************/ |
|
84 // SYM_TOKEN(identifier_c) |
|
85 void *visit(identifier_c *symbol) { |
|
86 TRACE("spec_init_sperator_c::identifier_c"); |
|
87 switch (search_what) { |
|
88 /* if we ever get called sith a simple identifier_c, then it must be a previously declared type... */ |
|
89 case search_spec: return symbol; |
|
90 case search_init: return NULL; |
|
91 } |
|
92 ERROR; /* should never occur */ |
|
93 return NULL; |
|
94 } |
|
95 |
|
96 |
|
97 /********************************/ |
|
98 /* B 1.3.3 - Derived data types */ |
|
99 /********************************/ |
|
100 |
|
101 /* simple_specification ASSIGN constant */ |
|
102 void *visit(simple_spec_init_c *symbol) { |
|
103 TRACE("spec_init_sperator_c::simple_spec_init_c"); |
|
104 switch (search_what) { |
|
105 case search_spec: return symbol->simple_specification; |
|
106 case search_init: return symbol->constant; |
|
107 } |
|
108 ERROR; /* should never occur */ |
|
109 return NULL; |
|
110 } |
|
111 |
|
112 /* subrange_specification ASSIGN signed_integer */ |
|
113 void *visit(subrange_spec_init_c *symbol) { |
|
114 TRACE("spec_init_sperator_c::subrange_spec_init_c"); |
|
115 switch (search_what) { |
|
116 case search_spec: return symbol->subrange_specification; |
|
117 case search_init: return symbol->signed_integer; |
|
118 } |
|
119 ERROR; /* should never occur */ |
|
120 return NULL; |
|
121 } |
|
122 |
|
123 /* enumerated_specification ASSIGN enumerated_value */ |
|
124 void *visit(enumerated_spec_init_c *symbol) { |
|
125 TRACE("spec_init_sperator_c::enumerated_spec_init_c"); |
|
126 switch (search_what) { |
|
127 case search_spec: return symbol->enumerated_specification; |
|
128 case search_init: return symbol->enumerated_value; |
|
129 } |
|
130 ERROR; /* should never occur */ |
|
131 return NULL; |
|
132 } |
|
133 |
|
134 /* structure_type_name ASSIGN structure_initialization */ |
|
135 /* structure_initialization may be NULL ! */ |
|
136 //SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) |
|
137 void *visit(initialized_structure_c *symbol) { |
|
138 TRACE("spec_init_sperator_c::initialized_structure_c"); |
|
139 switch (search_what) { |
|
140 case search_spec: return symbol->structure_type_name; |
|
141 case search_init: return symbol->structure_initialization; |
|
142 } |
|
143 ERROR; /* should never occur */ |
|
144 return NULL; |
|
145 } |
|
146 |
|
147 |
|
148 /******************************************/ |
|
149 /* B 1.4.3 - Declaration & Initialisation */ |
|
150 /******************************************/ |
|
151 |
|
152 /* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */ |
|
153 /* structure_initialization -> may be NULL ! */ |
|
154 void *visit(fb_name_decl_c *symbol) { |
|
155 TRACE("spec_init_sperator_c::fb_name_decl_c"); |
|
156 switch (search_what) { |
|
157 case search_spec: return symbol->function_block_type_name; |
|
158 case search_init: return symbol->structure_initialization; |
|
159 } |
|
160 ERROR; /* should never occur */ |
|
161 return NULL; |
|
162 } |
|
163 |
|
164 }; /* class spec_init_sperator_c */ |
|
165 |
|
166 |
|
167 |
|
168 spec_init_sperator_c *spec_init_sperator_c ::class_instance = NULL; |
|
169 spec_init_sperator_c::search_what_t spec_init_sperator_c::search_what; |
|