Skip to content

Commit

Permalink
Evaluate Comparison Expressions with Constant Operands during Compila…
Browse files Browse the repository at this point in the history
…tion

This patch supports evaluation of comparison expressions with
constants as operands during compile time.

Fixes #755
  • Loading branch information
Nirhar committed Jun 7, 2024
1 parent 668f9f2 commit 2ee062f
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions src/sema/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,150 @@ pub(crate) fn eval_constants_in_expression(
(None, true)
}
}
Expression::More {
loc,
left,
right,
} => {
let left = eval_constants_in_expression(left, diagnostics).0;
let right = eval_constants_in_expression(right, diagnostics).0;

if let (
Some(Expression::NumberLiteral { value: left, .. }),
Some(Expression::NumberLiteral { value: right, .. }),
) = (&left, &right)
{
(
Some(Expression::BoolLiteral {
loc: *loc,
value: (left.cmp(right) == core::cmp::Ordering::Greater)
}),
true,
)
} else {
(None, true)
}
}
Expression::Less {
loc,
left,
right,
} => {
let left = eval_constants_in_expression(left, diagnostics).0;
let right = eval_constants_in_expression(right, diagnostics).0;

if let (
Some(Expression::NumberLiteral { value: left, .. }),
Some(Expression::NumberLiteral { value: right, .. }),
) = (&left, &right)
{
(
Some(Expression::BoolLiteral {
loc: *loc,
value: (left.cmp(right) == core::cmp::Ordering::Less)
}),
true,
)
} else {
(None, true)
}
}
Expression::Equal {
loc,
left,
right,
} => {
let left = eval_constants_in_expression(left, diagnostics).0;
let right = eval_constants_in_expression(right, diagnostics).0;

if let (
Some(Expression::NumberLiteral { value: left, .. }),
Some(Expression::NumberLiteral { value: right, .. }),
) = (&left, &right)
{
(
Some(Expression::BoolLiteral {
loc: *loc,
value: (left.cmp(right) == core::cmp::Ordering::Equal)
}),
true,
)
} else {
(None, true)
}
}
Expression::NotEqual {
loc,
left,
right,
} => {
let left = eval_constants_in_expression(left, diagnostics).0;
let right = eval_constants_in_expression(right, diagnostics).0;

if let (
Some(Expression::NumberLiteral { value: left, .. }),
Some(Expression::NumberLiteral { value: right, .. }),
) = (&left, &right)
{
(
Some(Expression::BoolLiteral {
loc: *loc,
value: (left.cmp(right) != core::cmp::Ordering::Equal)
}),
true,
)
} else {
(None, true)
}
}
Expression::MoreEqual {
loc,
left,
right,
} => {
let left = eval_constants_in_expression(left, diagnostics).0;
let right = eval_constants_in_expression(right, diagnostics).0;

if let (
Some(Expression::NumberLiteral { value: left, .. }),
Some(Expression::NumberLiteral { value: right, .. }),
) = (&left, &right)
{
(
Some(Expression::BoolLiteral {
loc: *loc,
value: (left.cmp(right) == core::cmp::Ordering::Greater && left.cmp(right) == core::cmp::Ordering::Equal)
}),
true,
)
} else {
(None, true)
}
}
Expression::LessEqual {
loc,
left,
right,
} => {
let left = eval_constants_in_expression(left, diagnostics).0;
let right = eval_constants_in_expression(right, diagnostics).0;

if let (
Some(Expression::NumberLiteral { value: left, .. }),
Some(Expression::NumberLiteral { value: right, .. }),
) = (&left, &right)
{
(
Some(Expression::BoolLiteral {
loc: *loc,
value: (left.cmp(right) == core::cmp::Ordering::Less && left.cmp(right) == core::cmp::Ordering::Equal)
}),
true,
)
} else {
(None, true)
}
}
Expression::ZeroExt { loc, to, expr } => {
let expr = eval_constants_in_expression(expr, diagnostics).0;
if let Some(Expression::NumberLiteral { value, .. }) = expr {
Expand Down

0 comments on commit 2ee062f

Please sign in to comment.