I read that Objective-C was made by using preprocessor directives to add features of Smalltalk to C, which got me a little curious so I started tinkering with preprocessors in C++, just because I was bored and came up with this:
#include <iostream>
#include <string>
#define Constant const
#define Integer int
#define Real double
#define Boolean bool
#define Character char
#define String string;
#define System system
#define StandardLibrary std
#define OutputStream cout
int main()
{
Integer i = 1;
Integer ii = 2;
Integer iii = ii + i;
StandardLibrary::OutputStream<<iii;
System("pause");
return 0;
}
So yeah, it’s pretty obvious you can change the names using preprocessors, but how is it possible to implement features of one language into another language using preprocessors?
I’m not planning to make my own language through this. I’m just curious as to see how it works.
A language like Objective C can be implemented using a preprocessor, but not the C preprocessor, as the C preprocessor language is fairly limited, if it even is turing complete (it may not be), it’s at best a really nasty turing tarpit. A more powerful preprocessor will allow more significant alterations to syntax.
A preprocessor in general is a program that takes a source file and does text transformation according to some rules into some other set of source code, which is then compiled as for example C code.