From: jmk@plan9.bell-labs.com
To: 9fans@cse.psu.edu
Subject: Re: [9fans] KenC operator overloading.
Date: Sat, 7 May 2005 14:26:13 -0400 [thread overview]
Message-ID: <785c9b3ba79825b48fb2c35b0415ef91@plan9.bell-labs.com> (raw)
In-Reply-To: <427CF340.4060807@asgaard.homelinux.org>
>From the archives:
On Wed May 30 19:10:28 EDT 2001, jmk@plan9.bell-labs.com wrote:
> On Wed May 30 18:57:33 EDT 2001, mike@ducky.net wrote:
> > >you can't use typestr anymore. it's a reserved word.
> >
> > But what does it do? I just spent a few minutes looking
> > at the compiler source, and it was not obvious to me.
>
> Typestr was one of the things to come out of Ken's parting
> flurry of activity. Here's the complete documentation as we have
> it from his show-and-tell a couple of days before he left:
>
> #include <u.h>
> #include <libc.h>
>
> typestr struct _cmplx Cmplx;
> struct _cmplx
> {
> double r;
> double i;
> };
> int Zconv(va_list*, Fconv*);
>
> #pragma varargck type "Z" Cmplx
>
> void
> main(void)
> {
> Cmplx a, b;
> int ia[10];
> double d;
>
> fmtinstall('Z', Zconv);
> a = (Cmplx){1, 0};
> b = (Cmplx){0, 1};
>
> a = -((a + b) - (b - a)) + -((a - b) - (b + a));
> print("a = %Z\n", a);
> a += b;
> print("a = %Z\n", a);
>
> if(a == a)
> print("ok\n");
>
> a = (d = a);
> print("d = %g\n", d);
> print("a = %Z\n", a);
>
> a = (Cmplx)1.0;
> print("a = %Z\n", a);
> }
>
> /*
> * print conversion
> */
> int
> Zconv(va_list *arg, Fconv *fp)
> {
> char s[50];
> Cmplx z;
>
> z = va_arg(*arg, Cmplx);
> sprint(s, "(%g %g)", z.r, z.i);
> strconv(s, fp);
> return sizeof(z);
> }
>
> /*
> * operators
> */
> Cmplx
> _cmplx_pos_(Cmplx a)
> {
> Cmplx r;
>
> r.r = +a.r;
> r.i = +a.i;
> return r;
> }
>
> Cmplx
> _cmplx_neg_(Cmplx a)
> {
> Cmplx r;
>
> r.r = -a.r;
> r.i = -a.i;
> return r;
> }
>
> Cmplx
> _cmplx_add_(Cmplx a, Cmplx b)
> {
> Cmplx r;
>
> r.r = a.r + b.r;
> r.i = a.i + b.i;
> return r;
> }
>
> Cmplx
> _cmplx_sub_(Cmplx a, Cmplx b)
> {
> Cmplx r;
>
> r.r = a.r - b.r;
> r.i = a.i - b.i;
> return r;
> }
>
> int
> _cmplx_eq_(Cmplx a, Cmplx b)
> {
> Cmplx r;
>
> return a.r == b.r && a.i == b.i;
> }
>
> /*
> * assignment-op (can be made from assig and op)
> */
> Cmplx
> _cmplx_asadd_(Cmplx *a, Cmplx b)
> {
> *a = *a + b;
> return *a;
> }
>
> Cmplx
> _cmplx_assub_(Cmplx *a, Cmplx b)
> {
> *a = *a - b;
> return *a;
> }
>
> /*
> * conversions
> */
> int
> _cmplx_i_(Cmplx a)
> {
> return sqrt(a.r*a.r + a.i*a.i);
> }
>
> double
> _cmplx_d_(Cmplx a)
> {
> return sqrt(a.r*a.r + a.i*a.i);
> }
>
> Cmplx
> _d_cmplx_(double a)
> {
> Cmplx r;
>
> r.r = a;
> r.i = 0;
> return r;
> }
prev parent reply other threads:[~2005-05-07 18:26 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-05-07 16:56 "Nils O. Selåsdal"
2005-05-07 16:59 ` Rob Pike
2005-05-07 18:26 ` jmk [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=785c9b3ba79825b48fb2c35b0415ef91@plan9.bell-labs.com \
--to=jmk@plan9.bell-labs.com \
--cc=9fans@cse.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).