diff --git a/tests/if_tests.rs b/tests/if_tests.rs index f3f9394..5d8a64d 100644 --- a/tests/if_tests.rs +++ b/tests/if_tests.rs @@ -912,7 +912,182 @@ fn if_body_statements() { Some((&label_loop_begin, &label_loop_end)), ); assert!(t.is_empty_error()); - // println!("{ctx2:#?}"); + + assert!(block_state.borrow().context.clone().get().is_empty()); + assert!(block_state.borrow().parent.is_none()); + assert_eq!(block_state.borrow().children.len(), 1); + + let ctx = block_state.borrow().children[0].clone(); + assert!(ctx.borrow().parent.is_some()); + assert_eq!(ctx.borrow().children.len(), 2); + + let stm_ctx = ctx.borrow().context.clone().get(); + assert_eq!(stm_ctx.len(), 8); + assert_eq!( + stm_ctx[0], + SemanticStackContext::IfConditionExpression { + expr_result: ExpressionResult { + expr_type: Type::Primitive(PrimitiveTypes::U64), + expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::U64(1)), + }, + label_if_begin: String::from("if_begin").into(), + label_if_end: String::from("if_end").into() + } + ); + assert_eq!( + stm_ctx[1], + SemanticStackContext::SetLabel { + label: String::from("if_begin").into() + } + ); + assert_eq!( + stm_ctx[2], + SemanticStackContext::LetBinding { + let_decl: Value { + inner_name: "x.0".into(), + inner_type: Type::Primitive(PrimitiveTypes::Bool), + mutable: true, + alloca: false, + malloc: false + }, + expr_result: ExpressionResult { + expr_type: Type::Primitive(PrimitiveTypes::Bool), + expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(false)), + }, + } + ); + assert_eq!( + stm_ctx[3], + SemanticStackContext::Binding { + val: Value { + inner_name: "x.0".into(), + inner_type: Type::Primitive(PrimitiveTypes::Bool), + mutable: true, + alloca: false, + malloc: false + }, + expr_result: ExpressionResult { + expr_type: Type::Primitive(PrimitiveTypes::Bool), + expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(true)), + }, + } + ); + assert_eq!( + stm_ctx[4], + SemanticStackContext::Call { + call: Function { + inner_name: String::from("fn2").into(), + inner_type: Type::Primitive(PrimitiveTypes::U16), + parameters: vec![], + }, + params: vec![], + } + ); + assert_eq!( + stm_ctx[5], + SemanticStackContext::JumpFunctionReturn { + expr_result: ExpressionResult { + expr_type: Type::Primitive(PrimitiveTypes::Bool), + expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(true)), + } + } + ); + assert_eq!( + stm_ctx[6], + SemanticStackContext::JumpTo { + label: String::from("if_end").into() + } + ); + assert_eq!( + stm_ctx[7], + SemanticStackContext::SetLabel { + label: String::from("if_end").into() + } + ); + + let ch_ctx1 = ctx.borrow().children[0].clone(); + assert!(ch_ctx1.borrow().parent.is_some()); + assert!(ch_ctx1.borrow().children.is_empty()); + + let ctx1 = ch_ctx1.borrow().context.clone().get(); + assert_eq!(ctx1.len(), 4); + assert_eq!( + ctx1[0], + SemanticStackContext::IfConditionExpression { + expr_result: ExpressionResult { + expr_type: Type::Primitive(PrimitiveTypes::Bool), + expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(true)), + }, + label_if_begin: String::from("if_begin.0").into(), + label_if_end: String::from("if_end").into() + } + ); + assert_eq!( + ctx1[1], + SemanticStackContext::SetLabel { + label: String::from("if_begin.0").into() + } + ); + assert_eq!( + ctx1[2], + SemanticStackContext::Call { + call: Function { + inner_name: String::from("fn2").into(), + inner_type: Type::Primitive(PrimitiveTypes::U16), + parameters: vec![], + }, + params: vec![], + } + ); + assert_eq!( + ctx1[3], + SemanticStackContext::JumpTo { + label: String::from("if_end").into() + } + ); + + let ch_ctx2 = ctx.borrow().children[1].clone(); + assert!(ch_ctx2.borrow().parent.is_some()); + assert!(ch_ctx2.borrow().children.is_empty()); + + let ctx2 = ch_ctx2.borrow().context.clone().get(); + assert_eq!(ctx2.len(), 5); + assert_eq!( + ctx2[0], + SemanticStackContext::JumpTo { + label: String::from("loop_begin").into() + } + ); + + assert_eq!( + ctx2[1], + SemanticStackContext::SetLabel { + label: String::from("loop_begin").into() + } + ); + assert_eq!( + ctx2[2], + SemanticStackContext::Call { + call: Function { + inner_name: String::from("fn2").into(), + inner_type: Type::Primitive(PrimitiveTypes::U16), + parameters: vec![], + }, + params: vec![], + } + ); + assert_eq!( + ctx2[3], + SemanticStackContext::JumpTo { + label: String::from("loop_begin").into() + } + ); + assert_eq!( + ctx2[4], + SemanticStackContext::SetLabel { + label: String::from("loop_end").into() + } + ); } #[test]