読者です 読者をやめる 読者になる 読者になる

Cプリプロセッサで加算までやった

Programming C CPP

数値は2進リスト。

#define par_open (
#define par_close )
#define comma ,

#define cat_(a,b) a##b
#define cat(a,b) cat_(a,b)

#define defif_val_0(t, f) f
#define defif_val_1(t, f) t
#define defif_(b) defif_val_##b
#define defif(b) defif_(b)

#define defnot(b) defif(b)(0, 1)
#define defand(b1, b2) defif(b1)(b2, 0)
#define defor(b1, b2) defif(b1)(1, b2)
#define defxor(b1, b2) defif(b1)(defnot(b2), b2)

#define defnil
#define defnil_p_(car, cdr) defnil_p_true_
#define defnil_p_true_val 0
#define defnil_p_val 1
#define defnil_p(list) cat(defnil_p_ list, val)
#define defifnil(x) defif(defnil_p(x))
#define defcar_(car, cdr) car
#define defcar(list) defcar_ list
#define defcdr_(car, cdr) cdr
#define defcdr(list) defcdr_ list
#define defcadr(list) defcar(defcdr(list))
#define defcddr(list) defcdr(defcdr(list))
#define defcaddr(list) defcar(defcddr(list))
#define defcdddr(list) defcdr(defcddr(list))
#define defcadddr(list) defcar(defcdddr(list))
#define defcddddr(list) defcdr(defcdddr(list))
#define defreverse_p_(r, c) defnot(defnil_p(c))
#define defreverse_f_(r, c) ((defcar(c),r),defcdr(c))
#define defreverse_p(arg) defreverse_p_ arg
#define defreverse_f(arg) defreverse_f_ arg
#define defreverse(list) defcar(defwhile(defreverse_p, defreverse_f, (defnil,list)))

/* defwhileX repeats 2^X times. */
#define defwhile0(p, f, x) defif(p(x))(f(x), x)
#define defwhile1(p, f, x) defif(p(x))(defwhile0(p, f, defwhile0(p,f,x)), x)
#define defwhile2(p, f, x) defif(p(x))(defwhile1(p, f, defwhile1(p,f,x)), x)
#define defwhile3(p, f, x) defif(p(x))(defwhile2(p, f, defwhile2(p,f,x)), x)
#define defwhile4(p, f, x) defif(p(x))(defwhile3(p, f, defwhile3(p,f,x)), x)
#define defwhile5(p, f, x) defif(p(x))(defwhile4(p, f, defwhile4(p,f,x)), x)
#define defwhile6(p, f, x) defif(p(x))(defwhile5(p, f, defwhile5(p,f,x)), x)
#define defwhile7(p, f, x) defif(p(x))(defwhile6(p, f, defwhile6(p,f,x)), x)
#define defwhile8(p, f, x) defif(p(x))(defwhile7(p, f, defwhile7(p,f,x)), x)
#define defwhile9(p, f, x) defif(p(x))(defwhile8(p, f, defwhile8(p,f,x)), x)
#define defwhile10(p, f, x) defif(p(x))(defwhile9(p, f, defwhile9(p,f,x)), x)
#define defwhile11(p, f, x) defif(p(x))(defwhile10(p, f, defwhile10(p,f,x)), x)
#define defwhile12(p, f, x) defif(p(x))(defwhile11(p, f, defwhile11(p,f,x)), x)
#define defwhile13(p, f, x) defif(p(x))(defwhile12(p, f, defwhile12(p,f,x)), x)
#define defwhile14(p, f, x) defif(p(x))(defwhile13(p, f, defwhile13(p,f,x)), x)
#define defwhile15(p, f, x) defif(p(x))(defwhile14(p, f, defwhile14(p,f,x)), x)
#define defwhile16(p, f, x) defif(p(x))(defwhile15(p, f, defwhile15(p,f,x)), x)
#define defwhile17(p, f, x) defif(p(x))(defwhile16(p, f, defwhile16(p,f,x)), x)
#define defwhile18(p, f, x) defif(p(x))(defwhile17(p, f, defwhile17(p,f,x)), x)
#define defwhile19(p, f, x) defif(p(x))(defwhile18(p, f, defwhile18(p,f,x)), x)
#define defwhile20(p, f, x) defif(p(x))(defwhile19(p, f, defwhile19(p,f,x)), x)
#define defwhile(p, f, x) defwhile20(p, f, x)

#define testlist (a,(b,(c,)))
defif(defand(1,0))(true, false);
defif(defand(1,1))(true, false);

testlist;
defnil_p(testlist);
defnil_p(defcdr(testlist));
defnil_p(defcdr(defcdr(testlist)));
defnil_p(defcdr(defcdr(defcdr(testlist))));

#define add_a(x) a##x
#define loop(arg) (defcdr(defcar(arg)), cat(a,defcdr(arg)))
#define pred(arg) defnot(defnil_p(defcar(arg)))
defcdr(defwhile(pred, loop, (testlist,x)));
#define truefunc(x) 1

#define defnum0
#define defnum1 (1,defnum0)
#define defnum2 (0,defnum1)
#define defnum3 (1,defnum1)
#define defnum4 (0,defnum2)
#define defnum5 (1,defnum2)
#define defnum6 (0,defnum3)
#define defnum7 (1,defnum3)
#define defnum8 (0,defnum4)
#define defnum9 (1,defnum4)
#define defnumA (0,defnum5)
#define defnumB (1,defnum5)
#define defnumC (0,defnum6)
#define defnumD (1,defnum6)
#define defnumE (0,defnum7)
#define defnumF (1,defnum7)
#define defnumcar(x) defifnil(x)(0,defcar(x))
#define defnumcdr(x) defifnil(x)(defnil,defcdr(x))

#define defplus_fulladd_re(a,b,c) defxor(a,defxor(b,c))
#define defplus_fulladd_cn(a,b,c) defor(defand(a,defor(b,c)),defand(b,c))
#define defplus_p_(r,c,x,y) defnot(defand(defnot(c),defand(defnil_p(x),defnil_p(y))))
#define defplus_f_(r,c,x,y) ((defplus_fulladd_re(c,defnumcar(x),defnumcar(y)),r),defplus_fulladd_cn(c,defnumcar(x),defnumcar(y)),defnumcdr(x),defnumcdr(y))
#define defplus_p(arg) defplus_p_ arg
#define defplus_f(arg) defplus_f_ arg
#define defplus_r_(r,c,x,y) r
#define defplus_r(arg) defplus_r_ arg
#define defplus(x,y) defreverse(defplus_r(defwhile(defplus_p, defplus_f, (defnum0, 0, x, y))))
defplus(defnum6,defnum7);
defnumD;