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

Boring You To Death, one Macro at a Time!

Sign in to follow this  
TANSTAAFL

72 views

some of what makes this even better is that nobody cares...


#define FALSE 0
#define TRUE 1


TRUE and FALSE aren't strictly necessary, but I like having them just the same.



#define SET(position,value) >(position)[-]+(value)<(position)
#define ZERO(position) SET(position,0)


The first BF program that most people see is "[-]", which is exactly what ZERO(0) or SET(0,0) spits out.

For useful macroing, the assumption is that, whenever you are done with an operation, you want to move back to your original position.

So, with SET, the idea is move "position" cells to the right, clear the cell, set the cell to "value", and then move back to where you started.


#define ADD_N(position,value) >(position)+(value)<(position)
#define SUB_N(position,value) >(position)-(value)<(position)
#define INC(position) ADD_N(position,1)
#define DEC(position) SUB_N(position,1)


ADD_N, SUB_N, INC and DEC might seem really trivial, and almost useless to put in, but I have found otherwise. In fact, they didn't even make it in until I was doing IF and ENDIF.


#define IF(position) >(position)[<(position)
#define ENDIF_N(position,levels) >(position)[-]](levels)<(position)
#define ENDIF(position) ENDIF_N(position,1)


IF and ENDIF suddenly made a lot of other macros a lot simpler, especially after I made the macro processing tool remove symmetrical instruction pairs.

It is important to note that after ENDIF, the cell at "position" is zero.

ENDIF_N is kind of weird, and may need an example:

Normally, IF/ENDIF is used like this:


IF(temp)
//do something here
ENDIF(temp)



but sometimes, you do stuff like:


IF(temp)
//do something here that changes the cell at "temp"'s value
IF(temp)
//do something here
ENDIF(temp)
ENDIF(temp)



As written above, each ENDIF winds up with a [-] before the terminal ]. In the case of the second ENDIF, this is redundant, because we are using the same cell, so we use ENDIF_N, and rewrite the code:


IF(temp)
//do something here that changes the cell at "temp"'s value
IF(temp)
//do something here
ENDIF_N(temp,2)



ENDIF_N was written /after/ ENDIF was, but I then later retooled ENDIF to use ENDIF_N. YAY! MACROS!



#define WHILE(position) IF(position)
#define ENDWHILE_N(position,levels) >(position)](levels)<(position)
#define ENDWHILE(position) ENDWHILE_N(position,1)



WHILE and ENDWHILE are rather similar in nature to IF/ENDIF, just without the clearing of the variable at the end. Naturally, there is also an ENDWHILE_N.


#define ADD(from,to) WHILE(from)DEC(from)INC(to)ENDWHILE(from)
#define SUB(from,to) WHILE(from)DEC(from)DEC(to)ENDWHILE(from)


A program similar to ADD is typically one of the first few BF programs shown a beginner. SUB isn't far behind. These demonstrate the usefulness of the WHILE/ENDWHILE and INC/DEC macros.




#define COPY(original,copy,temp) ZERO(copy)ZERO(temp)WHILE(original)DEC(original)INC(copy)INC(temp)ENDWHILE(original)ADD(temp,original)


Another common task is to make a copy of a value in a cell while leaving that value in that cell. Unfortunately, this requires an extra cell in BF.



#define COMPARE(first,second,temp) ZERO(temp)ADD(first,temp)SUB(second,temp)IF(temp)INC(first)ENDIF(temp)


Nothing is easy in BF. Comparing two values to see if they are equal is... a little involved. Shows the usefulness of IF/ENDIF. If the values at "first" and "second" are equal, then the value at "first" becomes 1, otherwise it becomes 0. In any case, the values at "second" and "temp" wind up 0.


#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)ZERO(position)ENDIF(temp)
#define TESTEQUAL(position,value,temp) SUB(position,value)TESTZERO(position,temp)



And you'd also think that the equivalents of "(x==0)" and "(x!=0)" would be easy. Guess again. TESTNONZERO does the !=0, and TESTZERO does the ==0. Once you've got these, it is a relatively simple matter to check against any given value by subtracting it and checking if the result is 0, like TESTEQUAL does.

The various TESTn macros need a new name.

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