ENDCASE

Interpretation:

Interpretation semantics for this word are undefined.

Compilation:

( C: case-sys -- )

Mark the end of the CASE...OF...ENDOF...ENDCASE structure. Use case-sys to resolve the entire structure. Append the run-time semantics given below to the current definition.

Run-time:

( x -- )

Discard the case selector x and continue execution.

See:

Rationale:

Typical use:
: X ...
   CASE
   test1 OF ... ENDOF
   testn OF ... ENDOF
   ... ( default )
   ENDCASE ...
;

Testing:

ContributeContributions

enochavatar of enoch Proposal: end-caseComment2016-01-13 20:48:24

There are numerous old forum arguments against the endcase consuming the top stack value, typically the case selector. Isn't it time to add an end-case variant to the standard which would leave the top stack value intact. Based on my personal experience with Amforth-Shadow both forms are useful.

AntonErtlavatar of AntonErtl

I think the proposed name is too close to ENDCASE, so that's probably bug-prone. I guess we have to live with 0 ENDCASE for that function.

enochavatar of enoch

You meant, no doubt, dup endcase*. No point to rehash the old arguments for and against *endcase current behavior. What is needed IMHO is for the committee to invite user code statistics. In my applications endcase and end-case find equal use.

AntonErtlavatar of AntonErtl

I actually meant 0 ENDCASE. The stack might be empty before, so DUP could cause a stack underflow.

Thanks for the statistics.

JimPetersonavatar of JimPeterson

: 0ENDCASE [ 0 ] LITERAL POSTPONE ENDCASE ; IMMEDIATE ?

It would normalize/reserve the idea of a 0ENDCASE word, and systems could implement it in a more efficient way than pushing and immediately dropping a 0.

JimPetersonavatar of JimPeterson

Oops:

: 0ENDCASE 0 POSTPONE LITERAL POSTPONE ENDCASE ; IMMEDIATE

AntonErtlavatar of AntonErtl

Actually, in a number of modern Forth implementations, 0 DROP generates no code, and

: foo case 0 endcase ;

generates code for a word that just returns. I just tested this with development Gforth, iForth, SwiftForth, and VFX, and only SwiftForth generates more code. Doing this optimization (and similar ones) is relatively simple and explained in <2019Aug5.121829@mips.complang.tuwien.ac.at>.

JimPetersonavatar of JimPeterson

I'm simultaneously intrigued and disappointed. My system is far too resource constrained (like, 1980's-style computing constrained) to properly implement those optimizations, and I was tickled that Forth actually could do code like [ A 5 CELLS + ] LITERAL, leveraging the full power of Forth during the process of defining a word, almost like template meta-programming in C++ only less obtuse. I'll have to stick with 0ENDCASE for my system, though I might look into examining the previous instruction when encoding a DROP.

ruvavatar of ruv

I'll have to stick with 0ENDCASE for my system

Perhaps, ESAC is a better name. It's shorter, it's used in some other languages as pair to CASE, and it was already used exactly in this meaning in Forth publications.

Wil Baden, Forth Dimensions XVII.5, 1996 (p.15)

Instead of CASE ... OF ... ENDOF ... 0 ENDCASE Stretching Forth prefers CASE ... OF ... ELSE ... ESAC.

Wil Baden, Forth Dimensions, XVII.3, 1995 (p. 40)

: ESAC POSTONE FALSE POSTPONE ENDCASE ; IMMEDIATE
Reply New Version