|
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 /* Determine the data type of a specific constant or variable. |
|
28 * A reference to the relevant type definition is returned. |
|
29 * |
|
30 * For example: |
|
31 * 22 -> returns reference to a int_type_name_c object. |
|
32 * 22.2 -> returns reference to a real_type_name_c object. |
|
33 * LREAL#22.2 -> returns reference to a lreal_type_name_c object. |
|
34 * etc... |
|
35 */ |
|
36 class search_constant_type_c: public search_visitor_c { |
|
37 |
|
38 public: |
|
39 /**********************/ |
|
40 /* B.1.3 - Data types */ |
|
41 /**********************/ |
|
42 /***********************************/ |
|
43 /* B 1.3.1 - Elementary Data Types */ |
|
44 /***********************************/ |
|
45 static real_type_name_c real_type_name; |
|
46 static int_type_name_c int_type_name; |
|
47 static string_type_name_c string_type_name; |
|
48 static wstring_type_name_c wstring_type_name; |
|
49 static time_type_name_c time_type_name; |
|
50 static date_type_name_c date_type_name; |
|
51 static dt_type_name_c dt_type_name; |
|
52 static tod_type_name_c tod_type_name; |
|
53 |
|
54 |
|
55 public: |
|
56 symbol_c *get_type(symbol_c *constant) { |
|
57 return (symbol_c *)constant->accept(*this); |
|
58 } |
|
59 |
|
60 public: |
|
61 /*********************/ |
|
62 /* B 1.2 - Constants */ |
|
63 /*********************/ |
|
64 |
|
65 /******************************/ |
|
66 /* B 1.2.1 - Numeric Literals */ |
|
67 /******************************/ |
|
68 void *visit(real_c *symbol) {return (void *)&real_type_name;} |
|
69 void *visit(integer_c *symbol) {return (void *)&int_type_name;} |
|
70 void *visit(binary_integer_c *symbol) {return (void *)&int_type_name;} |
|
71 void *visit(octal_integer_c *symbol) {return (void *)&int_type_name;} |
|
72 void *visit(hex_integer_c *symbol) {return (void *)&int_type_name;} |
|
73 |
|
74 void *visit(numeric_literal_c *symbol) |
|
75 {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
76 void *visit(integer_literal_c *symbol) |
|
77 {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
78 void *visit(real_literal_c *symbol) |
|
79 {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
80 void *visit(bit_string_literal_c *symbol) |
|
81 {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
82 void *visit(boolean_literal_c *symbol) |
|
83 {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} |
|
84 |
|
85 /*******************************/ |
|
86 /* B.1.2.2 Character Strings */ |
|
87 /*******************************/ |
|
88 void *visit(double_byte_character_string_c *symbol) {return (void *)&wstring_type_name;} |
|
89 void *visit(single_byte_character_string_c *symbol) {return (void *)&string_type_name;} |
|
90 |
|
91 /***************************/ |
|
92 /* B 1.2.3 - Time Literals */ |
|
93 /***************************/ |
|
94 /************************/ |
|
95 /* B 1.2.3.1 - Duration */ |
|
96 /************************/ |
|
97 void *visit(neg_time_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
98 void *visit(duration_c *symbol) {return (void *)&time_type_name;} |
|
99 void *visit(fixed_point_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
100 void *visit(days_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
101 void *visit(hours_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
102 void *visit(minutes_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
103 void *visit(seconds_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
104 void *visit(milliseconds_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
105 |
|
106 /************************************/ |
|
107 /* B 1.2.3.2 - Time of day and Date */ |
|
108 /************************************/ |
|
109 void *visit(time_of_day_c *symbol) {return (void *)&tod_type_name;} |
|
110 void *visit(daytime_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
111 void *visit(date_c *symbol) {return (void *)&date_type_name;} |
|
112 void *visit(date_literal_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ |
|
113 void *visit(date_and_time_c *symbol) {return (void *)&dt_type_name;} |
|
114 }; |
|
115 |
|
116 real_type_name_c search_constant_type_c::real_type_name; |
|
117 int_type_name_c search_constant_type_c::int_type_name; |
|
118 string_type_name_c search_constant_type_c::string_type_name; |
|
119 wstring_type_name_c search_constant_type_c::wstring_type_name; |
|
120 time_type_name_c search_constant_type_c::time_type_name; |
|
121 date_type_name_c search_constant_type_c::date_type_name; |
|
122 dt_type_name_c search_constant_type_c::dt_type_name; |
|
123 tod_type_name_c search_constant_type_c::tod_type_name; |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |