[DGD]Adding new data type

Lorrimar lorrimar at treko.net.au
Tue Apr 25 11:09:06 CEST 2000


I want to make a new variable type called "vector". The vector represents a
position in 3D space and you should be able to do stuff like this to it:

vector a, b, c;

a.x = 5; a.y = -10; a.z = 5; // assignment to individual elements
b.x = 1; b.y = 4; b.z = -3;

c = a + b; // addition
c = {[ 1, 2, 3 ]}; // assignment of 3 element array to vector
c = a + 5; // addition of a scalar
c = 2 * a; // scalar multiplication

You get the idea. I had a look through data.c in the DGD sources but I was
daunted by its complexity. Despite knowing C fairly well, I have little
experience with parsers. Would anyone like to give any pointers to specific
functions I should look at to begin implementing this?

I have a few doubts about getting things like a.x going because I haven't
seen the . member operator used in LPC.

There could be an alternative:
object a, b, c;

a = clone_object(VECTOR); // make a vector
b = clone_object(VECTOR);
a->set_x(5); a->set_y(-10); a->set_z(5);
b->set_x(1); a->set_y(4); a->set_z(-3);

c = vector_add(a, b); // vector_add would be defined in auto
c = vector_from_array({[ 1, 2, 3 ]});
c = vector_add_scalar(a, 5);
c = vector_scale(a, 2);


This is a bit painful because the code is verbose without operator
overloading. And, with a lot of vector objects, performance may become a
little sluggish.

Does anyone have opinions on the advantages/disadvantages of each method? In
the long run I would like to have a vector data type but I'm worried that it
may be a little difficult.

/Rodney


List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list