Skip to content

Commit

Permalink
wrap V with Box
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Jun 13, 2024
1 parent b04fbd4 commit 2fcca58
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions examples/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
36 changes: 18 additions & 18 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl Interpreter {
None,
);
let v = self.eval(*for_.clone(), &scope).await?;
match v.value {
match *v.value {
V::Break => {
break;
}
Expand All @@ -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;
}
Expand All @@ -422,7 +422,7 @@ impl Interpreter {
None,
);
let v = self.eval(*for_.clone(), &scope).await?;
match v.value {
match *v.value {
V::Break => {
break;
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -569,20 +569,20 @@ 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 {
let Value {
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,
}
}
Expand All @@ -592,20 +592,20 @@ 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 {
let Value {
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,
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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, .. }) => {
Expand All @@ -751,7 +751,7 @@ impl Interpreter {
.await?;
}
ast::Expression::Obj(ast::Obj { value: target, .. }) => {
let value = <IndexMap<String, Value>>::try_from(value.value)?;
let value = <IndexMap<String, Value>>::try_from(value)?;
try_join_all(target.into_iter().map(|(key, item)| {
self.assign(scope, item, value.get(&key).cloned().unwrap_or_default())
}))
Expand Down
8 changes: 4 additions & 4 deletions src/interpreter/lib/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub fn std() -> HashMap<String, Value> {
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()
}),
Expand All @@ -268,7 +268,7 @@ pub fn std() -> HashMap<String, Value> {
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()
}),
Expand Down Expand Up @@ -1097,7 +1097,7 @@ pub fn std() -> HashMap<String, Value> {
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,
Expand Down Expand Up @@ -1215,7 +1215,7 @@ pub fn std() -> HashMap<String, Value> {
let codepoints = <Vec<Value>>::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(
|| {
Expand Down
8 changes: 4 additions & 4 deletions src/interpreter/primitive_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
};

pub fn get_prim_prop(target: Value, name: String) -> Result<Value, AiScriptError> {
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()
Expand Down Expand Up @@ -440,7 +440,7 @@ pub fn get_prim_prop(target: Value, name: String) -> Result<Value, AiScriptError
target
.iter()
.map(|i| {
if let V::Str(value) = &i.value {
if let V::Str(value) = &*i.value {
value
} else {
""
Expand Down Expand Up @@ -759,7 +759,7 @@ pub fn get_prim_prop(target: Value, name: String) -> Result<Value, AiScriptError
return;
}
for v in arr {
if let V::Arr(value) = v.value {
if let V::Arr(value) = *v.value {
flat(value.read().unwrap().clone(), depth - 1, result);
} else {
result.push(v);
Expand Down Expand Up @@ -787,7 +787,7 @@ pub fn get_prim_prop(target: Value, name: String) -> Result<Value, AiScriptError
.await?;
let mut result = Vec::new();
for value in mapped_vals {
if let V::Arr(value) = value.value {
if let V::Arr(value) = *value.value {
result.extend(value.read().unwrap().clone())
} else {
result.push(value)
Expand Down
20 changes: 10 additions & 10 deletions src/interpreter/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl TryFrom<Value> for bool {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand All @@ -63,7 +63,7 @@ impl TryFrom<Value> for VFn {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand All @@ -86,7 +86,7 @@ impl TryFrom<Value> for String {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand All @@ -109,7 +109,7 @@ impl TryFrom<Value> for f64 {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand All @@ -132,7 +132,7 @@ impl TryFrom<Value> for VObj {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand All @@ -148,7 +148,7 @@ impl TryFrom<Value> for IndexMap<String, Value> {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand All @@ -171,7 +171,7 @@ impl TryFrom<Value> for VArr {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand All @@ -187,7 +187,7 @@ impl TryFrom<Value> for Vec<Value> {
type Error = AiScriptError;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.value.try_into()
(*value.value).try_into()
}
}

Expand Down Expand Up @@ -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(),
})?;
Expand All @@ -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(),
},
Expand Down
15 changes: 9 additions & 6 deletions src/interpreter/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,23 @@ pub struct Attr {

#[derive(Clone, Debug, Default)]
pub struct Value {
pub value: V,
pub value: Box<V>,
pub attr: Option<Vec<Attr>>,
}

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))
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2fcca58

Please sign in to comment.