From: Thomas Walker Lynch Date: Thu, 27 Mar 2025 16:17:43 +0000 (+0000) Subject: looks like cpp_ext_0.c is complete, will make soem tweeks X-Git-Url: https://git.reasoningtechnology.com/style/static/git-logo.png?a=commitdiff_plain;h=6bee7951187afc52e841667c58a8fd9f7672838d;p=N looks like cpp_ext_0.c is complete, will make soem tweeks --- diff --git a/developer/example/cpp_ext_0.c b/developer/example/cpp_ext_0.c index 0ab2614..f9fe34c 100644 --- a/developer/example/cpp_ext_0.c +++ b/developer/example/cpp_ext_0.c @@ -35,7 +35,7 @@ DEBUG #include #define DEBUG_CPP -#define STR(x) #x +#define STR(...) #__VA_ARGS__ // print the macro and the evaluation of the macro #define SHOW(expr) printf("%s -> %s\n", #expr, STR(expr)) @@ -48,7 +48,10 @@ Constants #define SEMICOLON ; #define ZERO 0 -#define ONE 1 +#define ONE 1 + +#define FALSE 0 +#define TRUE 1 //--------- @@ -84,7 +87,7 @@ Primitive Concatenation /*=========================================================================== -Logic +Existence ===========================================================================*/ //---------------------------------------- @@ -160,10 +163,6 @@ Logic Connectors #define NOT_EQ(x_item ,y_item) \ NOT_MATCH_RWR( CAT4(_RWR_EQ__ ,x_item ,__oo__ ,y_item) ) -#if 0 - - - /*=========================================================================== IF-ELSE construct. Usage: IF_ELSE(condition)()() @@ -173,47 +172,44 @@ Logic Connectors The seemingly extra layer prevents BOOL_(condition) from being pasted with a ## which, if done, would prevent it from being evaluated. Recall, the first step in evaluation is a literal copy in of the arguments. ===symbol ========================================================================*/ - #define _IF_ELSE(condition) CAT2(_IF_ ,BOOL(condition)) + #define IF_ELSE(condition) CAT2(_IF_ ,BOOL(condition)) #define _IF_1(...) __VA_ARGS__ _IF_1_ELSE #define _IF_0(...) _IF_0_ELSE #define _IF_1_ELSE(...) #define _IF_0_ELSE(...) __VA_ARGS__ - /*=========================================================================== Access - see below the recursion section for Nth .. when it is written ;-) + see ext_1 with recursion for `Nth` ===========================================================================*/ // _FIRST defined in the logic section #define FIRST(pad ,...)\ - If_ELSE \ + IF_ELSE \ ( NOT_EXISTS(__VA_ARGS__) ) \ (pad) \ ( _FIRST(__VA_ARGS__) ) #define _REST(a ,...) __VA_ARGS__ #define REST(...)\ - If_ELSE \ + IF_ELSE \ ( NOT_EXISTS(__VA_ARGS__) ) \ () \ ( _REST(__VA_ARGS__) ) // _SECOND defined in the logic section #define SECOND(pad ,...) \ - If_ELSE \ + IF_ELSE \ ( NOT_EXISTS(__VA_ARGS__) ) \ (pad) \ ( _SECOND(__VA_ARGS__ ,pad) ) #define _THIRD(a ,b ,c ,...) c #define THIRD(pad ,...) \ - If_ELSE \ + IF_ELSE \ ( NOT_EXISTS(__VA_ARGS__) ) \ (pad) \ ( _THIRD(__VA_ARGS__ ,pad, pad) ) -#endif - #endif diff --git a/developer/example/try_4_if.c b/developer/example/try_4_if.c index 6df17ea..cdb60e6 100644 --- a/developer/example/try_4_if.c +++ b/developer/example/try_4_if.c @@ -7,8 +7,8 @@ int main(void){ //--------------------------------------------------------------------------- SHOW(IF_ELSE(1)(yes)(no)); -#if 0 SHOW(IF_ELSE(0)(yes)(no)); // → no + printf("\n"); //--------------------------------------------------------------------------- // Symbolic identifiers @@ -16,6 +16,7 @@ int main(void){ SHOW(IF_ELSE(TRUE)(ok)(fail)); // → ok SHOW(IF_ELSE(FALSE)(ok)(fail)); // → fail SHOW(IF_ELSE(foo)(alpha)(omega)); // → omega (foo is undefined) + printf("\n"); //--------------------------------------------------------------------------- // Logic expressions @@ -23,23 +24,27 @@ int main(void){ SHOW(IF_ELSE(AND(1 ,1))(pass)(fail)); // → pass SHOW(IF_ELSE(OR(0 ,0))(yes)(no)); // → no SHOW(IF_ELSE(NOT(0))(on)(off)); // → on + printf("\n"); //--------------------------------------------------------------------------- // Code-like output //--------------------------------------------------------------------------- SHOW(IF_ELSE(1)(int x = 1;)(int x = 2;)); // → int x = 1; SHOW(IF_ELSE(0)(int x = 1;)(int x = 2;)); // → int x = 2; + printf("\n"); //--------------------------------------------------------------------------- // Comma usage in true/false branches //--------------------------------------------------------------------------- SHOW(IF_ELSE(1)(a ,b ,c)(x ,y ,z)); // → a ,b ,c SHOW(IF_ELSE(0)(a ,b ,c)(x ,y ,z)); // → x ,y ,z + printf("\n"); //--------------------------------------------------------------------------- // Empty condition //--------------------------------------------------------------------------- SHOW(IF_ELSE()(true)(false)); // → false (BOOL() = 0) + printf("\n"); //--------------------------------------------------------------------------- // Nested IF_ELSE @@ -51,7 +56,7 @@ int main(void){ outer_false ) ); // → inner_false + printf("\n"); -#endif return 0; } diff --git a/developer/example/try_5_access.c b/developer/example/try_5_access.c new file mode 100644 index 0000000..e9db0ae --- /dev/null +++ b/developer/example/try_5_access.c @@ -0,0 +1,46 @@ +#include "cpp_ext_0.c" + +int main(void){ + + //--------------------------------------------------------------------------- + // FIRST + //--------------------------------------------------------------------------- + + SHOW(FIRST(foo)); // → foo (only pad, no args) + SHOW(FIRST(foo ,a)); // → a + SHOW(FIRST(foo ,a ,b ,c)); // → a + printf("\n"); + + //--------------------------------------------------------------------------- + // REST + //--------------------------------------------------------------------------- + + SHOW(REST()); // → (empty) + SHOW(REST(a)); // → (empty) + SHOW(REST(a ,b)); // → b + SHOW(REST(a ,b ,c ,d)); // → b ,c ,d + printf("\n"); + + //--------------------------------------------------------------------------- + // SECOND + //--------------------------------------------------------------------------- + + SHOW(SECOND(X)); // → X (only pad) + SHOW(SECOND(X ,a)); // → X (only one arg, fallback to pad) + SHOW(SECOND(X ,a ,b)); // → b + SHOW(SECOND(X ,a ,b ,c)); // → b + printf("\n"); + + //--------------------------------------------------------------------------- + // THIRD + //--------------------------------------------------------------------------- + + SHOW(THIRD(X)); // → X (pad returned) + SHOW(THIRD(X ,a)); // → X + SHOW(THIRD(X ,a ,b)); // → X + SHOW(THIRD(X ,a ,b ,c)); // → c + SHOW(THIRD(X ,a ,b ,c ,d)); // → c + printf("\n"); + + return 0; +}