From 0ff89870fe064c3b8c524e862f07a5faaa85dd14 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Sun, 6 Oct 2019 15:10:30 +0200 Subject: [PATCH] fix: Make the behaviour consistent for `Show Char` Now it prints a debug represnetation just like all the other `Show` implementations. `string.from_char` and `string.append_char` are added as replacements for turning a char to a readable string. --- repl/src/repl.rs | 4 +++- vm/src/primitives.rs | 33 +++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/repl/src/repl.rs b/repl/src/repl.rs index bcce9cc86c..1f75ed4073 100644 --- a/repl/src/repl.rs +++ b/repl/src/repl.rs @@ -634,7 +634,9 @@ pub fn run( ) -> impl Future { let vm = ::gluon::VmBuilder::new().build(); vm.global_env().set_debug_level(debug_level); - vm.get_database_mut().use_standard_lib(use_std_lib); + vm.get_database_mut() + .use_standard_lib(use_std_lib) + .run_io(true); try_future!( compile_repl(&vm).map_err(|err| err.emit_string(&vm.get_database().code_map()).unwrap()), diff --git a/vm/src/primitives.rs b/vm/src/primitives.rs index 8dbebfd5ee..166d934818 100644 --- a/vm/src/primitives.rs +++ b/vm/src/primitives.rs @@ -163,9 +163,9 @@ pub mod array { mod string { use super::*; - use crate::{thread::ThreadInternal, value::ValueStr}; + use crate::value::ValueStr; - pub fn append(lhs: WithVM<&str>, rhs: &str) -> RuntimeResult { + pub(crate) fn append(lhs: WithVM<&str>, rhs: &str) -> RuntimeResult, Error> { #[derive(Trace)] #[gluon(gluon_vm)] struct StrAppend<'b> { @@ -198,12 +198,31 @@ mod string { let vm = lhs.vm; let lhs = lhs.value; - let mut context = vm.context(); - let value = match context.alloc(StrAppend { lhs: lhs, rhs: rhs }) { + let mut context = vm.current_context(); + let mut context = context.context(); + let value = match alloc!(context, StrAppend { lhs: lhs, rhs: rhs }) { Ok(x) => x, Err(err) => return RuntimeResult::Panic(err), }; - RuntimeResult::Return(Getable::from_value(vm, Variants::from(value))) + context.stack.push(Variants::from(value)); + RuntimeResult::Return(Pushed::default()) + } + + pub(crate) fn append_char( + lhs: WithVM<&str>, + rhs: char, + ) -> RuntimeResult, Error> { + append(lhs, rhs.encode_utf8(&mut [0; 4])) + } + + pub(crate) fn from_char(c: WithVM) -> RuntimeResult, Error> { + append_char( + WithVM { + vm: c.vm, + value: "", + }, + c.value, + ) } pub fn split_at(s: &str, index: usize) -> RuntimeResult<(&str, &str), String> { @@ -277,7 +296,7 @@ fn show_float(f: f64) -> String { } fn show_char(c: char) -> String { - format!("{}", c) + format!("{:?}", c) } fn show_byte(c: u8) -> String { @@ -522,6 +541,8 @@ pub fn load_string(vm: &Thread) -> Result { trim_end => primitive!(1, std::string::prim::trim_end), trim_end_matches => primitive!(2, std::string::prim::trim_end_matches::<&str>), append => primitive!(2, "std.string.prim.append", string::append), + append_char => primitive!(2, "std.string.prim.append_char", string::append_char), + from_char => primitive!(1, "std.string.prim.from_char", string::from_char), slice => primitive!(3, "std.string.prim.slice", string::slice), from_utf8 => primitive!( 1,