author | Manuele Conti <conti.ma@alice.it> |
Sun, 13 May 2012 17:57:15 +0200 | |
changeset 557 | 95a2fe60a15c |
parent 511 | b22ae67d8003 |
child 889 | 5f380b99e95e |
permissions | -rw-r--r-- |
511
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
1 |
/* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
3 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
4 |
* Copyright (C) 2012 Mario de Sousa (msousa@fe.up.pt) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
5 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
6 |
* This program is free software: you can redistribute it and/or modify |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
7 |
* it under the terms of the GNU General Public License as published by |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
8 |
* the Free Software Foundation, either version 3 of the License, or |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
9 |
* (at your option) any later version. |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
10 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
14 |
* GNU General Public License for more details. |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
15 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
18 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
19 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
20 |
* This code is made available on the understanding that it will not be |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
21 |
* used in safety-critical situations without a full and competent review. |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
22 |
*/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
23 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
24 |
/* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
25 |
* An IEC 61131-3 compiler. |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
26 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
27 |
* Based on the |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
28 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
29 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
30 |
*/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
31 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
32 |
/* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
33 |
* A small helper visitor class, that will |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
34 |
* return the name (tokn_c *) of a variable, as it will |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
35 |
* appear in the variable declaration. |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
36 |
*/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
37 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
38 |
/* For ex.: |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
39 |
* VAR |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
40 |
* A : int; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
41 |
* B : ARRAY [1..9] of int; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
42 |
* C : some_struct_t; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
43 |
* END_VAR |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
44 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
45 |
* A := 56; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
46 |
* B[8] := 99; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
47 |
* C.e := 77; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
48 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
49 |
* Calling this visitor class with symbolic_variable_c instance referencing 'A' in |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
50 |
* the line 'A := 56', will return the string "A". |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
51 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
52 |
* Calling this visitor class with array_variable_c instance referencing 'B[8]' in |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
53 |
* the line 'B[8] := 99', will return the string "B". |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
54 |
* |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
55 |
* Calling this visitor class with array_variable_c instance referencing 'C.e' in |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
56 |
* the line 'C.e := 77', will return the string "C". |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
57 |
*/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
58 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
59 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
60 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
61 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
62 |
class get_var_name_c : public search_visitor_c { |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
63 |
public: |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
64 |
get_var_name_c(void) {}; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
65 |
~get_var_name_c(void) {}; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
66 |
static token_c *get_name(symbol_c *symbol); |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
67 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
68 |
private: |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
69 |
static get_var_name_c *singleton_instance_; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
70 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
71 |
private: |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
72 |
/*************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
73 |
/* B.1 - Common elements */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
74 |
/*************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
75 |
/*******************************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
76 |
/* B 1.1 - Letters, digits and identifiers */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
77 |
/*******************************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
78 |
// SYM_TOKEN(identifier_c) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
79 |
void *visit(identifier_c *symbol); |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
80 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
81 |
/*********************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
82 |
/* B 1.4 - Variables */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
83 |
/*********************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
84 |
// SYM_REF2(symbolic_variable_c, var_name, unused) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
85 |
void *visit(symbolic_variable_c *symbol); |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
86 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
87 |
/********************************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
88 |
/* B.1.4.1 Directly Represented Variables */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
89 |
/********************************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
90 |
// SYM_TOKEN(direct_variable_c) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
91 |
// void *visit(direct_variable_c *symbol); |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
92 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
93 |
/*************************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
94 |
/* B.1.4.2 Multi-element Variables */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
95 |
/*************************************/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
96 |
/* subscripted_variable '[' subscript_list ']' */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
97 |
// SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
98 |
void *visit(array_variable_c *symbol); |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
99 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
100 |
/* subscript_list ',' subscript */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
101 |
// SYM_LIST(subscript_list_c) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
102 |
// void *visit(subscript_list_c *symbol); |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
103 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
104 |
/* record_variable '.' field_selector */ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
105 |
/* WARNING: input and/or output variables of function blocks |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
106 |
* may be accessed as fields of a tructured variable! |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
107 |
* Code handling a structured_variable_c must take |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
108 |
* this into account! |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
109 |
*/ |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
110 |
// SYM_REF2(structured_variable_c, record_variable, field_selector) |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
111 |
void *visit(structured_variable_c *symbol); |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
112 |
}; |
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
113 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
114 |
|
b22ae67d8003
Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff
changeset
|
115 |