From 96fd8bab8630f2abcc74cfc69fdbccc530579b9c Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 28 Oct 2026 13:38:00 +0000 Subject: feat(library): add contextual conversion return overloads Co-Authored-By: Claude Opus 5.5 --- tests/i/contextual-conversions/case.json | 1 + tests/i/contextual-conversions/expected-stderr.txt | 4 +++ tests/i/contextual-conversions/expected.txt | 12 ++++++++ tests/i/contextual-conversions/notes.md | 7 +++++ tests/i/contextual-conversions/program.cps | 36 ++++++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 tests/i/contextual-conversions/case.json create mode 100644 tests/i/contextual-conversions/expected-stderr.txt create mode 100644 tests/i/contextual-conversions/expected.txt create mode 100644 tests/i/contextual-conversions/notes.md create mode 100644 tests/i/contextual-conversions/program.cps (limited to 'tests') diff --git a/tests/i/contextual-conversions/case.json b/tests/i/contextual-conversions/case.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/i/contextual-conversions/case.json @@ -0,0 +1 @@ +{} diff --git a/tests/i/contextual-conversions/expected-stderr.txt b/tests/i/contextual-conversions/expected-stderr.txt new file mode 100644 index 0000000..d5d8935 --- /dev/null +++ b/tests/i/contextual-conversions/expected-stderr.txt @@ -0,0 +1,4 @@ +[Fatal] line 18: STRING of length 2 cannot be converted to CHAR +[Fatal] line 23: STRING of length 3 cannot be converted to CHAR +[Fatal] line 28: STR_TO_NUM: 'bad' is not a number +[Fatal] line 33: INT argument out of INTEGER range diff --git a/tests/i/contextual-conversions/expected.txt b/tests/i/contextual-conversions/expected.txt new file mode 100644 index 0000000..7513eee --- /dev/null +++ b/tests/i/contextual-conversions/expected.txt @@ -0,0 +1,12 @@ +23.45 +7.0 +23 +-23 +7 +7:7.0 +7 +12 +length caught +real length caught +invalid caught +range caught diff --git a/tests/i/contextual-conversions/notes.md b/tests/i/contextual-conversions/notes.md new file mode 100644 index 0000000..efc6447 --- /dev/null +++ b/tests/i/contextual-conversions/notes.md @@ -0,0 +1,7 @@ +# Contextual conversions + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: the function table in reference/9618_w25_in_21.pdf permits REAL/INTEGER results from STR_TO_NUM and STRING/CHAR results from NUM_TO_STR. DEFAULT selection and truncation toward zero for INTEGER conversion are project rules. + +Trace: context-free numeric parsing returns REAL 23.45 and 7.0; INTEGER assignments truncate 23.45 and -23.45 to 23 and -23, and parse character '7' as 7. Default text formatting produces 7 and 7.0. CHAR assignment accepts the one-character integer representation. STRING assignment retains 12. The two longer CHAR results, invalid numeric text and INTEGER overflow throw at their caller lines and enter their respective catches. diff --git a/tests/i/contextual-conversions/program.cps b/tests/i/contextual-conversions/program.cps new file mode 100644 index 0000000..a90a815 --- /dev/null +++ b/tests/i/contextual-conversions/program.cps @@ -0,0 +1,36 @@ +DECLARE N : INTEGER +DECLARE Ch : CHAR +DECLARE Text : STRING +OUTPUT STR_TO_NUM("23.45") +OUTPUT STR_TO_NUM('7') +N <- STR_TO_NUM("23.45") +OUTPUT N +N <- STR_TO_NUM("-23.45") +OUTPUT N +N <- STR_TO_NUM('7') +OUTPUT N +OUTPUT NUM_TO_STR(7), ":", NUM_TO_STR(7.0) +Ch <- NUM_TO_STR(7) +OUTPUT Ch +Text <- NUM_TO_STR(12) +OUTPUT Text +TRY + Ch <- NUM_TO_STR(12) +CATCH + OUTPUT "length caught" +ENDTRY +TRY + Ch <- NUM_TO_STR(7.0) +CATCH + OUTPUT "real length caught" +ENDTRY +TRY + N <- STR_TO_NUM("bad") +CATCH + OUTPUT "invalid caught" +ENDTRY +TRY + N <- STR_TO_NUM("1e30") +CATCH + OUTPUT "range caught" +ENDTRY -- cgit v1.3.1-4-g156e