add basic Dbg desugaring and LowLevelDbg

This commit is contained in:
Brendan Hansknecht 2023-11-28 18:10:36 -08:00
parent 3f276b6018
commit b7f72eff86
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
12 changed files with 84 additions and 30 deletions

View file

@ -670,12 +670,12 @@ fn deep_copy_expr_help<C: CopyEnv>(env: &mut C, copied: &mut Vec<Variable>, expr
},
Dbg {
loc_condition,
loc_message,
loc_continuation,
variable,
symbol,
} => Dbg {
loc_condition: Box::new(loc_condition.map(|e| go_help!(e))),
loc_message: Box::new(loc_message.map(|e| go_help!(e))),
loc_continuation: Box::new(loc_continuation.map(|e| go_help!(e))),
variable: sub!(*variable),
symbol: *symbol,

View file

@ -269,7 +269,7 @@ pub enum Expr {
},
Dbg {
loc_condition: Box<Loc<Expr>>,
loc_message: Box<Loc<Expr>>,
loc_continuation: Box<Loc<Expr>>,
variable: Variable,
symbol: Symbol,
@ -1246,11 +1246,14 @@ pub fn canonicalize_expr<'a>(
output,
)
}
ast::Expr::Dbg(condition, continuation) => {
ast::Expr::Dbg(_, _) => {
internal_error!("Dbg should have been desugared by now")
}
ast::Expr::LowLevelDbg(message, continuation) => {
let mut output = Output::default();
let (loc_condition, output1) =
canonicalize_expr(env, var_store, scope, condition.region, &condition.value);
let (loc_message, output1) =
canonicalize_expr(env, var_store, scope, message.region, &message.value);
let (loc_continuation, output2) = canonicalize_expr(
env,
@ -1263,17 +1266,17 @@ pub fn canonicalize_expr<'a>(
output.union(output1);
output.union(output2);
// the symbol is used to bind the condition `x = condition`, and identify this `dbg`.
// the symbol is used to bind the message `x = message`, and identify this `dbg`.
// That would cause issues if we dbg a variable, like `dbg y`, because in the IR we
// cannot alias variables. Hence, we make the dbg use that same variable `y`
let symbol = match &loc_condition.value {
let symbol = match &loc_message.value {
Expr::Var(symbol, _) => *symbol,
_ => scope.gen_unique_symbol(),
};
(
Dbg {
loc_condition: Box::new(loc_condition),
loc_message: Box::new(loc_message),
loc_continuation: Box::new(loc_continuation),
variable: var_store.fresh(),
symbol,
@ -2094,14 +2097,14 @@ pub fn inline_calls(var_store: &mut VarStore, expr: Expr) -> Expr {
}
Dbg {
loc_condition,
loc_message,
loc_continuation,
variable,
symbol,
} => {
let loc_condition = Loc {
region: loc_condition.region,
value: inline_calls(var_store, loc_condition.value),
let loc_message = Loc {
region: loc_message.region,
value: inline_calls(var_store, loc_message.value),
};
let loc_continuation = Loc {
@ -2110,7 +2113,7 @@ pub fn inline_calls(var_store: &mut VarStore, expr: Expr) -> Expr {
};
Dbg {
loc_condition: Box::new(loc_condition),
loc_message: Box::new(loc_message),
loc_continuation: Box::new(loc_continuation),
variable,
symbol,
@ -2395,6 +2398,7 @@ pub fn is_valid_interpolation(expr: &ast::Expr<'_>) -> bool {
| ast::Expr::MalformedClosure => true,
// Newlines are disallowed inside interpolation, and these all require newlines
ast::Expr::Dbg(_, _)
| ast::Expr::LowLevelDbg(_, _)
| ast::Expr::Defs(_, _)
| ast::Expr::Expect(_, _)
| ast::Expr::When(_, _)
@ -3328,7 +3332,7 @@ impl crate::traverse::Visitor for ExpectCollector {
.insert(loc_condition.region, lookups_in_cond.to_vec());
}
Expr::Dbg {
loc_condition,
loc_message,
variable,
symbol,
..
@ -3336,7 +3340,7 @@ impl crate::traverse::Visitor for ExpectCollector {
let lookup = DbgLookup {
symbol: *symbol,
var: *variable,
region: loc_condition.region,
region: loc_message.region,
ability_info: None,
};

View file

@ -996,7 +996,7 @@ fn fix_values_captured_in_closure_expr(
..
}
| Dbg {
loc_condition,
loc_message: loc_condition,
loc_continuation,
..
} => {

View file

@ -461,13 +461,46 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc<Expr<'a>>) -> &'a Loc
})
}
Dbg(condition, continuation) => {
let desugared_condition = &*arena.alloc(desugar_expr(arena, condition));
let desugared_continuation = &*arena.alloc(desugar_expr(arena, continuation));
let region = condition.region;
// TODO desugar this in canonicalization instead, so we can work
// in terms of integers exclusively and not need to create strings
// which canonicalization then needs to look up, check if they're exposed, etc
let inspect = Var {
module_name: ModuleName::INSPECT,
ident: "inspect",
};
let loc_inspect_fn_var = arena.alloc(Loc {
value: inspect,
region,
});
let desugared_inspect_args = arena.alloc([desugar_expr(arena, condition)]);
let inspector = arena.alloc(Loc {
value: Apply(loc_inspect_fn_var, desugared_inspect_args, CalledVia::Space),
region,
});
let to_dbg_str = Var {
module_name: ModuleName::INSPECT,
ident: "toDbgStr",
};
let loc_to_dbg_str_fn_var = arena.alloc(Loc {
value: to_dbg_str,
region,
});
let to_dbg_str_args = arena.alloc([&*inspector]);
let dbg_str = arena.alloc(Loc {
value: Apply(loc_to_dbg_str_fn_var, to_dbg_str_args, CalledVia::Space),
region,
});
arena.alloc(Loc {
value: Dbg(desugared_condition, desugared_continuation),
value: LowLevelDbg(dbg_str, desugared_continuation),
region: loc_expr.region,
})
}
LowLevelDbg(_, _) => unreachable!("Only exists after desugaring"),
}
}

View file

@ -387,11 +387,11 @@ pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr, var: Variable) {
}
Expr::Dbg {
variable,
loc_condition,
loc_message,
loc_continuation,
symbol: _,
} => {
visitor.visit_expr(&loc_condition.value, loc_condition.region, *variable);
visitor.visit_expr(&loc_message.value, loc_message.region, *variable);
visitor.visit_expr(
&loc_continuation.value,
loc_continuation.region,