polymorphism in C

E

Edward A. Falk

(I've used polymorphism in my language projects for many years (I've only
recently discovered there was a special word for it!) The latest one is
implemented (indirectly) in C.

I would be interested in seeing a sample of how you did it.
 
B

BartC

Edward A. Falk said:
I would be interested in seeing a sample of how you did it.

By 'indirectly' I mean that an interpreter is written in C for the language
in question.

(The data structures I use *could* be used directly in C, and most things
done with functions calls, however my experiments seemed to show that this
wouldn't be much faster than just interpreting bytecode instead).

I also use dynamic typing, which as well as polymorphism (I couldn't tell
you which kind, apparently there are several) gives you generics for free!
But dynamic typing is also much slower (to execute) than a C++-like
approach.

The technique uses tagged data, which can be implemented in many ways. This
is how it might be done in Lua:

www.lua.org/doc/jucs05.pdf

(on page 5, using a couple of tagged unions). However my language is richer
and more ambitious, and so the unions are a bit more complex (see below).

In brief, every data type is stored inside a 16-byte struct, of which the
first 2 bytes are the type-tag. The data value has to fit inside the rest
(simple values are stored directly, others via a pointer to heap data).

This one struct is passed and returned by value everywhere (if GC was used,
perhaps passing a pointer to the struct would do; but I don't have GC).

Most of the actual type-dispatch (either single or double) is taken care of
inside the interpreter (in the C version, using switch statements;
previously, using lots of jump and call tables). It is unusual for an
application in this language to need or want to know the actual type of
anything. It's just not necessary!

(My data union. This uses anonymous unions and structs that might be
non-standard. i32 etc. are typedefs:

typedef struct _varrec {
union {
struct {
u16 tag;
u8 copy;
u8 alloc;
};
u32 tagx;
};

union { /* .value3 */
int value3;
int lower;
struct {
u16 elemtype;
union {
i16 shortlower;
struct {
i8 bitslower;
i8 bitoffset;
};
};
};
int cdata;
};

union {
struct {
union { /* .value */
int value;
uchar* svalue;
byte* ptr;
int* iptr;
struct _varrec* vptr;
int* retaddr;
};

union { /* .value2 */
int value2;
int length;
int upper;
};
};
i64 dvalue;
r64 xvalue;
};

} varrec;

)
 
S

Shao Miller

I would not expect that the constructs that were shared would be used in
"an object having a certain class," but only in the instances of a class
itself.

That is, 'myclass' seems indicative of "a new class" rather than "a new
myclass." I'd guess that "myobj" might have its first member as a
'Class *', thus saving most of the storage requirement you mention.

Obviously I could be wrong, as I've no idea which book this is inspired
by. I'd read a PDF (for this sort of thing in C) once where "class
objects" and "class instance objects" were distinct subjects, though.

Aha. I found the PDF I had read before. It seems convincingly to be
the OP's book (see the bottom of page with label "12"):

http://www.cs.rit.edu/~ats/books/ooc.pdf

- Shao Miller
 
B

BartC

Shao Miller said:
Aha. I found the PDF I had read before. It seems convincingly to be the
OP's book (see the bottom of page with label "12"):

http://www.cs.rit.edu/~ats/books/ooc.pdf

Yes, I see what you mean now. So 'Class' is actually the name of a
type-description struct, rather than an ill-advised name for a user object
type. In the actual example, 'String' is the name of the user type, and the
first element points to that table, the way I remember seeing this stuff
done before.

So the actual access from C *will* be a little messier, but that code only
needs to be present in a few places.

(And I haven't read the whole thing, but I would just have used an array
instead of the Class struct, using enumerations for the different kinds of
handlers. Actually I've just seen class hierarchy, metaclassing and
inheritance mentioned - this is where I bail out I think...)
 
J

JimB

James said:
On 12/17/2012 02:27 AM, Keith Thompson wrote:
...

I'm sure you know this, and it's implicit in what you've said, but for
Jack's sake

Did you mean "Jake"? Well yeah, he is noticeably absent, and also are the
meeting results of the C committee. The nothingness of C looking for a
scapegoat? (Moi?). Yes, C is dead. DEAD!

I of course am an asshole for knowing why C sucks? Same old, aint it? C
programmers went to the moon on masturbation, well then surely all can be
had upon masturbation. So is C.

You are "programmers", right? All I say is in confidence right? Well shut up
and "hear me out (?) (I'm not you):

You leave high school pimply and inadequate ("the girl you love", did the
girl they took from you set your stage? Isn't that just right: you give
opportunity to the evil law (oh, did I feign missing the quotes:
psychologists working for the judges working with "the states" attorneys...
I know "states attorney", very well. But would I dare? Would someone in a
nazi death camp defy the automation-killing-of-people gestapo?).

But I digress. Or I get drunk? It gets worse, huh. I get drunk and then get
mad and "tell you what I really think". Huh. And, "You can't handle the
truth." Because you in your girly-whites don't know... yes you do, hmm?
ASSHOLE: your JOB is to .. , bitch, what is your job? Pftt, you have none,
you are rapist. Rape is rape. Answer: NO. War: YOU die, for YOUR war.
Hello? YOU are fired.

Me done? Are you fucking kidding? You can't pay me. You are over your limit.
Go vote, cuz at least now it is verbal: rape is rape. So, go **** yourself
"Mr. President".

****

James K: wasn't talkin to ya or at ya or back at ya.. just having a "good
time" gettin drunk in detroit!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,204
Latest member
abhinav72673

Latest Threads

Top