public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
Added a return operator.

This new operator allows early return from blocks, which
includes both functions and class and instance methods.
The return operator accepts an optional argument which
is used as the return value. If no argument is given,
nil is returned.

See the unit tests in test/test_return.nu for examples.
timburks (author)
Thu May 01 16:22:19 -0700 2008
commit  c38c2414b8bf50eff8d8f61898fc190a63ffede4
tree    efdba2c0d62bb11df274fb47a9f65f06da7be645
parent  7730e3e43e1b9f70c589df63c0f973bb9fd26710
...
123
124
125
126
127
128
 
 
 
 
 
 
 
 
 
 
 
 
129
130
131
...
176
177
178
179
180
181
 
 
 
 
 
 
 
 
 
 
 
 
182
183
184
...
123
124
125
 
 
 
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
185
186
187
 
 
 
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
0
@@ -123,9 +123,18 @@ extern id Nu__null;
0
     // evaluate the body of the block with the saved context (implicit progn)
0
     id value = Nu__null;
0
     id cursor = body;
0
- while (cursor && (cursor != Nu__null)) {
0
- value = [[cursor car] evalWithContext:evaluation_context];
0
- cursor = [cursor cdr];
0
+ @try
0
+ {
0
+ while (cursor && (cursor != Nu__null)) {
0
+ value = [[cursor car] evalWithContext:evaluation_context];
0
+ cursor = [cursor cdr];
0
+ }
0
+ }
0
+ @catch (NuReturnException *exception) {
0
+ value = [exception value];
0
+ }
0
+ @catch (id exception) {
0
+ @throw(exception);
0
     }
0
     // NSLog(@"before releasing, evaluation context %@ retain count %d", evaluation_context, [evaluation_context retainCount]);
0
     // NSLog(@"before releasing, value %@ retain count %d", value, [value retainCount]);
0
@@ -176,9 +185,18 @@ extern id Nu__null;
0
     // evaluate the body of the block with the saved context (implicit progn)
0
     id value = Nu__null;
0
     id cursor = body;
0
- while (cursor && (cursor != Nu__null)) {
0
- value = [[cursor car] evalWithContext:evaluation_context];
0
- cursor = [cursor cdr];
0
+ @try
0
+ {
0
+ while (cursor && (cursor != Nu__null)) {
0
+ value = [[cursor car] evalWithContext:evaluation_context];
0
+ cursor = [cursor cdr];
0
+ }
0
+ }
0
+ @catch (NuReturnException *exception) {
0
+ value = [exception value];
0
+ }
0
+ @catch (id exception) {
0
+ @throw(exception);
0
     }
0
     [value retain];
0
     [value autorelease];
...
34
35
36
 
 
 
 
 
 
 
37
38
...
34
35
36
37
38
39
40
41
42
43
44
45
0
@@ -34,5 +34,12 @@ limitations under the License.
0
 @interface NuContinueException : NSException {}
0
 @end
0
 
0
+@interface NuReturnException : NSException
0
+{
0
+ id value;
0
+}
0
+
0
+@end
0
+
0
 // use this to test a value for "truth"
0
 bool nu_valueIsTrue(id value);
...
51
52
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
55
56
...
1603
1604
1605
1606
 
1607
1608
1609
...
1616
1617
1618
1619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1620
1621
1622
...
1772
1773
1774
 
1775
1776
1777
...
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
77
...
1624
1625
1626
 
1627
1628
1629
1630
...
1637
1638
1639
 
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
...
1810
1811
1812
1813
1814
1815
1816
0
@@ -51,6 +51,27 @@ limitations under the License.
0
 
0
 @end
0
 
0
+@implementation NuReturnException
0
+- (id) initWithValue:(id) v;
0
+{
0
+ [super initWithName:@"NuReturnException" reason:@"A return operator was evaluated" userInfo:nil];
0
+ value = [v retain];
0
+ return self; ;
0
+}
0
+
0
+- (void) dealloc
0
+{
0
+ [value release];
0
+ [super dealloc];
0
+}
0
+
0
+- (id) value
0
+{
0
+ return value;
0
+}
0
+
0
+@end
0
+
0
 @implementation NuOperator : NSObject
0
 - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context {return nil;}
0
 - (id) evalWithArguments:(id)cdr context:(NSMutableDictionary *)context {return [self callWithArguments:cdr context:context];}
0
@@ -1603,7 +1624,7 @@ id loadNuLibraryFile(NSString *nuFileName, id parser, id context, id symbolTable
0
 
0
 - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context
0
 {
0
- @throw [[NuBreakException alloc] init];
0
+ @throw [[[NuBreakException alloc] init] autorelease];
0
     return nil; // unreached
0
 }
0
 
0
@@ -1616,7 +1637,24 @@ id loadNuLibraryFile(NSString *nuFileName, id parser, id context, id symbolTable
0
 
0
 - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context
0
 {
0
- @throw [[NuContinueException alloc] init];
0
+ @throw [[[NuContinueException alloc] init] autorelease];
0
+ return nil; // unreached
0
+}
0
+
0
+@end
0
+
0
+@interface Nu_return_operator : NuOperator {}
0
+@end
0
+
0
+@implementation Nu_return_operator
0
+
0
+- (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context
0
+{
0
+ id value = nil;
0
+ if (cdr && cdr != Nu__null) {
0
+ value = [[cdr car] evalWithContext:context];
0
+ }
0
+ @throw [[[NuReturnException alloc] initWithValue:value] autorelease];
0
     return nil; // unreached
0
 }
0
 
0
@@ -1772,6 +1810,7 @@ void load_builtins(NuSymbolTable *symbolTable)
0
     install("for", Nu_for_operator);
0
     install("break", Nu_break_operator);
0
     install("continue", Nu_continue_operator);
0
+ install("return", Nu_return_operator);
0
 
0
     install("try", Nu_try_operator);
0
     #ifdef DARWIN

Comments

    No one has commented yet.