feat: error handling (partial)
This commit is contained in:
@@ -9,16 +9,12 @@ use regex::Regex;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Constructor)]
|
||||
pub struct Catchable {
|
||||
msg: Option<String>,
|
||||
msg: String,
|
||||
}
|
||||
|
||||
impl Display for Catchable {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
if let Some(ref msg) = self.msg {
|
||||
write!(f, "<error: {msg}>")
|
||||
} else {
|
||||
write!(f, "<error>")
|
||||
}
|
||||
write!(f, "<error: {}>", self.msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -180,13 +180,13 @@ impl Value {
|
||||
pub fn typename(&self) -> &'static str {
|
||||
use Value::*;
|
||||
match self {
|
||||
Const(_) => todo!(),
|
||||
Const(_) => unreachable!(),
|
||||
Thunk(_) => "thunk",
|
||||
ThunkRef(_) => "thunk",
|
||||
AttrSet(_) => "set",
|
||||
RecAttrSet(_) => "set",
|
||||
List(_) => "list",
|
||||
Catchable(_) => todo!(),
|
||||
Catchable(_) => unreachable!(),
|
||||
PrimOp(_) => "lambda",
|
||||
PartialPrimOp(_) => "lambda",
|
||||
Func(_) => "lambda",
|
||||
@@ -240,6 +240,7 @@ impl Value {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a && b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
@@ -248,13 +249,17 @@ impl Value {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a || b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eq(self, other: Value) -> Value {
|
||||
use Const::Bool;
|
||||
VmConst(Bool(self == other))
|
||||
match (self, other) {
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
(s, other) => VmConst(Bool(s == other)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lt(self, other: Value) -> Value {
|
||||
@@ -265,6 +270,7 @@ impl Value {
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => a < b as f64,
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => a < b,
|
||||
(VmConst(String(a)), VmConst(String(b))) => a < b,
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => return x,
|
||||
_ => todo!(),
|
||||
}))
|
||||
}
|
||||
@@ -274,6 +280,7 @@ impl Value {
|
||||
match self {
|
||||
VmConst(Int(int)) => VmConst(Int(-int)),
|
||||
VmConst(Float(float)) => VmConst(Float(-float)),
|
||||
x @ Value::Catchable(_) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
@@ -290,6 +297,7 @@ impl Value {
|
||||
string.push_str(b.as_str());
|
||||
VmConst(String(string))
|
||||
}
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
@@ -301,6 +309,7 @@ impl Value {
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 * b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a * b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a * b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
@@ -314,17 +323,17 @@ impl Value {
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 / b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a / b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a / b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn concat_string(&mut self, mut other: Value) -> &mut Self {
|
||||
if let (VmConst(Const::String(a)), VmConst(Const::String(b))) =
|
||||
(self.coerce_to_string(), other.coerce_to_string())
|
||||
{
|
||||
a.push_str(b.as_str());
|
||||
} else {
|
||||
todo!()
|
||||
match (self.coerce_to_string(), other.coerce_to_string()) {
|
||||
(VmConst(Const::String(a)), VmConst(Const::String(b))) => a.push_str(b.as_str()),
|
||||
(_, Value::Catchable(_)) => *self = other,
|
||||
(Value::Catchable(_), _) => (),
|
||||
_ => todo!(),
|
||||
}
|
||||
self
|
||||
}
|
||||
@@ -332,6 +341,9 @@ impl Value {
|
||||
pub fn push(&mut self, elem: Value) -> &mut Self {
|
||||
if let Value::List(list) = self {
|
||||
list.push(elem);
|
||||
} else if let Value::Catchable(_) = self {
|
||||
} else if let Value::Catchable(_) = elem {
|
||||
*self = elem;
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
@@ -339,10 +351,10 @@ impl Value {
|
||||
}
|
||||
|
||||
pub fn concat(self, other: Value) -> Value {
|
||||
if let (Value::List(a), Value::List(b)) = (self, other) {
|
||||
Value::List(a.concat(b))
|
||||
} else {
|
||||
todo!()
|
||||
match (self, other) {
|
||||
(Value::List(a), Value::List(b)) => Value::List(a.concat(b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,6 +363,9 @@ impl Value {
|
||||
attrs.push_attr(sym, val)
|
||||
} else if let Value::RecAttrSet(attrs) = self {
|
||||
attrs.push_attr(sym, val)
|
||||
} else if let Value::Catchable(_) = self {
|
||||
} else if let Value::Catchable(_) = val {
|
||||
*self = val
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
@@ -362,38 +377,43 @@ impl Value {
|
||||
(Value::AttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update(b)),
|
||||
(Value::RecAttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update_normal(b)),
|
||||
(Value::AttrSet(a), Value::RecAttrSet(b)) => Value::AttrSet(a.update_rec(b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, sym: Symbol) -> &mut Self {
|
||||
pub fn select(&mut self, sym: Symbol) -> Result<&mut Self> {
|
||||
let val = match self {
|
||||
Value::AttrSet(attrs) => attrs.select(sym.clone()).unwrap_or_else(|| {
|
||||
Value::Catchable(c::Catchable::new(Some(format!("{sym:?} not found"))))
|
||||
}),
|
||||
Value::RecAttrSet(attrs) => attrs.select(sym.clone()).unwrap_or_else(|| {
|
||||
Value::Catchable(c::Catchable::new(Some(format!("{sym:?} not found"))))
|
||||
}),
|
||||
_ => Value::Catchable(Catchable::new(Some(format!(
|
||||
Value::AttrSet(attrs) => attrs
|
||||
.select(sym.clone())
|
||||
.ok_or_else(|| Error::EvalError(format!("{sym:?} not found"))),
|
||||
Value::RecAttrSet(attrs) => attrs
|
||||
.select(sym.clone())
|
||||
.ok_or_else(|| Error::EvalError(format!("{sym:?} not found"))),
|
||||
Value::Catchable(_) => return Ok(self),
|
||||
_ => Err(Error::EvalError(format!(
|
||||
"cannot select from {:?}",
|
||||
self.typename()
|
||||
)))),
|
||||
};
|
||||
))),
|
||||
}?;
|
||||
*self = val;
|
||||
self
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn select_with_default(&mut self, sym: Symbol, default: Value) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
let val = attrs.select(sym).unwrap_or(default);
|
||||
*self = val;
|
||||
} else if let Value::RecAttrSet(attrs) = self {
|
||||
let val = attrs.select(sym).unwrap_or(default);
|
||||
*self = val;
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
self
|
||||
pub fn select_with_default(&mut self, sym: Symbol, default: Value) -> Result<&mut Self> {
|
||||
let val = match self {
|
||||
Value::AttrSet(attrs) => attrs.select(sym.clone()).unwrap_or(default),
|
||||
Value::RecAttrSet(attrs) => attrs.select(sym.clone()).unwrap_or(default),
|
||||
Value::Catchable(_) => return Ok(self),
|
||||
_ => {
|
||||
return Err(Error::EvalError(format!(
|
||||
"cannot select from {:?}",
|
||||
self.typename()
|
||||
)));
|
||||
}
|
||||
};
|
||||
*self = val;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn has_attr(&mut self, sym: Symbol) -> &mut Self {
|
||||
@@ -403,6 +423,7 @@ impl Value {
|
||||
} else if let Value::RecAttrSet(attrs) = self {
|
||||
let val = VmConst(Const::Bool(attrs.has_attr(sym)));
|
||||
*self = val;
|
||||
} else if let Value::Catchable(_) = self {
|
||||
} else {
|
||||
*self = VmConst(Const::Bool(false));
|
||||
}
|
||||
@@ -411,6 +432,7 @@ impl Value {
|
||||
|
||||
pub fn coerce_to_string(&mut self) -> &mut Self {
|
||||
if let VmConst(Const::String(_)) = self {
|
||||
} else if let Value::Catchable(_) = self {
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@@ -161,21 +161,21 @@ impl VM {
|
||||
stack.tos_mut()?.push_attr(sym, val);
|
||||
}
|
||||
OpCode::Select { sym } => {
|
||||
stack.tos_mut()?.force(self)?.select(Symbol::new(sym));
|
||||
stack.tos_mut()?.force(self)?.select(Symbol::new(sym))?;
|
||||
}
|
||||
OpCode::SelectOrDefault { sym } => {
|
||||
let default = stack.pop();
|
||||
stack
|
||||
.tos_mut()?
|
||||
.force(self)?
|
||||
.select_with_default(Symbol::new(sym), default);
|
||||
.select_with_default(Symbol::new(sym), default)?;
|
||||
}
|
||||
OpCode::SelectDynamic => {
|
||||
let mut val = stack.pop();
|
||||
val.force(self)?;
|
||||
val.coerce_to_string();
|
||||
let sym = val.unwrap_const().unwrap_string().into();
|
||||
stack.tos_mut()?.force(self)?.select(sym);
|
||||
stack.tos_mut()?.force(self)?.select(sym)?;
|
||||
}
|
||||
OpCode::SelectDynamicOrDefault => {
|
||||
let default = stack.pop();
|
||||
@@ -186,7 +186,7 @@ impl VM {
|
||||
stack
|
||||
.tos_mut()?
|
||||
.force(self)?
|
||||
.select_with_default(sym, default);
|
||||
.select_with_default(sym, default)?;
|
||||
}
|
||||
OpCode::HasAttr { sym } => {
|
||||
stack.tos_mut()?.force(self)?.has_attr(Symbol::new(sym));
|
||||
|
||||
Reference in New Issue
Block a user