Skip to content

Commit

Permalink
Fix enum bug
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalthaj committed Oct 7, 2024
1 parent 9322ca3 commit e4dfa87
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
63 changes: 62 additions & 1 deletion golem-rib/src/compiler/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ pub fn desugar_pattern_match(
}
}

internal::build_expr_from(if_else_branches).map(|expr| expr.add_infer_type(expr_type))
let str = internal::build_expr_from(if_else_branches).map(|expr| expr.add_infer_type(expr_type));
dbg!(str.clone().unwrap().to_string());
str
}

mod internal {
use crate::{ArmPattern, Expr, InferredType, MatchArm, VariableId};
use crate::call_type::CallType;

pub(crate) fn build_expr_from(if_branches: Vec<IfThenBranch>) -> Option<Expr> {
if let Some(branch) = if_branches.first() {
Expand Down Expand Up @@ -154,6 +157,8 @@ mod internal {
resolution: &Expr,
tag: Option<Expr>,
) -> Option<IfThenBranch> {
dbg!(tag.clone());
dbg!(arm_pattern_expr.clone());
match arm_pattern_expr {
Expr::Identifier(identifier, inferred_type) => {
let assign_var = Expr::Let(
Expand All @@ -172,6 +177,35 @@ mod internal {
Some(branch)
}

Expr::Call(CallType::EnumConstructor(name), _, _) => {
let cond = if let Some(t) = tag {
Expr::and(t, Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(name)),
)
} else {
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(name),
)
};

let branch = IfThenBranch {
condition: cond,
body: resolution.clone(),
};
Some(branch)
}


Expr::Call(CallType::EnumConstructor(name), _, _) => {
dbg!("here??");
let branch = IfThenBranch {
condition: Expr::equal_to(pred_expr.clone(), arm_pattern_expr.clone()),
body: resolution.clone(),
};
Some(branch)
}

_ => {
let branch = IfThenBranch {
Expand Down Expand Up @@ -281,6 +315,33 @@ mod internal {
}
}

InferredType::Enum(enum_cases) => {
let arg_pattern_opt = bind_patterns.first();

let inner_type = &enum_cases
.iter()
.find(|case_name| *case_name == constructor_name);


let cond = if let Some(t) = tag {
Expr::and(t, Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
))
} else {
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
)
};

Some(IfThenBranch {
condition:cond,
body: resolution.clone(),
})

}

// some(some(x)) =>
InferredType::Option(inner) if constructor_name == "some" => {

Expand Down
3 changes: 2 additions & 1 deletion golem-rib/src/interpreter/rib_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,8 @@ mod internal {
},
None => "err".to_string(),
},
_ => "untagged".to_string(),
TypeAnnotatedValue::Enum(enum_) => enum_.value,
_ => "untagged".to_string()
};

interpreter_stack.push_val(TypeAnnotatedValue::Str(tag));
Expand Down
11 changes: 11 additions & 0 deletions golem-rib/src/interpreter/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ mod comprehensive_test {

let expr = r#"
let str2: str = request.headers.name;
let none_str_response = function-some-enum-response(str2);
match none_str_response {
some(enum-a) => "a",
none => "not found"
}
"#;
let expr2 = r#"
let str1: str = request.body.name;
let str2: str = request.headers.name;
let str3: str = request.path.name;
Expand Down

0 comments on commit e4dfa87

Please sign in to comment.