Skip to content

Commit

Permalink
Merge branch 'master' of github.com:itfrombit/nu
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Sep 24, 2011
2 parents b622d89 + 0c764b2 commit 78d1bf3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 18 deletions.
8 changes: 0 additions & 8 deletions nu/nu.nu
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@
(do (ls)
(not (any (ls map:(do (x) (not x)))))))

;; Applies a function to a list of arguments.
;; For example (apply + '(1 2)) returns 3.
(global apply
(macro-0 _
(set __f (eval (car margs)))
(set __args (eval (cdr margs)))
(eval (cons __f __args))))

;; Evaluates an expression and raises a NuAssertionFailure if the result is false.
;; For example (assert (eq 1 1)) does nothing but (assert (eq (+ 1 1) 1)) throws
;; an exception.
Expand Down
10 changes: 7 additions & 3 deletions objc/NuMacro_1.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ - (void) dumpContext:(NSMutableDictionary*)context
NSArray* keys = [context allKeys];
int count = [keys count];
for (int i = 0; i < count; i++) {
#ifdef MACRO1_DEBUG
id key = [keys objectAtIndex:i];
#endif
Macro1Debug(@"contextdump: %@ = %@ [%@]", key,
NSLog(@"contextdump: %@ = %@ [%@]", key,
[[context objectForKey:key] stringValue],
[[context objectForKey:key] class]);
}
Expand Down Expand Up @@ -277,7 +275,9 @@ - (id) expandAndEval:(id)cdr context:(NSMutableDictionary*)calling_context evalF

Macro1Debug(@"Dumping context:");
Macro1Debug(@"---------------:");
#ifdef MACRO1_DEBUG
[self dumpContext:calling_context];
#endif

id old_args = [calling_context objectForKey:[symbolTable symbolWithCString:"*args"]];
[calling_context setPossiblyNullObject:cdr forKey:[symbolTable symbolWithCString:"*args"]];
Expand Down Expand Up @@ -318,7 +318,9 @@ - (id) expandAndEval:(id)cdr context:(NSMutableDictionary*)calling_context evalF

Macro1Debug(@"Dumping context (after destructure):");
Macro1Debug(@"-----------------------------------:");
#ifdef MACRO1_DEBUG
[self dumpContext:calling_context];
#endif

// evaluate the body of the block in the calling context (implicit progn)
id value = Nu__null;
Expand Down Expand Up @@ -365,7 +367,9 @@ - (id) expandAndEval:(id)cdr context:(NSMutableDictionary*)calling_context evalF

Macro1Debug(@"Dumping context at end:");
Macro1Debug(@"----------------------:");
#ifdef MACRO1_DEBUG
[self dumpContext:calling_context];
#endif

// restore the old value of *args
[self restoreArgs:old_args context:calling_context];
Expand Down
85 changes: 80 additions & 5 deletions objc/NuOperator.m
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,78 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context

@end

@interface Nu_apply_operator : NuOperator {}
@end

@implementation Nu_apply_operator
- (id) prependCell:(id)item withSymbol:(id)symbol
{
id qitem = [[[NuCell alloc] init] autorelease];
[qitem setCar:symbol];
[qitem setCdr:[[[NuCell alloc] init] autorelease]];
[[qitem cdr] setCar:item];
return qitem;
}

- (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context
{
NuSymbolTable *symbolTable = [context objectForKey:SYMBOLS_KEY];
id quoteSymbol = [symbolTable symbolWithString:@"quote"];

id fn = [cdr car];

// Arguments to fn can be anything, but last item must be a list
id qargs = Nu__null;
id qargs_cursor = Nu__null;
id cursor = [cdr cdr];

while (cursor && (cursor != Nu__null) && [cursor cdr] && ([cursor cdr] != Nu__null)) {
if (qargs == Nu__null) {
qargs = [[[NuCell alloc] init] autorelease];
qargs_cursor = qargs;
}
else {
[qargs_cursor setCdr:[[[NuCell alloc] init] autorelease]];
qargs_cursor = [qargs_cursor cdr];
}

id item = [[cursor car] evalWithContext:context];
id qitem = [self prependCell:item withSymbol:quoteSymbol];
[qargs_cursor setCar:qitem];
cursor = [cursor cdr];
}

// The rest of the arguments are in a list
id args = [cursor evalWithContext:context];
cursor = args;

while (cursor && (cursor != Nu__null)) {
if (qargs == Nu__null) {
qargs = [[[NuCell alloc] init] autorelease];
qargs_cursor = qargs;
}
else {
[qargs_cursor setCdr:[[[NuCell alloc] init] autorelease]];
qargs_cursor = [qargs_cursor cdr];
}
id item = [cursor car];

id qitem = [self prependCell:item withSymbol:quoteSymbol];
[qargs_cursor setCar:qitem];
cursor = [cursor cdr];
}

// Call the real function with the evaluated and quoted args
id expr = [[[NuCell alloc] init] autorelease];
[expr setCar:fn];
[expr setCdr:qargs];

id result = [expr evalWithContext:context];

return result;
}
@end

@interface Nu_cond_operator : NuOperator {}
@end

Expand Down Expand Up @@ -694,9 +766,9 @@ - (id) evalQuasiquote:(id)cdr context:(NSMutableDictionary *)context
{
NuSymbolTable *symbolTable = [context objectForKey:SYMBOLS_KEY];

id quasiquote_eval = [symbolTable symbolWithString:@"quasiquote-eval"];
id quasiquote_splice = [symbolTable symbolWithString:@"quasiquote-splice"];

id quasiquote_eval = [[symbolTable symbolWithString:@"quasiquote-eval"] value];
id quasiquote_splice = [[symbolTable symbolWithString:@"quasiquote-splice"] value];
QuasiLog(@"bq:Entered. callWithArguments cdr = %@", [cdr stringValue]);

id result = Nu__null;
Expand All @@ -716,12 +788,14 @@ - (id) evalQuasiquote:(id)cdr context:(NSMutableDictionary *)context
QuasiLog(@" quasiquote: null-list");
value = Nu__null;
}
else if ([[cursor car] car] == quasiquote_eval) {
//else if ([[cursor car] car] == quasiquote_eval) {
else if ([[symbolTable lookup:[[[[cursor car] car] stringValue] cStringUsingEncoding:NSUTF8StringEncoding]] value] == quasiquote_eval) {
QuasiLog(@"quasiquote-eval: Evaling: [[cursor car] cdr]: %@", [[[cursor car] cdr] stringValue]);
value = [[[cursor car] cdr] evalWithContext:context];
QuasiLog(@" quasiquote-eval: Value: %@", [value stringValue]);
}
else if ([[cursor car] car] == quasiquote_splice) {
//else if ([[cursor car] car] == quasiquote_splice) {
else if ([[symbolTable lookup:[[[[cursor car] car] stringValue] cStringUsingEncoding:NSUTF8StringEncoding]] value] == quasiquote_splice) {
QuasiLog(@"quasiquote-splice: Evaling: [[cursor car] cdr]: %@",
[[[cursor car] cdr] stringValue]);
value = [[[cursor car] cdr] evalWithContext:context];
Expand Down Expand Up @@ -2194,6 +2268,7 @@ void load_builtins(NuSymbolTable *symbolTable)
install("cons", Nu_cons_operator);
install("append", Nu_append_operator);

install("apply", Nu_apply_operator);
install("cond", Nu_cond_operator);
install("case", Nu_case_operator);
install("if", Nu_if_operator);
Expand Down
18 changes: 16 additions & 2 deletions test/test_list.nu
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,22 @@

(- testApply is
(assert_equal 1 (apply + '(1)))
(assert_equal 3 (apply + '(1 2))))

(assert_equal 3 (apply + '(1 2)))
(assert_equal '(a b c) (apply list '(a b c)))
(function identity (x) x)
(assert_equal 'a (apply identity '(a)))
(set a "foo")
(assert_equal 'a (apply identity '(a)))
(assert_equal "foo" (apply identity (list a)))
(assert_throws "NuUndefinedSymbol" (apply identity (list b)))
(assert_equal '(a b c d) (apply list 'a 'b '(c d)))
(set a 1)
(set b 2)
(set c 3)
(set d 4)
(assert_equal '(a 2 c 4) (apply list 'a b `(c ,d)))
(assert_equal 10 (apply + a b (list c d))))

(- testSort is
(assert_equal '() (sort '()))
(assert_equal '(1) (sort '(1)))
Expand Down

0 comments on commit 78d1bf3

Please sign in to comment.