From 45c9d37eb27d204a6202b33cb37e890af0171357 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Fri, 16 Oct 2026 10:04:27 +0000 Subject: feat(control-flow): implement ordered CASE OF matching Co-Authored-By: Claude Fable 5.1 --- doc/language/cpsc-language-overview.7.md | 5 ++-- doc/language/keyword/cpsc-keyword-case.7.md | 43 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 doc/language/keyword/cpsc-keyword-case.7.md (limited to 'doc') diff --git a/doc/language/cpsc-language-overview.7.md b/doc/language/cpsc-language-overview.7.md index 530eb45..b32b7bd 100644 --- a/doc/language/cpsc-language-overview.7.md +++ b/doc/language/cpsc-language-overview.7.md @@ -25,6 +25,7 @@ The compiler extensions TRY/CATCH/ENDTRY and THROW support anonymous exceptions - [cpsc-keyword-declare(7)](keyword/cpsc-keyword-declare.7.md): declare variables and arrays. - [cpsc-keyword-input(7)](keyword/cpsc-keyword-input.7.md): read a value from standard input. - [cpsc-keyword-output(7)](keyword/cpsc-keyword-output.7.md): print values to standard output. +- [cpsc-keyword-case(7)](keyword/cpsc-keyword-case.7.md): match values or ranges in order. - [cpsc-keyword-if(7)](keyword/cpsc-keyword-if.7.md): choose between branches. - [cpsc-keyword-try(7)](keyword/cpsc-keyword-try.7.md): catch or throw exceptions. - [cpsc-keyword-while(7)](keyword/cpsc-keyword-while.7.md): repeat while a condition is true. @@ -46,7 +47,7 @@ The compiler extensions TRY/CATCH/ENDTRY and THROW support anonymous exceptions - [cpsc-keyword-div(7)](keyword/cpsc-keyword-div.7.md): perform integer division. - [cpsc-keyword-mod(7)](keyword/cpsc-keyword-mod.7.md): find the integer division remainder. -Closing and auxiliary keywords are documented with their main constructs: THEN/ELSE/ENDIF with IF; CATCH/ENDTRY/THROW with TRY; DO/ENDWHILE with WHILE; UNTIL with REPEAT; TO/STEP/NEXT with FOR; RETURN/RETURNS/BYVAL/BYREF and function/procedure closing keywords with FUNCTION/PROCEDURE; READ/WRITE/APPEND with OPENFILE; ARRAY/OF and INTEGER/REAL/BOOLEAN/CHAR/STRING with DECLARE; ENDTYPE with TYPE; PUBLIC/PRIVATE/ENDCLASS with CLASS. TRUE/FALSE are BOOLEAN literals. +Closing and auxiliary keywords are documented with their main constructs: THEN/ELSE/ENDIF with IF; OF/OTHERWISE/ENDCASE with CASE; CATCH/ENDTRY/THROW with TRY; DO/ENDWHILE with WHILE; UNTIL with REPEAT; TO/STEP/NEXT with FOR; RETURN/RETURNS/BYVAL/BYREF and function/procedure closing keywords with FUNCTION/PROCEDURE; READ/WRITE/APPEND with OPENFILE; ARRAY/OF and INTEGER/REAL/BOOLEAN/CHAR/STRING with DECLARE; ENDTYPE with TYPE; PUBLIC/PRIVATE/ENDCLASS with CLASS. TRUE/FALSE are BOOLEAN literals. # BUILT-IN FUNCTIONS @@ -65,7 +66,7 @@ Closing and auxiliary keywords are documented with their main constructs: THEN/E # UNSUPPORTED FEATURES -CASE OF, CONSTANT, DATE, SET types and DEFINE, random-file operations (RANDOM/SEEK/GETRECORD/PUTRECORD), INT, RAND, INPUT into array elements or fields, string escapes, whole-array assignment or return, array fields, and class methods/constructors/destructors/inheritance (NEW/INHERITS/SUPER). +CONSTANT, DATE, SET types and DEFINE, random-file operations (RANDOM/SEEK/GETRECORD/PUTRECORD), INT, RAND, INPUT into array elements or fields, string escapes, whole-array assignment or return, array fields, and class methods/constructors/destructors/inheritance (NEW/INHERITS/SUPER). The individual pages note relevant differences from the Cambridge 2026 pseudocode guide. This manual describes the compiler's current support. diff --git a/doc/language/keyword/cpsc-keyword-case.7.md b/doc/language/keyword/cpsc-keyword-case.7.md new file mode 100644 index 0000000..d391d6c --- /dev/null +++ b/doc/language/keyword/cpsc-keyword-case.7.md @@ -0,0 +1,43 @@ +% CPSC-KEYWORD-CASE(7) + +# NAME + +cpsc-keyword-case - match values or ranges in source order + +# SYNOPSIS + +```text +CASE OF identifier + value : statements + lower TO upper : statements + OTHERWISE : statements +ENDCASE +``` + +# DESCRIPTION + +Read the selector once, then test labels in source order. Execute only the first matching branch. If nothing matches, execute the optional final OTHERWISE branch, or continue after ENDCASE. There is no fall-through. + +Each label header must start on a new line and end with a colon on that line. Its body may start after the colon or on following lines and may contain multiple statements or nested blocks. Indentation is for readability. + +Labels and range endpoints may be expressions, including function calls. A single value uses ordinary equality; a TO range includes both endpoints and uses ordinary ordering. Evaluate the lower endpoint first and evaluate the upper endpoint only if the lower comparison succeeds. Do not evaluate later labels after a match. Unlike CONSTANT initializers, CASE labels are not restricted to literals. + +STRING comparisons use contents. Enumerations and explicit pointer types follow their ordinary comparison rules; pointers support equality only. Records and whole arrays cannot be compared. The compiler emits conditional branches and disables jump tables for functions containing CASE. + +# EXAMPLE + +```text +DECLARE Move : CHAR +INPUT Move +CASE OF Move + 'W' : OUTPUT "up" + 'A' : + OUTPUT "left" + OUTPUT "turn" + OTHERWISE : OUTPUT "other" +ENDCASE +``` + +# SEE ALSO + +[cpsc-language-overview(7)](../cpsc-language-overview.7.md), [cpsc-keyword-if(7)](cpsc-keyword-if.7.md). -- cgit v1.3.1-4-g156e