langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,76 @@
#import <Foundation/Foundation.h>
@interface Delegator : NSObject {
id delegate;
}
- (id)delegate;
- (void)setDelegate:(id)obj;
- (NSString *)operation;
@end
@implementation Delegator
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)obj {
delegate = obj; // Weak reference
}
- (NSString *)operation {
if ([delegate respondsToSelector:@selector(thing)])
return [delegate thing];
return @"default implementation";
}
@end
// Any object may implement these
@interface NSObject (DelegatorDelegating)
- (NSString *)thing;
@end
@interface Delegate : NSObject
// Don't need to declare -thing because any NSObject has this method
@end
@implementation Delegate
- (NSString *)thing {
return @"delegate implementation";
}
@end
// Example usage
// Memory management ignored for simplification
int main() {
// Without a delegate:
Delegator *a = [[Delegator alloc] init];
NSLog(@"%d\n", [[a operation] isEqualToString:@"default implementation"]);
// With a delegate that does not implement thing:
[a setDelegate:@"A delegate may be any object"];
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
// With a delegate that implements "thing":
Delegate *d = [[Delegate alloc] init];
[a setDelegate:d];
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
return 0;
}

View file

@ -0,0 +1,44 @@
class DelegateA 'not implmenting thing()
'==============
'
string message
end class
class DelegateB 'implementing thing()
'==============
'
string message
method thing() as string
return message
end method
'
end class
Class Delegator
'==============
'
has DelegateA dgA
has DelegateB dgB
'
method operation() as DelegateB
dgB.message="Delegate Implementation"
return @dgB
end method
method thing() as string
return "not using Delegate"
end method
'
end class
'====
'TEST
'====
Delegator dgr
let dg=dgr.operation
print dgr.thing 'result "not using Delegate"
print dg.thing 'result "Delegate Implementation"

View file

@ -0,0 +1,43 @@
declare
class Delegator from BaseObject
attr
delegate:unit
meth set(DG)
{Object.is DG} = true %% assert: DG must be an object
delegate := DG
end
meth operation($)
if @delegate == unit then
{self default($)}
else
try
{@delegate thing($)}
catch error(object(lookup ...) ...) then
%% the delegate did not understand the message
{self default($)}
end
end
end
meth default($)
"default implementation"
end
end
class Delegate from BaseObject
meth thing($)
"delegate Implementation"
end
end
A = {New Delegator noop}
in
{System.showInfo {A operation($)}}
{A set({New BaseObject noop})}
{System.showInfo {A operation($)}}
{A set({New Delegate noop})}
{System.showInfo {A operation($)}}

View file

@ -0,0 +1,28 @@
class Non-Delegate { }
class Delegate {
method thing {
return "delegate implementation"
}
}
class Delegator {
has $.delegate is rw;
method operation {
$.delegate.^can( 'thing' ) ?? $.delegate.thing
!! "default implementation"
}
}
my Delegator $d .= new;
say "empty: "~$d.operation;
$d.delegate = Non-Delegate.new;
say "Non-Delegate: "~$d.operation;
$d.delegate = Delegate.new;
say "Delegate: "~$d.operation;

View file

@ -0,0 +1,32 @@
uses objectclass;
define :class Delegator;
slot delegate = false;
enddefine;
define :class Delegate;
enddefine;
define :method thing(x : Delegate);
'delegate implementation'
enddefine;
define :method operation(x : Delegator);
if delegate(x) and fail_safe(delegate(x), thing) then
;;; Return value is on the stack
else
'default implementation'
endif;
enddefine;
;;; Default, without a delegate
lvars a = newDelegator();
operation(a) =>
;;; a delegating to itself (works because Delegator does not
;;; implement thing)
a -> delegate(a);
operation(a) =>
;;; delegating to a freshly created Delegate
newDelegate() -> delegate(a);
operation(a) =>

View file

@ -0,0 +1,24 @@
a = new()
a.f = method(){
.x.print()
}
c = new()
c.g = method(){
(.x + 1).print()
}
# array of delegates
b = new()
b.delegate = new()
b.delegate[0] = a
b.delegate[1] = c
b.x = 3
b.f()
b.g()
# single delegate
d = new()
d.delegate = a
d.x = 7
d.f()