Showing posts with label macros. Show all posts
Showing posts with label macros. Show all posts

Tuesday, February 18, 2014

More macro Phun

Many believe syntax of C + + is weird, a bit unintuitive, ugly, cryptic and obfuscated. Some are pointing out the Pascal syntax as an opposite parallel.
Let’s note that the Pascal language was initially developed by N. Wirth as an educational language. This language demonstrates such conceptual peculiarities, like strict typing and availability of means of structured (procedural) programming. And, by N. Wirth, this language should facilitate forming of a good style of algorithmic thinking, and, thus, programming. In particular, the author tried to make its syntax intuitively clear even at the first acquaintance with the discipline “Programming”.
During long time Pascal was fairly considered one of the best programming languages for education purposes. Unfortunately, the versions of programming environments, which were used to teach the language (Turbo Pascal, Borland Pascal), have become outdated.
New programming systems, which are based on Pascal, for example, Delphi, are too expensive and industrially, not educationally, oriented. Particularly, the programming environment is poorly suitable to teach basic programming and algorithmization due to its complexity. Well..you can use C++ to write in whatever language you like.Well not exactly, but pretty much. Here's an example of a fun proggie I wrote.
It demonstrates nested procedures, which aren't present in the actual Pascal.



#include "Pascal.h"

Program Project1
begin

var
Integer(U) = 7;
Single(x) = Single(U / 1.51);
String(str) = "cool";
 
WriteLn(str);
WriteLn("Input a symbol : ");
Read(U); 

if U > 5 then 
begin 
 WriteLn("Pascal rulez !!");
 Exit(0); 
end 
else
 begin
   WriteLn("C rockz!");
 end;
 
 for U = 0 to U < 10 do  
 begin
    WriteLn("C++ powns all ");
    Inc(U,1); 
 end;
 
 procedure NestedProc 
 begin
   var 
   String(s); //4astna  promenliva
 
    Begin
        s = "Hello from a nested proc ";
        WriteLn(s);
    End; 

  end NestedProc; 

 NestedProc.exec();
 
 Exit(0);
 
end


Here's how the header Pascal.h looks like.
using namespace std;
#include 
#include 
#define Integer int
#define Single float
#define Pointer void*
#define begin {
#define end }
#define end; }
#define WriteLn(str) printf(str)
#define Program int main(int argc, char *argv[])
#define Project1 
#define if if(
#define then )
#define Exit return
#define Result(x) return x
#define Integer(x) int x
#define do ;)
#define for for(
#define Inc(x,y) x += y
#define to ;
#define Read(x) scanf("%u",&x)
#define String(name) char* name
#define Pointer(p) void* p
#define procedure struct
#define Begin void exec(){
#define End };                                   
#define var   
#define Addr(x) &x
#define deAddr(x) *x
                                  
                                       

Monday, February 17, 2014

Back to the pre-wheel epoch

Ever thought why so many people adore C++, and so many execrate it at the same time ? Well, actually I don't know, but I guess it has something to do with it's elemental power, wild beasty nature and unpredictable manner.And no, I'm not going into that weird 'pro-grammers' humor like for example -
"In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg." by the god-father Bjarne Stroustrup, or
"Writing in C or C++ is like running a chain saw with all the safety guards removed," , or
"C(++) is a write-only, high-level assembler language." or even this :
"C++ : Hard to learn and built to stay that way !!" , and this :
"How C++ is like teenage sex:

  • It is on everyone's mind all the time. 
  • Everyone talks about it all the time. 
  • Everyone thinks everyone else is doing it. 
  • Almost no one is really doing it. 
  • The few who are doing it are: A. Doing it poorly. B. Sure it will be better next time. C. Not practicing it safely."

Most people think that some of the coolest feature of C++ is it's templates.They are increasingly found to be powerful instruments for the development of cleaner, faster and smarter software. Indeed, templates have become the corner stone of several new C++ programming paradigms.Templates are very useful when implementing generic constructs like vectors, stacks, lists, queues which can be used with any arbitrary type. C++ templates provide a way to re-use source code as opposed to inheritance and composition which provide a way to re-use object code.
Ok , that's great, but i would say that actually the good'old C macros are just as cool as C++ templates. Generic programming and templates in C ? Yes it's pretty much possible, with a bit of macros involved :) Here's an example i wrote to test my wheel reinvention deduction.It's a basic attempt to replace the C++ STL vector<> container.
Sorry about the code formatting.

#define VECTOR(type) struct type##_Vector \
{  public :  type *pArray;  int len;  type illegal; \
  type##_Vector() {   len = 0;   pArray = NULL; }; \ 
 void ClearAll()  {    memset(pArray, 0, sizeof(type) * len); \
   len = 0;  };    void Erase()  {    if(pArray != NULL) \
     free(pArray);    len = 0;    pArray = NULL;  }; \
  int push_back(type Data)  {  \
    type* pTmp = (type*)malloc(sizeof(type) * len + sizeof(type) ); \
      if(pTmp == NULL)        return -1; \    
 memcpy(pTmp, pArray, sizeof(type) * len);      free(pArray); \
      pArray = pTmp;    pArray[len] = Data;    return len++;  }; \ 
   void SetItemAt(int pos, type data) \  
 {    if((pos > -1) && (pos <= len))  \
   {      pArray[pos] = data;    }  } \
   type GetItemAt(int pos)  {    if((pos > -1) && (pos <= len)) \
      return pArray[pos];    return illegal; \
 } \

} 


int main(int argc, char* argv[])
{
VECTOR(int) v;
v.push_back(14);
v.push_back(77);

class test {
public :
char name[64];
test(){strcpy(name ,"wtf"); };
void setName(char* Name){strcpy(name,Name);};
};

VECTOR(test) vtest;

test t;
vtest.push_back(t);
printf(vtest.GetItemAt(0).name);
vtest.Erase();
v.Erase();
return 0;
}