The Factor interface is slightly awkward because method selectors have to be defined with parsing words first.
What follows is an example that uses Apple's text-to-speech library to speak a string.
We start by defining classes and methods we need.
OBJC-MESSAGE: id alloc ;
OBJC-CLASS: NSString
: NSASCIIStringEncoding 1 ; inline
OBJC-MESSAGE: id initWithCString: char* encoding: uint ;
OBJC-CLASS: NSSpeechSynthesizer
OBJC-MESSAGE: id initWithVoice: id ;
OBJC-MESSAGE: bool startSpeakingString: id ;
Then, a utility word to create new
NSString
s:: <NSString> ( string -- alien )
NSString [alloc]
swap NSASCIIStringEncoding [initWithCString:encoding:] ;
Finally, we can use the above words to speak a string:
NSSpeechSynthesizer [alloc]
f [initWithVoice:]
dup "Hello from Factor" <NSString> [startSpeakingString:]
I'm still trying to come up with a more natural way to fit this into Factor syntax. Merging the selector into a single word name is pretty tacky.
In a future release, probably 0.81, I will write a Cocoa bindings and a Mac OS backend for Factor's UI.
In other news, I'm slowly converting the handbook from LaTeX into the Factor online help markup. I guess I'm about half-way through.
No comments:
Post a Comment