From 09250861d9b009db8c3da949c5de7f30b08920f3 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Sat, 15 May 2021 00:02:06 +0200 Subject: [PATCH] print ascii code of a character (ktoi) --- clib/printf.cpp | 18 +++++++++++++----- ml.cpp | 6 ++++-- stdlib/stdlib.lsp | 1 + tests/test.lsp | 2 ++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/clib/printf.cpp b/clib/printf.cpp index 3c56067..e626b6a 100644 --- a/clib/printf.cpp +++ b/clib/printf.cpp @@ -9,26 +9,34 @@ std::string mini_sprintf_format(bool left_align, bool sign, bool space_on_left, bool padding_by_zero, int width, int precision, int length, char specifier, const MlValue &value) { std::string s; + std::ostringstream stream_str; // PERF simpler solution..without string stream bool is_positive = false; if (specifier == 's') { return value.as_string(); } if (specifier == 'c') { - std::ostringstream stream_str; stream_str << (char) value.as_int(); return stream_str.str(); } if (specifier == 'i' || specifier == 'd') { - int ival = value.as_int(); - is_positive = ival >= 0; + if (value.is_number()) { + int ival = value.as_int(); + is_positive = ival >= 0; - s = std::to_string(ival); + s = std::to_string(ival); + } else if (value.is_string()) { // print ascii code of character + if (value.as_string().size() > 0) { + stream_str << (int)value.as_string()[0]; + return stream_str.str(); + } else { + // TODO handle empty string - error? + } + } } else if (specifier == 'f' || specifier == 'e') { double dval = value.as_float(); is_positive = dval >= 0; - std::ostringstream stream_str; if (specifier == 'f') stream_str << std::fixed; else diff --git a/ml.cpp b/ml.cpp index d5d83ba..77792a2 100644 --- a/ml.cpp +++ b/ml.cpp @@ -248,7 +248,8 @@ MlValue MlValue::cast_to_int() const { return *this; case FLOAT: return MlValue(long(stack_data.f)); - // Only ints and floats can be cast to an int + case STRING: + return MlValue(std::stol(str)); default: throw MlError(*this, MlEnvironment(), BAD_CAST); } @@ -261,7 +262,8 @@ MlValue MlValue::cast_to_float() const { return *this; case INT: return MlValue(float(stack_data.i)); - // Only ints and floats can be cast to a float + case STRING: + return MlValue(std::stod(str)); default: throw MlError(*this, MlEnvironment(), BAD_CAST); } diff --git a/stdlib/stdlib.lsp b/stdlib/stdlib.lsp index 537a8b3..fe95fd2 100644 --- a/stdlib/stdlib.lsp +++ b/stdlib/stdlib.lsp @@ -40,6 +40,7 @@ )) (defun itok (ascii) (sprintf "%c" (list ascii))) +(defun ktoi (char) (int (sprintf "%d" (list char)))) ; pause for interval of seconds (defun sleep (time) diff --git a/tests/test.lsp b/tests/test.lsp index 092581b..d01012c 100644 --- a/tests/test.lsp +++ b/tests/test.lsp @@ -73,6 +73,8 @@ (ut::define-test "result of (itok 65)" '(ut::assert-equal "A" (itok 65))) (ut::define-test "result of (itok 48)" '(ut::assert-equal "0" (itok 48))) +(ut::define-test "result of (ktoi \"A\")" '(ut::assert-equal 65 (ktoi "A"))) +(ut::define-test "result of (ktoi \"0\")" '(ut::assert-equal 48 (ktoi "0"))) (ut::define-test "result of (write-file \"/tmp/file\" \"write-file test\")" '(ut::assert-equal 1 (write-file "/tmp/file" "write-file test\n"))) (ut::define-test "result of (is-file? \"/tmp/file\")" '(ut::assert-true (is-file? "/tmp/file")))