Skip to content

Commit

Permalink
fix block return bug
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmindlin committed Aug 19, 2024
1 parent de1110b commit 8e327ff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion scout-interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = [
"web-scraping",
"crawling",
"scraping",
]
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion scout-interpreter/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub type EnvPointer = Arc<Mutex<Env>>;

#[derive(Debug, Default)]
pub struct Env {
store: HashMap<String, Arc<Object>>,
pub store: HashMap<String, Arc<Object>>,
outer: Mutex<Weak<Mutex<Env>>>,
}

Expand Down
24 changes: 15 additions & 9 deletions scout-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! check_return_eval {
if let $crate::object::Object::Return(ret) =
&*eval_block($block, $crawler, $env, $results).await?
{
return Ok(ret.clone());
return Ok(Arc::new($crate::object::Object::Return(ret.clone())));
}
};
}
Expand Down Expand Up @@ -156,7 +156,7 @@ fn eval_statement<'a>(
.await
.as_deref()
{
Ok(Object::Return(ret)) => return Ok(ret.clone()),
Ok(Object::Return(ret)) => return Ok(Arc::new(Object::Return(ret.clone()))),
// if it was successful but not a return, do nothing
Ok(_) => {}
Err(_) if catch_block.is_some() => {
Expand Down Expand Up @@ -544,15 +544,21 @@ async fn eval_block(
StmtKind::Return(rv) => {
return match rv {
None => Ok(Arc::new(Object::Return(Arc::new(Object::Null)))),
Some(expr) => Ok(Arc::new(Object::Return(
eval_expression(expr, crawler, env.clone(), results.clone()).await?,
))),
}
Some(expr) => {
let r =
eval_expression(expr, crawler, env.clone(), results.clone()).await?;
Ok(Arc::new(Object::Return(r)))
}
};
}
_ => {
res = eval_statement(stmt, crawler, env.clone(), results.clone())
.await?
.clone()
let temp = eval_statement(stmt, crawler, env.clone(), results.clone()).await?;
match &*temp {
Object::Return(_) => return Ok(temp.clone()),
_ => {
res = temp.clone();
}
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions scout-interpreter/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Object {
Boolean(_) => "bool",
Number(_) => "number",
Fn(_, _) => "fn",
Module(_) => "module",
_ => "object",
}
}
Expand All @@ -52,6 +53,17 @@ impl Object {

Some(new_vec)
}
Module(ev) => {
let inner = ev.lock().await;
let mut iterable = Vec::new();
for (id, obj) in &inner.store {
if obj.type_str() == "module" {
let item = vec![Arc::new(Str(id.clone())), obj.clone()];
iterable.push(Arc::new(Object::List(Mutex::new(item))))
}
}
Some(iterable)
}
_ => None,
}
}
Expand Down

0 comments on commit 8e327ff

Please sign in to comment.