public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
An incomplete attempt to clean up gensyms after macro evaluation.

From comments in macro.m:
// I would like to remove gensym values and symbols at the end of a 
macro's execution,
// but there is a problem with this: the gensym assignments could be used 
in a closure,
// and deleting them would cause that to break. See the 
testIvarAccessorMacro unit
// test for an example of this. So for now, the code below is disabled.

There should be no observable changes in this change set.
timburks (author)
Wed May 07 11:22:48 -0700 2008
commit  4f8d0b325e39e3630eea541bb46cd8a9cb47b452
tree    0da619da1298e4205a844899a1d1bb64a6762ffa
parent  01381604d45343aea5e1454cf20e6043a2fca232
...
184
185
186
187
188
 
 
 
 
 
 
 
 
189
190
191
...
203
204
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
207
208
...
184
185
186
 
 
187
188
189
190
191
192
193
194
195
196
197
...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
0
@@ -184,8 +184,14 @@ extern id Nu__null;
0
     id value = Nu__null;
0
 
0
     // if the macro contains gensyms, give them a unique prefix
0
- id bodyToEvaluate = ([[self gensyms] count] == 0)
0
- ? (id)body : [self body:body withGensymPrefix:[NSString stringWithFormat:@"g%ld", [NuMath random]] symbolTable:symbolTable];
0
+ int gensymCount = [[self gensyms] count];
0
+ id gensymPrefix = nil;
0
+ if (gensymCount > 0) {
0
+ gensymPrefix = [NSString stringWithFormat:@"g%ld", [NuMath random]];
0
+ }
0
+
0
+ id bodyToEvaluate = (gensymCount == 0)
0
+ ? (id)body : [self body:body withGensymPrefix:gensymPrefix symbolTable:symbolTable];
0
 
0
     // uncomment this to get the old (no gensym) behavior.
0
     //bodyToEvaluate = body;
0
@@ -203,6 +209,21 @@ extern id Nu__null;
0
     else {
0
         [calling_context setPossiblyNullObject:old_margs forKey:[symbolTable symbolWithCString:"margs"]];
0
     }
0
+ #if 0
0
+ // I would like to remove gensym values and symbols at the end of a macro's execution,
0
+ // but there is a problem with this: the gensym assignments could be used in a closure,
0
+ // and deleting them would cause that to break. See the testIvarAccessorMacro unit
0
+ // test for an example of this. So for now, the code below is disabled.
0
+ //
0
+ // remove the gensyms from the context; this also releases their assigned values
0
+ NSArray *gensymArray = [gensyms allObjects];
0
+ for (int i = 0; i < gensymCount; i++) {
0
+ NuSymbol *gensymBase = [gensymArray objectAtIndex:i];
0
+ NuSymbol *gensymSymbol = [symbolTable symbolWithString:[NSString stringWithFormat:@"%@%@", gensymPrefix, [gensymBase stringValue]]];
0
+ [calling_context removeObjectForKey:gensymSymbol];
0
+ [symbolTable removeSymbol:gensymSymbol];
0
+ }
0
+ #endif
0
     // NSLog(@"result is %@", value);
0
     return value;
0
 }
...
23
24
25
 
26
27
28
29
30
31
 
 
 
 
32
33
34
...
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
...
23
24
25
26
27
28
 
 
 
 
29
30
31
32
33
34
35
...
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
0
@@ -23,12 +23,13 @@ limitations under the License.
0
 #import <Foundation/Foundation.h>
0
 #import "nuinternals.h"
0
 
0
+@class NuSymbol;
0
 
0
 /*!
0
- @class NuSymbolTable
0
- @abstract The Nu symbol table class.
0
- @discussion Instances of NuSymbolTable manage collections of NuSymbol objects.
0
- By default, one NuSymbolTable object is shared by all NuParser objects and execution contexts in a process.
0
+ @class NuSymbolTable
0
+ @abstract The Nu symbol table class.
0
+ @discussion Instances of NuSymbolTable manage collections of NuSymbol objects.
0
+ By default, one NuSymbolTable object is shared by all NuParser objects and execution contexts in a process.
0
  */
0
 @interface NuSymbolTable : NSObject
0
 {
0
@@ -47,27 +48,29 @@ limitations under the License.
0
 - (id) lookup:(const char *) string;
0
 /*! Get an array containing all of the symbols in a symbol table. */
0
 - (NSArray *) all;
0
+/*! Remove a symbol from the symbol table */
0
+- (void) removeSymbol:(NuSymbol *) symbol;
0
 @end
0
 
0
 /*!
0
- @class NuSymbol
0
- @abstract The Nu symbol class.
0
- @discussion Instances of NuSymbol are used to uniquely represent strings in parsed Nu expressions.
0
- NuSymbol objects are used as keys in local evaluation contexts (typically of type NSMutableDictionary)
0
- and each NuSymbol may also have a global value bound to it.
0
- Symbols ending in a colon (':') are considered "labels" which evaluate to themselves without error,
0
- and when a label is found at the head of the list,
0
- the list is considered to be a special type of list called a property list.
0
- Each member of a property list is evaluated and the resulting list is returned with no further evaluation.
0
+ @class NuSymbol
0
+ @abstract The Nu symbol class.
0
+ @discussion Instances of NuSymbol are used to uniquely represent strings in parsed Nu expressions.
0
+ NuSymbol objects are used as keys in local evaluation contexts (typically of type NSMutableDictionary)
0
+ and each NuSymbol may also have a global value bound to it.
0
+ Symbols ending in a colon (':') are considered "labels" which evaluate to themselves without error,
0
+ and when a label is found at the head of the list,
0
+ the list is considered to be a special type of list called a property list.
0
+ Each member of a property list is evaluated and the resulting list is returned with no further evaluation.
0
  */
0
 @interface NuSymbol : NSObject <NSCoding>
0
 {
0
     NuSymbolTable *table;
0
     id value;
0
- @public // only for use by the symbol table
0
+ @public // only for use by the symbol table
0
     char *string;
0
     bool isLabel;
0
- bool isGensym; // in macro evaluation, symbol is replaced with an automatically-generated unique symbol.
0
+ bool isGensym; // in macro evaluation, symbol is replaced with an automatically-generated unique symbol.
0
 }
0
 
0
 /*! Get the global value of a symbol. */
...
100
101
102
 
 
 
 
 
 
 
103
104
105
...
100
101
102
103
104
105
106
107
108
109
110
111
112
0
@@ -100,6 +100,13 @@ static int add_to_array(st_data_t k, st_data_t v, st_data_t d)
0
     return array;
0
 }
0
 
0
+- (void) removeSymbol:(NuSymbol *) symbol
0
+{
0
+ //NSLog(@"removing symbol %@ from table", [symbol stringValue]);
0
+ st_delete(symbol_table, (st_data_t *) &(symbol->string), 0);
0
+ [symbol release]; // on behalf of the table
0
+}
0
+
0
 @end
0
 
0
 @implementation NuSymbol

Comments

    No one has commented yet.