Thursday, 5 December 2013

The Almighty '#define's.... !!

This article is meant to demonstrate the power of preprocessor directives, #define , to be specific. This is NOT meant to be a complete tutorial on pre-processors or programming. So here are some pre-requisites that are assumed for the target reader :
1. A background in c++ language.
2. A programming attitude.
3. A little knowledge about pre-processor directives would be better.
.
Let's first see what we are going to do. After reading this you would be able to "Make Your Own Mini Programming Language in c++". :)
Yeh, you read it right... You own language!!
By your own language i mean that you would be able to compile and run something like:
   
     START_CONSOLE_APP
     PRINT  "Hello World!"    ENDL
     CLOSE_CONSOLE_APP

yeh!! Isn't it cool??
.
Let's start :
First, what are pre-processors??
Pre- processors are those tools which analyse and do some work with your c++ code even before they are sent to the compiler. Pre-processors parse only the pre-processor directives (everything beginning with a '#' ).

What is #define??
It is one of the most widely used PP directive after #include. It is just used to provide an alias for something or some expressions. Although it is also used to define constants and macros ( macros??? want to know about them?! just google it.... ).

syntax :
          #define      ALIAS         ORIGINAL_EXPRESSION

example :
          #define           PI         3.1415


From above, PP directives don't end with semi-colon. They simply end when the line ends. The above code facilitates you to use 'PI' in place of 3.1415.... (not cool.... i know.)
.
If you are thinking : "Not a big deal, i could have simply defined a float variable to do the same." Just wait, the main power is still to be unleashed!!
.

A very good thing about #define is that you can provude alias to any thing and practically everything you want to....
example :  
      #define START     {
is perfectly fine!!
So, you can use :

       #define START   {
       #define END     }
       int main()
       START
       return 0;
       END

you can simply compile the above code in c++ without any errors! (Now, if you don't think that is cool, this article is NOT meant for you... You have wasted a lot of your time reading upto here...)
.
Now, i would simply give you complete code for the library i have made... it is commented wherever required...
/** my_lang.h
*   Just Edit this file as per your choice and make your own custom language...
*   Currently it provides only console input and output functions. But, it can certainly be extended
*   beyond all limits to include many many more functionalities in your OWN language...
*/

/* Just ignore the next two lines... They just ensure that including this file more than 1 time does not do any harm  */
#ifndef __MY_LANG_H__
#define __MY_LANG_H__

/** Here Starts the pre-processor directive sequence... */

// This will include iostream to provide console input and output
#include<iostream>
// This will be the start for any program
#define START_CONSOLE_APP       int main() {
// PRINT will be used to display something on the console
#define PRINT     std::cout<<
// MORE_P will allow to print more than one thing using one PRINT command
#define MORE_P      <<
// ENDL required after every line to terminate every line of the code
#define ENDL       ;

// Just defining alias for the data types
#define INTEGER int
#define DECIMAL float
#define LONG_DECIMAL  long long double
#define LONG_INTEGER long long int

// TAKE_INPUT takes the input from console
#define TAKE_INPUT std::cin>>
// MORE_I will allow to take more than one value using one TAKE_INPUT command
#define MORE_I      >>

// Just finish off your code with this command
#define CLOSE_CONSOLE_APP        return 0; }

/** All the directives end here... Ignore the next line. */

#endif // __MY_LANG_H__

/** I hope you liked it.... :)     */

.
Just copy paste above code and save it as "my_lang.h"   .
Now make a cpp file and type in :
/** main.cpp
*   Just a test file for the custom language
*/
// The only C++ Syntax code in this file
#include"my_lang.h"

START_CONSOLE_APP

PRINT 20  ENDL                   // Just Printing a number
PRINT "\nHello World!!!"   ENDL  // Printing a string

INTEGER n  ENDL                   // Declaring a variable
PRINT "\n Enter a number..."  ENDL
TAKE_INPUT n ENDL                 // Taking input from console

PRINT "\nYou Entered..." MORE_P n MORE_P "\n WOWEE... :)"  ENDL

CLOSE_CONSOLE_APP
.

Make sure that the cpp file and my_lang.h are in the same directory...
Compile and run the program... Here you go....
Just add more functions to your language and Have fun!! :)
.
It is my first article ever, so it might have been poorly written and things might not have been explained very well...

Please comment and let me know what you felt about this article or if you have any queries...