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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
/*
Ronny Wichers Schreur
University of Nijmegen
*/
#pragma segment scanner
# include <stdio.h>
# include <string.h>
# include <ctype.h>
# include <limits.h>
# undef H
# include "compiledefines.h"
# include "types.t"
#if defined (applec) || (defined (__MWERKS__) && !defined (_WINDOWS_)) || defined (__MRC__)
# define __ppc__
#endif
# include "system.h"
# include "syntaxtr.t"
# include "comsupport.h"
# include "scanner.h"
# include "sizes.h"
# if (defined (__MWERKS__) || defined (__MRC__) || (defined (GNU_C) && defined (_MAC_))) && !defined _WINDOWS_ /* && !defined (MAKE_MPW_TOOL) */
# define CACHE_DCL_FILES
# define CACHE_INLINE_FILES
# else
# undef CACHE_DCL_FILES
# undef CACHE_INLINE_FILES
# endif
char **ReservedWords;
static IdentP
NewIdent (TableKind tableKind, char *name)
{
IdentP ident;
ident = CompAllocType (struct ident);
ident->ident_table = tableKind;
ident->ident_name = name;
ident->ident_next = NULL;
ident->ident_environ = NULL;
ident->ident_symbol = NULL;
ident->ident_mark = 0;
return (ident);
} /* NewIdent */
#define CompAllocString(size) ((char*)CompAlloc(size))
static char *
AllocString (char *string, short length)
{
int i;
char *s, *newString;
s = newString = CompAllocString (length+1);
for (i = 0; i < length; i++)
*s++ = *string++;
*s = '\0';
return (newString);
} /* AllocString */
# define kIdentStringTableSizeBits 10
# define kIdentStringTableSize ((1 << kIdentStringTableSizeBits) - 1)
static IdentStringP *gIdentStringTable;
static IdentStringP
StringInTable (char *string, short length)
{
int i;
unsigned long hash;
IdentStringP identString, *identStringPtr;
char *s;
hash = 0;
s = string;
for (i = 0; i < length; i++)
{
hash <<= 2;
hash += *s++;
}
/* Compute (hash % kIdentStringTableSize) */
while (hash >= (kIdentStringTableSize<<1))
hash = (hash & kIdentStringTableSize) + (hash >> kIdentStringTableSizeBits);
if (hash >= kIdentStringTableSize)
hash -= kIdentStringTableSize;
identStringPtr = &gIdentStringTable [hash];
while ((identString = *identStringPtr) != NIL)
{
int compare;
compare = strncmp (identString->string, string, length);
if (compare == 0 && (compare = ((unsigned char *)identString->string) [length]) == 0)
/* found it */
break;
else if (compare > 0)
identStringPtr = &identString->left;
else /* if (compare < 0) */
identStringPtr = &identString->right;
}
if (identString == NIL)
{
identString = CompAllocType (struct ident_string);
identString->left = NIL;
identString->right = NIL;
identString->ident = NIL;
identString->string = AllocString (string, length);
*identStringPtr = identString;
}
return (identString);
} /* StringInTable */
IdentP
PutIdentStringInTable (IdentStringP identString, TableKind tableKind)
{
IdentP ident;
for (ident = identString->ident; ident != NIL; ident = ident->ident_next)
if (ident->ident_table == tableKind)
break;
if (ident == NIL)
{
ident = NewIdent (tableKind, identString->string);
ident->ident_next = identString->ident;
identString->ident = ident;
}
return (ident);
} /* PutIdentStringInTable */
IdentP
PutStringInHashTable (char *string, TableKind tableKind)
{
IdentStringP identString;
identString = StringInTable (string, strlen (string));
return (PutIdentStringInTable (identString, tableKind));
} /* PutStringInHashTable */
STRUCT (keyWordInfo, KeyWordInfo)
{
char *name;
Token token;
};
static void
PutKeyWordInTable (KeyWordInfoP keyWord)
{
IdentStringP identString;
IdentP ident;
identString = StringInTable (keyWord->name, strlen (keyWord->name));
ident = NewIdent (KeyWordTable, identString->string);
ident->ident_next = identString->ident;
ident->ident_environ = NIL;
ident->ident_symbol = (struct symbol *) keyWord->token;
identString->ident = ident;
} /* PutKeyWordInTable */
IdentP
RetrieveFromSymbolTable (char *string)
{
char *s;
unsigned long hash;
IdentStringP identString;
IdentP ident;
hash = 0;
for (s = string; *s != '\0'; s++)
{
hash <<= 2;
hash += *s;
}
/* Compute (hash % 1023) */
while (hash >= 2046)
hash = (hash & 1023) + (hash >> 10);
if (hash >= 1023)
hash -= 1023;
identString = gIdentStringTable [hash];
while (identString != NIL)
{
int compare;
compare = strcmp (identString->string, string);
if (compare == 0)
/* found it */
break;
else if (compare > 0)
identString = identString->left;
else /* if (compare < 0) */
identString = identString->right;
}
if (identString != NIL)
{
for (ident = identString->ident; ident != NIL; ident = ident->ident_next)
if (ident->ident_table == SymbolIdTable)
break;
}
else
ident = NIL;
return (ident);
} /* RetrieveFromSymbolTable */
/*
+-----------------------------------------------------------------------+
| ReadInlineCode scans all the imported SYSTEM modules and stores the |
| the encountered inline instructions in the symbol table. |
+-----------------------------------------------------------------------+
*/
char NextLine[LineLength];
/* has a command been read? */
static char *IsCommand (char *com, char *p)
{
while (*com++ == *p++)
if (*com == '\0')
return (p);
return ((char *) NIL);
}
/* scan a file for .inline-.end command pairs */
char *InlineCodeBuffer;
unsigned InlineBufferIndex, InlineBufferStart;
#ifdef CACHE_INLINE_FILES
struct inline_cache_list {
struct inline_cache_list * icache_next;
struct file_block * icache_file_blocks;
#if defined (__MWERKS__) || defined (THINK_C) || defined (__MRC__)
char icache_file_name[];
#else
char icache_file_name[0];
#endif
};
#define BUFFER_SIZE 1024
struct file_block {
int file_block_size;
struct file_block * file_block_next;
char file_block_data[BUFFER_SIZE];
};
struct file_block **next_file_block_l;
static int reading_from_cache=0;
static struct inline_cache_list * inline_cache=NULL;
static File inline_file;
static int chars_left_in_buffer;
static int end_of_file;
static char *buffer_p;
static int open_inline_file_for_block_reading (char *file_name)
{
struct inline_cache_list **icache_elem_p,*new_icache_elem;
int file_name_length;
chars_left_in_buffer=0;
end_of_file=0;
reading_from_cache=0;
for (icache_elem_p=&inline_cache; *icache_elem_p;
icache_elem_p=&(*icache_elem_p)->icache_next)
{
if (!strcmp ((*icache_elem_p)->icache_file_name,file_name)){
reading_from_cache=1;
next_file_block_l=&(*icache_elem_p)->icache_file_blocks;
return 1;
}
}
inline_file = FOpen (file_name, abcFile, "r");
if (inline_file==NULL)
return 0;
#if defined (THINK_C) || defined (POWER)
setvbuf (inline_file,NULL,_IOFBF,8192);
#endif
file_name_length=strlen (file_name);
new_icache_elem=(struct inline_cache_list*)Alloc (1,sizeof (struct inline_cache_list)+file_name_length+1);
strcpy (new_icache_elem->icache_file_name,file_name);
new_icache_elem->icache_next=NULL;
new_icache_elem->icache_file_blocks=NULL;
*icache_elem_p=new_icache_elem;
next_file_block_l=&new_icache_elem->icache_file_blocks;
return 1;
}
static int get_line_from_inline_file (char *line_buffer,int line_length)
{
char *line_buffer_p;
line_buffer_p=line_buffer;
for (;;){
while (chars_left_in_buffer>0){
char c;
c=*buffer_p++;
--chars_left_in_buffer;
if (c=='\r')
c='\n';
if (line_length>1){
--line_length;
*line_buffer_p++=c;
}
if (c=='\n'){
*line_buffer_p=0;
return 1;
}
}
if (!reading_from_cache){
struct file_block *file_block;
if (end_of_file){
*line_buffer_p=0;
return line_buffer!=line_buffer_p;
}
file_block=(struct file_block*)Alloc (1,sizeof (struct file_block));
chars_left_in_buffer=FRead (file_block->file_block_data,1,BUFFER_SIZE,inline_file);
buffer_p=file_block->file_block_data;
file_block->file_block_size=chars_left_in_buffer;
file_block->file_block_next=NULL;
end_of_file = chars_left_in_buffer!=BUFFER_SIZE;
*next_file_block_l=file_block;
next_file_block_l=&file_block->file_block_next;
} else {
struct file_block *file_block;
file_block=*next_file_block_l;
if (file_block==NULL){
*line_buffer_p=0;
return line_buffer!=line_buffer_p;
}
chars_left_in_buffer=file_block->file_block_size;
buffer_p=file_block->file_block_data;
if (chars_left_in_buffer==0){
*line_buffer_p=0;
return line_buffer!=line_buffer_p;
}
next_file_block_l=&file_block->file_block_next;
}
};
}
extern void clear_inline_cache (void);
void clear_inline_cache (void)
{
struct inline_cache_list *icache_elem,*next_icache_elem;
icache_elem=inline_cache;
inline_cache=NULL;
while (icache_elem!=NULL){
struct file_block *icache_file_blocks,*next_icache_file_block;
next_icache_elem=icache_elem->icache_next;
icache_file_blocks=icache_elem->icache_file_blocks;
icache_elem->icache_file_blocks=NULL;
Free (icache_elem);
while (icache_file_blocks!=NULL){
next_icache_file_block=icache_file_blocks->file_block_next;
Free (icache_file_blocks);
icache_file_blocks=next_icache_file_block;
}
icache_elem=next_icache_elem;
}
}
#endif
void ScanInlineFile (char *fname)
{
register char *tail, *instr, *importingModule, *importingExtension;
IdentP instrid;
int nrinstr;
#ifndef CACHE_INLINE_FILES
File f;
#endif
importingModule = CurrentModule;
importingExtension = CurrentExt;
CurrentModule = fname;
CurrentExt = GetFileExtension (abcFile);
#ifdef CACHE_INLINE_FILES
if (!open_inline_file_for_block_reading (fname))
#else
if (! (f = FOpen (fname, abcFile, "r")))
#endif
{ CurrentModule = importingModule;
CurrentExt = importingExtension;
return;
}
#ifndef CACHE_INLINE_FILES
# if defined (THINK_C) || defined (POWER)
setvbuf ((void*) f, NULL, _IOFBF, 8192);
# endif
#endif
CurrentLine = 0;
CurrentPhase = NULL;
for (;;){
#ifdef CACHE_INLINE_FILES
if (!get_line_from_inline_file (NextLine,LineLength))
#else
if (! FGetS (NextLine, LineLength, f))
#endif
break;
for (tail = NextLine; isspace (*tail); tail++)
;
/* if not at .inline reenter loop from top */
if ((tail = IsCommand (".inline", tail)) == NIL)
continue;
/* get the function name */
while (*tail == ' ' || *tail == '\t')
tail++;
/* terminate it with a '\0' */
for (instr = tail; ! isspace (*tail); tail++)
;
if (instr == tail)
continue;
*tail = '\0';
if (! (instrid = RetrieveFromSymbolTable (instr)))
continue;
if (instrid->ident_environ!=importingModule)
continue;
if ((instrid->ident_mark & INLINE_MASK) != 0)
{
StaticMessage (True, "%s", "multiple .inline directives", instr);
continue;
}
instrid->ident_mark |= INLINE_MASK;
/* Open the buffer for the next instructions */
InlineBufferIndex = InlineBufferStart;
for (nrinstr = 0; nrinstr <= MaxInlineInstr;){
#ifdef CACHE_INLINE_FILES
if (!get_line_from_inline_file (NextLine,LineLength)){
#else
if (! FGetS (NextLine, LineLength, f)){
#endif
StaticMessage (False, "%s", "%s no .end found in this file", instrid->ident_name,fname);
break;
}
for (tail = NextLine; *tail == ' ' || *tail == '\t'; tail++)
;
if (IsCommand (".end", tail))
break;
if (*tail != '\n' && *tail != '\0'){
instr = NextLine;
/* Copy this instruction into the buffer */
do
{ if (InlineBufferIndex < InlineBuffSize-2)
InlineCodeBuffer [InlineBufferIndex++] = *instr++;
else
DoFatalError ("too many inline instructions");
} while (*instr != '\n' && *instr != '\0');
/* close the instruction with a newline character */
InlineCodeBuffer [InlineBufferIndex++] = '\n';
nrinstr++;
}
}
if (nrinstr > MaxInlineInstr){
StaticMessage (False, "%s", "%s file contains too many instructions", instrid->ident_name,fname);
}
/* save the list of inline instructions */
/* if (InlineBufferIndex != InlineBufferStart){ */
instrid->ident_instructions = &InlineCodeBuffer [InlineBufferStart];
InlineBufferStart = InlineBufferIndex+1;
/* close the list with the NULL character */
InlineCodeBuffer [InlineBufferIndex] = '\0';
/* } */
}
#ifdef CACHE_INLINE_FILES
if (!reading_from_cache)
FClose (inline_file);
#else
FClose (f);
#endif
CurrentModule = importingModule;
CurrentExt = importingExtension;
}
void
ScanInitIdentStringTable (void)
{
int i;
/*
RWS +++ clean up symbols
*/
ReservedWords = (char **) CompAlloc ((unsigned long) NumberOfKeywords * SizeOf (char *));
ReservedWords [(int) errorsym] = "Erroneous";
ReservedWords [(int) barsym] = "|";
ReservedWords [(int) strictsym] = "!";
ReservedWords [(int) opensym] = "(";
ReservedWords [(int) closesym] = ")";
ReservedWords [(int) opensquaresym] = "[";
ReservedWords [(int) closesquaresym] = "]";
ReservedWords [(int) colonsym] = ":";
ReservedWords [(int) typesym] = "::";
ReservedWords [(int) semicolonsym] = ";";
ReservedWords [(int) commasym] = ",";
ReservedWords [(int) dotsym] = ".";
ReservedWords [(int) openbracesym] = "{";
ReservedWords [(int) closebracesym] = "}";
ReservedWords [(int) arrowsym] = "->";
ReservedWords [(int) abstypesym] = "AbsType";
ReservedWords [(int) arraysym] = "{ }";
ReservedWords [(int) strictarraysym] = "{ ! }";
ReservedWords [(int) unboxedarraysym] = "{ # }";
ReservedWords [(int) atsym] = "at";
ReservedWords [(int) boolsym] = "Bool";
ReservedWords [(int) charsym] = "Char";
ReservedWords [(int) codesym] = "code";
ReservedWords [(int) defsym] = "definition";
ReservedWords [(int) falsesym] = "False";
ReservedWords [(int) filesym] = "File";
ReservedWords [(int) allsym] = "All";
ReservedWords [(int) fromsym] = "from";
/* RWS ... hack */
ReservedWords [(int) ifsym] = "if ";
/* ... RWS */
ReservedWords [(int) impsym] = "implementation";
ReservedWords [(int) importsym] = "import";
ReservedWords [(int) intsym] = "Int";
ReservedWords [(int) macrosym] = "macro";
ReservedWords [(int) modulesym] = "module";
ReservedWords [(int) procidsym] = "ProcId";
ReservedWords [(int) redidsym] = "RedId";
ReservedWords [(int) realsym] = "Real";
ReservedWords [(int) rulesym] = "rule";
/* */
ReservedWords [(int) stringsym] = "_STRING";
/* */
ReservedWords [(int) systemsym] = "system";
ReservedWords [(int) truesym] = "True";
ReservedWords [(int) typedefsym] = "type";
ReservedWords [(int) applysym] = "=>";
ReservedWords [(int) uniquesym] = "*";
ReservedWords [(int) worldsym] = "World";
gIdentStringTable = (struct ident_string**)CompAlloc (kIdentStringTableSize * sizeof (struct ident_string));
for (i = 0; i < kIdentStringTableSize; i++)
gIdentStringTable [i] = NIL;
}
static KeyWordInfoS gKeyWords [] =
{
{ "export", kTokenExport },
{ "import", kTokenImport },
{ "from", kTokenFrom },
{ "definition", kTokenDefinition },
{ "implementation", kTokenImplementation },
{ "system", kTokenSystem },
{ "module", kTokenModule },
{ "let", kTokenLet },
{ "in", kTokenIn },
{ "case", kTokenCase },
{ "of", kTokenOf },
{ "if", kTokenIf },
{ "with", kTokenWith },
{ "where", kTokenWhere },
{ "code", kTokenCode },
{ "True", kTokenTrue },
{ "False", kTokenFalse },
/* { "overload", kTokenOverload }, */
{ "instance", kTokenInstance },
{ "default", kTokenDefault },
{ "class", kTokenClass },
{ "infix", kTokenInfix },
{ "infixl", kTokenInfixL },
{ "infixr", kTokenInfixR },
{ "\\", '\\' },
{ "\\\\", kTokenDoubleBackSlash },
{ "#", '#' },
{ "#!", kTokenHashExclamationMark },
{ "=", '=' },
{ "|", '|' },
{ ".", '.' },
{ "!", '!' },
{ "&", '&' },
{ "..", kTokenDotDot },
{ "=:", kTokenEqualColon },
#ifndef H
{ ":", ':' },
#endif
{ ":==", kTokenColonDoubleEqual },
{ "=>", kTokenDoubleRightArrow },
{ "<-", kTokenLeftArrow },
{ "<-:", kTokenLeftArrowColon },
{ "->", kTokenRightArrow }
#ifdef H
,{ "data", kTokenData }
,{ "type", kTokenType }
,{ "@", kTokenAtSign }
,{ "then", kTokenThen }
,{ "else", kTokenElse }
,{ "interface", kTokenInterface }
#endif
};
# define ArraySize(array) ((unsigned) (sizeof (array) / sizeof (array[0])))
void
ScanInitialise (void)
{
int i;
#ifndef CLEAN2
gCharTypeTable = (unsigned char*)CompAlloc (256 * sizeof (unsigned char)),
InitialiseCharTypeTable (gCharTypeTable);
gStateNormalTable = (ScanState*)CompAlloc (256 * sizeof (ScanState)),
InitialiseStateNormalTable (gStateNormalTable);
gStateInstructionsTable = (ScanState*)CompAlloc (256 * sizeof (ScanState)),
InitialiseStateInstructionTable (gStateInstructionsTable);
ScanSetMode (kScanModeNormal);
gInputBuffer = (unsigned char*)CompAlloc (kInputBufferSize);
#endif
ScanInitIdentStringTable();
for (i = 0; i < ArraySize (gKeyWords); i++)
PutKeyWordInTable (&gKeyWords [i]);
} /* ScanInitialise */
void
InitScanner (void)
{
InlineCodeBuffer = (char*)CompAlloc (InlineBuffSize);
InlineBufferStart = 0;
} /* InitScanner */
|