(
context: &mut ParserContext,
env: &mut Environment,
tokens: &[Token],
position: &mut usize,
)
| 2304 | } |
| 2305 | |
| 2306 | fn parse_term_expression( |
| 2307 | context: &mut ParserContext, |
| 2308 | env: &mut Environment, |
| 2309 | tokens: &[Token], |
| 2310 | position: &mut usize, |
| 2311 | ) -> Result<Box<dyn Expr>, Box<Diagnostic>> { |
| 2312 | let mut lhs = parse_factor_expression(context, env, tokens, position)?; |
| 2313 | |
| 2314 | 'parse_expr: while *position < tokens.len() && is_term_operator(&tokens[*position]) { |
| 2315 | let operator = &tokens[*position]; |
| 2316 | |
| 2317 | // Consume `+` or `-` operator |
| 2318 | *position += 1; |
| 2319 | |
| 2320 | let rhs = parse_factor_expression(context, env, tokens, position)?; |
| 2321 | |
| 2322 | let lhs_type = lhs.expr_type(); |
| 2323 | let rhs_type = rhs.expr_type(); |
| 2324 | |
| 2325 | // Parse and Check sides for `+` operator |
| 2326 | if operator.kind == TokenKind::Plus { |
| 2327 | let expected_rhs_types = lhs_type.can_perform_add_op_with(); |
| 2328 | |
| 2329 | // Can perform this operator between LHS and RHS |
| 2330 | if expected_rhs_types.contains(&rhs_type) { |
| 2331 | lhs = Box::new(ArithmeticExpr { |
| 2332 | left: lhs, |
| 2333 | operator: ArithmeticOperator::Plus, |
| 2334 | right: rhs, |
| 2335 | result_type: lhs_type.add_op_result_type(&rhs_type), |
| 2336 | }); |
| 2337 | |
| 2338 | continue 'parse_expr; |
| 2339 | } |
| 2340 | |
| 2341 | // Check if RHS expr can be implicit casted to Expected LHS type to make this |
| 2342 | // Expression valid |
| 2343 | for expected_type in expected_rhs_types.iter() { |
| 2344 | if !expected_type.has_implicit_cast_from(&rhs) { |
| 2345 | continue; |
| 2346 | } |
| 2347 | |
| 2348 | let casting = Box::new(CastExpr { |
| 2349 | value: rhs, |
| 2350 | result_type: expected_type.clone(), |
| 2351 | }); |
| 2352 | |
| 2353 | lhs = Box::new(ArithmeticExpr { |
| 2354 | left: lhs, |
| 2355 | operator: ArithmeticOperator::Plus, |
| 2356 | right: casting, |
| 2357 | result_type: lhs_type.add_op_result_type(expected_type), |
| 2358 | }); |
| 2359 | |
| 2360 | continue 'parse_expr; |
| 2361 | } |
| 2362 | |
| 2363 | // Check if LHS expr can be implicit casted to Expected RHS type to make this |
no test coverage detected
searching dependent graphs…