From 0e363d0d836ebda4472456ca31c66b2dcc52998b Mon Sep 17 00:00:00 2001 From: Arata Date: Wed, 3 Jul 2024 22:42:59 +0900 Subject: [PATCH] feat: impl !if --- crates/ide/src/eval.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/crates/ide/src/eval.rs b/crates/ide/src/eval.rs index dfe9871..2258d4f 100644 --- a/crates/ide/src/eval.rs +++ b/crates/ide/src/eval.rs @@ -1172,6 +1172,45 @@ impl EvalExpr for SimpleExpr { Some(Value::List(concatenated_list, *arg_item_typ)) } + BangOperatorOp::XIf => { + if args.len() != 3 { + ctx.error(loc.range, "expected three operand in tenary operator"); + return None; + } + + let mut iter_args = args.into_iter(); + let test = iter_args.next().unwrap(); + let then = iter_args.next().unwrap(); + let else_ = iter_args.next().unwrap(); + if !then.typ().isa(&ctx.symbol_map, &else_.typ()) { + ctx.error( + else_.loc().range, + format!( + "inconsistent types '{}' and '{}' for !if", + then.typ(), + else_.typ(), + ), + ); + return None; + } + + let test_loc = test.loc(); + let test_value = test.eval_expr(ctx)?; + let Some(Value::Bit(test)) = test_value.cast_to(&ctx.symbol_map, &Type::Bit) + else { + ctx.error( + test_loc.range, + "if condition must of type bit, bits, or int.", + ); + return None; + }; + + if test { + then.eval_expr(ctx) + } else { + else_.eval_expr(ctx) + } + } _ => { ctx.error( loc.range,