Skip to content

Commit 23673df

Browse files
BrianHawleyzsx
authored andcommitted
Get rid of if/else
Per request: http://issue.cc/r3/2077 Conflicts: src/boot/natives.r src/core/n-control.c
1 parent e9dcea1 commit 23673df

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

src/boot/errors.r

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Note: [
3131
no-load: [{cannot load: } :arg1]
3232
exited: [{exit occurred}]
3333
deprecated: {deprecated function not allowed}
34-
else-gone: {ELSE is obsolete - use the EITHER function}
3534
]
3635

3736
Syntax: [

src/boot/natives.r

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,10 @@ halt: native [
186186
]
187187

188188
if: native [
189-
{If condition is TRUE, evaluates the block.}
189+
{If TRUE condition, return arg; evaluate blocks by default.}
190190
condition
191-
then-block [block!]
192-
/else "If not true, evaluate this block"
193-
else-block [block!]
191+
true-branch
192+
/only "Return block arg instead of evaluating it."
194193
]
195194

196195
loop: native [
@@ -306,9 +305,10 @@ try: native [
306305
]
307306

308307
unless: native [
309-
{Evaluates the block if condition is not TRUE.}
308+
{If FALSE condition, return arg; evaluate blocks by default.}
310309
condition
311-
block [block!]
310+
false-branch
311+
/only "Return block arg instead of evaluating it."
312312
]
313313

314314
until: native [

src/core/n-control.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -597,17 +597,6 @@ enum {
597597
}
598598

599599

600-
/***********************************************************************
601-
**
602-
*/ REBNATIVE(else)
603-
/*
604-
***********************************************************************/
605-
{
606-
Trap0(RE_ELSE_GONE);
607-
DEAD_END;
608-
}
609-
610-
611600
/***********************************************************************
612601
**
613602
*/ REBNATIVE(exit)
@@ -625,16 +614,13 @@ enum {
625614
/*
626615
***********************************************************************/
627616
{
628-
REBVAL *cond = D_ARG(1);
629-
REBVAL *body = D_ARG(2);
630-
631-
if (!D_REF(3)) { // no /else
632-
if (IS_FALSE(cond)) return R_NONE;
633-
} else
634-
if (IS_FALSE(cond)) body = D_ARG(4);
635-
636-
DO_BLK(body);
637-
return R_TOS1;
617+
if (IS_FALSE(D_ARG(1))) return R_NONE;
618+
if (IS_BLOCK(D_ARG(2)) && !D_REF(3) /* not using /ONLY */) {
619+
DO_BLK(D_ARG(2));
620+
return R_TOS1;
621+
} else {
622+
return R_ARG2;
623+
}
638624
}
639625

640626

0 commit comments

Comments
 (0)