author | Mario de Sousa <msousa@fe.up.pt> |
Mon, 04 Apr 2011 11:19:48 +0100 | |
changeset 269 | 98fc461e1888 |
parent 265 | 4d222f46f8cc |
child 279 | c0453b7f99df |
permissions | -rwxr-xr-x |
181 | 1 |
/* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
181 | 3 |
* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
181 | 5 |
* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
6 |
* This program is free software: you can redistribute it and/or modify |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
7 |
* it under the terms of the GNU General Public License as published by |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
8 |
* the Free Software Foundation, either version 3 of the License, or |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
9 |
* (at your option) any later version. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
10 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
14 |
* GNU General Public License for more details. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
15 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
18 |
* |
181 | 19 |
* |
20 |
* This code is made available on the understanding that it will not be |
|
21 |
* used in safety-critical situations without a full and competent review. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
25 |
* An IEC 61131-3 compiler. |
181 | 26 |
* |
27 |
* Based on the |
|
28 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
29 |
* |
|
30 |
*/ |
|
31 |
||
32 |
||
33 |
/* Decomposes a variable instance name into its constituents, |
|
34 |
* example: |
|
35 |
* window.points[1].coordinate.x |
|
36 |
* |
|
37 |
* will succesfully return |
|
38 |
* - window |
|
39 |
* - points |
|
40 |
* - coordinate |
|
41 |
* - x |
|
42 |
* on succesive calls to decompose_var_instance_name_c::next_part() |
|
43 |
*/ |
|
44 |
||
45 |
||
46 |
||
47 |
#include "decompose_var_instance_name.hh" |
|
48 |
||
49 |
decompose_var_instance_name_c::decompose_var_instance_name_c(symbol_c *variable_instance_name) { |
|
50 |
variable_name = variable_instance_name; |
|
51 |
next_variable_name = NULL; |
|
52 |
current_recursive_variable_name = NULL; |
|
53 |
previously_returned_variable_name = NULL; |
|
54 |
} |
|
55 |
||
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
56 |
symbol_c *decompose_var_instance_name_c::next_part(bool increment) { |
181 | 57 |
/* We must always start from the top! |
58 |
* See note in the structured_variable_c visitor |
|
59 |
* to understand why... |
|
60 |
*/ |
|
61 |
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
|
62 |
if (increment) |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
63 |
next_variable_name = current_recursive_variable_name; |
181 | 64 |
|
65 |
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
|
66 |
return NULL; |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
67 |
if (increment) |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
68 |
previously_returned_variable_name = res; |
181 | 69 |
return res; |
70 |
} |
|
71 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
72 |
/*************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
73 |
/* B.1 - Common elements */ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
74 |
/*************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
75 |
/*******************************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
76 |
/* 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
|
77 |
/*******************************************/ |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
78 |
/* 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
|
79 |
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
|
80 |
|
181 | 81 |
/*********************/ |
82 |
/* B 1.4 - Variables */ |
|
83 |
/*********************/ |
|
84 |
void *decompose_var_instance_name_c::visit(symbolic_variable_c *symbol) {return (void *)(symbol->var_name);} |
|
85 |
||
86 |
/********************************************/ |
|
87 |
/* B.1.4.1 Directly Represented Variables */ |
|
88 |
/********************************************/ |
|
89 |
void *decompose_var_instance_name_c::visit(direct_variable_c *symbol) {return (void *)symbol;} |
|
90 |
||
91 |
/*************************************/ |
|
92 |
/* B.1.4.2 Multi-element Variables */ |
|
93 |
/*************************************/ |
|
94 |
/* subscripted_variable '[' subscript_list ']' */ |
|
95 |
// SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
96 |
void *decompose_var_instance_name_c::visit(array_variable_c *symbol) { |
|
97 |
/* NOTE: the subscripted_variable may itself be a structure!, |
|
98 |
* so we must recursevily visit! |
|
99 |
*/ |
|
100 |
return symbol->subscripted_variable->accept(*this); |
|
101 |
} |
|
102 |
||
103 |
/* record_variable '.' field_selector */ |
|
104 |
/* WARNING: input and/or output variables of function blocks |
|
105 |
* may be accessed as fields of a tructured variable! |
|
106 |
* Code handling a structured_variable_c must take |
|
107 |
* this into account! |
|
108 |
*/ |
|
109 |
//SYM_REF2(structured_variable_c, record_variable, field_selector) |
|
110 |
void *decompose_var_instance_name_c::visit(structured_variable_c *symbol) { |
|
111 |
/* NOTE: The following code will not work, as structured_variable_c |
|
112 |
* are grouped on the left, and not on the right! |
|
113 |
* |
|
114 |
* example: window.origin.x |
|
115 |
* will result in |
|
116 |
* s1 = structured_variable_c("window, "origin"); |
|
117 |
* s2 = structured_variable_c(s1, "x"); |
|
118 |
* AND NOT |
|
119 |
* s1 = structured_variable_c("origin", "x"); |
|
120 |
* s2 = structured_variable_c("window", s1); |
|
121 |
* |
|
122 |
* as the following code assumes!! |
|
123 |
* |
|
124 |
current_variable_name = symbol->field_selector; |
|
125 |
return symbol->record_variable->accept(*this); |
|
126 |
*/ |
|
127 |
||
128 |
/* The correct code, is therefore more complex... */ |
|
129 |
if (next_variable_name == symbol) { |
|
130 |
/* NOTE: field_selector is always an identifier_c, |
|
131 |
* so we do not have to recursevily visit it again... |
|
132 |
* return (void *)symbol->field_selector->accept(*this); -> NOT REQUIRED!! |
|
133 |
*/ |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
202
diff
changeset
|
134 |
return (void *)symbol->field_selector; |
181 | 135 |
} |
136 |
||
137 |
current_recursive_variable_name = symbol; |
|
138 |
return symbol->record_variable->accept(*this); |
|
139 |
} |
|
140 |
||
141 |
||
142 |
||
143 |