Sign in to follow this  
  • entries
    292
  • comments
    557
  • views
    153569

BF Macros, Take 3

Sign in to follow this  
TANSTAAFL

71 views

I got the second half of the BF macro preprocessor done, and I now have a collection of useful BF macros:

#define FALSE 0
#define TRUE 1
#define SET(position,value) >(position)[-]+(value)<(position)
#define ZERO(position) SET(position,0)
#define INC(position) >(position)+<(position)
#define DEC(position) >(position)-<(position)
#define IF(position) >(position)[<(position)
#define ENDIF(position) >(position)[-]]<(position)
#define WHILE(position) IF(position)
#define ENDWHILE(position) >(position)]<(position)
#define ADD(from,to) WHILE(from)DEC(from)INC(to)ENDWHILE(from)
#define ADD_N(position,value) >(position)+(value)<(position)
#define SUB_N(position,value) >(position)-(value)<(position)
#define SUB(from,to) WHILE(from)DEC(from)DEC(to)ENDWHILE(from)
#define COPY(original,copy,temp) ZERO(copy)ZERO(temp)WHILE(original)DEC(original)INC(copy)INC(temp)ENDWHILE(original)ADD(temp,original)
#define COMPARE(first,second,temp) ZERO(temp)ADD(first,temp)SUB(second,temp)IF(temp)INC(first)ENDIF(temp)
#define TESTNONZERO(position,temp) ZERO(temp)ADD(position,temp)IF(temp)INC(position)ENDIF(temp)
#define TESTZERO(position,temp) ZERO(temp)ADD(position,temp)INC(position)IF(temp)SET(position,FALSE)ENDIF(temp)



I added a redundancy check on the tail end of my macro utility.

After all of the <(x) have been expanded, it recursively removes any instance of "<>", "><", "+-", and "-+" that it finds within the resulting code. Based on how simple the preprocessor is, I either had to make it do math, or eliminate adjacent pairs of instructions that cancel out. Removing adjacent pairs was simpler.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now