Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / gen / deb.c
1 /*     Loglan82 Compiler&Interpreter
2      Copyright (C) 1993 Institute of Informatics, University of Warsaw
3      Copyright (C)  1993, 1994 LITA, Pau
4      
5      This program is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published by
7      the Free Software Foundation; either version 2 of the License, or
8      (at your option) any later version.
9      
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14      
15
16 =======================================================================
17 */
18
19 #include <stdio.h>
20 #include "glodefs.h"
21
22
23 #ifndef NO_PROTOTYPES
24
25 static long hash_search(char *);
26 static int ord(char);
27
28 #else
29
30 static long hash_search();
31 static int ord();
32
33 #endif
34
35
36
37 /* ===================== for debugger ======================== */
38 /*        files organization :                                 */
39 /* fil21 ( .DEB ) :                                            */
40 /*       hash(maxhash),                                         */
41 /*       idict(maxidict), ind, prot(ind)                       */
42 /* dfile ( .DCD ) :                                            */
43 /*       hash(maxhash),                                         */
44 /*       idict(maxidict), protbreakl, ind, prot(ind)           */
45
46 #define maxhash 8000
47 #define maxidict 500
48
49 static int idict[maxidict];  /* dictionary of prototypes */
50 static int *prot;          /* array of prototypes  */
51 static int ind;         /* last used in PROT */
52
53 void db3(){
54   /* put dispnr and offset into prototypes */
55    int i=0,j,pr,prz,el;
56
57    while( i < maxidict ){
58
59       if( idict[i] != 0 ){
60
61         /* pr - address of zero word of the prototype */
62          pr = idict[i];
63         /* dispnr */
64          prot[pr+3] = i;
65         /* hash table of prototype */
66          for( j = pr+5 ; j <= pr+12 ; j++){
67
68             el = prot[j];
69             prz = prot[el];
70            /* el - address of zero word of list element */
71             while( prz != -100 ){
72
73                if( prot[el+1] % 8 < 4 ){     /* not not taken */
74
75                   prz = prot[el+2];
76                  /* prz - address of debugger protype */
77                   if( prz < -15 ){              /* variable or constant */
78
79                      prz = -prz;
80                      if( prot[prz] == 5 )        /* variable */
81                         prot[prz+3] = ipmem[prot[prz+3] - 2];
82                   }
83                }
84                el = el+3;
85                prz = prot[el];  /* next element */
86             } /* while */
87          } /* for */
88       }
89       i++;
90    } /* while */
91 }
92
93 long hash[8000];
94
95 void  ts3( fname )  char *fname; {
96
97    /* wpisuje dispnr i offset do prototypow debuggera */
98    /* czyta prototypy z pliku fil21, pisze na dfile */
99
100    FILE *fil21;         /* input - prototypes from TS2 */
101
102    char fnameaux[30];
103    long position;
104
105    strcpy( fnameaux, fname );
106    fil21 = fopen( strcat( fnameaux, ".deb" ) , "rw" );
107    fseek( fil21, maxhash * sizeof(int) ,0 );
108    fread( idict, sizeof(*idict), maxidict, fil21 );
109    fread( &ind, sizeof(ind), 1, fil21 );
110   /* reading  PROT */
111    position = ftell( fil21 );
112    prot=(int *)calloc(ind+1,sizeof(int));
113    fread( prot+1, sizeof(int), ind*4, fil21 ); 
114   /* changing prototypes  */
115    db3();
116   /* writing  PROT */
117    fseek( fil21, position, 0 );
118    fwrite( prot+1, sizeof(int), ind*4, fil21 );
119 fseek( fil21, 0L, 0);
120 fread( hash, 8000, sizeof(long), fil21 );
121 printf("searching do for begin aaa ala i nikldksjkehtr");getchar();
122 printf("#%ld",hash_search("do"));getchar();
123 printf("#%ld",hash_search("for"));getchar();
124 printf("#%ld",hash_search("begin"));getchar();
125 printf("#%ld",hash_search("aaa"));getchar();
126 printf("#%ld",hash_search("ala"));getchar();
127 printf("#%ld",hash_search("i"));getchar();
128 printf("#%ld",hash_search("niksehfkertk"));getchar();
129    fclose( fil21 );
130    free(prot);
131 }
132
133 /* ======================================================== */
134
135 #define BYTE 64
136 #define M 1009
137
138 static long hash_search (str) char *str;{
139
140    long name[21];
141    int result;
142    int i=0;
143    int H;   /* do poszukiwan po hash */
144    int N;   /* do przechodzenia po name */
145
146  /*  pakowanie nazwy do poszukiwan w hash */
147
148    for( i=0; i<20 && *str!='\0'; i++ )
149       if( i % 2 == 0 )   name[ i/2 ] = ord( *( str++ ) );
150       else               name[ i/2 ] = name[ i/2 ] * BYTE + ord( *( str++ ) );
151
152    name[ (i+1)/2 ]= -1 ;
153    i= i/2 + i%2;
154    H = ( name[0] % M ) * 2;
155
156    for(;;){
157 printf("comparing name H=%d,name[0]=%d",H,name[0]);getchar();
158
159       N=0;
160       result = H;
161
162       while( hash[ H+1 ]  <0  &&  hash[ H ] == name[ N ] ){
163 printf("Nth pare OK %d",N);getchar();
164
165          H = - hash[ H+1 ] - 1;
166          N++;
167
168       }
169
170       if( hash[ H ] != name[ N ] ){
171
172          while( hash[ H+1 ] <0 )   H = - hash[ H+1 ] - 1;
173          if( hash[ H+1 ] == 0 )  return -1;
174          H = hash[ H+1 ] - 1;
175          continue;
176
177       }
178
179       if( name[ N+1 ] == -1 )  return result;
180       if( hash[ H+1 ] ==  0 )  return -1;
181
182       H = hash[ H+1 ] - 1;
183
184    }
185
186
187
188
189 static int ord_tab[]={
190 63,63,63,63,63,63,63,63,36,63,63,63,37,
191 36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,
192 63,43,63,63,63,63,44,52,53,48,39,
193 42,40,38,41,0,1,2,3,4,5,6,7,8,9,47,45,50,49,51,63,63,
194 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
195 30,31,32,33,34,35,63,63,63,63,46,
196 63,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
197 30,31,32,33,34,35
198 };
199
200 static int ord(c) char c;{
201    if( c>122 )  return 63;
202    return ord_tab[ c-1 ];
203 }
204
205