aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pseudocode-lib/string.cps25
-rw-r--r--tests/i/contextual-conversions/case.json1
-rw-r--r--tests/i/contextual-conversions/expected-stderr.txt4
-rw-r--r--tests/i/contextual-conversions/expected.txt12
-rw-r--r--tests/i/contextual-conversions/notes.md7
-rw-r--r--tests/i/contextual-conversions/program.cps36
6 files changed, 81 insertions, 4 deletions
diff --git a/pseudocode-lib/string.cps b/pseudocode-lib/string.cps
index 135f5d7..96de023 100644
--- a/pseudocode-lib/string.cps
+++ b/pseudocode-lib/string.cps
@@ -68,20 +68,37 @@ OVERLOAD FUNCTION OPERATOR&(A : STRING, B : STRING) RETURNS STRING
CALL _string_impl_concat(Result._HANDLE, A._HANDLE, B._HANDLE)
RETURN Result
ENDFUNCTION
-OVERLOAD FUNCTION NUM_TO_STR(Number : INTEGER) RETURNS STRING
+OVERLOAD FUNCTION NUM_TO_STR(Number : INTEGER) RETURNS DEFAULT STRING
DECLARE Result : STRING
CALL _string_impl_from_int(Result._HANDLE, (_STRING_C_INTEGER)Number)
RETURN Result
ENDFUNCTION
-OVERLOAD FUNCTION NUM_TO_STR(Number : REAL) RETURNS STRING
+OVERLOAD FUNCTION NUM_TO_STR(Number : REAL) RETURNS DEFAULT STRING
DECLARE Result : STRING
CALL _string_impl_from_real(Result._HANDLE, (C_DOUBLE)Number)
RETURN Result
ENDFUNCTION
-OVERLOAD FUNCTION STR_TO_NUM(S : STRING) RETURNS REAL
+OVERLOAD FUNCTION NUM_TO_STR(Number : INTEGER) RETURNS CHAR
+ DECLARE Text : STRING
+ Text <- NUM_TO_STR(Number)
+ RETURN Text
+ENDFUNCTION
+OVERLOAD FUNCTION NUM_TO_STR(Number : REAL) RETURNS CHAR
+ DECLARE Text : STRING
+ Text <- NUM_TO_STR(Number)
+ RETURN Text
+ENDFUNCTION
+OVERLOAD FUNCTION STR_TO_NUM(S : STRING) RETURNS DEFAULT REAL
RETURN (REAL)_string_impl_to_real(S._HANDLE)
ENDFUNCTION
-OVERLOAD FUNCTION STR_TO_NUM(C : CHAR) RETURNS REAL
+OVERLOAD FUNCTION STR_TO_NUM(S : STRING) RETURNS INTEGER
+ RETURN INT((REAL)_string_impl_to_real(S._HANDLE))
+ENDFUNCTION
+OVERLOAD FUNCTION STR_TO_NUM(C : CHAR) RETURNS DEFAULT REAL
+ DECLARE S : STRING(1, (C_CHAR)C)
+ RETURN STR_TO_NUM(S)
+ENDFUNCTION
+OVERLOAD FUNCTION STR_TO_NUM(C : CHAR) RETURNS INTEGER
DECLARE S : STRING(1, (C_CHAR)C)
RETURN STR_TO_NUM(S)
ENDFUNCTION
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