Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Dec 2005 02:25:25 +0100
From:      Hans Petter Selasky <hselasky@c2i.net>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Standard C-macro scripting
Message-ID:  <200512170225.26237.hselasky@c2i.net>
In-Reply-To: <200512151234.29933.hselasky@c2i.net>
References:  <200512131936.11640.hselasky@c2i.net> <20051215093059.GL77268@cirb503493.alcatel.com.au> <200512151234.29933.hselasky@c2i.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thursday 15 December 2005 12:34, Hans Petter Selasky wrote:
> On Thursday 15 December 2005 10:30, Peter Jeremy wrote:
> > On Wed, 2005-Dec-14 01:05:45 +0100, Hans Petter Selasky wrote:
> > >On Tuesday 13 December 2005 20:48, John Baldwin wrote:
> > >> Honestly, I think I've now been scarred for life. :-/  I think that
> > >> this stuff would be so obscure that no one else would be able to help
> > >> with maintenace.
> >

Will introducing the word "IF" in the conditional expressions make things more 
clear?

>
> > >And how would you solve it?
> >
> > See above.
>
> It is just that one has to build up the macro C-scripting language with
> some helper macros like NOT/AND/OR/XOR/YES/NO/SWITCH ... to make it easy to
> use.

It is very amazing, but one can actually compare 32-bit hexadecimal numbers by 
only defining a handful of macros. One might think that to compare two 
hexadecimal numbers, ranging from '0' to 'F' inclusivly, one would need to 
define 256 macros. But no. The secret here is to think binary. Look at the 
"EQ_2" macro below. It compares two 16-bit hexadecimal numbers.

/*
 * Standard C-macro scripting
 * ==========================
 *
 * Principle:
 *
 * A macro true will keep something.
 * A macro false will discard something.
 *
 */
#define YES(...) __VA_ARGS__ /* logical true */
#define NO(...)              /* logical false */

/* definition of logical operations */

#define ___AND_(...) 
#define ___AND_1(...) 
#define ___AND_11(...) __VA_ARGS__
#define __AND(a,b) ___AND_##a##b
#define _AND(a,b) __AND(a,b) 
#define AND(a,b) _AND(a(1),b(1))

#define ___OR_(...) 
#define ___OR_1(...) __VA_ARGS__
#define ___OR_11(...) __VA_ARGS__
#define __OR(a,b) ___OR_##a##b
#define _OR(a,b) __OR(a,b) 
#define OR(a,b) _OR(a(1),b(1))

#define ___XOR_(...) 
#define ___XOR_1(...) __VA_ARGS__
#define ___XOR_11(...)
#define __XOR(a,b) ___XOR_##a##b
#define _XOR(a,b) __XOR(a,b) 
#define XOR(a,b) _XOR(a(1),b(1))

#define ___NOT_(...) __VA_ARGS__
#define ___NOT_1(...) 
#define __NOT(a) ___NOT_##a
#define _NOT(a) __NOT(a) 
#define NOT(a) _NOT(a(1))

/* hexadecimal to binary conversion */

#define HEX_TO_BIN_0  NO, NO, NO, NO
#define HEX_TO_BIN_1  NO, NO, NO,YES
#define HEX_TO_BIN_2  NO, NO,YES, NO
#define HEX_TO_BIN_3  NO, NO,YES,YES
#define HEX_TO_BIN_4  NO,YES, NO, NO
#define HEX_TO_BIN_5  NO,YES, NO,YES
#define HEX_TO_BIN_6  NO,YES,YES, NO
#define HEX_TO_BIN_7  NO,YES,YES,YES
#define HEX_TO_BIN_8 YES, NO, NO, NO
#define HEX_TO_BIN_9 YES, NO, NO,YES
#define HEX_TO_BIN_A YES, NO,YES, NO
#define HEX_TO_BIN_B YES, NO,YES,YES
#define HEX_TO_BIN_C YES,YES, NO, NO
#define HEX_TO_BIN_D YES,YES, NO,YES
#define HEX_TO_BIN_E YES,YES,YES, NO
#define HEX_TO_BIN_F YES,YES,YES,YES

/* logical comparison of hexadecimal values */

#define ___EQ_0(a,b,c,d,e,f,g,h) \
  NOT(OR(OR(XOR(a,e),XOR(b,f)),OR(XOR(c,g),XOR(d,h))))
#define __EQ_0(...) ___EQ_0(__VA_ARGS__)
#define _EQ_0(a,b) __EQ_0(HEX_TO_BIN_##a, HEX_TO_BIN_##b)
#define EQ_0(...) _EQ_0(__VA_ARGS__)
#define EQ_1(a,b,c,d) AND(EQ_0(a,c),EQ_0(b,d))
#define EQ_2(a,b,c,d,e,f,g,h) AND(EQ_1(a,b,e,f),EQ_1(c,d,g,h))

/* symbolic wrappers for conditional expression */

#define IF(a) a
#define IFE(a,b,c) a b NOT(a) c




/* examples */


#define VAR_A YES
#define VAR_B NO
#define VAR_C NO

IF(OR(AND(VAR_A,VAR_B),VAR_C))
(
   keep it 1
)

IF(OR(OR(VAR_A,VAR_B),VAR_C))
(
   keep it 2
)

IFE(YES,
(
   statement is true
),
(
   statement is false
))

IF(EQ_0(A,B))( 0xA == 0xB )
IF(EQ_1(A,B, A,A))( 0xAB == 0xAA )
IF(EQ_2(A,B,C,0, A,B,C,0))( 0xABC0 == 0xABC0 )



Here is the output from "cpp":

# cpp test.h

keep it 2

statement is true

0xABC0 == 0xABC0



What more functions would a C-macro scripting language require ?



--HPS




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200512170225.26237.hselasky>