0

I'm implementing a neo-fork of Midnight Commander in a style resembling OO programming. It's however little irritating to pass the object as the first argument for virtual methods. I wonder if there is a C pre-processor that would be lifting this requirement? E.g.: that would translate some special dereference operator:

object-->method(a, b, c);

into:

object->method(object, a, b, c)

? It is so simple task that I could even be implementing it by myself. However, is there a ready to use preprocessor?

psprint
  • 349
  • 1
  • 10
  • 3
    I don't have an answer for you, but experience have thought me that code generation to be an evil leaky and usually bloated abstraction. It leaves you with a custom language no one else knows. If you want to emulate the C++ style well just use the real thing. I prefer the normal C-style class_method(object, ...) and find little value in stuffing function pointers into to your object. – Allan Wind Mar 07 '21 at 11:14
  • 1
    @AllanWind: it could work also for non-virtual functions, e.g.: `object→method(a.b,c)` would translate to: `method(object,a,b,c)` – just one more (and last) special dereference operator. – psprint Mar 07 '21 at 11:48
  • 2
    Why not use C++? – HolyBlackCat Mar 07 '21 at 11:51
  • 1
    I think that C++ is a bloated langue. The so many template-exaggerations introduced in recent years – I like C for its simplicity. – psprint Mar 07 '21 at 16:58
  • 1
    btw... `a-->b` will actually work in C as `(a--) > b` – tstanisl Mar 07 '21 at 21:35
  • 2
    You don't have to use the extra features you don't want. Nothing stops you from writing in "C with classes". – HolyBlackCat Mar 08 '21 at 09:11
  • C++ is predicated on its features being zero cost if you don't use them. – quamrana Mar 08 '21 at 09:38
  • @tstanisl Yes, that is the (in)famous "downto operator" https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c-c :D – chtz Mar 08 '21 at 14:58

2 Answers2

3

What you suggest is done by any C++ compiler (for virtual member functions).

So a possible approach could be to transform your C code into C++ code. In many cases, it is easy (since C++ was designed, 20 years ago, as a superset of C. It later evolved independently).

You could also consider writing your C code generator (technically, a translator from your dialect to C), with a parser developed with GNU bison or ANTLR.

Look also into the Gobject layer of GTK. It could be inspirational (and GTK is open source)

If you develop your own programming dialect, please document it well (with EBNF for syntax).

Another approach might be to embed some existing interpreter (like Lua, Ocaml, GNU guile, etc...) in your application.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

You could try following macro:

#define CALL(OBJ, METHOD, ...) (OBJ)->METHOD(OBJ, __VA_ARGS__)

Now the call:

CALL(object, method, a, b, c)

expands as:

object->method(object, a, b, c)

The issue is that at the method must take at least two arguments (object and something). Otherwise the compiler will complain about extra ,. There exist methods to lift this limitation but they are either compiler specific or very tricky.

tstanisl
  • 13,520
  • 2
  • 25
  • 40
  • Thanks. It's an interesting option. It isn't as simple as -> operator, however I think that it could be a good solution, resembling Objective C messages… I could write a similar macro `MESSAGE(object,method,…)` and also a non-virtual macro `#define MESSAGE2(OBJ, METHOD, ...) (METHOD(OBJ,## __VA_ARGS__)` so that non-virtual methods would have been covered… I wonder if an automatic detection of virtual methods could be created? I can use some GCC builtins, no problem. Macro should detect if there is a field of name of method, I think. Or detect if object is a struct, maybe? – psprint Mar 07 '21 at 17:07
  • @psprint, what about wrapping virtual calls as non-virtual ```ret_type obj_method(struct obj* x, ...) { return x->method(x, ...); }``` and using only `MESSAGE2`-like macro ? – tstanisl Mar 07 '21 at 17:53