basic canonicalization

This commit is contained in:
Folkert 2022-08-09 13:59:24 +02:00
parent 15665d612d
commit bcbc8b4d50
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
7 changed files with 74 additions and 3 deletions

View file

@ -279,6 +279,7 @@ fn to_pending_def<'a>(
Type(TypeDef::Ability { .. }) => todo_abilities!(),
Value(AstValueDef::Expect { .. }) => todo!(),
Value(AstValueDef::ExpectFx { .. }) => todo!(),
SpaceBefore(sub_def, _) | SpaceAfter(sub_def, _) => {
to_pending_def(env, sub_def, scope, pattern_type)

View file

@ -106,6 +106,12 @@ impl Expects {
preceding_comment: Vec::with_capacity(capacity),
}
}
fn push(&mut self, loc_can_condition: Loc<Expr>, preceding_comment: Region) {
self.conditions.push(loc_can_condition.value);
self.regions.push(loc_can_condition.region);
self.preceding_comment.push(preceding_comment);
}
}
/// A Def that has had patterns and type annnotations canonicalized,
@ -907,6 +913,7 @@ fn canonicalize_value_defs<'a>(
// once we've finished assembling the entire scope.
let mut pending_value_defs = Vec::with_capacity(value_defs.len());
let mut pending_expects = Vec::with_capacity(value_defs.len());
let mut pending_expect_fx = Vec::with_capacity(value_defs.len());
for loc_pending_def in value_defs {
match loc_pending_def.value {
@ -921,6 +928,10 @@ fn canonicalize_value_defs<'a>(
PendingValue::Expect(pending_expect) => {
pending_expects.push(pending_expect);
}
PendingValue::ExpectFx(pending_expect) => {
pending_expect_fx.push(pending_expect);
}
}
}
@ -979,6 +990,7 @@ fn canonicalize_value_defs<'a>(
}
let mut expects = Expects::with_capacity(pending_expects.len());
let mut expect_fx = Expects::with_capacity(pending_expects.len());
for pending in pending_expects {
let (loc_can_condition, can_output) = canonicalize_expr(
@ -989,9 +1001,7 @@ fn canonicalize_value_defs<'a>(
&pending.condition.value,
);
expects.conditions.push(loc_can_condition.value);
expects.regions.push(loc_can_condition.region);
expects.preceding_comment.push(pending.preceding_comment);
expects.push(loc_can_condition, pending.preceding_comment);
output.union(can_output);
}
@ -2391,6 +2401,7 @@ fn to_pending_type_def<'a>(
enum PendingValue<'a> {
Def(PendingValueDef<'a>),
Expect(PendingExpect<'a>),
ExpectFx(PendingExpect<'a>),
SignatureDefMismatch,
}
@ -2503,6 +2514,14 @@ fn to_pending_value_def<'a>(
condition,
preceding_comment: *preceding_comment,
}),
ExpectFx {
condition,
preceding_comment,
} => PendingValue::Expect(PendingExpect {
condition,
preceding_comment: *preceding_comment,
}),
}
}

View file

@ -92,6 +92,16 @@ fn desugar_value_def<'a>(arena: &'a Bump, def: &'a ValueDef<'a>) -> ValueDef<'a>
preceding_comment: *preceding_comment,
}
}
ExpectFx {
condition,
preceding_comment,
} => {
let desugared_condition = &*arena.alloc(desugar_expr(arena, condition));
ExpectFx {
condition: desugared_condition,
preceding_comment: *preceding_comment,
}
}
}
}

View file

@ -159,6 +159,7 @@ impl<'a> Formattable for ValueDef<'a> {
Body(loc_pattern, loc_expr) => loc_pattern.is_multiline() || loc_expr.is_multiline(),
AnnotatedBody { .. } => true,
Expect { condition, .. } => condition.is_multiline(),
ExpectFx { condition, .. } => condition.is_multiline(),
}
}
@ -233,6 +234,9 @@ impl<'a> Formattable for ValueDef<'a> {
fmt_body(buf, &loc_pattern.value, &loc_expr.value, indent);
}
Expect { condition, .. } => fmt_expect(buf, condition, self.is_multiline(), indent),
ExpectFx { condition, .. } => {
fmt_expect_fx(buf, condition, self.is_multiline(), indent)
}
AnnotatedBody {
ann_pattern,
ann_type,
@ -303,6 +307,27 @@ fn fmt_expect<'a, 'buf>(
condition.format(buf, return_indent);
}
fn fmt_expect_fx<'a, 'buf>(
buf: &mut Buf<'buf>,
condition: &'a Loc<Expr<'a>>,
is_multiline: bool,
indent: u16,
) {
buf.ensure_ends_with_newline();
buf.indent(indent);
buf.push_str("expect-fx");
let return_indent = if is_multiline {
buf.newline();
indent + INDENT
} else {
buf.spaces(1);
indent
};
condition.format(buf, return_indent);
}
pub fn fmt_value_def<'a, 'buf>(
buf: &mut Buf<'buf>,
def: &roc_parse::ast::ValueDef<'a>,

View file

@ -547,6 +547,13 @@ impl<'a> RemoveSpaces<'a> for ValueDef<'a> {
condition: arena.alloc(condition.remove_spaces(arena)),
preceding_comment,
},
ExpectFx {
condition,
preceding_comment,
} => ExpectFx {
condition: arena.alloc(condition.remove_spaces(arena)),
preceding_comment,
},
}
}
}

View file

@ -203,6 +203,10 @@ fn generate_entry_docs<'a>(
ValueDef::Expect { .. } => {
// Don't generate docs for `expect`s
}
ValueDef::ExpectFx { .. } => {
// Don't generate docs for `expect-fx`s
}
},
Ok(type_index) => match &defs.type_defs[type_index.index()] {
TypeDef::Alias {

View file

@ -335,6 +335,11 @@ pub enum ValueDef<'a> {
condition: &'a Loc<Expr<'a>>,
preceding_comment: Region,
},
ExpectFx {
condition: &'a Loc<Expr<'a>>,
preceding_comment: Region,
},
}
#[derive(Debug, Clone, PartialEq, Default)]