|
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 * Definition of the Abstract Syntax data structure components |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> /* required for exit() */ |
|
31 #include <string.h> |
|
32 |
|
33 #include "absyntax.hh" |
|
34 //#include "../stage1_2/iec.hh" /* required for BOGUS_TOKEN_ID, etc... */ |
|
35 #include "visitor.hh" |
|
36 |
|
37 #define ABORT(str) {printf("ERROR: %s\n", str); exit(0);} |
|
38 |
|
39 |
|
40 |
|
41 symbol_c *tree_root = NULL; |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 /* The base class of all symbols */ |
|
47 symbol_c::symbol_c(void) { |
|
48 lineno = 0; |
|
49 } |
|
50 |
|
51 symbol_c::symbol_c(long lineno) { |
|
52 this->lineno = lineno; |
|
53 } |
|
54 |
|
55 |
|
56 |
|
57 token_c::token_c(const char *value) { |
|
58 this->value = value; |
|
59 // printf("New token: %s\n", value); |
|
60 } |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 list_c::list_c(void) { |
|
68 n = 0; |
|
69 elements = NULL; |
|
70 } |
|
71 |
|
72 list_c::list_c(symbol_c *elem) { |
|
73 n = 0; |
|
74 elements = NULL; |
|
75 add_element(elem); |
|
76 } |
|
77 |
|
78 /* insert a new element */ |
|
79 void list_c::add_element(symbol_c *elem) { |
|
80 //printf("list_c::add_element()\n"); |
|
81 n++; |
|
82 elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); |
|
83 if (elements == NULL) |
|
84 ABORT("Out of memory"); |
|
85 elements[n - 1] = elem; |
|
86 } |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 #define SYM_LIST(class_name_c) \ |
|
93 void *class_name_c::accept(visitor_c &visitor) {return visitor.visit(this);} |
|
94 |
|
95 #define SYM_TOKEN(class_name_c) \ |
|
96 class_name_c::class_name_c(const char *value): token_c(value) {} \ |
|
97 void *class_name_c::accept(visitor_c &visitor) {return visitor.visit(this);} |
|
98 |
|
99 #define SYM_REF0(class_name_c) \ |
|
100 void *class_name_c::accept(visitor_c &visitor) {return visitor.visit(this);} |
|
101 |
|
102 |
|
103 #define SYM_REF2(class_name_c, ref1, ref2) \ |
|
104 class_name_c::class_name_c(symbol_c *ref1, \ |
|
105 symbol_c *ref2) { \ |
|
106 this->ref1 = ref1; \ |
|
107 this->ref2 = ref2; \ |
|
108 } \ |
|
109 void *class_name_c::accept(visitor_c &visitor) {return visitor.visit(this);} |
|
110 |
|
111 |
|
112 #define SYM_REF4(class_name_c, ref1, ref2, ref3, ref4) \ |
|
113 class_name_c::class_name_c(symbol_c *ref1, \ |
|
114 symbol_c *ref2, \ |
|
115 symbol_c *ref3, \ |
|
116 symbol_c *ref4) { \ |
|
117 this->ref1 = ref1; \ |
|
118 this->ref2 = ref2; \ |
|
119 this->ref3 = ref3; \ |
|
120 this->ref4 = ref4; \ |
|
121 } \ |
|
122 void *class_name_c::accept(visitor_c &visitor) {return visitor.visit(this);} |
|
123 |
|
124 |
|
125 #define SYM_REF6(class_name_c, ref1, ref2, ref3, ref4, ref5, ref6) \ |
|
126 class_name_c::class_name_c(symbol_c *ref1, \ |
|
127 symbol_c *ref2, \ |
|
128 symbol_c *ref3, \ |
|
129 symbol_c *ref4, \ |
|
130 symbol_c *ref5, \ |
|
131 symbol_c *ref6) { \ |
|
132 this->ref1 = ref1; \ |
|
133 this->ref2 = ref2; \ |
|
134 this->ref3 = ref3; \ |
|
135 this->ref4 = ref4; \ |
|
136 this->ref5 = ref5; \ |
|
137 this->ref6 = ref6; \ |
|
138 } \ |
|
139 void *class_name_c::accept(visitor_c &visitor) {return visitor.visit(this);} |
|
140 |
|
141 |
|
142 |
|
143 #include "absyntax.def" |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 #undef SYM_LIST |
|
149 #undef SYM_TOKEN |
|
150 #undef SYM_TOKEN |
|
151 #undef SYM_REF0 |
|
152 #undef SYM_REF2 |
|
153 #undef SYM_REF4 |
|
154 #undef SYM_REF6 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |