44 #include <iostream> |
44 #include <iostream> |
45 #include <fstream> |
45 #include <fstream> |
46 #include <stdlib.h> |
46 #include <stdlib.h> |
47 |
47 |
48 #include "stage4.hh" |
48 #include "stage4.hh" |
49 |
49 #include "../main.hh" // required for ERROR() and ERROR_MSG() macros. |
50 |
50 |
51 |
51 |
|
52 |
|
53 |
|
54 #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) |
|
55 #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) |
|
56 #include <stdarg.h> |
|
57 |
|
58 void stage4err(const char *stage4_generator_id, symbol_c *symbol1, symbol_c *symbol2, const char *errmsg, ...) { |
|
59 va_list argptr; |
|
60 va_start(argptr, errmsg); /* second argument is last fixed pamater of stage4err() */ |
|
61 |
|
62 if ((symbol1 != NULL) && (symbol2 != NULL)) |
|
63 fprintf(stderr, "%s:%d-%d..%d-%d: ", |
|
64 FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column, |
|
65 LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column); |
|
66 |
|
67 fprintf(stderr, "error %s: ", stage4_generator_id); |
|
68 vfprintf(stderr, errmsg, argptr); |
|
69 fprintf(stderr, "\n"); |
|
70 // error_count++; |
|
71 va_end(argptr); |
|
72 } |
52 |
73 |
53 |
74 |
54 |
75 |
55 stage4out_c::stage4out_c(std::string indent_level): |
76 stage4out_c::stage4out_c(std::string indent_level): |
56 m_file(NULL) { |
77 m_file(NULL) { |
57 out = &std::cout; |
78 out = &std::cout; |
58 this->indent_level = indent_level; |
79 this->indent_level = indent_level; |
59 this->indent_spaces = ""; |
80 this->indent_spaces = ""; |
|
81 allow_output = true; |
60 } |
82 } |
61 |
83 |
62 stage4out_c::stage4out_c(const char *dir, const char *radix, const char *extension, std::string indent_level) { |
84 stage4out_c::stage4out_c(const char *dir, const char *radix, const char *extension, std::string indent_level) { |
63 std::string filename(radix); |
85 std::string filename(radix); |
64 filename += "."; |
86 filename += "."; |
112 indent_spaces.erase(indent_spaces.length() - indent_level.length(), indent_level.length()); |
134 indent_spaces.erase(indent_spaces.length() - indent_level.length(), indent_level.length()); |
113 else |
135 else |
114 indent_spaces.erase(); |
136 indent_spaces.erase(); |
115 } |
137 } |
116 |
138 |
117 |
139 void *stage4out_c::print( std::string value) {if (!allow_output) return NULL; *out << value; return NULL;} |
118 void *stage4out_c::print(const char *str) { |
140 void *stage4out_c::print( const char *value) {if (!allow_output) return NULL; *out << value; return NULL;} |
119 if (!allow_output) return NULL; |
141 //void *stage4out_c::print( int64_t value) {if (!allow_output) return NULL; *out << value; return NULL;} |
120 *out << str; |
142 //void *stage4out_c::print( uint64_t value) {if (!allow_output) return NULL; *out << value; return NULL;} |
121 return NULL; |
143 void *stage4out_c::print( real64_t value) {if (!allow_output) return NULL; *out << value; return NULL;} |
122 } |
144 void *stage4out_c::print( int value) {if (!allow_output) return NULL; *out << value; return NULL;} |
123 |
145 void *stage4out_c::print( long int value) {if (!allow_output) return NULL; *out << value; return NULL;} |
124 void *stage4out_c::print_integer(int integer) { |
146 void *stage4out_c::print( long long int value) {if (!allow_output) return NULL; *out << value; return NULL;} |
125 if (!allow_output) return NULL; |
147 void *stage4out_c::print(unsigned int value) {if (!allow_output) return NULL; *out << value; return NULL;} |
126 *out << integer; |
148 void *stage4out_c::print(unsigned long int value) {if (!allow_output) return NULL; *out << value; return NULL;} |
127 return NULL; |
149 void *stage4out_c::print(unsigned long long int value) {if (!allow_output) return NULL; *out << value; return NULL;} |
128 } |
150 |
129 |
151 |
130 void *stage4out_c::print_long_integer(unsigned long l_integer, bool suffix) { |
152 void *stage4out_c::print_long_integer(unsigned long l_integer, bool suffix) { |
131 if (!allow_output) return NULL; |
153 if (!allow_output) return NULL; |
132 *out << l_integer; |
154 *out << l_integer; |
133 if (suffix) *out << "UL"; |
155 if (suffix) *out << "UL"; |
138 if (!allow_output) return NULL; |
160 if (!allow_output) return NULL; |
139 *out << ll_integer; |
161 *out << ll_integer; |
140 if (suffix) *out << "ULL"; |
162 if (suffix) *out << "ULL"; |
141 return NULL; |
163 return NULL; |
142 } |
164 } |
|
165 |
143 |
166 |
144 void *stage4out_c::printupper(const char *str) { |
167 void *stage4out_c::printupper(const char *str) { |
145 if (!allow_output) return NULL; |
168 if (!allow_output) return NULL; |
146 for (int i = 0; str[i] != '\0'; i++) |
169 for (int i = 0; str[i] != '\0'; i++) |
147 *out << (unsigned char)toupper(str[i]); |
170 *out << (unsigned char)toupper(str[i]); |
171 else |
194 else |
172 *out << (unsigned char)toupper(str[i]); |
195 *out << (unsigned char)toupper(str[i]); |
173 return NULL; |
196 return NULL; |
174 } |
197 } |
175 |
198 |
176 |
|
177 void *stage4out_c::print(std::string str) { |
|
178 if (!allow_output) return NULL; |
|
179 *out << str; |
|
180 return NULL; |
|
181 } |
|
182 |
199 |
183 |
200 |
184 void *stage4out_c::printupper(std::string str) { |
201 void *stage4out_c::printupper(std::string str) { |
185 if (!allow_output) return NULL; |
202 if (!allow_output) return NULL; |
186 /* The string standard class does not have a converter member function to upper case. |
203 /* The string standard class does not have a converter member function to upper case. |
225 |
242 |
226 int stage4(symbol_c *tree_root, const char *builddir) { |
243 int stage4(symbol_c *tree_root, const char *builddir) { |
227 stage4out_c s4o; |
244 stage4out_c s4o; |
228 visitor_c *generate_code = new_code_generator(&s4o, builddir); |
245 visitor_c *generate_code = new_code_generator(&s4o, builddir); |
229 |
246 |
230 if (NULL == generate_code) |
247 if (NULL == generate_code) ERROR; |
231 return -1; |
|
232 |
248 |
233 tree_root->accept(*generate_code); |
249 tree_root->accept(*generate_code); |
234 |
250 |
235 delete_code_generator(generate_code); |
251 delete_code_generator(generate_code); |
236 |
252 |