1// 2// PtrStack.m 3// ANTLR 4// 5// Created by Alan Condit on 6/9/10. 6// Copyright 2010 Alan's MachineWorks. All rights reserved. 7// 8#define SUCCESS (0) 9#define FAILURE (-1) 10 11#import "PtrStack.h" 12#import "Tree.h" 13 14/* 15 * Start of PtrStack 16 */ 17@implementation PtrStack 18 19+(PtrStack *)newPtrStack 20{ 21 return [[PtrStack alloc] init]; 22} 23 24+(PtrStack *)newPtrStack:(NSInteger)cnt 25{ 26 return [[PtrStack alloc] initWithLen:cnt]; 27} 28 29-(id)init 30{ 31 self = [super initWithLen:HASHSIZE]; 32 if ( self != nil ) { 33 } 34 return( self ); 35} 36 37-(id)initWithLen:(NSInteger)cnt 38{ 39 self = [super initWithLen:cnt]; 40 if ( self != nil ) { 41 } 42 return( self ); 43} 44 45-(void)dealloc 46{ 47#ifdef DEBUG_DEALLOC 48 NSLog( @"called dealloc in PtrStack" ); 49#endif 50 [super dealloc]; 51} 52 53-(void)deletePtrStack:(PtrStack *)np 54{ 55 LinkBase *tmp, *rtmp; 56 NSInteger idx; 57 58 if ( self.fNext != nil ) { 59 for( idx = 0; idx < BuffSize; idx++ ) { 60 tmp = ptrBuffer[idx]; 61 while ( tmp ) { 62 rtmp = tmp; 63 tmp = [tmp getfNext]; 64 [rtmp release]; 65 } 66 } 67 } 68} 69 70#ifdef DONTUSENOMO 71#ifdef USERDOC 72/* 73 * HASH hash entry to get index to table 74 * NSInteger hash( PtrStack *self, char *s ); 75 * 76 * Inputs: NSString *s string to find 77 * 78 * Returns: NSInteger hashed value 79 * 80 * Last Revision 9/03/90 81 */ 82#endif 83-(NSInteger)hash:(NSString *)s /* form hash value for string s */ 84{ 85 NSInteger hashval; 86 const char *tmp; 87 88 tmp = [s cStringUsingEncoding:NSASCIIStringEncoding]; 89 for( hashval = 0; *tmp != '\0'; ) 90 hashval += *tmp++; 91 LastHash = hashval % HashSize; 92 return( LastHash ); 93} 94 95#ifdef USERDOC 96/* 97 * LOOKUP search hashed list for entry 98 * id lookup:(NSString *)s; 99 * 100 * Inputs: NSString *s string to find 101 * 102 * Returns: RuleMemo * pointer to entry 103 * 104 * Last Revision 9/03/90 105 */ 106#endif 107-(id)lookup:(NSString *)s 108{ 109 LinkBase *np; 110 111 for( np = ptrBuffer[[self hash:s]]; np != nil; np = [np getfNext] ) { 112 if ( [s isEqualToString:[np getName]] ) { 113 return( np ); /* found it */ 114 } 115 } 116 return( nil ); /* not found */ 117} 118 119#ifdef USERDOC 120/* 121 * INSTALL search hashed list for entry 122 * NSInteger install( PtrStack *self, id sym ); 123 * 124 * Inputs: RuleMemo *sym -- symbol ptr to install 125 * NSInteger scope -- level to find 126 * 127 * Returns: Boolean TRUE if installed 128 * FALSE if already in table 129 * 130 * Last Revision 9/03/90 131 */ 132#endif 133-(id)install:(id)sym 134{ 135 LinkBase *np; 136 137 np = [self lookup:[sym getName]]; 138 if ( np == nil ) { 139 [sym setFNext:ptrBuffer[ LastHash ]]; 140 ptrBuffer[ LastHash ] = [sym retain]; 141 return( ptrBuffer[ LastHash ] ); 142 } 143 return( nil ); /* not found */ 144} 145#endif 146 147-(id)getptrBufferEntry:(NSInteger)idx 148{ 149 return( ptrBuffer[idx] ); 150} 151 152-(id *)getptrBuffer 153{ 154 return( ptrBuffer ); 155} 156 157-(void)setptrBuffer:(id *)np 158{ 159 ptrBuffer = np; 160} 161 162#ifdef DONTUSENOMO 163/* 164 * works only for maplist indexed not by name but by TokenNumber 165 */ 166- (id)getName:(NSInteger)ttype 167{ 168 id np; 169 NSInteger aTType; 170 171 aTType = ttype % HashSize; 172 for( np = ptrBuffer[ttype]; np != nil; np = [np getfNext] ) { 173 if ( np.index == ttype ) { 174 return( np ); /* found it */ 175 } 176 } 177 return( nil ); /* not found */ 178} 179 180- (id)getTType:(NSString *)name 181{ 182 return [self lookup:name]; 183} 184#endif 185 186- (id) copyWithZone:(NSZone *)aZone 187{ 188 return [super copyWithZone:aZone]; 189} 190 191@end 192