Simple CASE implementation.

This commit is contained in:
Brad Nelson
2022-09-23 22:42:05 -07:00
parent c49bb6571e
commit 58f5cb249d
5 changed files with 77 additions and 1 deletions

View File

@ -18,6 +18,7 @@ needs base_tests.fs
needs utils_tests.fs
needs vocabulary_tests.fs
needs locals_tests.fs
needs case_tests.fs
needs doloop_tests.fs
needs conditionals_tests.fs
needs float_tests.fs

19
common/case.fs Normal file
View File

@ -0,0 +1,19 @@
\ Copyright 2022 Bradley D. Nelson
\
\ Licensed under the Apache License, Version 2.0 (the "License");
\ you may not use this file except in compliance with the License.
\ You may obtain a copy of the License at
\
\ http://www.apache.org/licenses/LICENSE-2.0
\
\ Unless required by applicable law or agreed to in writing, software
\ distributed under the License is distributed on an "AS IS" BASIS,
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\ See the License for the specific language governing permissions and
\ limitations under the License.
: CASE ( n -- ) postpone ahead >r postpone begin
postpone ahead r> postpone then swap ; immediate
: ENDCASE drop postpone drop postpone then ; immediate
: OF ( n -- ) postpone over postpone = postpone if ; immediate
: ENDOF postpone drop over postpone again postpone then ; immediate

48
common/case_tests.fs Normal file
View File

@ -0,0 +1,48 @@
\ Copyright 2021 Bradley D. Nelson
\
\ Licensed under the Apache License, Version 2.0 (the "License");
\ you may not use this file except in compliance with the License.
\ You may obtain a copy of the License at
\
\ http://www.apache.org/licenses/LICENSE-2.0
\
\ Unless required by applicable law or agreed to in writing, software
\ distributed under the License is distributed on an "AS IS" BASIS,
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\ See the License for the specific language governing permissions and
\ limitations under the License.
( Test CASE Works )
e: test-case
: foo
case
1 of ." one" cr endof
2 of ." two" cr endof
." other: " dup . cr
endcase
;
1 foo
out: one
2 foo
out: two
3 foo
out: other: 3
;e
e: test-case-dup
: foo
case
1 of ." one" cr endof
2 of ." two" cr endof
1 of ." onemore" cr endof
." other: " dup . cr
endcase
;
1 foo
out: one
2 foo
out: two
3 foo
out: other: 3
;e

View File

@ -26,6 +26,13 @@ also internals
>link
repeat drop ;
e: check-case
out: ENDOF
out: OF
out: ENDCASE
out: CASE
;e
e: check-locals
out: +to
out: to
@ -507,6 +514,7 @@ e: check-asm
;e
e: check-phase2
check-case
check-locals
check-asm
check-utils