Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor parameters evaluation

  • Loading branch information...
commit 8b31e3376e5b04af0435544ec5c388bfc11090af 1 parent 4008cbf
@scutdavy authored
Showing with 23 additions and 23 deletions.
  1. +5 −0 objc/Nu.h
  2. +18 −23 objc/Nu.m
View
5 objc/Nu.h
@@ -97,6 +97,10 @@
The cdr of the last element in a list is nil.
In Nu, nil is represented with the <code>[NSNull null]</code> object.
*/
+
+typedef BOOL(^NUCellPairBlock)(id value1, id value2);
+typedef id(^NUCellMapBlock)(id cell, NSMutableDictionary *context);
+typedef id (^NUAccumulationBlock)(id sum, id obj, NSMutableDictionary *context);
@interface NuCell : NSObject <NSCoding>
@property (nonatomic, strong) id car;
@property (nonatomic, strong) id cdr;
@@ -110,6 +114,7 @@
- (id) evalAsPrognInContext:(NSMutableDictionary *) context;
+- (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context;
/*! Create a new cell with a specifed car and cdr. */
+ (id) cellWithCar:(id)car cdr:(id)cdr;
View
41 objc/Nu.m
@@ -68,9 +68,7 @@
#import "Nu.h"
#define IS_NOT_NULL(xyz) ((xyz) && (((id) (xyz)) != [NSNull null]))
-typedef BOOL(^NUCellPairBlock)(id value1, id value2);
-typedef id(^NUCellMapBlock)(id cell, NSMutableDictionary *context);
-typedef id (^NUAccumulationBlock)(id sum, id obj, NSMutableDictionary *context);
+
// We'd like for this to be in the ObjC2 API, but it isn't.
static void nu_class_addInstanceVariable_withSignature(Class thisClass, const char *variableName, const char *signature);
@@ -611,7 +609,6 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)calling_context{
}
//NSLog(@"block eval %@", [cdr stringValue]);
// loop over the parameters, looking up their values in the calling_context and copying them into the evaluation_context
- id plist = _parameters;
id vlist = cdr;
id evaluation_context = [_context mutableCopy];
@@ -619,24 +616,20 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)calling_context{
NuSymbolTable *symbolTable = [evaluation_context objectForKey:SYMBOLS_KEY];
[evaluation_context setPossiblyNullObject:cdr forKey:[symbolTable symbolWithString:@"*args"]];
- while (plist && (plist != [NSNull NU_null])) {
- id parameter = [plist car];
+ for (id pcursor in [_parameters cellEnumerator]) {
+ id parameter = [pcursor car];
if ([[parameter stringValue] characterAtIndex:0] == '*') {
- id varargs = [[[NuCell alloc] init] autorelease];
- id cursor = varargs;
- while (vlist != [NSNull NU_null]) {
- [cursor setCdr:[[[NuCell alloc] init] autorelease]];
- cursor = [cursor cdr];
- id value = [vlist car];
- if (calling_context && (calling_context != [NSNull NU_null]))
- value = [value evalWithContext:calling_context];
- [cursor setCar:value];
- vlist = [vlist cdr];
- }
- [evaluation_context setPossiblyNullObject:[varargs cdr] forKey:parameter];
- plist = [plist cdr];
+ id varargs = [vlist map:^id(id cell, NSMutableDictionary *context) {
+ id value = [cell car];
+ if (context && context != [NSNull NU_null]) {
+ value = [value evalWithContext:context];
+ }
+ return value;
+ } context:calling_context];
+
+ [evaluation_context setPossiblyNullObject:varargs forKey:parameter];
// this must be the last element in the parameter list
- if (plist != [NSNull NU_null]) {
+ if ([pcursor cdr] != [NSNull NU_null]) {
[NSException raise:@"NuBadParameterList"
format:@"Variable argument list must be the last parameter in the parameter list: %@",
[_parameters stringValue]];
@@ -646,12 +639,11 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)calling_context{
id value = [vlist car];
if (calling_context && (calling_context != [NSNull NU_null]))
value = [value evalWithContext:calling_context];
- //NSLog(@"setting %@ = %@", parameter, value);
[evaluation_context setPossiblyNullObject:value forKey:parameter];
- plist = [plist cdr];
vlist = [vlist cdr];
}
}
+
// evaluate the body of the block with the saved context (implicit progn)
id value = [NSNull NU_null];
@try {
@@ -2783,7 +2775,6 @@ - (id) eachEvaluatedListInContext:(NSMutableDictionary *) context;
- (id) reduce:(NUAccumulationBlock) block initial:(id) initial context:(NSMutableDictionary *) context;
-- (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context;
@end
@@ -3902,6 +3893,10 @@ - (id) evalAsPrognInContext:(NSMutableDictionary *) context{
return nil;
}
+- (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context{
+ return nil;
+}
+
@end
@implementation NSArray(Nu)
Please sign in to comment.
Something went wrong with that request. Please try again.