Skip to content

Commit 01648fe

Browse files
committed
[Fix] Asserts Type Checker now checks Error Messages in the Asserts
1 parent 31bf3ac commit 01648fe

1 file changed

Lines changed: 134 additions & 18 deletions

File tree

src/type_checker/statement_type_checker.rs

Lines changed: 134 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ pub fn check_stmt(
2222
Statement::TypeDeclaration(name, cons) => check_adt_declarations_stmt(name, cons, env),
2323
Statement::Return(exp) => check_return_stmt(exp, env),
2424

25-
Statement::Assert(expr1, expr2) => check_assert(expr1, expr2, env),
26-
Statement::AssertTrue(expr, _) => check_assert_true(expr, env),
27-
Statement::AssertFalse(expr, _) => check_assert_false(expr, env),
28-
Statement::AssertEQ(lhs, rhs, _) => check_assert_eq(lhs, rhs, env),
29-
Statement::AssertNEQ(lhs, rhs, _) => check_assert_neq(lhs, rhs, env),
25+
Statement::Assert(expr1, errmsg) => check_assert(expr1, errmsg, env),
26+
Statement::AssertTrue(expr1, errmsg) => check_assert_true(expr1, errmsg, env),
27+
Statement::AssertFalse(expr1, errmsg) => check_assert_false(expr1, errmsg, env),
28+
Statement::AssertEQ(lhs, rhs, errmsg) => check_assert_eq(lhs, rhs, errmsg, env),
29+
Statement::AssertNEQ(lhs, rhs, errmsg) => check_assert_neq(lhs, rhs, errmsg, env),
3030

3131
_ => Err("Not implemented yet".to_string()),
3232
}
@@ -256,45 +256,57 @@ fn check_assert(
256256
}
257257

258258

259-
fn check_assert_true(expr: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
260-
let expr_type = check_expr(*expr, env)?;
259+
fn check_assert_true(expr1: Box<Expression>, expr2: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
260+
let expr_type = check_expr(*expr1, env)?;
261+
let expr_type2 = check_expr(*expr2, env)?;
261262
if expr_type != Type::TBool {
262263
Err("[Type Error] AssertTrue expression must be of type Boolean.".to_string())
264+
} else if expr_type2 != Type::TString {
265+
Err("[Type Error] Second Assert expression must be of type CString.".to_string())
263266
} else {
264267
Ok(env.clone())
265268
}
266269
}
267270

268-
fn check_assert_false(expr: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
269-
let expr_type = check_expr(*expr, env)?;
271+
fn check_assert_false(expr1: Box<Expression>, expr2: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
272+
let expr_type = check_expr(*expr1, env)?;
273+
let expr_type2 = check_expr(*expr2, env)?;
270274
if expr_type != Type::TBool {
271275
Err("[Type Error] AssertFalse expression must be of type Boolean.".to_string())
276+
} else if expr_type2 != Type::TString {
277+
Err("[Type Error] Second Assert expression must be of type CString.".to_string())
272278
} else {
273279
Ok(env.clone())
274280
}
275281
}
276282

277-
fn check_assert_eq(lhs: Box<Expression>, rhs: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
283+
fn check_assert_eq(lhs: Box<Expression>, rhs: Box<Expression>, err: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
278284
let lhs_type = check_expr(*lhs, env)?;
279285
let rhs_type = check_expr(*rhs, env)?;
286+
let err_type = check_expr(*err, env)?;
280287
if lhs_type != rhs_type {
281288
Err(format!(
282289
"[Type Error] AssertEQ expressions must have the same type. Found {:?} and {:?}.",
283290
lhs_type, rhs_type
284291
))
292+
} else if err_type != Type::TString {
293+
Err("[Type Error] Third Assert expression must be of type CString.".to_string())
285294
} else {
286295
Ok(env.clone())
287296
}
288297
}
289298

290-
fn check_assert_neq(lhs: Box<Expression>, rhs: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
299+
fn check_assert_neq(lhs: Box<Expression>, rhs: Box<Expression>, err: Box<Expression>, env: &Environment<Type>) -> Result<Environment<Type>, ErrorMessage> {
291300
let lhs_type = check_expr(*lhs, env)?;
292301
let rhs_type = check_expr(*rhs, env)?;
302+
let err_type = check_expr(*err, env)?;
293303
if lhs_type != rhs_type {
294304
Err(format!(
295305
"[Type Error] AssertNEQ expressions must have the same type. Found {:?} and {:?}.",
296306
lhs_type, rhs_type
297307
))
308+
} else if err_type != Type::TString {
309+
Err("[Type Error] Third Assert expression must be of type CString.".to_string())
298310
} else {
299311
Ok(env.clone())
300312
}
@@ -746,22 +758,22 @@ fn test_assert_bool_error() {
746758
let env: Environment<Type> = Environment::new();
747759
let stmt = Statement::Assert(
748760
Box::new(Expression::CInt(1)), // não booleano
749-
Box::new(Expression::CTrue), // segundo argumento pode ser qualquer um válido
761+
Box::new(Expression::CString("msg".to_string())), // segundo argumento pode ser qualquer um válido
750762
);
751763
assert!(check_stmt(stmt, &env).is_err());
752764
}
753765

754766
#[test]
755767
fn test_assert_true_ok() {
756768
let env = Environment::new();
757-
let stmt = Statement::AssertTrue(Box::new(Expression::CTrue), "ok".to_string());
769+
let stmt = Statement::AssertTrue(Box::new(Expression::CTrue), Box::new(Expression::CString("ok".to_string())));
758770
assert!(check_stmt(stmt, &env).is_ok());
759771
}
760772

761773
#[test]
762774
fn test_assert_false_ok() {
763775
let env = Environment::new();
764-
let stmt = Statement::AssertFalse(Box::new(Expression::CFalse), "false".to_string());
776+
let stmt = Statement::AssertFalse(Box::new(Expression::CFalse), Box::new(Expression::CString("false".to_string())));
765777
assert!(check_stmt(stmt, &env).is_ok());
766778
}
767779

@@ -771,7 +783,7 @@ fn test_assert_eq_same_type() {
771783
let stmt = Statement::AssertEQ(
772784
Box::new(Expression::CInt(1)),
773785
Box::new(Expression::CInt(2)),
774-
"eq".to_string(),
786+
Box::new(Expression::CString("eq".to_string())),
775787
);
776788
assert!(check_stmt(stmt, &env).is_ok());
777789
}
@@ -782,7 +794,7 @@ fn test_assert_eq_mismatch_type() {
782794
let stmt = Statement::AssertEQ(
783795
Box::new(Expression::CInt(1)),
784796
Box::new(Expression::CString("x".to_string())),
785-
"eq".to_string(),
797+
Box::new(Expression::CString("eq".to_string())),
786798
);
787799
assert!(check_stmt(stmt, &env).is_err());
788800
}
@@ -793,7 +805,7 @@ fn test_assert_neq_same_type() {
793805
let stmt = Statement::AssertNEQ(
794806
Box::new(Expression::CInt(1)),
795807
Box::new(Expression::CInt(2)),
796-
"neq".to_string(),
808+
Box::new(Expression::CString("neq".to_string())),
797809
);
798810
assert!(check_stmt(stmt, &env).is_ok());
799811
}
@@ -804,10 +816,114 @@ fn test_assert_neq_mismatch_type() {
804816
let stmt = Statement::AssertNEQ(
805817
Box::new(Expression::CTrue),
806818
Box::new(Expression::CString("x".to_string())),
807-
"neq".to_string(),
819+
Box::new(Expression::CString("neq".to_string())),
820+
);
821+
assert!(check_stmt(stmt, &env).is_err());
822+
}
823+
824+
#[test]
825+
fn test_assert_error_msg_not_string() {
826+
let env = Environment::new();
827+
let stmt = Statement::Assert(
828+
Box::new(Expression::CTrue),
829+
Box::new(Expression::CTrue), // Error message must be a string
830+
);
831+
assert!(check_stmt(stmt, &env).is_err());
832+
}
833+
834+
#[test]
835+
fn test_assert_true_error_msg_not_string() {
836+
let env = Environment::new();
837+
let stmt = Statement::AssertTrue(
838+
Box::new(Expression::CTrue),
839+
Box::new(Expression::CTrue), // Error message must be a string
840+
);
841+
assert!(check_stmt(stmt, &env).is_err());
842+
}
843+
844+
#[test]
845+
fn test_assert_false_error_msg_not_string() {
846+
let env = Environment::new();
847+
let stmt = Statement::AssertFalse(
848+
Box::new(Expression::CFalse),
849+
Box::new(Expression::CTrue), // Error message must be a string
850+
);
851+
assert!(check_stmt(stmt, &env).is_err());
852+
}
853+
854+
#[test]
855+
fn test_assert_eq_error_msg_not_string() {
856+
let env = Environment::new();
857+
let stmt = Statement::AssertEQ(
858+
Box::new(Expression::CTrue),
859+
Box::new(Expression::CTrue),
860+
Box::new(Expression::CTrue), // Error message must be a string
861+
);
862+
assert!(check_stmt(stmt, &env).is_err());
863+
}
864+
865+
#[test]
866+
fn test_assert_neq_error_msg_not_string() {
867+
let env = Environment::new();
868+
let stmt = Statement::AssertNEQ(
869+
Box::new(Expression::CTrue),
870+
Box::new(Expression::CFalse),
871+
Box::new(Expression::CTrue), // Error message must be a string
808872
);
809873
assert!(check_stmt(stmt, &env).is_err());
810874
}
811875

876+
#[test]
877+
fn test_assert_error_msg_string() {
878+
let env = Environment::new();
879+
let stmt = Statement::Assert(
880+
Box::new(Expression::CTrue),
881+
Box::new(Expression::CString("assert".to_string())), // Error message must be a string
882+
);
883+
assert!(check_stmt(stmt, &env).is_ok());
884+
}
885+
886+
#[test]
887+
fn test_assert_true_error_msg_string() {
888+
let env = Environment::new();
889+
let stmt = Statement::AssertTrue(
890+
Box::new(Expression::CTrue),
891+
Box::new(Expression::CString("asserttrue".to_string())), // Error message must be a string
892+
);
893+
assert!(check_stmt(stmt, &env).is_ok());
894+
}
895+
896+
#[test]
897+
fn test_assert_false_error_msg_string() {
898+
let env = Environment::new();
899+
let stmt = Statement::AssertFalse(
900+
Box::new(Expression::CFalse),
901+
Box::new(Expression::CString("assertfalse".to_string())), // Error message must be a string
902+
);
903+
assert!(check_stmt(stmt, &env).is_ok());
904+
}
905+
906+
#[test]
907+
fn test_assert_eq_error_msg_string() {
908+
let env = Environment::new();
909+
let stmt = Statement::AssertEQ(
910+
Box::new(Expression::CTrue),
911+
Box::new(Expression::CTrue),
912+
Box::new(Expression::CString("eq".to_string())), // Error message must be a string
913+
);
914+
assert!(check_stmt(stmt, &env).is_ok());
915+
}
916+
917+
#[test]
918+
fn test_assert_neq_error_msg_string() {
919+
let env = Environment::new();
920+
let stmt = Statement::AssertNEQ(
921+
Box::new(Expression::CTrue),
922+
Box::new(Expression::CFalse),
923+
Box::new(Expression::CString("neq".to_string())), // Error message must be a string
924+
);
925+
assert!(check_stmt(stmt, &env).is_ok());
926+
}
927+
812928

813929
}

0 commit comments

Comments
 (0)