Simple CASE implementation.
This commit is contained in:
2
Makefile
2
Makefile
@ -211,7 +211,7 @@ COMMON_PHASE1e = common/comments.fs \
|
|||||||
common/floats.fs \
|
common/floats.fs \
|
||||||
common/structures.fs
|
common/structures.fs
|
||||||
|
|
||||||
COMMON_PHASE2 = common/utils.fs common/code.fs common/locals.fs
|
COMMON_PHASE2 = common/utils.fs common/code.fs common/locals.fs common/case.fs
|
||||||
|
|
||||||
COMMON_FILETOOLS = common/tasks.fs common/streams.fs \
|
COMMON_FILETOOLS = common/tasks.fs common/streams.fs \
|
||||||
common/filetools.fs common/including.fs \
|
common/filetools.fs common/including.fs \
|
||||||
|
|||||||
@ -18,6 +18,7 @@ needs base_tests.fs
|
|||||||
needs utils_tests.fs
|
needs utils_tests.fs
|
||||||
needs vocabulary_tests.fs
|
needs vocabulary_tests.fs
|
||||||
needs locals_tests.fs
|
needs locals_tests.fs
|
||||||
|
needs case_tests.fs
|
||||||
needs doloop_tests.fs
|
needs doloop_tests.fs
|
||||||
needs conditionals_tests.fs
|
needs conditionals_tests.fs
|
||||||
needs float_tests.fs
|
needs float_tests.fs
|
||||||
|
|||||||
19
common/case.fs
Normal file
19
common/case.fs
Normal 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
48
common/case_tests.fs
Normal 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
|
||||||
@ -26,6 +26,13 @@ also internals
|
|||||||
>link
|
>link
|
||||||
repeat drop ;
|
repeat drop ;
|
||||||
|
|
||||||
|
e: check-case
|
||||||
|
out: ENDOF
|
||||||
|
out: OF
|
||||||
|
out: ENDCASE
|
||||||
|
out: CASE
|
||||||
|
;e
|
||||||
|
|
||||||
e: check-locals
|
e: check-locals
|
||||||
out: +to
|
out: +to
|
||||||
out: to
|
out: to
|
||||||
@ -507,6 +514,7 @@ e: check-asm
|
|||||||
;e
|
;e
|
||||||
|
|
||||||
e: check-phase2
|
e: check-phase2
|
||||||
|
check-case
|
||||||
check-locals
|
check-locals
|
||||||
check-asm
|
check-asm
|
||||||
check-utils
|
check-utils
|
||||||
|
|||||||
Reference in New Issue
Block a user