a little more progress on errors

This commit is contained in:
Jonathan Turner 2021-08-10 18:31:34 +12:00
parent 828585a312
commit f62e3119c4
2 changed files with 31 additions and 17 deletions

View File

@ -286,14 +286,22 @@ pub fn report_shell_error(
let config = codespan_reporting::term::Config::default();
let diagnostic = match error {
ShellError::OperatorMismatch(operator, ty1, span1, ty2, span2) => {
let (diag_file_id1, diag_range1) = convert_span_to_diag(working_set, span1)?;
let (diag_file_id2, diag_range2) = convert_span_to_diag(working_set, span2)?;
ShellError::OperatorMismatch {
op_span,
lhs_ty,
lhs_span,
rhs_ty,
rhs_span,
} => {
let (lhs_file_id, lhs_range) = convert_span_to_diag(working_set, lhs_span)?;
let (rhs_file_id, rhs_range) = convert_span_to_diag(working_set, rhs_span)?;
let (op_file_id, op_range) = convert_span_to_diag(working_set, op_span)?;
Diagnostic::error()
.with_message(format!("Type mismatch during operation '{}'", operator))
.with_message("Type mismatch during operation")
.with_labels(vec![
Label::primary(diag_file_id1, diag_range1).with_message(ty1.to_string()),
Label::secondary(diag_file_id2, diag_range2).with_message(ty2.to_string()),
Label::primary(op_file_id, op_range).with_message("type mismatch for operator"),
Label::secondary(lhs_file_id, lhs_range).with_message(lhs_ty.to_string()),
Label::secondary(rhs_file_id, rhs_range).with_message(rhs_ty.to_string()),
])
}
ShellError::Unsupported(span) => {

View File

@ -7,7 +7,13 @@ use crate::{
#[derive(Debug)]
pub enum ShellError {
OperatorMismatch(String, Type, Span, Type, Span),
OperatorMismatch {
op_span: Span,
lhs_ty: Type,
lhs_span: Span,
rhs_ty: Type,
rhs_span: Span,
},
Unsupported(Span),
InternalError(String),
VariableNotFound(Span),
@ -107,7 +113,7 @@ impl Display for Value {
}
impl Value {
pub fn add(&self, rhs: &Value) -> Result<Value, ShellError> {
pub fn add(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
let span = crate::parser::span(&[self.span(), rhs.span()]);
match (self, rhs) {
@ -132,13 +138,13 @@ impl Value {
span,
}),
_ => Err(ShellError::OperatorMismatch(
"+".into(),
self.get_type(),
self.span(),
rhs.get_type(),
rhs.span(),
)),
_ => Err(ShellError::OperatorMismatch {
op_span: op,
lhs_ty: self.get_type(),
lhs_span: self.span(),
rhs_ty: rhs.get_type(),
rhs_span: rhs.span(),
}),
}
}
}
@ -421,18 +427,18 @@ pub fn eval_expression(
}),
Expr::Var(var_id) => stack
.get_var(*var_id)
.map(|x| x.with_span(expr.span))
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
Expr::Call(call) => eval_call(state, stack, call),
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
Expr::BinaryOp(lhs, op, rhs) => {
let op_span = op.span;
let lhs = eval_expression(state, stack.clone(), lhs)?;
let op = eval_operator(state, stack.clone(), op)?;
let rhs = eval_expression(state, stack, rhs)?;
match op {
Operator::Plus => lhs.add(&rhs),
Operator::Plus => lhs.add(op_span, &rhs),
_ => Ok(Value::Nothing { span: expr.span }),
}
}