add dedicated RecordUpdate tag to parse ast

This commit is contained in:
Folkert 2021-03-20 16:06:07 +01:00
parent 0e7106280c
commit 60265b5d2a
5 changed files with 97 additions and 0 deletions

View file

@ -284,6 +284,54 @@ pub fn canonicalize_expr<'a>(
}
}
}
ast::Expr::RecordUpdate {
fields,
update: loc_update,
final_comments: _,
} => {
let (can_update, update_out) =
canonicalize_expr(env, var_store, scope, loc_update.region, &loc_update.value);
if let Var(symbol) = &can_update.value {
match canonicalize_fields(env, var_store, scope, region, fields) {
Ok((can_fields, mut output)) => {
output.references = output.references.union(update_out.references);
let answer = Update {
record_var: var_store.fresh(),
ext_var: var_store.fresh(),
symbol: *symbol,
updates: can_fields,
};
(answer, output)
}
Err(CanonicalizeRecordProblem::InvalidOptionalValue {
field_name,
field_region,
record_region,
}) => (
Expr::RuntimeError(roc_problem::can::RuntimeError::InvalidOptionalValue {
field_name,
field_region,
record_region,
}),
Output::default(),
),
}
} else {
// only (optionally qualified) variables can be updated, not arbitrary expressions
let error = roc_problem::can::RuntimeError::InvalidRecordUpdate {
region: can_update.region,
};
let answer = Expr::RuntimeError(error.clone());
env.problems.push(Problem::RuntimeError(error));
(answer, Output::default())
}
}
ast::Expr::Str(literal) => flatten_str_literal(env, var_store, scope, literal),
ast::Expr::List {
items: loc_elems, ..