chore: assignment patterns refactor tuple into struct

This commit is contained in:
rvcas 2024-03-11 18:53:29 -04:00 committed by Lucas
parent b6b52ba508
commit 97247ce949
24 changed files with 205 additions and 161 deletions

View File

@ -1436,6 +1436,27 @@ impl Default for Bls12_381Point {
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct AssignmentPattern {
pub pattern: UntypedPattern,
pub annotation: Option<Annotation>,
}
impl AssignmentPattern {
pub fn new(pattern: UntypedPattern, annotation: Option<Annotation>) -> AssignmentPattern {
Self {
pattern,
annotation,
}
}
}
impl From<AssignmentPattern> for Vec1<AssignmentPattern> {
fn from(value: AssignmentPattern) -> Self {
Vec1::new(value)
}
}
pub type UntypedAssignmentKind = AssignmentKind<bool>;
pub type TypedAssignmentKind = AssignmentKind<()>;

View File

@ -1,10 +1,10 @@
use crate::{
ast::{
self, Annotation, Arg, ArgName, BinOp, Bls12_381Point, ByteArrayFormatPreference, CallArg,
Curve, DataType, DataTypeKey, DefinitionLocation, IfBranch, Located, LogicalOpChainKind,
ParsedCallArg, Pattern, RecordConstructorArg, RecordUpdateSpread, Span, TraceKind,
TypedAssignmentKind, TypedClause, TypedDataType, TypedRecordUpdateArg, UnOp,
UntypedAssignmentKind, UntypedClause, UntypedPattern, UntypedRecordUpdateArg,
self, Annotation, Arg, ArgName, AssignmentPattern, BinOp, Bls12_381Point,
ByteArrayFormatPreference, CallArg, Curve, DataType, DataTypeKey, DefinitionLocation,
IfBranch, Located, LogicalOpChainKind, ParsedCallArg, Pattern, RecordConstructorArg,
RecordUpdateSpread, Span, TraceKind, TypedAssignmentKind, TypedClause, TypedDataType,
TypedRecordUpdateArg, UnOp, UntypedAssignmentKind, UntypedClause, UntypedRecordUpdateArg,
},
builtins::void,
parser::token::Base,
@ -518,7 +518,7 @@ pub enum UntypedExpr {
Assignment {
location: Span,
value: Box<Self>,
patterns: Vec1<(UntypedPattern, Option<Annotation>)>,
patterns: Vec1<AssignmentPattern>,
kind: UntypedAssignmentKind,
},

View File

@ -1,12 +1,12 @@
use crate::{
ast::{
Annotation, Arg, ArgName, ArgVia, AssignmentKind, BinOp, ByteArrayFormatPreference,
CallArg, ClauseGuard, Constant, CurveType, DataType, Definition, Function, IfBranch,
LogicalOpChainKind, ModuleConstant, Pattern, RecordConstructor, RecordConstructorArg,
RecordUpdateSpread, Span, TraceKind, TypeAlias, TypedArg, UnOp, UnqualifiedImport,
UntypedArg, UntypedArgVia, UntypedAssignmentKind, UntypedClause, UntypedClauseGuard,
UntypedDefinition, UntypedFunction, UntypedModule, UntypedPattern, UntypedRecordUpdateArg,
Use, Validator, CAPTURE_VARIABLE,
Annotation, Arg, ArgName, ArgVia, AssignmentKind, AssignmentPattern, BinOp,
ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, CurveType, DataType, Definition,
Function, IfBranch, LogicalOpChainKind, ModuleConstant, Pattern, RecordConstructor,
RecordConstructorArg, RecordUpdateSpread, Span, TraceKind, TypeAlias, TypedArg, UnOp,
UnqualifiedImport, UntypedArg, UntypedArgVia, UntypedAssignmentKind, UntypedClause,
UntypedClauseGuard, UntypedDefinition, UntypedFunction, UntypedModule, UntypedPattern,
UntypedRecordUpdateArg, Use, Validator, CAPTURE_VARIABLE,
},
docvec,
expr::{FnStyle, UntypedExpr, DEFAULT_ERROR_STR, DEFAULT_TODO_STR},
@ -679,7 +679,7 @@ impl<'comments> Formatter<'comments> {
fn assignment<'a>(
&mut self,
patterns: &'a Vec1<(UntypedPattern, Option<Annotation>)>,
patterns: &'a Vec1<AssignmentPattern>,
value: &'a UntypedExpr,
kind: UntypedAssignmentKind,
) -> Document<'a> {
@ -691,12 +691,13 @@ impl<'comments> Formatter<'comments> {
let symbol = if kind.is_backpassing() { "<-" } else { "=" };
match patterns.first() {
(
UntypedPattern::Constructor {
name, module: None, ..
},
AssignmentPattern {
pattern:
UntypedPattern::Constructor {
name, module: None, ..
},
annotation,
) if name == "True"
} if name == "True"
&& annotation.is_none()
&& kind.is_expect()
&& patterns.len() == 1 =>
@ -704,17 +705,22 @@ impl<'comments> Formatter<'comments> {
keyword.to_doc().append(self.case_clause_value(value))
}
_ => {
let patterns = patterns.into_iter().map(|(pattern, annotation)| {
self.pop_empty_lines(pattern.location().end);
let patterns = patterns.into_iter().map(
|AssignmentPattern {
pattern,
annotation,
}| {
self.pop_empty_lines(pattern.location().end);
let pattern = self.pattern(pattern);
let pattern = self.pattern(pattern);
let annotation = annotation
.as_ref()
.map(|a| ": ".to_doc().append(self.annotation(a)));
let annotation = annotation
.as_ref()
.map(|a| ": ".to_doc().append(self.annotation(a)));
pattern.append(annotation).group()
});
pattern.append(annotation).group()
},
);
keyword
.to_doc()

View File

@ -15,8 +15,8 @@ Test(
name: "False",
},
patterns: [
(
Constructor {
AssignmentPattern {
pattern: Constructor {
is_record: false,
location: 38..42,
name: "True",
@ -26,8 +26,8 @@ Test(
with_spread: false,
tipo: (),
},
None,
),
annotation: None,
},
],
kind: Expect {
backpassing: false,

View File

@ -26,13 +26,13 @@ Fn(
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 17..18,
name: "x",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -12,6 +12,10 @@ pub fn let_(
.ignore_then(
pattern()
.then(just(Token::Colon).ignore_then(annotation()).or_not())
.map(|(pattern, annotation)| ast::AssignmentPattern {
pattern,
annotation,
})
.separated_by(just(Token::Comma))
.at_least(1),
)
@ -44,6 +48,10 @@ pub fn expect(
.ignore_then(
pattern()
.then(just(Token::Colon).ignore_then(annotation()).or_not())
.map(|(pattern, annotation)| ast::AssignmentPattern {
pattern,
annotation,
})
.separated_by(just(Token::Comma))
.at_least(1)
.then(choice((just(Token::Equal), just(Token::LArrow))))
@ -57,8 +65,8 @@ pub fn expect(
let (patterns, kind) = opt_pattern.unwrap_or_else(|| {
(
vec![(
ast::UntypedPattern::Constructor {
vec![ast::AssignmentPattern {
pattern: ast::UntypedPattern::Constructor {
is_record: false,
location: span,
name: "True".to_string(),
@ -68,8 +76,8 @@ pub fn expect(
with_spread: false,
tipo: (),
},
None,
)],
annotation: None,
}],
Token::Equal,
)
});

View File

@ -17,13 +17,13 @@ Assignment {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 16..17,
name: "x",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -47,13 +47,13 @@ Assignment {
],
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..5,
name: "b",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -13,8 +13,8 @@ Assignment {
},
},
patterns: [
(
Constructor {
AssignmentPattern {
pattern: Constructor {
is_record: false,
location: 7..14,
name: "Some",
@ -33,8 +33,8 @@ Assignment {
with_spread: false,
tipo: (),
},
None,
),
annotation: None,
},
],
kind: Expect {
backpassing: false,

View File

@ -21,8 +21,8 @@ Assignment {
},
},
patterns: [
(
Constructor {
AssignmentPattern {
pattern: Constructor {
is_record: false,
location: 0..29,
name: "True",
@ -32,8 +32,8 @@ Assignment {
with_spread: false,
tipo: (),
},
None,
),
annotation: None,
},
],
kind: Expect {
backpassing: false,

View File

@ -17,13 +17,13 @@ Assignment {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 13..14,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -32,8 +32,8 @@ Assignment {
],
},
patterns: [
(
Constructor {
AssignmentPattern {
pattern: Constructor {
is_record: false,
location: 0..21,
name: "True",
@ -43,8 +43,8 @@ Assignment {
with_spread: false,
tipo: (),
},
None,
),
annotation: None,
},
],
kind: Expect {
backpassing: false,

View File

@ -17,13 +17,13 @@ Assignment {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 14..15,
name: "b",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -32,13 +32,13 @@ Assignment {
],
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..5,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -17,13 +17,13 @@ Assignment {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 14..15,
name: "b",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -32,13 +32,13 @@ Assignment {
],
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..5,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -17,13 +17,13 @@ Assignment {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 16..17,
name: "b",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -36,13 +36,13 @@ Assignment {
],
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..5,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -12,8 +12,8 @@ Assignment {
},
},
patterns: [
(
Constructor {
AssignmentPattern {
pattern: Constructor {
is_record: false,
location: 0..11,
name: "True",
@ -23,8 +23,8 @@ Assignment {
with_spread: false,
tipo: (),
},
None,
),
annotation: None,
},
],
kind: Expect {
backpassing: false,

View File

@ -28,13 +28,13 @@ Sequence {
location: 8..18,
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..5,
name: "x",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -118,13 +118,13 @@ Sequence {
return_annotation: None,
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 24..33,
name: "map_add_x",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -15,13 +15,13 @@ Sequence {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 8..9,
name: "i",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -37,13 +37,13 @@ Sequence {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 28..29,
name: "j",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -63,13 +63,13 @@ Sequence {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 48..49,
name: "k",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -29,13 +29,13 @@ Assignment {
tail: None,
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..9,
name: "thing",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -41,13 +41,13 @@ Sequence {
],
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..9,
name: "tuple",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -28,13 +28,13 @@ Sequence {
location: 8..15,
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 4..5,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -86,13 +86,13 @@ When {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 55..62,
name: "amazing",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -20,13 +20,13 @@ Module {
name: "bar",
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 19..20,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -64,13 +64,13 @@ Module {
name: "bar",
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 56..57,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -119,13 +119,13 @@ Module {
},
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 93..94,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,
@ -170,13 +170,13 @@ Module {
location: 130..137,
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 126..127,
name: "a",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -25,13 +25,13 @@ Module {
preferred_format: Utf8String,
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 17..18,
name: "x",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -23,13 +23,13 @@ Module {
preferred_format: Utf8String,
},
patterns: [
(
Var {
AssignmentPattern {
pattern: Var {
location: 17..18,
name: "x",
},
None,
),
annotation: None,
},
],
kind: Let {
backpassing: false,

View File

@ -8,7 +8,7 @@ use super::{
};
use crate::{
ast::{
self, Annotation, Arg, ArgName, AssignmentKind, BinOp, Bls12_381Point,
self, Annotation, Arg, ArgName, AssignmentKind, AssignmentPattern, BinOp, Bls12_381Point,
ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, Curve, IfBranch,
LogicalOpChainKind, Pattern, RecordUpdateSpread, Span, TraceKind, TraceLevel, Tracing,
TypedArg, TypedCallArg, TypedClause, TypedClauseGuard, TypedIfBranch, TypedPattern,
@ -269,7 +269,10 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
} => {
// at this point due to backpassing rewrites,
// patterns is guaranteed to have one item
let (pattern, annotation) = patterns.into_vec().swap_remove(0);
let AssignmentPattern {
pattern,
annotation,
} = patterns.into_vec().swap_remove(0);
self.infer_assignment(pattern, *value, kind, &annotation, location)
}
@ -966,7 +969,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
value: UntypedExpr::Assignment {
location,
value: untyped_value.into(),
patterns: Vec1::new((untyped_pattern, Some(ann))),
patterns: AssignmentPattern::new(untyped_pattern, Some(ann)).into(),
kind,
},
});
@ -1013,14 +1016,15 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
name: "...".to_string(),
location: Span::empty(),
}),
patterns: Vec1::new((untyped_pattern, None)),
patterns: AssignmentPattern::new(untyped_pattern, None)
.into(),
kind: AssignmentKind::Let { backpassing: true },
}
}
_ => UntypedExpr::Assignment {
location: Span::empty(),
value: Box::new(untyped_value),
patterns: Vec1::new((untyped_pattern, None)),
patterns: AssignmentPattern::new(untyped_pattern, None).into(),
kind: AssignmentKind::let_(),
},
},
@ -1753,7 +1757,12 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
let mut names = Vec::new();
for (index, (pattern, annotation)) in patterns.into_iter().enumerate() {
for (index, assignment_pattern) in patterns.into_iter().enumerate() {
let AssignmentPattern {
pattern,
annotation,
} = assignment_pattern;
// In case where we have a Pattern that isn't simply a let-binding to a name, we do insert an extra let-binding
// in front of the continuation sequence. This is because we do not support patterns in function argument
// (which is perhaps something we should support?).
@ -1773,7 +1782,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
name: name.clone(),
}
.into(),
patterns: Vec1::new((pattern, annotation)),
patterns: AssignmentPattern::new(pattern, annotation).into(),
// erase backpassing while preserving assignment kind.
kind: match kind {
AssignmentKind::Let { .. } => AssignmentKind::let_(),
@ -1878,12 +1887,12 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
return Err(Error::UnexpectedMultiPatternAssignment {
arrow: patterns
.last()
.0
.pattern
.location()
.map(|_, c_end| (c_end + 1, c_end + 1)),
location: patterns[1..]
.iter()
.map(|(p, _)| p.location())
.map(|ap| ap.pattern.location())
.reduce(|acc, loc| acc.union(loc))
.unwrap_or(location)
.map_end(|current| current - 1),
@ -2182,7 +2191,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
sample: UntypedExpr::Assignment {
location: Span::empty(),
value: Box::new(subject.clone()),
patterns: Vec1::new((clauses[0].patterns[0].clone(), None)),
patterns: AssignmentPattern::new(clauses[0].patterns[0].clone(), None).into(),
kind: AssignmentKind::let_(),
},
});