Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: scutdavy/nu
base: 4d39db573b
...
head fork: scutdavy/nu
compare: c762f7eaff
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 42 additions and 61 deletions.
  1. +19 −0 objc/Nu.h
  2. +23 −61 objc/Nu.m
View
19 objc/Nu.h
@@ -79,6 +79,10 @@
#pragma mark -
#pragma mark List Representation
+@class NuCell;
+@interface NuCellEnumerator : NSEnumerator
++ (instancetype) enumeratorWithCell:(NuCell *) cell;
+@end
/*!
@class NuCell
@@ -93,10 +97,25 @@
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;
+/**
+ * cell enumerator for list
+ *
+ * @return enumerator
+ */
+- (NuCellEnumerator *) cellEnumerator;
+
+
+- (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;
/*! Get the last object in a list by traversing the list. Use this carefully. */
View
84 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);
@@ -577,26 +575,6 @@ - (id) initWithParameters:(NuCell *)p body:(NuCell *)b context:(NSMutableDiction
[_context setPossiblyNullObject:c forKey:PARENT_KEY];
[_context setPossiblyNullObject:[c objectForKey:SYMBOLS_KEY] forKey:SYMBOLS_KEY];
#endif
-
- // Check for the presence of "*args" in parameter list
- id plist = _parameters;
-
- if (!( ([_parameters length] == 1)
- && ([[[_parameters car] stringValue] isEqualToString:@"*args"])))
- {
- while (plist && (plist != [NSNull NU_null]))
- {
- id parameter = [plist car];
-
- if ([[parameter stringValue] isEqualToString:@"*args"])
- {
- printf("Warning: Overriding implicit variable '*args'.\n");
- return self;
- }
-
- plist = [plist cdr];
- }
- }
}
return self;
}
@@ -631,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];
@@ -639,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]];
@@ -666,21 +639,15 @@ - (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];
- id cursor = _body;
- @try
- {
- while (cursor && (cursor != [NSNull NU_null])) {
- value = [[cursor car] evalWithContext:evaluation_context];
- cursor = [cursor cdr];
- }
+ @try {
+ value = [_body evalAsPrognInContext:evaluation_context];
}
@catch (NuReturnException *exception) {
value = [exception value];
@@ -746,13 +713,9 @@ - (id) evalWithArguments:(id)cdr context:(NSMutableDictionary *)calling_context
}
// evaluate the body of the block with the saved context (implicit progn)
id value = [NSNull NU_null];
- id cursor = _body;
@try
{
- while (cursor && (cursor != [NSNull NU_null])) {
- value = [[cursor car] evalWithContext:evaluation_context];
- cursor = [cursor cdr];
- }
+ value = [_body evalAsPrognInContext:evaluation_context];
}
@catch (NuReturnException *exception) {
value = [exception value];
@@ -2778,10 +2741,6 @@ + (NSString *) stringValue{
#pragma mark - NuCell.m
-@interface NuCellEnumerator : NSEnumerator
-+ (instancetype) enumeratorWithCell:(NuCell *) cell;
-@end
-
@interface NuCellEnumerator ()
@property (nonatomic, strong) NuCell *cursor;
@end
@@ -2809,11 +2768,10 @@ @interface NuCell ()
- (id) allChainedPairs:(NUCellPairBlock) block context:(NSMutableDictionary *) context;
- (id) eitherChainedPairs:(NUCellPairBlock) block context:(NSMutableDictionary *) context;
- (id) eachEvaluatedListInContext:(NSMutableDictionary *) context;
-- (NuCellEnumerator *) cellEnumerator;
+
- (id) reduce:(NUAccumulationBlock) block initial:(id) initial context:(NSMutableDictionary *) context;
-- (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context;
-- (id) evalAsPrognInContext:(NSMutableDictionary *) context;
+
@end
@implementation NuCell
@@ -3931,6 +3889,10 @@ - (id) evalAsPrognInContext:(NSMutableDictionary *) context{
return nil;
}
+- (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context{
+ return nil;
+}
+
@end
@implementation NSArray(Nu)

No commit comments for this range

Something went wrong with that request. Please try again.