source: buchla-68k/ram/cminit.c@ e225e77

Last change on this file since e225e77 was e225e77, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Added missing includes and declarations.

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/*
2 =============================================================================
3 cminit.c -- C-Meta parser functions
4 Version 5 -- 1987-07-10 -- D.N. Lynx Crowe
5
6 Some parser functions in C, modelled after HyperMeta(tm).
7 Designed for, but not dedicated to, LR(k), top-down, recursive parsing.
8 =============================================================================
9*/
10
11#include <stddefs.h>
12#include <ctype.h>
13
14#include "stdio.h"
15
16#define CM_MXSTR 257 /* maximum parser string result length */
17
18#define CM_DBLK if (!QQanch) while (*QQip EQ ' ') ++QQip
19
20int16_t QQsw; /* parser result switch */
21int16_t QQanch; /* parser anchored match switch */
22
23int8_t *QQin; /* parser initial input pointer */
24int8_t *QQip; /* parser current input pointer */
25int8_t *QQop; /* parser string output pointer */
26
27int32_t QQnum; /* parser numeric result */
28int16_t QQlnum; /* parser list element number result */
29int8_t QQdig; /* parser digit result */
30int8_t QQchr; /* parser character result */
31
32int8_t QQstr[CM_MXSTR]; /* parser string result */
33
34/*
35
36*/
37
38/*
39 =============================================================================
40 CMinit(ip) -- initialize the parser to work on the string at 'ip'.
41 =============================================================================
42*/
43
44void CMinit(int8_t *ip)
45{
46 register int16_t i;
47 register int8_t *t;
48
49 QQip = ip;
50 QQin = ip;
51 QQsw = TRUE;
52 QQanch = FALSE;
53 QQdig = '?';
54 QQchr = '?';
55 QQnum = 0;
56 QQop = QQstr;
57 t = QQstr;
58
59 for (i = 0; i < CM_MXSTR; i++)
60 *t++ = '\0';
61}
62
63/*
64
65*/
66
67/*
68 =============================================================================
69 CMchr(c) -- attempt to match character 'c' in the input.
70 =============================================================================
71*/
72
73int16_t CMchr(int8_t c)
74{
75 CM_DBLK;
76
77 if (c NE *QQip)
78 return(QQsw = FALSE);
79
80 QQchr = *QQip++;
81 return(QQsw = TRUE);
82}
83
84/*
85
86*/
87
88/*
89 =============================================================================
90 CMuchr(c) -- attempt to match character 'c' in the input, ignoring case.
91 =============================================================================
92*/
93
94int16_t CMuchr(int8_t c)
95{
96 register int8_t t;
97
98 CM_DBLK;
99
100 t = *QQip;
101
102 if (isascii(c))
103 if (isupper(c))
104 c = _tolower(c);
105
106 if (isascii(t))
107 if (isupper(t))
108 t = _tolower(t);
109
110 if (c NE t)
111 return(QQsw = FALSE);
112
113 QQchr = *QQip++;
114 return(QQsw = TRUE);
115}
116
117/*
118
119*/
120
121/*
122 =============================================================================
123 CMstr(s) -- attempt to match string at 's' in the input.
124 =============================================================================
125*/
126
127int16_t CMstr(int8_t *s)
128{
129 register int8_t *t;
130 int8_t *q;
131
132 CM_DBLK;
133
134 t = QQip;
135 q = s;
136
137 while (*s) {
138
139 if (*t++ NE *s++)
140 return(QQsw = FALSE);
141 }
142
143 QQop = QQstr;
144
145 while (*QQop++ = *q++) ;
146
147 QQip = t;
148 return(QQsw = TRUE);
149}
150
151/*
152
153*/
154
155/*
156 =============================================================================
157 CMustr(s) -- attempt to match string 's' in the input, ignoring case.
158 =============================================================================
159*/
160
161int16_t CMustr(int8_t *s)
162{
163 register int8_t *t, t1, t2;
164 int8_t *q;
165
166 CM_DBLK;
167
168 t = QQip;
169 q = s;
170
171 while (*s) {
172
173 t1 = *t++;
174 t2 = *s++;
175
176 if (isascii(t1))
177 if (isupper(t1))
178 t1 = _tolower(t1);
179
180 if (isascii(t2))
181 if (isupper(t2))
182 t2 = _tolower(t2);
183
184 if (t1 NE t2)
185 return(QQsw = FALSE);
186 }
187
188 QQop = QQstr;
189
190 while (*QQop++ = *q++) ;
191
192 QQip = t;
193 return(QQsw = TRUE);
194}
195
196/*
197
198*/
199
200/*
201 =============================================================================
202 CMlong() -- attempt to parse a digit string in the input as a long.
203 =============================================================================
204*/
205
206int16_t CMlong(void)
207{
208 register int8_t *p;
209 register int32_t n;
210 register int8_t c;
211
212 CM_DBLK;
213
214 p = QQip;
215 n = 0L;
216 c = *p++;
217
218 if (!isascii(c))
219 return(QQsw = FALSE);
220
221 if (!isdigit(c))
222 return(QQsw = FALSE);
223
224 n = c - '0';
225
226 while (c = *p) {
227
228 if (!isascii(c))
229 break;
230
231 if (!isdigit(c))
232 break;
233
234 n = (n * 10) + (c - '0');
235 ++p;
236 }
237
238 QQip = p;
239 QQnum = n;
240 return(QQsw = TRUE);
241}
242
243/*
244
245*/
246
247/*
248 =============================================================================
249 CMdig() -- attempt to match a digit in the input string.
250 =============================================================================
251*/
252
253int16_t CMdig(void)
254{
255 register int8_t c;
256
257 CM_DBLK;
258
259 c = *QQip;
260
261 if (!isascii(c))
262 return(QQsw = FALSE);
263
264 if (!isdigit(c))
265 return(QQsw = FALSE);
266
267 QQdig = c;
268 ++QQip;
269 return(QQsw = TRUE);
270}
271
272/*
273
274*/
275
276/*
277 =============================================================================
278 CMlist(l) -- attempt to match a string from the list 'l' in the input.
279 =============================================================================
280*/
281
282int16_t CMlist(int8_t *l[])
283{
284 register int16_t n;
285 register int8_t *p, *q;
286
287 CM_DBLK;
288
289 n = 0;
290
291 while (p = *l++) {
292
293 q = p;
294
295 if (CMstr(p)) {
296
297 QQop = QQstr;
298
299 while (*QQop++ = *q++) ;
300
301 QQlnum = n;
302 return(QQsw = TRUE);
303 }
304
305 ++n;
306 }
307
308 return(QQsw = FALSE);
309}
310
311/*
312
313*/
314
315/*
316 =============================================================================
317 CMulist(l) -- attempt to match a string from the list 'l' in the input
318 ignoring case in both the list and the input string.
319 =============================================================================
320*/
321
322int16_t CMulist(int8_t *l[])
323{
324 register int16_t n;
325 register int8_t *p, *q;
326
327 CM_DBLK;
328
329 n = 0;
330
331 while (p = *l++) {
332
333 q = p;
334
335 if (CMustr(p)) {
336
337 QQop = QQstr;
338
339 while (*QQop++ = *q++) ;
340
341 QQlnum = n;
342 return(QQsw = TRUE);
343 }
344
345 ++n;
346 }
347
348 return(QQsw = FALSE);
349}
350
351/*
352
353*/
354
355/*
356 =============================================================================
357 CMstat(msg) -- output 'msg and dump parser status. Returns QQsw.
358 =============================================================================
359*/
360
361int16_t CMstat(int8_t *msg)
362{
363 register int8_t *tp;
364
365 tp = QQin;
366 printf("%s\r\n", msg);
367 printf(" QQsw: %s, QQanch: %s, QQchr: 0x%02x <%c>, QQdig: %c\r\n",
368 (QQsw ? "OK" : "NOGO"), (QQanch ? "anchored" : "deblanked"),
369 QQchr, (isascii(QQchr) ? (isprint(QQchr) ? QQchr : ' ') : ' '),
370 QQdig);
371 printf(" QQnum: %ld, QQlnum: %d\r\n", QQnum, QQlnum);
372 printf(" QQstr: %s\r\n", QQstr);
373 printf(" {%s}\r\n", QQin);
374 printf(" ");
375
376 while (tp++ NE QQip)
377 printf(" ");
378
379 printf("^\r\n");
380 return(QQsw);
381}
Note: See TracBrowser for help on using the repository browser.