Skip to content

Commit

Permalink
rename ResultAnyExt
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Nov 18, 2023
1 parent 0901c1a commit 734863a
Show file tree
Hide file tree
Showing 29 changed files with 174 additions and 157 deletions.
27 changes: 14 additions & 13 deletions src/eval/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +63,7 @@ fn ct_expr_env_vec_to_rt_expr_env_vec(
_ => unreachable!()
})
.collect::<Vec<ExprEnvEntry>>()
.ok()
.wrap_ok()
}

fn def_map_to_env_vec(
Expand All @@ -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<TypeEnvEntry> =
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<TypeEnvEntry> = 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>(
Expand Down
10 changes: 6 additions & 4 deletions src/eval/eval_expr/case/apply/fn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,9 +18,10 @@ pub fn eval_to_bool(
expr: &Rc<Expr>
) -> Result<bool, EvalErr> {
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!()
}
}
Expand All @@ -31,7 +32,8 @@ pub fn eval_to_int(
expr: &Rc<Expr>
) -> Result<i64, EvalErr> {
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!()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/eval/eval_expr/case/apply/fn/primitive_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -96,5 +96,5 @@ pub fn primitive_apply(
PrimitiveOp::Or(Some(e)) =>
bool_expr(lhs_bool(e)? || rhs_bool()?),
}
.ok()
.wrap_ok()
}
6 changes: 3 additions & 3 deletions src/eval/eval_expr/case/apply/fn/source_lhs_to_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -53,7 +53,7 @@ pub fn source_lhs_expr_to_closure(
.unwrap_or(expr_env.clone())
)
.r()
.ok(),
.wrap_ok(),

// 由于现在 Closure 和 PrimitiveOp 会捕获环境
// 所以可以对 lhs_expr 进行自由求值
Expand Down
14 changes: 7 additions & 7 deletions src/eval/eval_expr/case/apply/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -43,7 +43,7 @@ fn test_part2() {
.wrap_some()
));

assert_eq!(evaluated, r.ok());
assert_eq!(evaluated, r.wrap_ok());
}

// add 10 10
Expand All @@ -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
Expand All @@ -86,7 +86,7 @@ fn test_part4() {
.wrap_some()
));

assert_eq!(evaluated, r.ok());
assert_eq!(evaluated, r.wrap_ok());
}

// and false
Expand All @@ -107,7 +107,7 @@ fn test_part5() {
.wrap_some()
));

assert_eq!(evaluated, r.ok());
assert_eq!(evaluated, r.wrap_ok());
}

// and true false
Expand All @@ -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());
}
4 changes: 2 additions & 2 deletions src/eval/eval_expr/case/closure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,5 +24,5 @@ pub fn case_closure(
output_expr.clone(),
eval_env.clone().wrap_some()
)
.ok()
.wrap_ok()
}
10 changes: 5 additions & 5 deletions src/eval/eval_expr/case/closure/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -68,7 +68,7 @@ fn test_part3() {
expr_env.wrap_some()
);

assert_eq!(evaluated, r.ok());
assert_eq!(evaluated, r.wrap_ok());
}

// (_: Int) -> 1
Expand All @@ -94,5 +94,5 @@ fn test_part4() {
expr_env.wrap_some()
);

assert_eq!(evaluated, r.ok());
assert_eq!(evaluated, r.wrap_ok());
}
6 changes: 3 additions & 3 deletions src/eval/eval_expr/case/cond/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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());
}
4 changes: 2 additions & 2 deletions src/eval/eval_expr/case/discard.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/eval/eval_expr/case/env_ref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,6 +24,6 @@ pub fn case_env_ref(
None => EvalErr::EnvRefNotFound(format!(
"EnvRef::{ref_name:?} not found in expr env"
))
.err()
.wrap_err()
}
}
8 changes: 4 additions & 4 deletions src/eval/eval_expr/case/env_ref/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
8 changes: 4 additions & 4 deletions src/eval/eval_expr/case/int.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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]
Expand All @@ -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());
}
}
Loading

0 comments on commit 734863a

Please sign in to comment.