From 2fcca58c795077e7f161bdb4aeeab7803e28477d Mon Sep 17 00:00:00 2001 From: poppingmoon <63451158+poppingmoon@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:05:36 +0900 Subject: [PATCH] wrap V with Box --- .github/workflows/ci.yml | 2 +- examples/console.rs | 7 +++--- src/interpreter.rs | 36 +++++++++++++++--------------- src/interpreter/lib/std.rs | 8 +++---- src/interpreter/primitive_props.rs | 8 +++---- src/interpreter/util.rs | 20 ++++++++--------- src/interpreter/value.rs | 15 ++++++++----- tests/test.rs | 2 +- 8 files changed, 51 insertions(+), 47 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 832f829..757ebb6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - run: cargo test --release + - run: cargo test fmt: runs-on: ubuntu-latest steps: diff --git a/examples/console.rs b/examples/console.rs index 918b0ae..d658341 100644 --- a/examples/console.rs +++ b/examples/console.rs @@ -54,9 +54,10 @@ async fn main() -> Result<()> { rl.add_history_entry(&input)?; input.clear(); let result = aiscript.exec(script).await.unwrap(); - match result { - Some(Value { value: V::Null, .. }) | None => (), - Some(Value { value, .. }) => println!("{}", value.repr_value()), + if let Some(Value { value, .. }) = result { + if *value != V::Null { + println!("{}", value.repr_value()); + } } } Err(err) => { diff --git a/src/interpreter.rs b/src/interpreter.rs index 125908c..99e5acd 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -370,7 +370,7 @@ impl Interpreter { None, ); let v = self.eval(*for_.clone(), &scope).await?; - match v.value { + match *v.value { V::Break => { break; } @@ -396,7 +396,7 @@ impl Interpreter { let mut i = 0.0; while i < times { let v = self.eval(*for_.clone(), scope).await?; - match v.value { + match *v.value { V::Break => { break; } @@ -422,7 +422,7 @@ impl Interpreter { None, ); let v = self.eval(*for_.clone(), &scope).await?; - match v.value { + match *v.value { V::Break => { break; } @@ -443,7 +443,7 @@ impl Interpreter { &scope.create_child_scope(HashMap::new(), None), ) .await?; - match v.value { + match *v.value { V::Break => { break Value::null(); } @@ -569,10 +569,10 @@ impl Interpreter { value: left_value, attr, } = self.eval(*left, scope).await?; - let left_value = bool::try_from(left_value)?; + let left_value = bool::try_from(*left_value)?; if !left_value { Value { - value: V::Bool(left_value), + value: Box::new(V::Bool(left_value)), attr, } } else { @@ -580,9 +580,9 @@ impl Interpreter { value: right_value, attr, } = self.eval(*right, scope).await?; - let right_value = bool::try_from(right_value)?; + let right_value = bool::try_from(*right_value)?; Value { - value: V::Bool(right_value), + value: Box::new(V::Bool(right_value)), attr, } } @@ -592,10 +592,10 @@ impl Interpreter { value: left_value, attr, } = self.eval(*left, scope).await?; - let left_value = bool::try_from(left_value)?; + let left_value = bool::try_from(*left_value)?; if left_value { Value { - value: V::Bool(left_value), + value: Box::new(V::Bool(left_value)), attr, } } else { @@ -603,9 +603,9 @@ impl Interpreter { value: right_value, attr, } = self.eval(*right, scope).await?; - let right_value = bool::try_from(right_value)?; + let right_value = bool::try_from(*right_value)?; Value { - value: V::Bool(right_value), + value: Box::new(V::Bool(right_value)), attr, } } @@ -624,7 +624,7 @@ impl Interpreter { ast::Expression::Index(ast::Index { target, index, .. }) => { let target = self.eval(*target, scope).await?; let i = self.eval(*index, scope).await?; - match target.value { + match *target.value { V::Arr(arr) => { let i = f64::try_from(i)?; let item = if i.trunc() == i { @@ -658,7 +658,7 @@ impl Interpreter { } ast::Expression::Prop(ast::Prop { target, name, .. }) => { let value = self.eval(*target.clone(), scope).await?; - if let V::Obj(value) = value.value { + if let V::Obj(value) = *value.value { if let Some(value) = value.read().unwrap().get(&name) { value.clone() } else { @@ -682,7 +682,7 @@ impl Interpreter { let mut v = Value::null(); for node in program { v = self.eval(node, scope).await?; - if let V::Return(_) | V::Break | V::Continue = v.value { + if let V::Return(_) | V::Break | V::Continue = *v.value { return Ok(v); } } @@ -715,7 +715,7 @@ impl Interpreter { ast::Expression::Index(ast::Index { target, index, .. }) => { let assignee = self.eval(*target.clone(), scope).await?; let i = self.eval(*index, scope).await?; - match assignee.value { + match *assignee.value { V::Arr(arr) => { let i = f64::try_from(i)?; if i.trunc() == i && arr.read().unwrap().get(i as usize).is_some() { @@ -740,7 +740,7 @@ impl Interpreter { } ast::Expression::Prop(ast::Prop { target, name, .. }) => { let assignee = self.eval(*target.clone(), scope).await?; - let assignee = VObj::try_from(assignee.value)?; + let assignee = VObj::try_from(assignee)?; assignee.write().unwrap().insert(name, value); } ast::Expression::Arr(ast::Arr { value: target, .. }) => { @@ -751,7 +751,7 @@ impl Interpreter { .await?; } ast::Expression::Obj(ast::Obj { value: target, .. }) => { - let value = >::try_from(value.value)?; + let value = >::try_from(value)?; try_join_all(target.into_iter().map(|(key, item)| { self.assign(scope, item, value.get(&key).cloned().unwrap_or_default()) })) diff --git a/src/interpreter/lib/std.rs b/src/interpreter/lib/std.rs index 230f5cd..5685369 100644 --- a/src/interpreter/lib/std.rs +++ b/src/interpreter/lib/std.rs @@ -256,7 +256,7 @@ pub fn std() -> HashMap { async move { let mut args = args.into_iter(); let v = expect_any(args.next())?; - Ok(Value::str(v.value.display_type().to_string())) + Ok(Value::str(v.display_type().to_string())) } .boxed() }), @@ -268,7 +268,7 @@ pub fn std() -> HashMap { async move { let mut args = args.into_iter(); let v = expect_any(args.next())?; - Ok(Value::str(v.value.repr_value().to_string())) + Ok(Value::str(v.repr_value().to_string())) } .boxed() }), @@ -1097,7 +1097,7 @@ pub fn std() -> HashMap { async move { let mut args = args.into_iter(); let seed = expect_any(args.next())?; - Ok(match seed.value { + Ok(match *seed.value { V::Num(num) => Some(num.to_string()), V::Str(str) => Some(str), _ => None, @@ -1215,7 +1215,7 @@ pub fn std() -> HashMap { let codepoints = >::try_from(args.next().unwrap_or_default())?; let mut s = String::new(); for codepoint in codepoints { - let codepoint = f64::try_from(codepoint.value)?; + let codepoint = f64::try_from(codepoint)?; s += char::from_u32(codepoint as u32) .map_or_else( || { diff --git a/src/interpreter/primitive_props.rs b/src/interpreter/primitive_props.rs index 2452feb..fc7e866 100644 --- a/src/interpreter/primitive_props.rs +++ b/src/interpreter/primitive_props.rs @@ -15,7 +15,7 @@ use super::{ }; pub fn get_prim_prop(target: Value, name: String) -> Result { - Ok(match target.value { + Ok(match *target.value { V::Num(target) => match name.as_str() { "to_str" => Value::fn_native(move |_, _| { async move { Ok(Value::str(target.to_string())) }.boxed() @@ -440,7 +440,7 @@ pub fn get_prim_prop(target: Value, name: String) -> Result Result Result for bool { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -63,7 +63,7 @@ impl TryFrom for VFn { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -86,7 +86,7 @@ impl TryFrom for String { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -109,7 +109,7 @@ impl TryFrom for f64 { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -132,7 +132,7 @@ impl TryFrom for VObj { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -148,7 +148,7 @@ impl TryFrom for IndexMap { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -171,7 +171,7 @@ impl TryFrom for VArr { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -187,7 +187,7 @@ impl TryFrom for Vec { type Error = AiScriptError; fn try_from(value: Value) -> Result { - value.value.try_into() + (*value.value).try_into() } } @@ -340,7 +340,7 @@ impl Serialize for VWithMemo { let mut seq = serializer.serialize_seq(Some(value.len()))?; for e in value.iter() { seq.serialize_element(&VWithMemo { - value: e.value.clone(), + value: *e.value.clone(), processed_arrays: processed_arrays.clone(), processed_objects: self.processed_objects.clone(), })?; @@ -361,7 +361,7 @@ impl Serialize for VWithMemo { map.serialize_entry( k, &VWithMemo { - value: v.value.clone(), + value: *v.value.clone(), processed_arrays: self.processed_arrays.clone(), processed_objects: processed_objects.clone(), }, diff --git a/src/interpreter/value.rs b/src/interpreter/value.rs index 65256e7..319561d 100644 --- a/src/interpreter/value.rs +++ b/src/interpreter/value.rs @@ -101,20 +101,23 @@ pub struct Attr { #[derive(Clone, Debug, Default)] pub struct Value { - pub value: V, + pub value: Box, pub attr: Option>, } impl Value { - pub const fn new(value: V) -> Self { - Value { value, attr: None } + pub fn new(value: V) -> Self { + Value { + value: value.into(), + attr: None, + } } - pub const fn null() -> Self { + pub fn null() -> Self { Value::new(V::Null) } - pub const fn bool(value: bool) -> Self { + pub fn bool(value: bool) -> Self { Value::new(V::Bool(value)) } @@ -181,7 +184,7 @@ impl Value { } pub fn unwrap_ret(v: Value) -> Value { - if let V::Return(value) = v.value { + if let V::Return(value) = *v.value { *value } else { v diff --git a/tests/test.rs b/tests/test.rs index 2368eda..e747720 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -5025,7 +5025,7 @@ mod std { .unwrap(); test("<: Math:sin(Math:PI / 4) * Math:cos(Math:PI / 4)", |res| { - assert!((f64::try_from(res.value).unwrap() - 0.5).abs() <= f64::EPSILON) + assert!((f64::try_from(res).unwrap() - 0.5).abs() <= f64::EPSILON) }) .await .unwrap();