diff --git a/src/eval/env/mod.rs b/src/eval/env/mod.rs index 6103d9c..5a1dc4d 100644 --- a/src/eval/env/mod.rs +++ b/src/eval/env/mod.rs @@ -17,7 +17,7 @@ use crate::infer::infer_type_of_defs::{ }; use crate::infra::option::WrapOption; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::infra::vec::VecExt; use crate::lexer::lexical_analyze; use crate::parser::ast::parse_ast; @@ -63,7 +63,7 @@ fn ct_expr_env_vec_to_rt_expr_env_vec( _ => unreachable!() }) .collect::>() - .ok() + .wrap_ok() } fn def_map_to_env_vec( @@ -73,23 +73,24 @@ fn def_map_to_env_vec( let (ct_type_env_vec, ct_expr_env_vec) = def_map_to_ct_env_vec(type_def_map, expr_def_map); - let rt_type_env_vec: Vec = - ct_type_env_vec - .clone() - .into_iter() - .map(|(n, t)| (n, t.into())) - .try_fold(vec![], |acc, x| match x { - (n, Some(t)) => acc.chain_push((n, t)).ok(), - x => InferErr::of(format!("Invalid type def: {x:?}")) - .err() - })?; + let rt_type_env_vec: Vec = ct_type_env_vec + .clone() + .into_iter() + .map(|(n, t)| (n, t.into())) + .try_fold(vec![], |acc, x| match x { + (n, Some(t)) => acc + .chain_push((n, t)) + .wrap_ok(), + x => InferErr::of(format!("Invalid type def: {x:?}")) + .wrap_err() + })?; let rt_expr_env_vec = ct_expr_env_vec_to_rt_expr_env_vec( CtTypeEnv::new(ct_type_env_vec), ct_expr_env_vec )?; - (rt_type_env_vec, rt_expr_env_vec).ok() + (rt_type_env_vec, rt_expr_env_vec).wrap_ok() } pub fn parse_to_env<'t>( diff --git a/src/eval/eval_expr/case/apply/fn/mod.rs b/src/eval/eval_expr/case/apply/fn/mod.rs index 56d2483..fa096fe 100644 --- a/src/eval/eval_expr/case/apply/fn/mod.rs +++ b/src/eval/eval_expr/case/apply/fn/mod.rs @@ -7,7 +7,7 @@ use crate::eval::r#macro::{false_type, namely_type, true_type}; use crate::eval::r#type::eval_err::EvalErr; use crate::eval::r#type::expr::Expr; use crate::eval::r#type::r#type::Type; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub mod primitive_apply; pub mod source_lhs_to_closure; @@ -18,9 +18,10 @@ pub fn eval_to_bool( expr: &Rc ) -> Result { match eval_expr(type_env, expr_env, expr)? { - Expr::Int(Type::NamelyType(n), 1) if n == "True" => true.ok(), + Expr::Int(Type::NamelyType(n), 1) if n == "True" => + true.wrap_ok(), Expr::Int(Type::NamelyType(n), 0) if n == "False" => - false.ok(), + false.wrap_ok(), _ => unreachable!() } } @@ -31,7 +32,8 @@ pub fn eval_to_int( expr: &Rc ) -> Result { match eval_expr(type_env, expr_env, expr)? { - Expr::Int(Type::NamelyType(n), i) if n == "Int" => i.ok(), + Expr::Int(Type::NamelyType(n), i) if n == "Int" => + i.wrap_ok(), _ => unreachable!() } } diff --git a/src/eval/eval_expr/case/apply/fn/primitive_apply.rs b/src/eval/eval_expr/case/apply/fn/primitive_apply.rs index 69d672c..3f5e472 100644 --- a/src/eval/eval_expr/case/apply/fn/primitive_apply.rs +++ b/src/eval/eval_expr/case/apply/fn/primitive_apply.rs @@ -13,7 +13,7 @@ use crate::eval::eval_expr::EvalRet; use crate::eval::r#type::expr::primitive_op::PrimitiveOp; use crate::eval::r#type::expr::Expr; use crate::infra::option::WrapOption; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub fn primitive_apply( type_env: &TypeEnv, @@ -96,5 +96,5 @@ pub fn primitive_apply( PrimitiveOp::Or(Some(e)) => bool_expr(lhs_bool(e)? || rhs_bool()?), } - .ok() + .wrap_ok() } diff --git a/src/eval/eval_expr/case/apply/fn/source_lhs_to_closure.rs b/src/eval/eval_expr/case/apply/fn/source_lhs_to_closure.rs index 59c85f9..17c1346 100644 --- a/src/eval/eval_expr/case/apply/fn/source_lhs_to_closure.rs +++ b/src/eval/eval_expr/case/apply/fn/source_lhs_to_closure.rs @@ -10,7 +10,7 @@ use crate::eval::r#type::expr::Expr; use crate::eval::r#type::r#type::Type; use crate::infra::either::{Either, EitherAnyExt}; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub fn source_lhs_expr_to_closure( type_env: &'_ TypeEnv, @@ -42,7 +42,7 @@ pub fn source_lhs_expr_to_closure( .unwrap_or(expr_env.clone()) ) .l() - .ok(), + .wrap_ok(), Expr::PrimitiveOp(_, op, lhs_eval_env) => ( op.deref().clone(), @@ -53,7 +53,7 @@ pub fn source_lhs_expr_to_closure( .unwrap_or(expr_env.clone()) ) .r() - .ok(), + .wrap_ok(), // 由于现在 Closure 和 PrimitiveOp 会捕获环境 // 所以可以对 lhs_expr 进行自由求值 diff --git a/src/eval/eval_expr/case/apply/test.rs b/src/eval/eval_expr/case/apply/test.rs index 5ef9ebf..71f447f 100644 --- a/src/eval/eval_expr/case/apply/test.rs +++ b/src/eval/eval_expr/case/apply/test.rs @@ -6,7 +6,7 @@ use crate::eval::r#type::expr::primitive_op::PrimitiveOp; use crate::eval::r#type::expr::Expr; use crate::infra::option::WrapOption; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // neg 10 #[test] @@ -22,7 +22,7 @@ fn test_part1() { let r = Expr::Int(namely_type!("Int"), -10); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // add 10 @@ -43,7 +43,7 @@ fn test_part2() { .wrap_some() )); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // add 10 10 @@ -65,7 +65,7 @@ fn test_part3() { let r = Expr::Int(namely_type!("Int"), 20); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // not false @@ -86,7 +86,7 @@ fn test_part4() { .wrap_some() )); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // and false @@ -107,7 +107,7 @@ fn test_part5() { .wrap_some() )); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // and true false @@ -129,5 +129,5 @@ fn test_part6() { let r = Expr::Int(namely_type!("False"), 0); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } diff --git a/src/eval/eval_expr/case/closure/mod.rs b/src/eval/eval_expr/case/closure/mod.rs index c257dc3..8b9c629 100644 --- a/src/eval/eval_expr/case/closure/mod.rs +++ b/src/eval/eval_expr/case/closure/mod.rs @@ -5,7 +5,7 @@ use crate::eval::eval_expr::EvalRet; use crate::eval::r#type::expr::Expr; use crate::eval::r#type::r#type::Type; use crate::infra::option::WrapOption; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; #[cfg(test)] mod test; @@ -24,5 +24,5 @@ pub fn case_closure( output_expr.clone(), eval_env.clone().wrap_some() ) - .ok() + .wrap_ok() } diff --git a/src/eval/eval_expr/case/closure/test.rs b/src/eval/eval_expr/case/closure/test.rs index 773092d..3b2b024 100644 --- a/src/eval/eval_expr/case/closure/test.rs +++ b/src/eval/eval_expr/case/closure/test.rs @@ -5,7 +5,7 @@ use crate::eval::r#macro::{closure_type, namely_type}; use crate::eval::r#type::expr::Expr; use crate::infra::option::WrapOption; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // (a: Int) -> 1 #[test] @@ -23,7 +23,7 @@ fn test_part1() { let evaluated = eval_expr(&type_env, &expr_env, &expr.clone().rc()); - assert_ne!(evaluated, expr.ok()); + assert_ne!(evaluated, expr.wrap_ok()); } // (_: Int) -> 1 @@ -42,7 +42,7 @@ fn test_part2() { let evaluated = eval_expr(&type_env, &expr_env, &expr.clone().rc()); - assert_ne!(evaluated, expr.ok()); + assert_ne!(evaluated, expr.wrap_ok()); } // (a: Int) -> 1 @@ -68,7 +68,7 @@ fn test_part3() { expr_env.wrap_some() ); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // (_: Int) -> 1 @@ -94,5 +94,5 @@ fn test_part4() { expr_env.wrap_some() ); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } diff --git a/src/eval/eval_expr/case/cond/test.rs b/src/eval/eval_expr/case/cond/test.rs index 6e2328c..1848b16 100644 --- a/src/eval/eval_expr/case/cond/test.rs +++ b/src/eval/eval_expr/case/cond/test.rs @@ -4,7 +4,7 @@ use crate::eval::eval_expr::eval_expr; use crate::eval::r#macro::namely_type; use crate::eval::r#type::expr::Expr; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // if false then 10 else 20 #[test] @@ -21,7 +21,7 @@ fn test_part1() { let r = Expr::Int(namely_type!("Int"), 20); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // if true then 10 else 20 @@ -39,5 +39,5 @@ fn test_part2() { let r = Expr::Int(namely_type!("Int"), 10); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } diff --git a/src/eval/eval_expr/case/discard.rs b/src/eval/eval_expr/case/discard.rs index 300ecd4..ce69e1a 100644 --- a/src/eval/eval_expr/case/discard.rs +++ b/src/eval/eval_expr/case/discard.rs @@ -1,11 +1,11 @@ use crate::eval::eval_expr::EvalRet; use crate::eval::r#type::eval_err::EvalErr; use crate::eval::r#type::r#type::Type; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub fn case_discard(type_annot: &Type) -> EvalRet { EvalErr::EvalDiscard(format!("Trying to eval _:{type_annot:?}")) - .err() + .wrap_err() } #[cfg(test)] diff --git a/src/eval/eval_expr/case/env_ref/mod.rs b/src/eval/eval_expr/case/env_ref/mod.rs index 898e99a..4ba9b31 100644 --- a/src/eval/eval_expr/case/env_ref/mod.rs +++ b/src/eval/eval_expr/case/env_ref/mod.rs @@ -7,7 +7,7 @@ use crate::eval::env::expr_env::ExprEnv; use crate::eval::env::type_env::TypeEnv; use crate::eval::eval_expr::{eval_expr, EvalRet}; use crate::eval::r#type::eval_err::EvalErr; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub fn case_env_ref( type_env: &TypeEnv, @@ -24,6 +24,6 @@ pub fn case_env_ref( None => EvalErr::EnvRefNotFound(format!( "EnvRef::{ref_name:?} not found in expr env" )) - .err() + .wrap_err() } } diff --git a/src/eval/eval_expr/case/env_ref/test.rs b/src/eval/eval_expr/case/env_ref/test.rs index b582fce..c82103c 100644 --- a/src/eval/eval_expr/case/env_ref/test.rs +++ b/src/eval/eval_expr/case/env_ref/test.rs @@ -5,7 +5,7 @@ use crate::eval::r#macro::namely_type; use crate::eval::r#type::expr::Expr; use crate::infra::option::WrapOption; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // def a = 10 #[test] @@ -26,7 +26,7 @@ fn test_part1() { let r = Expr::Int(namely_type!("Int"), 10); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // def a = 10 @@ -57,7 +57,7 @@ fn test_part2() { let r = Expr::Int(namely_type!("Int"), 5); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // def b = 10 @@ -99,5 +99,5 @@ fn test_part3() { let r = Expr::Int(namely_type!("Int"), 10); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } diff --git a/src/eval/eval_expr/case/int.rs b/src/eval/eval_expr/case/int.rs index 91bfa2d..1064cd6 100644 --- a/src/eval/eval_expr/case/int.rs +++ b/src/eval/eval_expr/case/int.rs @@ -1,10 +1,10 @@ use crate::eval::eval_expr::EvalRet; use crate::eval::r#type::expr::Expr; use crate::eval::r#type::r#type::Type; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub fn case_int(type_annot: &Type, int_value: &i64) -> EvalRet { - Expr::Int(type_annot.clone(), *int_value).ok() + Expr::Int(type_annot.clone(), *int_value).wrap_ok() } #[cfg(test)] @@ -15,7 +15,7 @@ mod test { use crate::eval::r#macro::namely_type; use crate::eval::r#type::expr::Expr; use crate::infra::rc::RcAnyExt; - use crate::infra::result::ResultAnyExt; + use crate::infra::result::WrapResult; // 10: Int #[test] @@ -27,6 +27,6 @@ mod test { let evaluated = eval_expr(&type_env, &expr_env, &expr.clone().rc()); - assert_eq!(evaluated, expr.ok()); + assert_eq!(evaluated, expr.wrap_ok()); } } diff --git a/src/eval/eval_expr/case/let/test.rs b/src/eval/eval_expr/case/let/test.rs index b0cdf59..529c3b5 100644 --- a/src/eval/eval_expr/case/let/test.rs +++ b/src/eval/eval_expr/case/let/test.rs @@ -4,7 +4,7 @@ use crate::eval::eval_expr::eval_expr; use crate::eval::r#macro::namely_type; use crate::eval::r#type::expr::Expr; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // let a = 10 in a #[test] @@ -23,7 +23,7 @@ fn test_part1() { let r = Expr::Int(namely_type!("Int"), 10); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // let a = 20, b = 10 in a @@ -50,7 +50,7 @@ fn test_part2() { let r = Expr::Int(namely_type!("Int"), 20); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // let a = 20, b = 10, a = 5 in a @@ -85,5 +85,5 @@ fn test_part3() { let r = Expr::Int(namely_type!("Int"), 5); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } diff --git a/src/eval/eval_expr/case/match/mod.rs b/src/eval/eval_expr/case/match/mod.rs index 2dea72a..844164e 100644 --- a/src/eval/eval_expr/case/match/mod.rs +++ b/src/eval/eval_expr/case/match/mod.rs @@ -12,7 +12,7 @@ use crate::eval::r#type::eval_err::EvalErr; use crate::eval::r#type::expr::Expr; use crate::infra::option::WrapOption; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub fn case_match( type_env: &TypeEnv, @@ -44,6 +44,6 @@ pub fn case_match( EvalErr::NonExhaustiveMatch(format!( "Non-exhaustive match expr cases: {case_vec:?}" )) - .err() + .wrap_err() }) } diff --git a/src/eval/eval_expr/case/match/test.rs b/src/eval/eval_expr/case/match/test.rs index c061114..fd4ad39 100644 --- a/src/eval/eval_expr/case/match/test.rs +++ b/src/eval/eval_expr/case/match/test.rs @@ -7,7 +7,7 @@ use crate::eval::r#macro::namely_type; use crate::eval::r#type::eval_err::EvalErr; use crate::eval::r#type::expr::Expr; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // match 5 with // | 10 -> 1 @@ -37,7 +37,7 @@ fn test_part1() { let r = Expr::Int(namely_type!("Int"), 0); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // match 5 with @@ -68,7 +68,7 @@ fn test_part2() { let r = Expr::Int(namely_type!("Int"), 2); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // match 15 with @@ -101,7 +101,7 @@ fn test_part3() { let r = Expr::Int(namely_type!("Int"), 15); - assert_eq!(evaluated, r.ok()); + assert_eq!(evaluated, r.wrap_ok()); } // match 5 with diff --git a/src/eval/eval_expr/case/primitive_op/mod.rs b/src/eval/eval_expr/case/primitive_op/mod.rs index 8a43b5c..4a5a27c 100644 --- a/src/eval/eval_expr/case/primitive_op/mod.rs +++ b/src/eval/eval_expr/case/primitive_op/mod.rs @@ -4,7 +4,7 @@ use crate::eval::eval_expr::EvalRet; use crate::eval::r#type::expr::primitive_op::PrimitiveOp; use crate::eval::r#type::expr::Expr; use crate::eval::r#type::r#type::Type; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; #[cfg(test)] mod test; @@ -13,5 +13,5 @@ pub fn case_primitive_op( type_annot: &Type, op: &Rc ) -> EvalRet { - Expr::PrimitiveOp(type_annot.clone(), op.clone(), None).ok() + Expr::PrimitiveOp(type_annot.clone(), op.clone(), None).wrap_ok() } diff --git a/src/eval/eval_expr/case/primitive_op/test.rs b/src/eval/eval_expr/case/primitive_op/test.rs index 2720c14..41aa614 100644 --- a/src/eval/eval_expr/case/primitive_op/test.rs +++ b/src/eval/eval_expr/case/primitive_op/test.rs @@ -6,7 +6,7 @@ use crate::eval::r#type::expr::primitive_op::PrimitiveOp; use crate::eval::r#type::expr::Expr; use crate::infra::option::WrapOption; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // add #[test] @@ -22,7 +22,7 @@ fn test_part1() { let evaluated = eval_expr(&type_env, &expr_env, &expr.clone().rc()); - assert_eq!(evaluated, expr.ok()); + assert_eq!(evaluated, expr.wrap_ok()); } // add 1 @@ -44,7 +44,7 @@ fn test_part2() { let evaluated = eval_expr(&type_env, &expr_env, &expr.clone().rc()); - assert_eq!(evaluated, expr.ok()); + assert_eq!(evaluated, expr.wrap_ok()); } // add @@ -66,7 +66,7 @@ fn test_part3() { expr_env.wrap_some() ); - assert_ne!(evaluated, r.ok()); + assert_ne!(evaluated, r.wrap_ok()); } // add 1 @@ -98,5 +98,5 @@ fn test_part4() { expr_env.wrap_some() ); - assert_ne!(evaluated, r.ok()); + assert_ne!(evaluated, r.wrap_ok()); } diff --git a/src/eval/eval_expr/case/struct/mod.rs b/src/eval/eval_expr/case/struct/mod.rs index dfc98e8..59e1b30 100644 --- a/src/eval/eval_expr/case/struct/mod.rs +++ b/src/eval/eval_expr/case/struct/mod.rs @@ -10,7 +10,7 @@ use crate::eval::r#type::eval_err::EvalErr; use crate::eval::r#type::expr::{Expr, StructField}; use crate::eval::r#type::r#type::Type; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::infra::vec::VecExt; pub fn case_struct( @@ -27,15 +27,15 @@ pub fn case_struct( sf_t.clone(), eval_expr(type_env, expr_env, sf_e)?.rc() ) - .ok() + .wrap_ok() }) .try_fold(vec![], |acc, x| { acc.chain_push(x?) - .ok::() + .wrap_ok::() }); match struct_vec { - Ok(vec) => Expr::Struct(type_annot.clone(), vec).ok(), - Err(e) => e.err() + Ok(vec) => Expr::Struct(type_annot.clone(), vec).wrap_ok(), + Err(e) => e.wrap_err() } } diff --git a/src/eval/eval_expr/case/struct/test.rs b/src/eval/eval_expr/case/struct/test.rs index 374ac76..5bae97f 100644 --- a/src/eval/eval_expr/case/struct/test.rs +++ b/src/eval/eval_expr/case/struct/test.rs @@ -4,7 +4,7 @@ use crate::eval::eval_expr::eval_expr; use crate::eval::r#macro::{namely_type, prod_type}; use crate::eval::r#type::expr::Expr; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; // { a: Int = 10, b: Bool = true, c: Unit = ()} #[test] @@ -39,5 +39,5 @@ fn test_part1() { let evaluated = eval_expr(&type_env, &expr_env, &expr.clone().rc()); - assert_eq!(evaluated, expr.ok()); + assert_eq!(evaluated, expr.wrap_ok()); } diff --git a/src/eval/eval_expr/case/unit.rs b/src/eval/eval_expr/case/unit.rs index 7935314..b2ee3d5 100644 --- a/src/eval/eval_expr/case/unit.rs +++ b/src/eval/eval_expr/case/unit.rs @@ -1,10 +1,10 @@ use crate::eval::eval_expr::EvalRet; use crate::eval::r#type::expr::Expr; use crate::eval::r#type::r#type::Type; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; pub fn case_unit(type_annot: &Type) -> EvalRet { - Expr::Unit(type_annot.clone()).ok() + Expr::Unit(type_annot.clone()).wrap_ok() } #[cfg(test)] @@ -15,7 +15,7 @@ mod test { use crate::eval::r#macro::namely_type; use crate::eval::r#type::expr::Expr; use crate::infra::rc::RcAnyExt; - use crate::infra::result::ResultAnyExt; + use crate::infra::result::WrapResult; // (): Unit #[test] @@ -27,6 +27,6 @@ mod test { let evaluated = eval_expr(&type_env, &expr_env, &expr.clone().rc()); - assert_eq!(evaluated, expr.ok()); + assert_eq!(evaluated, expr.wrap_ok()); } } diff --git a/src/infer/infer_type/case/match/case_ri.rs b/src/infer/infer_type/case/match/case_ri.rs index 488f3f5..73690b9 100644 --- a/src/infer/infer_type/case/match/case_ri.rs +++ b/src/infer/infer_type/case/match/case_ri.rs @@ -10,7 +10,7 @@ use crate::infra::option::WrapOption; use crate::infra::quad::{Quad, QuadAnyExt}; use crate::infra::r#fn::id; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::infra::vec::VecExt; use crate::parser::expr::r#type::Expr; use crate::parser::r#type::r#type::{OptType, Type}; @@ -38,14 +38,14 @@ pub fn case_ri( type_env, case_expr ) { Ok(env_inject) => - (case_expr, env_inject, then_expr).ok(), + (case_expr, env_inject, then_expr).wrap_ok(), Err((new, old)) => TypeMissMatch::of_dup_capture(old, new) .quad_r() - .err(), + .wrap_err(), } }) - .try_fold(vec![], |acc, x| acc.chain_push(x?).ok()); + .try_fold(vec![], |acc, x| acc.chain_push(x?).wrap_ok()); match vec { Ok(vec) => vec, @@ -64,7 +64,7 @@ pub fn case_ri( Quad::L(typed_case_expr) => typed_case_expr .unwrap_type_annot() .clone() - .ok(), + .wrap_ok(), Quad::ML(rc) => // 确保 case_expr 是模式匹配意义上的常量, 原理与 case_t_rc 相同 if rc @@ -85,13 +85,17 @@ pub fn case_ri( rc.typed_expr .unwrap_type_annot() .clone() - .ok() + .wrap_ok() } else { // 虽然本质上是 case_expr 非模式匹配常量 // 但是实际上还是 target_expr 信息不足所致, 原错误返回之 - original_err.clone().err() + original_err + .clone() + .wrap_err() }, - _ => original_err.clone().err() // 原理同上 + _ => original_err + .clone() + .wrap_err() // 原理同上 } }) // 采用激进的类型推导策略 @@ -103,15 +107,20 @@ pub fn case_ri( Ok(t) => { match acc { // 对于头一个类型, 只需让它成为初始 acc 类型 - None => t.wrap_some().ok(), + None => t.wrap_some().wrap_ok(), // 对于之后的每一个类型, 让它和之前 acc 类型合一 Some(acc) => match acc.unify(type_env, &t) { - Some(new_acc) => new_acc.wrap_some().ok(), - None => original_err.clone().err() + Some(new_acc) => + new_acc.wrap_some().wrap_ok(), + None => original_err + .clone() + .wrap_err() } } } - Err(_) => original_err.clone().err() + Err(_) => original_err + .clone() + .wrap_err() } }); diff --git a/src/infer/infer_type/case/match/case_t_rc/mod.rs b/src/infer/infer_type/case/match/case_t_rc/mod.rs index 3483256..9301a52 100644 --- a/src/infer/infer_type/case/match/case_t_rc/mod.rs +++ b/src/infer/infer_type/case/match/case_t_rc/mod.rs @@ -10,7 +10,7 @@ use crate::infer::infer_type::case::r#match::r#fn::{destruct_match_const_to_expr use crate::infer::infer_type::r#type::infer_type_ret::InferTypeRet; use crate::infer::infer_type::r#type::type_miss_match::TypeMissMatch; use crate::infra::quad::QuadAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::infra::vec::VecExt; use crate::parser::r#type::r#type::OptType; use crate::parser::expr::r#type::Expr; @@ -41,14 +41,14 @@ pub fn case_t_rc( type_env, &case_expr ) { Ok(env_inject) => - (case_expr, env_inject, then_expr).ok(), + (case_expr, env_inject, then_expr).wrap_ok(), Err((new, old)) => TypeMissMatch::of_dup_capture(old, new) .quad_r() - .err(), + .wrap_err(), } }) - .try_fold(vec![], |acc, x| acc.chain_push(x?).ok()); + .try_fold(vec![], |acc, x| acc.chain_push(x?).wrap_ok()); match vec { Ok(vec) => vec, diff --git a/src/infer/infer_type/case/match/case_t_rc/on_has_expect_type.rs b/src/infer/infer_type/case/match/case_t_rc/on_has_expect_type.rs index 179cece..20f92eb 100644 --- a/src/infer/infer_type/case/match/case_t_rc/on_has_expect_type.rs +++ b/src/infer/infer_type/case/match/case_t_rc/on_has_expect_type.rs @@ -10,7 +10,7 @@ use crate::infer::infer_type::r#type::type_miss_match::TypeMissMatch; use crate::infra::option::WrapOption; use crate::infra::quad::{Quad, QuadAnyExt}; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::parser::expr::r#type::Expr; use crate::parser::r#type::r#type::Type; @@ -65,14 +65,15 @@ where if then_expr_type .can_lift_to(type_env, &expect_type) { - (typed_then_expr, outer_constraint).ok() + (typed_then_expr, outer_constraint) + .wrap_ok() } else { TypeMissMatch::of_type( then_expr_type, &expect_type ) .quad_r() - .err() + .wrap_err() } } // 同样需要去除对常量环境的约束 @@ -88,10 +89,10 @@ where }) ) .quad_mr() - .err(), + .wrap_err(), // 获取 then_expr_type 时类型不匹配 - r => r.err() + r => r.wrap_err() } } ); @@ -111,18 +112,19 @@ where // 与累积约束合并 .try_fold(EnvRefConstraint::empty(), |acc, x| match x { Ok((_, c)) => match acc.extend_new(c.clone()) { - Some(acc) => acc.ok(), - None => TypeMissMatch::of_constraint(&acc, &c).err() + Some(acc) => acc.wrap_ok(), + None => + TypeMissMatch::of_constraint(&acc, &c).wrap_err(), }, Err(Quad::MR(ri)) => match acc .extend_new(ri.constraint.clone()) { - Some(acc) => acc.ok(), + Some(acc) => acc.wrap_ok(), None => TypeMissMatch::of_constraint(&acc, &ri.constraint) - .err(), + .wrap_err(), }, - _ => acc.ok() + _ => acc.wrap_ok() }); // 如果合并约束时发生冲突, 立即返回 diff --git a/src/infer/infer_type/case/match/case_t_rc/on_no_expect_type.rs b/src/infer/infer_type/case/match/case_t_rc/on_no_expect_type.rs index 9b239b8..e4a78b9 100644 --- a/src/infer/infer_type/case/match/case_t_rc/on_no_expect_type.rs +++ b/src/infer/infer_type/case/match/case_t_rc/on_no_expect_type.rs @@ -9,7 +9,7 @@ use crate::infer::infer_type::r#type::type_miss_match::TypeMissMatch; use crate::infra::option::WrapOption; use crate::infra::quad::{Quad, QuadAnyExt}; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::parser::expr::r#type::Expr; pub fn on_no_expect_type( @@ -52,7 +52,7 @@ where ) }); - (then_expr_type, outer_constraint).ok() + (then_expr_type, outer_constraint).wrap_ok() } // 同样需要去除对常量环境的约束 Quad::MR(ri) => ReqInfo::of( @@ -67,10 +67,10 @@ where }) ) .quad_mr() - .err(), + .wrap_err(), // 获取 then_expr_type 时类型不匹配 - r => r.err() + r => r.wrap_err() } } ); @@ -89,18 +89,19 @@ where .clone() .try_fold(EnvRefConstraint::empty(), |acc, x| match x { Ok((_, c)) => match acc.extend_new(c.clone()) { - Some(acc) => acc.ok(), - None => TypeMissMatch::of_constraint(&acc, &c).err() + Some(acc) => acc.wrap_ok(), + None => + TypeMissMatch::of_constraint(&acc, &c).wrap_err(), }, Err(Quad::MR(ri)) => match acc .extend_new(ri.constraint.clone()) { - Some(acc) => acc.ok(), + Some(acc) => acc.wrap_ok(), None => TypeMissMatch::of_constraint(&acc, &ri.constraint) - .err(), + .wrap_err(), }, - _ => acc.ok() + _ => acc.wrap_ok() }); // 如果合并约束时发生冲突, 立即返回 @@ -116,10 +117,10 @@ where .filter_map(|x| x.ok()) .map(|(t, _)| t) .try_reduce(|acc, t| match acc.unify(type_env, &t) { - Some(acc) => acc.ok(), + Some(acc) => acc.wrap_ok(), None => TypeMissMatch::of_type(&acc, &t) .quad_r() - .err() + .wrap_err() }); // TODO: what the magic? diff --git a/src/infer/infer_type/case/match/fn.rs b/src/infer/infer_type/case/match/fn.rs index d1bc37f..c737358 100644 --- a/src/infer/infer_type/case/match/fn.rs +++ b/src/infer/infer_type/case/match/fn.rs @@ -11,7 +11,7 @@ use crate::infra::option::WrapOption; use crate::infra::quad::Quad; use crate::infra::r#fn::id; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::infra::triple::Triple; use crate::infra::vec::VecExt; use crate::parser::expr::r#type::Expr; @@ -82,8 +82,8 @@ pub fn destruct_match_const_to_expr_env_inject( (capture_name.clone(), tc, src), (capture_name, old_tc, old_src) ) - .err(), - None => acc.ok() + .wrap_err(), + None => acc.wrap_ok() } ) .map(|hash_map| { @@ -173,11 +173,11 @@ pub fn is_case_expr_valid<'t>( } }) .try_fold(vec![], |acc, x| match x { - Quad::L(e) => acc.chain_push(e).ok(), + Quad::L(e) => acc.chain_push(e).wrap_ok(), Quad::ML(rc) => acc .chain_push(rc.typed_expr) - .ok(), + .wrap_ok(), Quad::MR(_) => unreachable!(), - Quad::R(err) => err.err() + Quad::R(err) => err.wrap_err() }) } diff --git a/src/infer/infer_type/case/struct/fn.rs b/src/infer/infer_type/case/struct/fn.rs index a8d5f18..a3c1a93 100644 --- a/src/infer/infer_type/case/struct/fn.rs +++ b/src/infer/infer_type/case/struct/fn.rs @@ -5,7 +5,7 @@ use crate::infer::infer_type::r#type::type_miss_match::TypeMissMatch; use crate::infra::option::WrapOption; use crate::infra::quad::QuadAnyExt; use crate::infra::r#fn::id; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::parser::expr::r#type::Expr; use crate::parser::r#type::r#type::Type; use crate::parser::r#type::r#type::{OptType, ProdField}; @@ -46,7 +46,7 @@ pub fn is_struct_vec_of_type_then_get_prod_vec( }) .all(id)) .then_some(prod_vec) - .ok(), + .wrap_ok(), Some(Type::SumType(sum_vec)) => sum_vec .into_iter() @@ -63,20 +63,20 @@ pub fn is_struct_vec_of_type_then_get_prod_vec( format!( "{expect_type:?} <> type of Struct{struct_vec:?}" )).quad_r() - .err() + .wrap_err() ), Some(t) => TypeMissMatch::of(format!( "{t:?} <> type of Struct{struct_vec:?}" )).quad_r() - .err(), + .wrap_err(), None => TypeMissMatch::of(format!( "{expect_type:?} not found in type env" )).quad_r() - .err() + .wrap_err() } } else { - None.ok() + None.wrap_ok() } } diff --git a/src/infer/infer_type/case/struct/mod.rs b/src/infer/infer_type/case/struct/mod.rs index bc51d08..43ecd74 100644 --- a/src/infer/infer_type/case/struct/mod.rs +++ b/src/infer/infer_type/case/struct/mod.rs @@ -13,7 +13,7 @@ use crate::infer::infer_type::r#type::require_info::ReqInfo; use crate::infer::infer_type::r#type::type_miss_match::TypeMissMatch; use crate::infra::option::WrapOption; use crate::infra::quad::Quad; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::parser::expr::r#type::{Expr, StructField}; use crate::parser::r#type::r#type::OptType; use crate::parser::r#type::r#type::Type; @@ -86,27 +86,28 @@ pub fn case( let sf_t = typed_sf_e .unwrap_type_annot() .clone(); - (sf_n, sf_t, constraint, typed_sf_e).ok() + (sf_n, sf_t, constraint, typed_sf_e).wrap_ok() } - mr => mr.err() + mr => mr.wrap_err() }); let outer_constraint = sf_n_and_sf_t_with_constraint_and_expr .clone() .try_fold(EnvRefConstraint::empty(), |acc, x| match x { Ok((.., c, _)) => match acc.extend_new(c.clone()) { - Some(acc) => acc.ok(), - None => TypeMissMatch::of_constraint(&acc, &c).err() + Some(acc) => acc.wrap_ok(), + None => + TypeMissMatch::of_constraint(&acc, &c).wrap_err(), }, Err(Quad::MR(ri)) => match acc .extend_new(ri.constraint.clone()) { - Some(acc) => acc.ok(), + Some(acc) => acc.wrap_ok(), None => TypeMissMatch::of_constraint(&acc, &ri.constraint) - .err(), + .wrap_err(), }, - _ => acc.ok() + _ => acc.wrap_ok() }); // 如果合并约束时发生冲突, 立即返回 diff --git a/src/infer/infer_type_of_defs.rs b/src/infer/infer_type_of_defs.rs index b2ef34a..23c340c 100644 --- a/src/infer/infer_type_of_defs.rs +++ b/src/infer/infer_type_of_defs.rs @@ -8,7 +8,7 @@ use crate::infer::infer_type::r#type::env_ref_constraint::EnvRefConstraint; use crate::infer::infer_type::r#type::type_miss_match::TypeMissMatch; use crate::infra::quad::Quad; use crate::infra::rc::RcAnyExt; -use crate::infra::result::ResultAnyExt; +use crate::infra::result::WrapResult; use crate::infra::vec::VecExt; enum EntryAction { @@ -62,7 +62,7 @@ pub fn infer_type_of_defs( entry, EnvRefConstraint::empty() ) - .ok() + .wrap_ok() } Quad::ML(rc) => { let entry = { @@ -79,13 +79,14 @@ pub fn infer_type_of_defs( (n, tc, EnvRefSrc::Src(rc.typed_expr)) }; - EntryAction::Remove(entry, rc.constraint).ok() + EntryAction::Remove(entry, rc.constraint) + .wrap_ok() } Quad::MR(ri) => { let entry = (n, tc, src); - EntryAction::Keep(entry, ri.constraint).ok() + EntryAction::Keep(entry, ri.constraint).wrap_ok() } - Quad::R(e) => InferErr::of(e.info).err() + Quad::R(e) => InferErr::of(e.info).wrap_err() }, EnvRefSrc::NoSrc => match tc { TypeConstraint::Constraint(_) => { @@ -94,7 +95,7 @@ pub fn infer_type_of_defs( entry, EnvRefConstraint::empty() ) - .ok() + .wrap_ok() } TypeConstraint::Free => { let entry = (n, tc, src); @@ -102,7 +103,7 @@ pub fn infer_type_of_defs( entry, EnvRefConstraint::empty() ) - .ok() + .wrap_ok() } } }) @@ -114,7 +115,7 @@ pub fn infer_type_of_defs( let constraint_acc = match constraint_acc .extend_new(c.clone()) { - Some(c) => c.ok(), + Some(c) => c.wrap_ok(), None => InferErr::of( TypeMissMatch::of_constraint( &constraint_acc, @@ -122,7 +123,7 @@ pub fn infer_type_of_defs( ) .info ) - .err() + .wrap_err() }?; ( @@ -130,13 +131,13 @@ pub fn infer_type_of_defs( inferred, constraint_acc ) - .ok() + .wrap_ok() } EntryAction::Remove(entry, c) => { let constraint_acc = match constraint_acc .extend_new(c.clone()) { - Some(c) => c.ok(), + Some(c) => c.wrap_ok(), None => InferErr::of( TypeMissMatch::of_constraint( &constraint_acc, @@ -144,7 +145,7 @@ pub fn infer_type_of_defs( ) .info ) - .err() + .wrap_err() }?; ( @@ -152,20 +153,20 @@ pub fn infer_type_of_defs( inferred.chain_push(entry), constraint_acc ) - .ok() + .wrap_ok() } }, - Err(e) => e.err() + Err(e) => e.wrap_err() } )?; if need_to_infer.is_empty() { // 当没有 def 需要推导时 // 此时 constraint_acc 一定为空, 因为不存在类型不确定的 def 可供约束 - inferred.ok() + inferred.wrap_ok() } else if constraint_acc.is_empty() { // 仍有 def 需要推导, 但本轮次并未产生新的约束 - InferErr::of("Need info to infer defs").err() + InferErr::of("Need info to infer defs").wrap_err() } else { // 仍有 def 需要推导, 且本轮次产生了新的约束 // 将已推导出类型的 def 和约束合并到环境, 进行下一轮推导 @@ -220,6 +221,6 @@ pub fn infer_type_of_defs( vec![inferred, inferred_from_next_round] .concat() - .ok() + .wrap_ok() } } diff --git a/src/infra/result.rs b/src/infra/result.rs index c94727e..160ee77 100644 --- a/src/infra/result.rs +++ b/src/infra/result.rs @@ -1,11 +1,11 @@ -pub trait ResultAnyExt +pub trait WrapResult where Self: Sized { #[inline] - fn ok(self) -> Result { Ok(self) } + fn wrap_ok(self) -> Result { Ok(self) } #[inline] - fn err(self) -> Result { Err(self) } + fn wrap_err(self) -> Result { Err(self) } } -impl ResultAnyExt for T {} +impl WrapResult for T {}