Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C preprocessor secrets explained

Name: Anonymous 2017-07-01 16:52

String concatenation is a primitive operation, but it combines with term rewriting
Become extremely versatile(like e.g. brainfuck while being deceptively simple is turing complete and capable of complex computations) since it.

More theory on that is here:
https://en.wikipedia.org/wiki/Rewriting
(note the non-determinism and context sensitive expansion of terms)
Contrast with https://en.wikipedia.org/wiki/Regular_expression



Here is example of purely concatenative boolean operation
#define bool_merge2_(a,b) a##b
#define bool_merge2(a,b) bool_merge2_(a,b)
#define bool_neg0 1
#define bool_neg1 0
// arg:0 -> bool_neg~0= 1
#define bool_neg(v) bool_merge2(bool_neg,v)

#define bool_and00 0
#define bool_and01 0
#define bool_and10 0
#define bool_and11 1
#define bool_merge3_(a,b,c) a##b##c
#define bool_merge3(a,b,c) bool_merge3_(a,b,c)
#define bool_and(x,y) bool_merge3(bool_and,x,y)
#define bool_nand(x,y) bool_neg(bool_and(x,y))
//more at https://www.reddit.com/r/frozenvoid/wiki/void2/boolean_h

Recursion is emulated via iterative steps:
#define funcarg1(f,args...)
#define funcarg2(f,args...)
#define funcarg3(f,args...) f,funcarg2(f,args) //each can refer to earlier function
//where funcarg is merged with argument count
merge(funcarg,argcount(args))(f,args) //and called with supplied arguments
result: funcarg##3(f,args) =>funcarg3(f,args)

Tuples as arguments:
#define id(x...) x //returns the arguments as is
#define maketuple(x...) (x) //returns the argument in parens
#define atuple maketuple(1,2,3) -> (1,2,3)
what happed when id is passed a tuple instead of arguments?
id atuple -> id(1,2,3) -> 1,2,3 //tuple is expanded back to list
(id atuple) -> (id(1,2,3))->(1,2,3)
Merging tuples is easy
#define mergetuples(a,b) (id a,id b)
mergetuples((1,2,3),(a,b,c)) =>(id(1,2,3),id(a,b,c))=>(1,2,3,a,b,c)
You can extract specific members of tuple with another macro
#define thirdarg(a,b,c,args...) c =>
#define atuple4 (1,3,4,5)
thirdarg atuple4 => 4



Finding argument count)see https://www.reddit.com/r/frozenvoid/wiki/void2/argcount_h)
The basic idea is exploiting the floating position of argument name
#define counter(a1,a2,a3,a4,...) a4 //the function returns argument at position a4
counter(3,2,1,^0) returns 0, but what if initial arguments are shifted forward
counter(a,b,c,^3,2,1,0) returns 3 since the numbers are shifter forward
A helper function countargs(args...) sends them to counter:
#define countargs(args...) count(args,3,2,1,0) //which allows to return the token
//which represents the amount of arguments given to countargs

Argument manipulation:
#define car(a,args...) a //return the first argument passes
#define cdr(a,args...) args //return the rest of arglist
#define funcall(f,args...) f(args) //use the first argument as function on rest

Primitive apply with iterative steps
#define apply0(f,args...) //do nothing
#define apply1(f,a,args...) f(a)
#define apply2(f,a,args...) f(a),apply1(f,args)
#define apply3(f,a,args...) f(a),apply2(f,args)
#define apply(f,args...) merge(apply,argcount(args))(f,args)
Can be generalized to use merge
#define apply0(f,args...) //do nothing
#define apply1(f,a,args...) f(a)
#define apply2(f,a,args...) f(a),merge(apply,argcount(args))(f,args)
#define apply3(f,a,args...) f(a),merge(apply,argcount(args))(f,args)

creating arbitrary lists from arguments
#define listx2(a) a,a
#define listx3(a) a,listx2(a)
#define listx4(a) a,listx3(a)

Determine is macro is defined to be 1 by substituting expansion with
a comma and passing it to primitive argument counter
https://www.reddit.com/r/frozenvoid/wiki/void2/isdefined_h
#define isdefined1(macro) isdefined1_(macro)
#define isdefinedtest1_1 ,
#define isdefined1_(value) isdefined1__(isdefinedtest1_##value)
#define isdefined1__(comma) isdefined1___(comma 1, 0)
#define isdefined1___(_, v, ...) v

This can used to compare symbolic digits (see https://www.reddit.com/r/frozenvoid/wiki/void2/istoken_h )
By applying boolean operator OR on successive comparisons of tokens.

Name: Anonymous 2017-07-01 17:02

Become extremely versatile(...) since it.
Since it what?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List