From owner-freebsd-hackers@FreeBSD.ORG Sat Dec 17 01:24:12 2005 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E853516A41F for ; Sat, 17 Dec 2005 01:24:11 +0000 (GMT) (envelope-from hselasky@c2i.net) Received: from swip.net (mailfe03.swip.net [212.247.154.65]) by mx1.FreeBSD.org (Postfix) with ESMTP id F189743D45 for ; Sat, 17 Dec 2005 01:24:10 +0000 (GMT) (envelope-from hselasky@c2i.net) X-T2-Posting-ID: Y1QAsIk9O44SO+J/q9KNyQ== X-Cloudmark-Score: 0.000000 [] Received: from mp-217-136-76.daxnet.no ([193.217.136.76] verified) by mailfe03.swip.net (CommuniGate Pro SMTP 5.0.2) with ESMTP id 56031647 for freebsd-hackers@freebsd.org; Sat, 17 Dec 2005 02:24:07 +0100 From: Hans Petter Selasky To: freebsd-hackers@freebsd.org Date: Sat, 17 Dec 2005 02:25:25 +0100 User-Agent: KMail/1.7 References: <200512131936.11640.hselasky@c2i.net> <20051215093059.GL77268@cirb503493.alcatel.com.au> <200512151234.29933.hselasky@c2i.net> In-Reply-To: <200512151234.29933.hselasky@c2i.net> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200512170225.26237.hselasky@c2i.net> Subject: Re: Standard C-macro scripting X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: hselasky@c2i.net List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Dec 2005 01:24:12 -0000 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