9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] KenC operator overloading.
@ 2005-05-07 16:56 "Nils O. Selåsdal"
  2005-05-07 16:59 ` Rob Pike
  2005-05-07 18:26 ` jmk
  0 siblings, 2 replies; 3+ messages in thread
From: "Nils O. Selåsdal" @ 2005-05-07 16:56 UTC (permalink / raw)
  To: 9fans

Hello,

I recently ventured down the cmd/cc/ sources. And what did I discover ?
Take a peek of the following (sorry ass of an -) example and see yourself.

#include <u.h>
#include <libc.h>

struct Foo {
	int x,y;
};

typestr struct Foo Foo;

Foo Foo_add_(Foo a,Foo b)
{
	return (Foo){a.x+b.x, a.y+b.y};
}

void main(void)
{
	Foo f1 = {1,1},f2 = {2,2},f3;
	f3 = f1 + f2;

	print("%d,%d\n",f3.x,f3.y);	
	exits(nil);
}

Neat, no ?

Any particular reason this isn't documented ?

--
Nils O. Selåsdal


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] KenC operator overloading.
  2005-05-07 16:56 [9fans] KenC operator overloading "Nils O. Selåsdal"
@ 2005-05-07 16:59 ` Rob Pike
  2005-05-07 18:26 ` jmk
  1 sibling, 0 replies; 3+ messages in thread
From: Rob Pike @ 2005-05-07 16:59 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

yes, there is a reason. it was one of the last features ken put into
his compiler before he left bell labs, and it was far from fully
implemented or fully tested.

-rob


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] KenC operator overloading.
  2005-05-07 16:56 [9fans] KenC operator overloading "Nils O. Selåsdal"
  2005-05-07 16:59 ` Rob Pike
@ 2005-05-07 18:26 ` jmk
  1 sibling, 0 replies; 3+ messages in thread
From: jmk @ 2005-05-07 18:26 UTC (permalink / raw)
  To: 9fans

>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;
> }



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-05-07 18:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-07 16:56 [9fans] KenC operator overloading "Nils O. Selåsdal"
2005-05-07 16:59 ` Rob Pike
2005-05-07 18:26 ` jmk

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).