I'm thinking of taking the output from the C preprocessor like this:
gcc -o compute.translation_unit -E -P compute.c
So then I'll have this translation unit and I'll run it through my own preprocessor, which will implement my previous code I shared about 2 weeks ago for doing polymorphism in C :
https://groups.google.com/forum/m/#!topic/comp.lang.c++/5lKrGQRQHQk
All my operators must be discernible from C operators, and so to invoke a method on an object, I might use syntax like:
object_name~>Method_Name(5, "dog");
(Note that that is a tilda instead of a hyphen, i.e. "~>" instead of "->", so it won't be confused with C's operator to access the members of a dereferenced pointer).
My preprocessor will turn this line into:
Virtcall(object_name,Method_Name)(5,"dog");
Construction and destruction of objects will be handled by simply declaring a class, perhaps with the 'class' keyword. I might also have a 'method' keyword like this:
class MyClass {
method void Constructor(int);
method void Destructor(void);
method void Some_Method(int, char const*);
int a;
double b;
void *p;
};
Similar to the old C style of always writing 'struct' or 'union' before a struct or union, you will always have to write 'class' before a class, and so you would create an object like this:
class MyClass obj(42);
And you would take a pointer in a function's parameter list like this:
void Some_Global_Func(class MyClass *p);
So if you put the following snippet through my preprocessor:
{
class MyClass obj(42);
obj~>Some_Method(5, "dog");
}
then it will come out as:
{
struct MyClass obj;
MyClass_Construct(&obj, 42);
Virtcall(&obj,Some_Method)(5,"dog");
Virtcall(&obj,Destructor)();
}
In order to be able to call the destructor at the right time, I think I'll use 'defer' which you can see on this page if you scroll down about half way:
https://mort.coffee/home/obscure-c-features/
So then I'll take the output of my own preprocessor and feed it back into the C compiler. The whole process will be something like:
gcc -o compute.preprocessed -E -P compute.c
gotham_preprocessor -o compute.translation_unit compute.preprocessed
gcc -o compute.o -c compute.translation_unit
So then you'll have an object file, "compute.o" which you can give forward into the linker.
So you might ask, what's the point in all this if we already have C++?
Well there are still loads of very small, very slow 8-bit microcontrollers with less than a megibyte of RAM/ROM that don't have a C++ compiler. My new preprocessor will make it a lot easier to port C++ code with polymorphic classes to C.