author | Edouard TISSERANT <edouard.tisserant@gmail.com> |
Sun, 29 Nov 2009 19:09:56 +0100 | |
changeset 218 | 413842f6152f |
parent 202 | da1a8186f86f |
child 238 | 0919986a5c98 |
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 |
||
49 |
symbol_c *decompose_var_instance_name_c::next_part(void) { |
|
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); |
|
55 |
next_variable_name = current_recursive_variable_name; |
|
56 |
||
57 |
if (previously_returned_variable_name == res) |
|
58 |
return NULL; |
|
59 |
previously_returned_variable_name = res; |
|
60 |
return res; |
|
61 |
} |
|
62 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
63 |
|
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
64 |
/*************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
65 |
/* B.1 - Common elements */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
66 |
/*************************/ |
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 |
/* 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
|
69 |
/*******************************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
70 |
/* 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
|
71 |
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
|
72 |
|
181 | 73 |
/*********************/ |
74 |
/* B 1.4 - Variables */ |
|
75 |
/*********************/ |
|
76 |
void *decompose_var_instance_name_c::visit(symbolic_variable_c *symbol) {return (void *)(symbol->var_name);} |
|
77 |
||
78 |
/********************************************/ |
|
79 |
/* B.1.4.1 Directly Represented Variables */ |
|
80 |
/********************************************/ |
|
81 |
void *decompose_var_instance_name_c::visit(direct_variable_c *symbol) {return (void *)symbol;} |
|
82 |
||
83 |
/*************************************/ |
|
84 |
/* B.1.4.2 Multi-element Variables */ |
|
85 |
/*************************************/ |
|
86 |
/* subscripted_variable '[' subscript_list ']' */ |
|
87 |
// SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
88 |
void *decompose_var_instance_name_c::visit(array_variable_c *symbol) { |
|
89 |
/* NOTE: the subscripted_variable may itself be a structure!, |
|
90 |
* so we must recursevily visit! |
|
91 |
*/ |
|
92 |
return symbol->subscripted_variable->accept(*this); |
|
93 |
} |
|
94 |
||
95 |
/* record_variable '.' field_selector */ |
|
96 |
/* WARNING: input and/or output variables of function blocks |
|
97 |
* may be accessed as fields of a tructured variable! |
|
98 |
* Code handling a structured_variable_c must take |
|
99 |
* this into account! |
|
100 |
*/ |
|
101 |
//SYM_REF2(structured_variable_c, record_variable, field_selector) |
|
102 |
void *decompose_var_instance_name_c::visit(structured_variable_c *symbol) { |
|
103 |
/* NOTE: The following code will not work, as structured_variable_c |
|
104 |
* are grouped on the left, and not on the right! |
|
105 |
* |
|
106 |
* example: window.origin.x |
|
107 |
* will result in |
|
108 |
* s1 = structured_variable_c("window, "origin"); |
|
109 |
* s2 = structured_variable_c(s1, "x"); |
|
110 |
* AND NOT |
|
111 |
* s1 = structured_variable_c("origin", "x"); |
|
112 |
* s2 = structured_variable_c("window", s1); |
|
113 |
* |
|
114 |
* as the following code assumes!! |
|
115 |
* |
|
116 |
current_variable_name = symbol->field_selector; |
|
117 |
return symbol->record_variable->accept(*this); |
|
118 |
*/ |
|
119 |
||
120 |
/* The correct code, is therefore more complex... */ |
|
121 |
if (next_variable_name == symbol) { |
|
122 |
/* NOTE: field_selector is always an identifier_c, |
|
123 |
* so we do not have to recursevily visit it again... |
|
124 |
* return (void *)symbol->field_selector->accept(*this); -> NOT REQUIRED!! |
|
125 |
*/ |
|
126 |
return (void *)symbol->field_selector; |
|
127 |
} |
|
128 |
||
129 |
current_recursive_variable_name = symbol; |
|
130 |
return symbol->record_variable->accept(*this); |
|
131 |
} |
|
132 |
||
133 |
||
134 |
||
135 |