error handling

This commit is contained in:
2022-06-03 12:39:14 +02:00
parent 88d5ac6ad7
commit 895439cbc9
4 changed files with 70 additions and 5 deletions

View File

@@ -43,6 +43,7 @@ pub enum Instruction {
},
Each(String, Box<Instruction>, Vec<Instruction>),
While(Box<Instruction>, Vec<Instruction>),
Handle(Vec<Instruction>, Vec<Instruction>, String),
}
impl Instruction {
@@ -230,6 +231,19 @@ impl Instruction {
util::find_node(&node, "do").ok_or(MissingChild("while", "from"))?,
)?,
),
"handle" => Instruction::Handle(
Instruction::from_children(
util::find_node(&node, "try").ok_or(MissingChild("handle", "try"))?,
)?,
Instruction::from_children(
util::find_node(&node, "catch").ok_or(MissingChild("handle", "try"))?,
)?,
util::find_node(&node, "catch")
.ok_or(MissingChild("handle", "try"))?
.attribute("variable")
.ok_or(MissingAttribute("catch", "variable"))?
.to_string(),
),
tag => Err(format!("unknown tag '{}'", tag))?,
})
}
@@ -698,6 +712,18 @@ impl Instruction {
}
None
}
Instruction::Handle(try_block, catch_block, variable) => {
for ins in try_block {
if let Err(e) = ins.run(ctx, globals) {
ctx.assign(variable.clone(), Value::String(e.to_string()));
for ins in catch_block {
ins.run(ctx, globals)?;
}
break;
}
}
None
}
}
} else {
None