From cf740c4cd0670271ca7265e4c418890421059e1f Mon Sep 17 00:00:00 2001 From: Oldes Date: Sun, 7 Oct 2018 22:19:29 +0200 Subject: [PATCH] FEAT: making UNSET! as a conditionally "TRUTHY" related issues: https://github.com/rebol/rebol-issues/issues/564 https://github.com/rebol/rebol-issues/issues/850 It reverts changes discussed in above issues as I consider this behavior to be more useful and it is compatible with Rebol2 and Red. My main reason is to be able use PRINT inside ALL blocks without need to worry if these would affect the evaluation. Like in this case: ``` if all [ not headers any [ all [ d1: find conn/data crlfbin d2: find/tail d1 crlf2bin print "server using standard content separator of #{0D0A0D0A}" ] all [ d1: find conn/data #{0A} d2: find/tail d1 #{0A0A} print "server using malformed line separator of #{0A0A}" ] ] ][ ... ] ``` --- src/boot/natives.r | 8 ++++---- src/core/n-control.c | 2 +- src/mezz/base-defs.r | 2 +- src/tests/run-tests.r3 | 1 + src/tests/units/conditional-test.r3 | 32 +++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 src/tests/units/conditional-test.r3 diff --git a/src/boot/natives.r b/src/boot/natives.r index f25ac2950f..f64b621d69 100644 --- a/src/boot/natives.r +++ b/src/boot/natives.r @@ -132,7 +132,7 @@ do: native [ either: native [ {If TRUE condition return first arg, else second; evaluate blocks by default.} - condition + condition [any-type!] true-branch false-branch /only "Suppress evaluation of block args." @@ -188,7 +188,7 @@ halt: native [ if: native [ {If TRUE condition, return arg; evaluate blocks by default.} - condition + condition [any-type!] true-branch /only "Return block arg instead of evaluating it." ] @@ -307,7 +307,7 @@ try: native [ unless: native [ {If FALSE condition, return arg; evaluate blocks by default.} - condition + condition [any-type!] false-branch /only "Return block arg instead of evaluating it." ] @@ -818,7 +818,7 @@ log-e: native [ not: native [ {Returns the logic complement.} - value {(Only FALSE and NONE return TRUE)} + value [any-type!] {(Only FALSE and NONE return TRUE)} ] square-root: native [ diff --git a/src/core/n-control.c b/src/core/n-control.c index 0347b77f04..ebea515ab6 100644 --- a/src/core/n-control.c +++ b/src/core/n-control.c @@ -294,7 +294,7 @@ enum { while (index < SERIES_TAIL(block)) { index = Do_Next(block, index, 0); // stack volatile ds = DS_POP; // volatile stack reference - if (!IS_FALSE(ds) && !IS_UNSET(ds)) return R_TOS1; + if (!IS_FALSE(ds)) return R_TOS1; } return R_NONE; } diff --git a/src/mezz/base-defs.r b/src/mezz/base-defs.r index 776114405f..4889b12e02 100644 --- a/src/mezz/base-defs.r +++ b/src/mezz/base-defs.r @@ -136,7 +136,7 @@ scalar?: func [ true?: func [ "Returns true if an expression can be used as true." - val ; Note: No [any-type!] - we want unset! to fail. + val [any-type!] ;- we want unset! not to fail. ] [not not :val] quote: func [ diff --git a/src/tests/run-tests.r3 b/src/tests/run-tests.r3 index e45fbd61d7..8e987f043d 100644 --- a/src/tests/run-tests.r3 +++ b/src/tests/run-tests.r3 @@ -18,6 +18,7 @@ wrap load %units/dh-test.r3 wrap load %units/port-test.r3 wrap load %units/checksum-test.r3 wrap load %units/enum-test.r3 +wrap load %units/conditional-test.r3 wrap load %units/crash-test.r3 diff --git a/src/tests/units/conditional-test.r3 b/src/tests/units/conditional-test.r3 new file mode 100644 index 0000000000..79d5c3c2d3 --- /dev/null +++ b/src/tests/units/conditional-test.r3 @@ -0,0 +1,32 @@ +Rebol [ + Title: "Rebol conditional test script" + Author: "Olds" + File: %conditional-test.r3 + Tabs: 4 + Needs: [%../quick-test-module.r3] +] + +~~~start-file~~~ "Conditional" + +===start-group=== "Dealing with unset! value in conditions" + --test-- "any" + --assert unset? any [() 2] + --assert true? any [() 2] + --test-- "all" + --assert true? all [] + --assert true? all [()] + --assert 3 = all [() 1 2 3] + --assert unset? all [1 ()] + --assert 1 = if all [1 ()][1] + --assert 1 = either all [()][1][2] + --test-- "any and all" + if any [ + all [false x: 1 ()] + all [true x: 2 ()] + ][ x: 2 * x] + --assert x = 4 + + +===end-group=== + +~~~end-file~~~ \ No newline at end of file