Change EvaluatedValue::String to Rc<str>

master
Nick Krichevsky 2024-05-21 22:21:28 -04:00
parent 0b4d4d6d21
commit 32f6628328
2 changed files with 7 additions and 7 deletions

View File

@ -1,5 +1,6 @@
use std::{ use std::{
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
rc::Rc,
string::ParseError, string::ParseError,
}; };
@ -24,7 +25,7 @@ impl From<&LiteralValue> for EvaluatedValue {
LiteralValue::False => EvaluatedValue::Boolean(false), LiteralValue::False => EvaluatedValue::Boolean(false),
LiteralValue::Nil => EvaluatedValue::Nil, LiteralValue::Nil => EvaluatedValue::Nil,
LiteralValue::Number(number) => EvaluatedValue::Number(*number), LiteralValue::Number(number) => EvaluatedValue::Number(*number),
LiteralValue::String(string) => EvaluatedValue::String(string.clone()), LiteralValue::String(string) => EvaluatedValue::String(string.clone().into()),
} }
} }
} }
@ -178,7 +179,6 @@ impl ExprVisitor<Result<EvaluatedValue, ScriptError>> for InterpreterRunner<'_>
location: String::new(), location: String::new(),
line: name.line(), line: name.line(),
}) })
// TODO: AAAAA this cloning sucks
.cloned() .cloned()
} }
} }
@ -241,7 +241,8 @@ fn evaluate_addition(
} }
(EvaluatedValue::String(left_value), EvaluatedValue::String(right_value)) => { (EvaluatedValue::String(left_value), EvaluatedValue::String(right_value)) => {
Ok(EvaluatedValue::String(left_value + right_value.as_ref())) let concatted = left_value.to_string() + right_value.as_ref();
Ok(EvaluatedValue::String(concatted.into()))
} }
// TODO: we could improve this error (and others) to include the types // TODO: we could improve this error (and others) to include the types

View File

@ -1,10 +1,9 @@
use thiserror::Error; use std::rc::Rc;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum EvaluatedValue { pub enum EvaluatedValue {
Number(f64), Number(f64),
String(String), String(Rc<str>),
Boolean(bool), Boolean(bool),
Nil, Nil,
} }
@ -35,7 +34,7 @@ impl_value_try_from!(
); );
impl_value_try_from!( impl_value_try_from!(
String, Rc<str>,
EvaluatedValue::String(s) => s, EvaluatedValue::String(s) => s,
"value is not of type string" "value is not of type string"
); );