author | Mario de Sousa <msousa@fe.up.pt> |
Thu, 31 Mar 2011 10:52:03 +0100 | |
changeset 259 | b6d7c71ff6d8 |
parent 238 | 0919986a5c98 |
child 265 | 4d222f46f8cc |
permissions | -rwxr-xr-x |
181 | 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 |
/* Decomposes a variable instance name into its constituents, |
|
27 |
* example: |
|
28 |
* window.points[1].coordinate.x |
|
29 |
* |
|
30 |
* will succesfully return |
|
31 |
* - window |
|
32 |
* - points |
|
33 |
* - coordinate |
|
34 |
* - x |
|
35 |
* on succesive calls to decompose_var_instance_name_c::next_part() |
|
36 |
*/ |
|
37 |
||
38 |
||
39 |
||
40 |
#include "decompose_var_instance_name.hh" |
|
41 |
||
42 |
decompose_var_instance_name_c::decompose_var_instance_name_c(symbol_c *variable_instance_name) { |
|
43 |
variable_name = variable_instance_name; |
|
44 |
next_variable_name = NULL; |
|
45 |
current_recursive_variable_name = NULL; |
|
46 |
previously_returned_variable_name = NULL; |
|
47 |
} |
|
48 |
||
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
49 |
symbol_c *decompose_var_instance_name_c::next_part(bool increment) { |
181 | 50 |
/* We must always start from the top! |
51 |
* See note in the structured_variable_c visitor |
|
52 |
* to understand why... |
|
53 |
*/ |
|
54 |
symbol_c *res = (symbol_c *)variable_name->accept(*this); |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
55 |
if (increment) |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
56 |
next_variable_name = current_recursive_variable_name; |
181 | 57 |
|
58 |
if (previously_returned_variable_name == res) |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
59 |
return NULL; |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
60 |
if (increment) |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
61 |
previously_returned_variable_name = res; |
181 | 62 |
return res; |
63 |
} |
|
64 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
65 |
/*************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
66 |
/* B.1 - Common elements */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
67 |
/*************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
68 |
/*******************************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
69 |
/* B 1.1 - Letters, digits and identifiers */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
70 |
/*******************************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
71 |
/* sometimes (e.g. FB calls) the name of the variable is stored directly in an identifier_c object */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
72 |
void *decompose_var_instance_name_c::visit(identifier_c *symbol) {return (void *)symbol;} |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
73 |
|
181 | 74 |
/*********************/ |
75 |
/* B 1.4 - Variables */ |
|
76 |
/*********************/ |
|
77 |
void *decompose_var_instance_name_c::visit(symbolic_variable_c *symbol) {return (void *)(symbol->var_name);} |
|
78 |
||
79 |
/********************************************/ |
|
80 |
/* B.1.4.1 Directly Represented Variables */ |
|
81 |
/********************************************/ |
|
82 |
void *decompose_var_instance_name_c::visit(direct_variable_c *symbol) {return (void *)symbol;} |
|
83 |
||
84 |
/*************************************/ |
|
85 |
/* B.1.4.2 Multi-element Variables */ |
|
86 |
/*************************************/ |
|
87 |
/* subscripted_variable '[' subscript_list ']' */ |
|
88 |
// SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
89 |
void *decompose_var_instance_name_c::visit(array_variable_c *symbol) { |
|
90 |
/* NOTE: the subscripted_variable may itself be a structure!, |
|
91 |
* so we must recursevily visit! |
|
92 |
*/ |
|
93 |
return symbol->subscripted_variable->accept(*this); |
|
94 |
} |
|
95 |
||
96 |
/* record_variable '.' field_selector */ |
|
97 |
/* WARNING: input and/or output variables of function blocks |
|
98 |
* may be accessed as fields of a tructured variable! |
|
99 |
* Code handling a structured_variable_c must take |
|
100 |
* this into account! |
|
101 |
*/ |
|
102 |
//SYM_REF2(structured_variable_c, record_variable, field_selector) |
|
103 |
void *decompose_var_instance_name_c::visit(structured_variable_c *symbol) { |
|
104 |
/* NOTE: The following code will not work, as structured_variable_c |
|
105 |
* are grouped on the left, and not on the right! |
|
106 |
* |
|
107 |
* example: window.origin.x |
|
108 |
* will result in |
|
109 |
* s1 = structured_variable_c("window, "origin"); |
|
110 |
* s2 = structured_variable_c(s1, "x"); |
|
111 |
* AND NOT |
|
112 |
* s1 = structured_variable_c("origin", "x"); |
|
113 |
* s2 = structured_variable_c("window", s1); |
|
114 |
* |
|
115 |
* as the following code assumes!! |
|
116 |
* |
|
117 |
current_variable_name = symbol->field_selector; |
|
118 |
return symbol->record_variable->accept(*this); |
|
119 |
*/ |
|
120 |
||
121 |
/* The correct code, is therefore more complex... */ |
|
122 |
if (next_variable_name == symbol) { |
|
123 |
/* NOTE: field_selector is always an identifier_c, |
|
124 |
* so we do not have to recursevily visit it again... |
|
125 |
* return (void *)symbol->field_selector->accept(*this); -> NOT REQUIRED!! |
|
126 |
*/ |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
127 |
return (void *)symbol->field_selector; |
181 | 128 |
} |
129 |
||
130 |
current_recursive_variable_name = symbol; |
|
131 |
return symbol->record_variable->accept(*this); |
|
132 |
} |
|
133 |
||
134 |
||
135 |
||
136 |