This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicoSca.c
419 lines (352 loc) · 13 KB
/
PicoSca.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*-----------------------------------*/
/* >>>Pico<<< */
/* Theo D'Hondt */
/* VUB Programming Technology Lab */
/* (c) 1997 */
/*-----------------------------------*/
/* Scanning */
/*-----------------------------------*/
/*-----------------------------------*/
/* integer => <integer> */
/* float => <float> */
/* text => <string> */
/* name => <string> */
/* relop => <string> */
/* mulop => <string> */
/* addop => <string> */
/* left-parenthesis => <null> */
/* right-parenthesis => <null> */
/* left-bracket => <null> */
/* right-bracket => <null> */
/* left-brace => <null> */
/* right-brace => <null> */
/* colon => <null> */
/* colon-equal => <null> */
/* semicolon => <null> */
/* comma => <null> */
/* commercial-at => <null> */
/* end-of-entry => <null> */
/*-----------------------------------*/
#include <stdlib.h>
#include <string.h>
#include "Pico.h"
#include "PicoMai.h"
#include "PicoSca.h"
/* private macros */
#define SCAN_CHECK(allowed)\
(scan_ch && ((allowed >> scan_char_tab[scan_ch]) & 1))
#define SCAN_UNCHECK(allowed)\
(scan_ch && !((allowed >> scan_char_tab[scan_ch]) & 1))
#define SCAN_NEXT_CH()\
(scan_ch = scan_text[scan_stop]) ? scan_stop++ : 0
/* private types */
typedef enum scan_fun_index { Aop = 0,
Apo = 1,
Bkq = 2,
Cat = 3,
Col = 4,
Com = 5,
Dgt = 6,
Eol = 7,
Eql = 8,
Exp = 9,
Ill = 10,
Lbc = 11,
Lbr = 12,
Lpr = 13,
Ltr = 14,
Mns = 15,
Mop = 16,
Per = 17,
Pls = 18,
Quo = 19,
Rbc = 20,
Rbr = 21,
Rop = 22,
Rpr = 23,
Smc = 24,
Wsp = 25,
Xop = 26 } scan_fun_index;
typedef _TKN_TYPE_ scan_fun(_NIL_TYPE_);
/* private prototypes */
static _TKN_TYPE_ scan_Aop(_NIL_TYPE_);
static _TKN_TYPE_ scan_Apo(_NIL_TYPE_);
static _TKN_TYPE_ scan_Bkq(_NIL_TYPE_);
static _TKN_TYPE_ scan_Cat(_NIL_TYPE_);
static _TKN_TYPE_ scan_Col(_NIL_TYPE_);
static _TKN_TYPE_ scan_Com(_NIL_TYPE_);
static _TKN_TYPE_ scan_Dgt(_NIL_TYPE_);
static _TKN_TYPE_ scan_Ill(_NIL_TYPE_);
static _TKN_TYPE_ scan_Ltr(_NIL_TYPE_);
static _TKN_TYPE_ scan_Lbc(_NIL_TYPE_);
static _TKN_TYPE_ scan_Lbr(_NIL_TYPE_);
static _TKN_TYPE_ scan_Lpr(_NIL_TYPE_);
static _TKN_TYPE_ scan_Mop(_NIL_TYPE_);
static _TKN_TYPE_ scan_Quo(_NIL_TYPE_);
static _TKN_TYPE_ scan_Rbc(_NIL_TYPE_);
static _TKN_TYPE_ scan_Rbr(_NIL_TYPE_);
static _TKN_TYPE_ scan_Rop(_NIL_TYPE_);
static _TKN_TYPE_ scan_Rpr(_NIL_TYPE_);
static _TKN_TYPE_ scan_Smc(_NIL_TYPE_);
static _TKN_TYPE_ scan_Wsp(_NIL_TYPE_);
static _TKN_TYPE_ scan_Xop(_NIL_TYPE_);
static _TKN_TYPE_ scan_Per(_NIL_TYPE_); //TOEGEVOEGD
/* private variables */
static _CHA_TYPE_ scan_ch = 0;
static _STR_TYPE_ scan_text = 0;
static _SGN_TYPE_ scan_mark = 0;
static _POS_TYPE_ scan_index = 0;
static _POS_TYPE_ scan_start = 0;
static _POS_TYPE_ scan_stop = 0;
static const scan_fun_index scan_char_tab[]
/*NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI */
= {Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Eol,Wsp,Wsp,
/*DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US */
Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,Wsp,
/* ! " # $ % & ' ( ) * + , - . / */
Wsp,Xop,Quo,Rop,Aop,Aop,Mop,Apo,Lpr,Rpr,Mop,Pls,Com,Mns,Per,Mop,
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
Dgt,Dgt,Dgt,Dgt,Dgt,Dgt,Dgt,Dgt,Dgt,Dgt,Col,Smc,Rop,E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at,
scan_Col,
scan_Com,
scan_Dgt,
scan_Wsp,
scan_Rop,
scan_Ltr,
scan_Ill,
scan_Lbc,
scan_Lbr,
scan_Lpr,
scan_Ltr,
scan_Aop,
scan_Mop,
scan_Per, //AANGEPAST: was Scan_ill. Een punt is nu toegelaten.
scan_Aop,
scan_Quo,
scan_Rbc,
scan_Rbr,
scan_Rop,
scan_Rpr,
scan_Smc,
scan_Wsp,
scan_Xop };
static const _UNS_TYPE_ operator_mask = (1<<Aop)+(1<<Eql)+(1<<Mns)+(1<<Mop)+
(1<<Pls)+(1<<Rop)+(1<<Xop);
static const _UNS_TYPE_ name_mask = (1<<Dgt)+(1<<Exp)+(1<<Ltr);
static const _UNS_TYPE_ eol_apo_mask = (1<<Apo)+(1<<Eol);
static const _UNS_TYPE_ apo_mask = (1<<Apo);
static const _UNS_TYPE_ eol_bkq_mask = (1<<Bkq)+(1<<Eol);
static const _UNS_TYPE_ bkq_mask = (1<<Bkq);
static const _UNS_TYPE_ eol_quo_mask = (1<<Eol)+(1<<Quo);
static const _UNS_TYPE_ quo_mask = (1<<Quo);
static const _UNS_TYPE_ digit_mask = (1<<Dgt);
static const _UNS_TYPE_ sign_mask = (1<<Mns)+(1<<Pls);
static const _UNS_TYPE_ period_mask = (1<<Per);
static const _UNS_TYPE_ exponent_mask = (1<<Exp);
static const _UNS_TYPE_ equal_mask = (1<<Eql);
static const _UNS_TYPE_ wsp_mask = (1<<Eol)+(1<<Wsp);
/* private functions */
static _NIL_TYPE_ scan_store_next_ch(_NIL_TYPE_)
{ _TEXT_BUF_[scan_index++] = scan_ch;
if (scan_index == _BUF_SIZE_)
_scan_error_(_BUF_ERROR_);
else
SCAN_NEXT_CH(); }
static _NIL_TYPE_ scan_terminate_text(_NIL_TYPE_)
{ _TEXT_BUF_[scan_index] = '\0';
scan_index = 0; }
static _NIL_TYPE_ scan_operator(_NIL_TYPE_)
{ do
scan_store_next_ch();
while (SCAN_CHECK(operator_mask));
scan_terminate_text(); }
static _NIL_TYPE_ scan_name(_NIL_TYPE_)
{ do
scan_store_next_ch();
while (SCAN_CHECK(name_mask));
scan_terminate_text(); }
static _TKN_TYPE_ scan_exponent(_NIL_TYPE_)
{ scan_store_next_ch();
if (SCAN_CHECK(sign_mask))
scan_store_next_ch();
if (SCAN_CHECK(digit_mask))
do
scan_store_next_ch();
while (SCAN_CHECK(digit_mask));
else
_scan_error_(_DIG_ERROR_);
scan_terminate_text();
return _FRC_TOKEN_; }
static _TKN_TYPE_ scan_fraction(_NIL_TYPE_)
{ scan_store_next_ch();
if (SCAN_CHECK(digit_mask))
do
scan_store_next_ch();
while (SCAN_CHECK(digit_mask));
else
_scan_error_(_DIG_ERROR_);
if (SCAN_CHECK(exponent_mask))
return scan_exponent();
else
{ scan_terminate_text();
return _FRC_TOKEN_; }}
static _TKN_TYPE_ scan_number(_NIL_TYPE_)
{ do
scan_store_next_ch();
while (SCAN_CHECK(digit_mask));
if (SCAN_CHECK(period_mask))
return scan_fraction();
else
if (SCAN_CHECK(exponent_mask))
return scan_exponent();
else
{ scan_terminate_text();
return _NBR_TOKEN_; }}
static _TKN_TYPE_ scan_Aop(_NIL_TYPE_)
{ scan_operator();
return _AOP_TOKEN_; }
static _TKN_TYPE_ scan_Apo(_NIL_TYPE_)
{ SCAN_NEXT_CH();
while (SCAN_UNCHECK(eol_apo_mask))
scan_store_next_ch();
if (SCAN_CHECK(apo_mask))
SCAN_NEXT_CH();
else
_scan_error_(_TXT_ERROR_);
scan_terminate_text();
return _TXT_TOKEN_; }
static _TKN_TYPE_ scan_Bkq(_NIL_TYPE_)
{ do
SCAN_NEXT_CH();
while (SCAN_UNCHECK(eol_bkq_mask));
if (SCAN_CHECK(bkq_mask))
SCAN_NEXT_CH();
return _scan_(); }
static _TKN_TYPE_ scan_Cat(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _CAT_TOKEN_; }
static _TKN_TYPE_ scan_Col(_NIL_TYPE_)
{ SCAN_NEXT_CH();
if (SCAN_CHECK(equal_mask))
{ SCAN_NEXT_CH();
return _CEQ_TOKEN_; }
else
return _COL_TOKEN_; }
static _TKN_TYPE_ scan_Com(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _COM_TOKEN_; }
static _TKN_TYPE_ scan_Dgt(_NIL_TYPE_)
{ return scan_number(); }
static _TKN_TYPE_ scan_Ill(_NIL_TYPE_)
{ _scan_error_(_ILL_ERROR_);
return _END_TOKEN_; }
static _TKN_TYPE_ scan_Lbc(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _LBC_TOKEN_; }
static _TKN_TYPE_ scan_Lbr(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _LBR_TOKEN_; }
static _TKN_TYPE_ scan_Lpr(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _LPR_TOKEN_; }
static _TKN_TYPE_ scan_Ltr(_NIL_TYPE_)
{ scan_name();
return _NAM_TOKEN_; }
static _TKN_TYPE_ scan_Mop(_NIL_TYPE_)
{ scan_operator();
return _MOP_TOKEN_; }
static _TKN_TYPE_ scan_Quo(_NIL_TYPE_)
{ SCAN_NEXT_CH();
while (SCAN_UNCHECK(eol_quo_mask))
scan_store_next_ch();
if (SCAN_CHECK(quo_mask))
SCAN_NEXT_CH();
else
_scan_error_(_TXT_ERROR_);
scan_terminate_text();
return _TXT_TOKEN_; }
static _TKN_TYPE_ scan_Rbc(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _RBC_TOKEN_; }
static _TKN_TYPE_ scan_Rbr(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _RBR_TOKEN_; }
static _TKN_TYPE_ scan_Rop(_NIL_TYPE_)
{ scan_operator();
return _ROP_TOKEN_; }
static _TKN_TYPE_ scan_Rpr(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _RPR_TOKEN_; }
static _TKN_TYPE_ scan_Smc(_NIL_TYPE_)
{ SCAN_NEXT_CH();
return _SMC_TOKEN_; }
static _TKN_TYPE_ scan_Wsp(_NIL_TYPE_)
{ do
SCAN_NEXT_CH();
while (SCAN_CHECK(wsp_mask));
if (scan_ch)
return _scan_();
else
return _END_TOKEN_; }
static _TKN_TYPE_ scan_Xop(_NIL_TYPE_)
{ scan_operator();
return _XOP_TOKEN_; }
static _TKN_TYPE_ scan_Per(_NIL_TYPE_) //TOEGEVOEGD: Skip de punt en geef een _PER_TOKEN_ terug.
{ SCAN_NEXT_CH();
return _PER_TOKEN_; }
/* public functions */
_NIL_TYPE_ _scan_init_(const _STR_TYPE_ str,
const _SGN_TYPE_ tst)
{ scan_start = 0;
scan_stop = 0;
scan_index = 0;
scan_mark = tst;
scan_text = str;
SCAN_NEXT_CH(); }
_TKN_TYPE_ _scan_(_NIL_TYPE_)
{ scan_fun_index index;
if (scan_ch)
{ index = scan_char_tab[scan_ch];
scan_start = scan_stop;
return (*scan_fun_tab[index])(); }
else
return _END_TOKEN_; }
_NIL_TYPE_ _scan_error_(const _RES_TYPE_ err)
{ if (scan_mark && scan_start)
_mark_(scan_start-1, scan_stop-1);
scan_index = 0;
_error_(err); }
_SGN_TYPE_ _scan_number_(_NIL_TYPE_)
{ return atoi(_TEXT_BUF_); }
_FLO_TYPE_ _scan_fraction_(_NIL_TYPE_)
{ return atof(_TEXT_BUF_); }
_STR_TYPE_ _scan_string_(_NIL_TYPE_)
{ return _TEXT_BUF_; }