]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/mc-boot/GPushBackInput.c
Merge modula-2 front end onto gcc.
[thirdparty/gcc.git] / gcc / m2 / mc-boot / GPushBackInput.c
1 /* do not edit automatically generated by mc from PushBackInput. */
2 /* PushBackInput.mod provides a method for pushing back and consuming input.
3
4 Copyright (C) 2001-2021 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 #include "config.h"
29 #include "system.h"
30 # if !defined (PROC_D)
31 # define PROC_D
32 typedef void (*PROC_t) (void);
33 typedef struct { PROC_t proc; } PROC;
34 # endif
35
36 # if !defined (FALSE)
37 # define FALSE (1==0)
38 # endif
39
40 #if defined(__cplusplus)
41 # undef NULL
42 # define NULL 0
43 #endif
44 #define _PushBackInput_H
45 #define _PushBackInput_C
46
47 # include "GFIO.h"
48 # include "GDynamicStrings.h"
49 # include "GASCII.h"
50 # include "GDebug.h"
51 # include "GStrLib.h"
52 # include "GNumberIO.h"
53 # include "GStrIO.h"
54 # include "GStdIO.h"
55 # include "Glibc.h"
56
57 # define MaxPushBackStack 8192
58 # define MaxFileName 4096
59 typedef struct PushBackInput__T2_a PushBackInput__T2;
60
61 typedef struct PushBackInput__T3_a PushBackInput__T3;
62
63 struct PushBackInput__T2_a { char array[MaxFileName+1]; };
64 struct PushBackInput__T3_a { char array[MaxPushBackStack+1]; };
65 static PushBackInput__T2 FileName;
66 static PushBackInput__T3 CharStack;
67 static unsigned int ExitStatus;
68 static unsigned int Column;
69 static unsigned int StackPtr;
70 static unsigned int LineNo;
71 static unsigned int Debugging;
72
73 /*
74 Open - opens a file for reading.
75 */
76
77 extern "C" FIO_File PushBackInput_Open (const char *a_, unsigned int _a_high);
78
79 /*
80 GetCh - gets a character from either the push back stack or
81 from file, f.
82 */
83
84 extern "C" char PushBackInput_GetCh (FIO_File f);
85
86 /*
87 PutCh - pushes a character onto the push back stack, it also
88 returns the character which has been pushed.
89 */
90
91 extern "C" char PushBackInput_PutCh (char ch);
92
93 /*
94 PutString - pushes a string onto the push back stack.
95 */
96
97 extern "C" void PushBackInput_PutString (const char *a_, unsigned int _a_high);
98
99 /*
100 PutStr - pushes a dynamic string onto the push back stack.
101 The string, s, is not deallocated.
102 */
103
104 extern "C" void PushBackInput_PutStr (DynamicStrings_String s);
105
106 /*
107 Error - emits an error message with the appropriate file, line combination.
108 */
109
110 extern "C" void PushBackInput_Error (const char *a_, unsigned int _a_high);
111
112 /*
113 WarnError - emits an error message with the appropriate file, line combination.
114 It does not terminate but when the program finishes an exit status of
115 1 will be issued.
116 */
117
118 extern "C" void PushBackInput_WarnError (const char *a_, unsigned int _a_high);
119
120 /*
121 WarnString - emits an error message with the appropriate file, line combination.
122 It does not terminate but when the program finishes an exit status of
123 1 will be issued.
124 */
125
126 extern "C" void PushBackInput_WarnString (DynamicStrings_String s);
127
128 /*
129 Close - closes the opened file.
130 */
131
132 extern "C" void PushBackInput_Close (FIO_File f);
133
134 /*
135 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
136 */
137
138 extern "C" unsigned int PushBackInput_GetExitStatus (void);
139
140 /*
141 SetDebug - sets the debug flag on or off.
142 */
143
144 extern "C" void PushBackInput_SetDebug (unsigned int d);
145
146 /*
147 GetColumnPosition - returns the column position of the current character.
148 */
149
150 extern "C" unsigned int PushBackInput_GetColumnPosition (void);
151
152 /*
153 GetCurrentLine - returns the current line number.
154 */
155
156 extern "C" unsigned int PushBackInput_GetCurrentLine (void);
157
158 /*
159 ErrChar - writes a char, ch, to stderr.
160 */
161
162 static void ErrChar (char ch);
163
164 /*
165 Init - initialize global variables.
166 */
167
168 static void Init (void);
169
170
171 /*
172 ErrChar - writes a char, ch, to stderr.
173 */
174
175 static void ErrChar (char ch)
176 {
177 FIO_WriteChar (FIO_StdErr, ch);
178 }
179
180
181 /*
182 Init - initialize global variables.
183 */
184
185 static void Init (void)
186 {
187 ExitStatus = 0;
188 StackPtr = 0;
189 LineNo = 1;
190 Column = 0;
191 }
192
193
194 /*
195 Open - opens a file for reading.
196 */
197
198 extern "C" FIO_File PushBackInput_Open (const char *a_, unsigned int _a_high)
199 {
200 char a[_a_high+1];
201
202 /* make a local copy of each unbounded array. */
203 memcpy (a, a_, _a_high+1);
204
205 Init ();
206 StrLib_StrCopy ((const char *) a, _a_high, (char *) &FileName.array[0], MaxFileName);
207 return FIO_OpenToRead ((const char *) a, _a_high);
208 /* static analysis guarentees a RETURN statement will be used before here. */
209 __builtin_unreachable ();
210 }
211
212
213 /*
214 GetCh - gets a character from either the push back stack or
215 from file, f.
216 */
217
218 extern "C" char PushBackInput_GetCh (FIO_File f)
219 {
220 char ch;
221
222 if (StackPtr > 0)
223 {
224 StackPtr -= 1;
225 if (Debugging)
226 {
227 StdIO_Write (CharStack.array[StackPtr]);
228 }
229 return CharStack.array[StackPtr];
230 }
231 else
232 {
233 if ((FIO_EOF (f)) || (! (FIO_IsNoError (f))))
234 {
235 ch = ASCII_nul;
236 }
237 else
238 {
239 do {
240 ch = FIO_ReadChar (f);
241 } while (! (((ch != ASCII_cr) || (FIO_EOF (f))) || (! (FIO_IsNoError (f)))));
242 if (ch == ASCII_lf)
243 {
244 Column = 0;
245 LineNo += 1;
246 }
247 else
248 {
249 Column += 1;
250 }
251 }
252 if (Debugging)
253 {
254 StdIO_Write (ch);
255 }
256 return ch;
257 }
258 /* static analysis guarentees a RETURN statement will be used before here. */
259 __builtin_unreachable ();
260 }
261
262
263 /*
264 PutCh - pushes a character onto the push back stack, it also
265 returns the character which has been pushed.
266 */
267
268 extern "C" char PushBackInput_PutCh (char ch)
269 {
270 if (StackPtr < MaxPushBackStack)
271 {
272 CharStack.array[StackPtr] = ch;
273 StackPtr += 1;
274 }
275 else
276 {
277 Debug_Halt ((const char *) "max push back stack exceeded, increase MaxPushBackStack", 55, 150, (const char *) "../../gcc-git-devel-modula2/gcc/m2/gm2-libs/PushBackInput.mod", 61);
278 }
279 return ch;
280 /* static analysis guarentees a RETURN statement will be used before here. */
281 __builtin_unreachable ();
282 }
283
284
285 /*
286 PutString - pushes a string onto the push back stack.
287 */
288
289 extern "C" void PushBackInput_PutString (const char *a_, unsigned int _a_high)
290 {
291 unsigned int l;
292 char a[_a_high+1];
293
294 /* make a local copy of each unbounded array. */
295 memcpy (a, a_, _a_high+1);
296
297 l = StrLib_StrLen ((const char *) a, _a_high);
298 while (l > 0)
299 {
300 l -= 1;
301 if ((PushBackInput_PutCh (a[l])) != a[l])
302 {
303 Debug_Halt ((const char *) "assert failed", 13, 132, (const char *) "../../gcc-git-devel-modula2/gcc/m2/gm2-libs/PushBackInput.mod", 61);
304 }
305 }
306 }
307
308
309 /*
310 PutStr - pushes a dynamic string onto the push back stack.
311 The string, s, is not deallocated.
312 */
313
314 extern "C" void PushBackInput_PutStr (DynamicStrings_String s)
315 {
316 unsigned int i;
317
318 i = DynamicStrings_Length (s);
319 while (i > 0)
320 {
321 i -= 1;
322 if ((PushBackInput_PutCh (DynamicStrings_char (s, static_cast<int> (i)))) != (DynamicStrings_char (s, static_cast<int> (i))))
323 {
324 Debug_Halt ((const char *) "assert failed", 13, 113, (const char *) "../../gcc-git-devel-modula2/gcc/m2/gm2-libs/PushBackInput.mod", 61);
325 }
326 }
327 }
328
329
330 /*
331 Error - emits an error message with the appropriate file, line combination.
332 */
333
334 extern "C" void PushBackInput_Error (const char *a_, unsigned int _a_high)
335 {
336 char a[_a_high+1];
337
338 /* make a local copy of each unbounded array. */
339 memcpy (a, a_, _a_high+1);
340
341 StdIO_PushOutput ((StdIO_ProcWrite) {(StdIO_ProcWrite_t) ErrChar});
342 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
343 StdIO_Write (':');
344 NumberIO_WriteCard (LineNo, 0);
345 StdIO_Write (':');
346 StrIO_WriteString ((const char *) a, _a_high);
347 StrIO_WriteLn ();
348 StdIO_PopOutput ();
349 FIO_Close (FIO_StdErr);
350 libc_exit (1);
351 }
352
353
354 /*
355 WarnError - emits an error message with the appropriate file, line combination.
356 It does not terminate but when the program finishes an exit status of
357 1 will be issued.
358 */
359
360 extern "C" void PushBackInput_WarnError (const char *a_, unsigned int _a_high)
361 {
362 char a[_a_high+1];
363
364 /* make a local copy of each unbounded array. */
365 memcpy (a, a_, _a_high+1);
366
367 StdIO_PushOutput ((StdIO_ProcWrite) {(StdIO_ProcWrite_t) ErrChar});
368 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
369 StdIO_Write (':');
370 NumberIO_WriteCard (LineNo, 0);
371 StdIO_Write (':');
372 StrIO_WriteString ((const char *) a, _a_high);
373 StrIO_WriteLn ();
374 StdIO_PopOutput ();
375 ExitStatus = 1;
376 }
377
378
379 /*
380 WarnString - emits an error message with the appropriate file, line combination.
381 It does not terminate but when the program finishes an exit status of
382 1 will be issued.
383 */
384
385 extern "C" void PushBackInput_WarnString (DynamicStrings_String s)
386 {
387 typedef char *WarnString__T1;
388
389 WarnString__T1 p;
390
391 p = static_cast<WarnString__T1> (DynamicStrings_string (s));
392 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
393 StdIO_Write (':');
394 NumberIO_WriteCard (LineNo, 0);
395 StdIO_Write (':');
396 do {
397 if (p != NULL)
398 {
399 if ((*p) == ASCII_lf)
400 {
401 StrIO_WriteLn ();
402 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
403 StdIO_Write (':');
404 NumberIO_WriteCard (LineNo, 0);
405 StdIO_Write (':');
406 }
407 else
408 {
409 StdIO_Write ((*p));
410 }
411 p += 1;
412 }
413 } while (! ((p == NULL) || ((*p) == ASCII_nul)));
414 ExitStatus = 1;
415 }
416
417
418 /*
419 Close - closes the opened file.
420 */
421
422 extern "C" void PushBackInput_Close (FIO_File f)
423 {
424 FIO_Close (f);
425 }
426
427
428 /*
429 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
430 */
431
432 extern "C" unsigned int PushBackInput_GetExitStatus (void)
433 {
434 return ExitStatus;
435 /* static analysis guarentees a RETURN statement will be used before here. */
436 __builtin_unreachable ();
437 }
438
439
440 /*
441 SetDebug - sets the debug flag on or off.
442 */
443
444 extern "C" void PushBackInput_SetDebug (unsigned int d)
445 {
446 Debugging = d;
447 }
448
449
450 /*
451 GetColumnPosition - returns the column position of the current character.
452 */
453
454 extern "C" unsigned int PushBackInput_GetColumnPosition (void)
455 {
456 if (StackPtr > Column)
457 {
458 return 0;
459 }
460 else
461 {
462 return Column-StackPtr;
463 }
464 /* static analysis guarentees a RETURN statement will be used before here. */
465 __builtin_unreachable ();
466 }
467
468
469 /*
470 GetCurrentLine - returns the current line number.
471 */
472
473 extern "C" unsigned int PushBackInput_GetCurrentLine (void)
474 {
475 return LineNo;
476 /* static analysis guarentees a RETURN statement will be used before here. */
477 __builtin_unreachable ();
478 }
479
480 extern "C" void _M2_PushBackInput_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
481 {
482 PushBackInput_SetDebug (FALSE);
483 Init ();
484 }
485
486 extern "C" void _M2_PushBackInput_finish (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
487 {
488 }