public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
Variable scoping change.

With this change, assignments made within methods cannot
affect symbols outside of the local class scope.

===========================
Example:

(set x 0)
(set z 0)

(class Tester is NSObject
  (set y 0)
  (+ doSomething is
    (set x 1)
    (set y 1)
    ...
    )
  (+ getY is y)
  (+ getZ is z))

(Tester doSomething)
===========================

Previously, the assignment to x in (Tester +doSomething)
would modify the top-level x since that binding was in
the lexical closure of doSomething. Now that same assignment
won't affect the top-level x because assignments are limited
to the local class scope. Variables outside the scope are
still visible, (Tester +getZ) returns the value of z at
the top-level.

(Tester +doSomething) also assigns a value to y. Because
there is an assignment to y in the class context, that
assignment is the one modified by the set operator. Thus
class variables can be created using closures. This is
consistent with previous versions of Nu. Only the behavior
regarding x is modified by these changes.

These changes also include some minor cleanup to the
(NuCell -stringValue) method.
timburks (author)
Sat May 03 16:26:01 -0700 2008
commit  49b1304ba9b5de8ee9418f581eb1aaee363ab4b5
tree    bc85ab5fb511aac573b45e9b3ab837cc26aa69af
parent  134ad119134c33139262b55b437140a70f50a144
...
149
150
151
 
 
 
 
 
 
 
 
 
 
 
152
153
154
...
168
169
170
171
 
 
172
173
174
...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
179
180
181
 
182
183
184
185
186
0
@@ -149,6 +149,17 @@ extern id Nu__null;
0
     return [self callWithArguments:cdr context:calling_context];
0
 }
0
 
0
+id getObjectFromContext(id context, id symbol)
0
+{
0
+ while (IS_NOT_NULL(context)) {
0
+ id object = [context objectForKey:symbol];
0
+ if (object)
0
+ return object;
0
+ context = [context objectForKey:PARENT_KEY];
0
+ }
0
+ return nil;
0
+}
0
+
0
 - (id) evalWithArguments:(id)cdr context:(NSMutableDictionary *)calling_context self:(id)object
0
 {
0
     int numberOfArguments = [cdr length];
0
@@ -168,7 +179,8 @@ extern id Nu__null;
0
     // NSLog(@"after copying, evaluation context %@ retain count %d", evaluation_context, [evaluation_context retainCount]);
0
     if (object) {
0
         NuSymbolTable *symbolTable = [evaluation_context objectForKey:SYMBOLS_KEY];
0
- NuClass *c = [context objectForKey:[symbolTable symbolWithString:@"_class"]];
0
+ // look up one level for the _class value, but allow for it to be higher (in the perverse case of nested method declarations).
0
+ NuClass *c = getObjectFromContext([context objectForKey:PARENT_KEY], [symbolTable symbolWithString:@"_class"]);
0
         [evaluation_context setPossiblyNullObject:object forKey:[symbolTable symbolWithCString:"self"]];
0
         [evaluation_context setPossiblyNullObject:[NuSuper superWithObject:object ofClass:[c wrappedClass]] forKey:[symbolTable symbolWithCString:"super"]];
0
     }
...
1168
1169
1170
1171
1172
 
 
1173
1174
1175
...
1168
1169
1170
 
 
1171
1172
1173
1174
1175
0
@@ -1168,8 +1168,8 @@ id add_method_to_class(Class c, NSString *methodName, NSString *signature, NuBlo
0
     SEL selector = sel_register_name(method_name_str);
0
     #endif
0
 
0
- NuSymbolTable *symbolTable = [[block context] objectForKey:SYMBOLS_KEY];
0
- [[block context] setPossiblyNullObject:[[NuClass alloc] initWithClass:c] forKey:[symbolTable symbolWithCString:"_class"]];
0
+ //NuSymbolTable *symbolTable = [[block context] objectForKey:SYMBOLS_KEY];
0
+ //[[block context] setPossiblyNullObject:[[NuClass alloc] initWithClass:c] forKey:[symbolTable symbolWithCString:"_class"]];
0
 
0
     IMP imp = construct_method_handler(selector, block, signature_str);
0
     if (imp == NULL) {
...
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
...
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
0
@@ -162,35 +162,38 @@ limitations under the License.
0
 
0
 - (NSMutableString *) stringValue
0
 {
0
- NuCell *cell = self;
0
+ NuCell *cursor = self;
0
     NSMutableString *result = [NSMutableString stringWithString:@"("];
0
- bool first = true;
0
- while (IS_NOT_NULL(cell)) {
0
- if (first)
0
- first = false;
0
- else
0
+ int count = 0;
0
+ while (IS_NOT_NULL(cursor)) {
0
+ if (count > 0)
0
             [result appendString:@" "];
0
- id mycar = [cell car];
0
- if (nu_objectIsKindOfClass(mycar, [NuCell class])) {
0
- [result appendString:[mycar stringValue]];
0
+ count++;
0
+ id item = [cursor car];
0
+ if (nu_objectIsKindOfClass(item, [NuCell class])) {
0
+ [result appendString:[item stringValue]];
0
         }
0
- else if (mycar && (mycar != Nu__null)) {
0
- if ([mycar respondsToSelector:@selector(escapedStringRepresentation)]) {
0
- [result appendString:[mycar escapedStringRepresentation]];
0
+ else if (IS_NOT_NULL(item)) {
0
+ if ([item respondsToSelector:@selector(escapedStringRepresentation)]) {
0
+ [result appendString:[item escapedStringRepresentation]];
0
             }
0
             else {
0
- [result appendString:[mycar description]];
0
+ [result appendString:[item description]];
0
             }
0
         }
0
         else {
0
             [result appendString:@"()"];
0
         }
0
- cell = [cell cdr];
0
+ cursor = [cursor cdr];
0
         // check for dotted pairs
0
- if (IS_NOT_NULL(cell) && !nu_objectIsKindOfClass(cell, [NuCell class])) {
0
+ if (IS_NOT_NULL(cursor) && !nu_objectIsKindOfClass(cursor, [NuCell class])) {
0
             [result appendString:@" . "];
0
- [result appendString:[cell description]];
0
- break;
0
+ if ([cursor respondsToSelector:@selector(escapedStringRepresentation)]) {
0
+ [result appendString:[cursor escapedStringRepresentation]];
0
+ }
0
+ else {
0
+ [result appendString:[cursor description]];
0
+ } break;
0
         }
0
     }
0
     [result appendString:@")"];
...
651
652
653
 
 
654
655
656
657
658
659
 
 
 
660
661
662
...
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
0
@@ -651,12 +651,17 @@ limitations under the License.
0
     }
0
     else {
0
         #ifndef CLOSE_ON_VALUES
0
+ NuSymbolTable *symbolTable = [context objectForKey:SYMBOLS_KEY];
0
+ id classSymbol = [symbolTable symbolWithCString:"_class"];
0
         id searchContext = context;
0
         while (searchContext) {
0
             if ([searchContext objectForKey:symbol]) {
0
                 [searchContext setPossiblyNullObject:result forKey:symbol];
0
                 return result;
0
             }
0
+ else if ([searchContext objectForKey:classSymbol]) {
0
+ break;
0
+ }
0
             searchContext = [searchContext objectForKey:PARENT_KEY];
0
         }
0
         #endif

Comments

    No one has commented yet.