code wiki / (root) / nx_macro_v4_test.nx

nx_macro_v4_test.nx source

↩ module page · 66 lines · 2196 B

1// nx_macro_v4_test.nx -- exercise auto-injected target defines + @undef. 2 3import "nx_kernel_v2.nx" 4 5// Built-in: TARGET_RV64 should be defined when compiling --target asm. 6// Built-in: NXC_VERSION always defined. 7 8@ifdef TARGET_RV64 9func active_target_value() -> nx_int { return 64 } 10@endif 11 12@ifndef TARGET_RV64 13func active_target_value() -> nx_int { return 0 } 14@endif 15 16@ifdef NXC_VERSION 17func compiler_present() -> nx_int { return 1 } 18@endif 19 20// User-defined feature flag, then retracted, then conditionally-not-present. 21@macro EXPERIMENT 1 22 23// First @ifdef sees it as defined. 24@ifdef EXPERIMENT 25func experiment_v1() -> nx_int { return 100 } 26@endif 27 28// Now retract it. 29@undef EXPERIMENT 30 31// Next @ifndef should see it as NOT defined. 32@ifndef EXPERIMENT 33func experiment_v2() -> nx_int { return 200 } 34@endif 35 36// And a second @ifdef must DROP its body since EXPERIMENT no longer defined. 37@ifdef EXPERIMENT 38func IF_THIS_COMPILED_UNDEF_FAILED() -> nx_int { return BROKEN_IDENT_PROOF } 39@endif 40 41func main() -> nx_exit { 42 println("=== nx_macro_v4 -- auto-defines + @undef smoke ===" as *u8) 43 44 let t: nx_int = active_target_value() 45 if t != 64 { println("T1 target FAIL" as *u8); return 1 } 46 println("T1 target PASS TARGET_RV64 auto-injected for --target asm" as *u8) 47 48 let v: nx_int = compiler_present() 49 if v != 1 { println("T2 nxc_version FAIL" as *u8); return 2 } 50 println("T2 nxc_version PASS NXC_VERSION auto-injected" as *u8) 51 52 let e1: nx_int = experiment_v1() 53 if e1 != 100 { println("T3 experiment_v1 FAIL" as *u8); return 3 } 54 println("T3 experiment_v1 PASS @ifdef EXPERIMENT before @undef kept body" as *u8) 55 56 let e2: nx_int = experiment_v2() 57 if e2 != 200 { println("T4 experiment_v2 FAIL" as *u8); return 4 } 58 println("T4 experiment_v2 PASS @ifndef EXPERIMENT after @undef kept body" as *u8) 59 60 println("" as *u8) 61 println("=== Drop-block proof ===" as *u8) 62 println("If @undef had not retracted EXPERIMENT, the second @ifdef block" as *u8) 63 println("would have compiled BROKEN_IDENT_PROOF -- a non-existent identifier." as *u8) 64 println("Successful compilation proves @undef worked." as *u8) 65 return 0 66}