From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <785c9b3ba79825b48fb2c35b0415ef91@plan9.bell-labs.com> Date: Sat, 7 May 2005 14:26:13 -0400 From: jmk@plan9.bell-labs.com To: 9fans@cse.psu.edu Subject: Re: [9fans] KenC operator overloading. In-Reply-To: <427CF340.4060807@asgaard.homelinux.org> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Topicbox-Message-UUID: 466f5f58-ead0-11e9-9d60-3106f5b1d025 >>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 > #include > > 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; > }