Skip to content

Commit

Permalink
initial changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bwburnsides committed Dec 26, 2023
1 parent a793015 commit 91d72dc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/expr/builtin_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,38 @@ pub fn eval_builtin_assert(
query: &mut expr::EvalFunctionQuery)
-> Result<expr::Value, ()>
{
query.ensure_arg_number(1)?;
query.ensure_min_max_arg_number(1, 2)?;

let condition = query.args[0].value.expect_bool(
query.report,
query.args[0].span)?;
let condition = query.args[0]
.value
.expect_bool(
query.report,
query.args[0].span)?;

let msg = if query.args.len() >= 2
{
diagn::Message::error_span(
format!(
"assertion failed: {}",
query.args[1]
.value
.expect_string(query.report, query.args[1].span)?
.utf8_contents
.clone()
),
query.span,
)
}
else {
diagn::Message::error_span("assertion failed", query.span)
};

if condition
{
Ok(expr::Value::Void)
}
else
{
let msg = diagn::Message::error_span(
"assertion failed",
query.span);

Ok(expr::Value::FailedConstraint(
query.report.wrap_in_parents_capped(msg)))
}
Expand Down
23 changes: 23 additions & 0 deletions src/expr/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,29 @@ impl<'a> EvalFunctionQuery<'a>
Ok(())
}
}

pub fn ensure_min_max_arg_number(
&mut self,
minimum_expected_arg_number: usize,
maximum_expected_arg_number: usize,
) -> Result<(), ()> {
if !((self.args.len() >= minimum_expected_arg_number)
&& (self.args.len() <= maximum_expected_arg_number))
{
self.report.error_span(
format!(
"function expected {} to {} arguments (but got {})",
minimum_expected_arg_number,
maximum_expected_arg_number,
self.args.len()
),
self.span,
);
Err(())
} else {
Ok(())
}
}
}


Expand Down

0 comments on commit 91d72dc

Please sign in to comment.