3

Macro enable to easily alias keywords in C, but can it be used to change macro keywords too, so instead of:

#include <stdlib.h>

#define se if

one may write

#inkludu <stdlib.h>

#difinu se if

In other words, can preprocessing directives be aliased, preferably out of the code itself, for example with a compiler argument such as -D for gcc.

A simple test as the following will fail:

#define difinu define
#difinu valoro 2

int main() {
    int aĵo = valoro;
    return 0;
}

with the following error:

 % clang makro.c -o makro
makro.c:2:2: error: invalid preprocessing directive
#difinu valoro 2
 ^
makro.c:5:16: error: use of undeclared identifier 'valoro'
    int aĵo = valoro;
              ^
2 errors generated.
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
psychoslave
  • 2,783
  • 3
  • 27
  • 44
  • 1
    Have you tried? And please choose one of C and C++, the two languages are not equal and their preprocessors aren't equal either. – fuz Aug 05 '16 at 08:38
  • No. You cannot use the preprocessor to change the way the preprocessor and the language syntax works. – Shark Aug 05 '16 at 08:42
  • Ok, I added documentation of what I already attempted, and restricted the question to C (although a C++ solution would be of interest to me). – psychoslave Aug 05 '16 at 08:49
  • What the flip are you trying to accomplish by translating the entire C language into Esperanto? – Lightness Races in Orbit Aug 05 '16 at 10:23
  • Well, as suggested bellow, having a dedicated language sure make more sense as a long term project, with selfhosting and all. Especially as one may rely – at least partially – on the Esperanto regular syntax within its design. Simply on the road I would appreciate using as much Esperanto as I can. Also my goal is to use full accurate words rather than shortened counterparts and other non alphabetic tokens. Now that may not be a largely shared desire, but that would enable to write code as I please. – psychoslave Aug 05 '16 at 16:11

4 Answers4

6

No. Macros do not change the ways preprocessor directives are handled (but of course can change according to conditional directives like #if). In particular, the following is wrong

///WRONG CODE
#define inc include
/// the following directive is not recognized as an include
#inc <stdio.h>

BTW, having

#define se if

is really a bad habit, even if it is possible. It makes your code se (x>0) {printf("negative x=%d\n", x);} much more difficult to read.

Consider perhaps preprocessing your source with some other preprocessor like m4 or GPP. This amounts to generate your C (or C++) code from something else.

My feeling is that metaprogramming is often a good idea: you write some specialized program which would e.g. emit C or C++ code (and you improve your build procedure, e.g. your Makefile, accordingly). But you might design a real C or C++ code generator (which would work on and process some kind of AST). Parser generators (incorrectly known as compiler-compilers) like ANTLR or bison are a good example of this. And Qt has moc.

See also this & that answers to related questions.

Don't forget to read several textbooks (notably related to compilers) before attempting your own code generator (or domain specific language implementations).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • It's along the lines of `#define TRUE 0 #define FALSE 1` – Shark Aug 05 '16 at 08:42
  • I wasn't aware of GPP, which seems a good fit for what I'm doing. That is mere translation of C using Esperanto keywords, without syntax modification. Thank you. So for this particular project, having "se" as conditional statement makes sense. – psychoslave Aug 05 '16 at 09:03
  • 1
    But doing that is really a bad idea. You want the C code to be readable by other developers world wide. Just translating keywords to Esperanto is simply *confusing* and does not add any value. If you insist on making a programming language with Esperanto keywords, design and implement your own such programming language (and make it *better* than C). – Basile Starynkevitch Aug 05 '16 at 09:04
  • 1
    I agree it's not a good self sustaining goal. However making this kind of project appears to me as a good way to learn what vocabulary may be useful, and concepts useful in the design and implementation of more original programming language. Thank you for your advice. – psychoslave Aug 05 '16 at 09:14
  • Then, your approach will waste a lot of time. Better start designing & implementing your "better-esperanto-like" programming language, and *incrementally* add features in it. Read the references given in the other answers I am mentioning. – Basile Starynkevitch Aug 05 '16 at 09:16
2

On a historical note, however please note that yes, some times ago there were compilers that made it possible to redefine the macro definitions, and a proof for this fact is the following entry from IOCCC 1985 which obviously compiled happily on a Vax 780/4.2BSD in those days:

http://ioccc.org/1985/lycklama/lycklama.c

which starts with:

#define o define
#o ___o write
#o ooo (unsigned)
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
0

"#define" is the keyword to pre processor for "MACRO substitution ( text replacement) for compilation/compiler". consider code compilation stages in c, 'pre processor->compiler->assembler->linker->loader'..

so, when you compile this code, pre processor trying to search the keyword "#difinu" which is not present.so you are getting the error from preprocessor stage itself.

moreover "#define" is single keyword, how can you expect pre processor to treat this as "#"+"define" . For example

#define game olympic 
main (){
 int abcgame =10;// will it become "abcolympic" ??
 return;
}
0

Localized languages do exist. Algol68 (available under Linux) is such a very old language (1968), that by the way is one of the superior languages that exist:

IF x < y THEN x ELSE y FI := 3;
( x < y | x | y ) := 3;
IF x < y THEN sin ELSE cos FI(3.14)

In your case I would write an additional preprocessor say for .eocpp and .eohpp to .cpp and .hpp. That could x-translate the special characters, or even a dictionary translation.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138