Skip to content

Commit

Permalink
Test: extend if_body_statements
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Nov 5, 2023
1 parent dcb3ca5 commit 26dc812
Showing 1 changed file with 176 additions and 1 deletion.
177 changes: 176 additions & 1 deletion tests/if_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 26dc812

Please sign in to comment.