Skip to content

Commit 87f3bb0

Browse files
committed
Document and rename AST nodes for clarity
1 parent 6d4e5b0 commit 87f3bb0

File tree

9 files changed

+43
-29
lines changed

9 files changed

+43
-29
lines changed

src/ast.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,38 +596,48 @@ pub enum Pattern<Constructor, Type> {
596596
value: String,
597597
},
598598

599+
/// The creation of a variable.
600+
/// e.g. `assert [this_is_a_var, .._] = x`
599601
Var {
600602
location: SrcSpan,
601603
name: String,
602604
},
603605

606+
/// A reference to a variable in a bit string. This is always a variable
607+
/// being used rather than a new variable being assigned.
604608
VarUsage {
605609
location: SrcSpan,
606610
name: String,
607611
type_: Type,
608612
},
609613

614+
/// A name given to a sub-pattern using the `as` keyword.
615+
/// e.g. `assert #(1, [_, _] as the_list) = x`
610616
Assign {
611617
name: String,
612618
location: SrcSpan,
613619
pattern: Box<Self>,
614620
},
615621

622+
/// A pattern that binds to any value but does not assign a variable.
623+
/// Always starts with an underscore.
616624
Discard {
617625
name: String,
618626
location: SrcSpan,
619627
},
620628

621-
Nil {
629+
EmptyList {
622630
location: SrcSpan,
623631
},
624632

625-
Cons {
633+
// TODO: use a vector rather than a linked list here
634+
ListCons {
626635
location: SrcSpan,
627636
head: Box<Self>,
628637
tail: Box<Self>,
629638
},
630639

640+
/// The constructor for a custom type. Starts with an uppercase letter.
631641
Constructor {
632642
location: SrcSpan,
633643
name: String,
@@ -655,8 +665,8 @@ impl<A, B> Pattern<A, B> {
655665
Pattern::Int { location, .. }
656666
| Pattern::Var { location, .. }
657667
| Pattern::VarUsage { location, .. }
658-
| Pattern::Nil { location, .. }
659-
| Pattern::Cons { location, .. }
668+
| Pattern::EmptyList { location, .. }
669+
| Pattern::ListCons { location, .. }
660670
| Pattern::Float { location, .. }
661671
| Pattern::Discard { location, .. }
662672
| Pattern::String { location, .. }
@@ -668,11 +678,11 @@ impl<A, B> Pattern<A, B> {
668678

669679
pub fn put_list_cons_location_start(self, start: usize) -> Self {
670680
match self {
671-
Pattern::Cons {
681+
Pattern::ListCons {
672682
location: SrcSpan { end, .. },
673683
head,
674684
tail,
675-
} => Pattern::Cons {
685+
} => Pattern::ListCons {
676686
location: SrcSpan { start, end },
677687
head,
678688
tail,

src/ast/typed.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ pub enum TypedExpr {
4242
return_annotation: Option<TypeAst>,
4343
},
4444

45-
ListNil {
45+
EmptyList {
4646
location: SrcSpan,
4747
typ: Arc<Type>,
4848
},
4949

50+
// TODO: use a vector rather than a linked list here
5051
ListCons {
5152
location: SrcSpan,
5253
typ: Arc<Type>,
@@ -168,7 +169,7 @@ impl TypedExpr {
168169
| Self::BinOp { location, .. }
169170
| Self::Tuple { location, .. }
170171
| Self::String { location, .. }
171-
| Self::ListNil { location, .. }
172+
| Self::EmptyList { location, .. }
172173
| Self::ListCons { location, .. }
173174
| Self::TupleIndex { location, .. }
174175
| Self::ModuleSelect { location, .. }
@@ -196,7 +197,7 @@ impl TypedExpr {
196197
| Self::BinOp { location, .. }
197198
| Self::Tuple { location, .. }
198199
| Self::String { location, .. }
199-
| Self::ListNil { location, .. }
200+
| Self::EmptyList { location, .. }
200201
| Self::ListCons { location, .. }
201202
| Self::TupleIndex { location, .. }
202203
| Self::ModuleSelect { location, .. }
@@ -219,7 +220,7 @@ impl TypedExpr {
219220
fn type_(&self) -> Arc<Type> {
220221
match self {
221222
Self::Fn { typ, .. } => typ.clone(),
222-
Self::ListNil { typ, .. } => typ.clone(),
223+
Self::EmptyList { typ, .. } => typ.clone(),
223224
Self::Assignment { typ, .. } => typ.clone(),
224225
Self::Int { typ, .. } => typ.clone(),
225226
Self::Seq { then, .. } => then.type_(),

src/ast/untyped.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub enum UntypedExpr {
3939
location: SrcSpan,
4040
},
4141

42+
// TODO: use a vector rather than a linked list here
4243
ListCons {
4344
location: SrcSpan,
4445
head: Box<Self>,

src/erl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ fn float<'a>(value: &str) -> Document<'a> {
882882

883883
fn expr_list_cons<'a>(head: &'a TypedExpr, tail: &'a TypedExpr, env: &mut Env<'a>) -> Document<'a> {
884884
list_cons(head, tail, env, maybe_block_expr, |expr| match expr {
885-
TypedExpr::ListNil { .. } => ListType::Nil,
885+
TypedExpr::EmptyList { .. } => ListType::Nil,
886886

887887
TypedExpr::ListCons { head, tail, .. } => ListType::Cons { head, tail },
888888

@@ -1411,7 +1411,7 @@ fn erlang_error<'a>(
14111411

14121412
fn expr<'a>(expression: &'a TypedExpr, env: &mut Env<'a>) -> Document<'a> {
14131413
match expression {
1414-
TypedExpr::ListNil { .. } => "[]".to_doc(),
1414+
TypedExpr::EmptyList { .. } => "[]".to_doc(),
14151415

14161416
TypedExpr::Todo {
14171417
label, location, ..

src/erl/pattern.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(super) fn to_doc<'a>(
1111
env: &mut Env<'a>,
1212
) -> Document<'a> {
1313
match p {
14-
Pattern::Nil { .. } => "[]".to_doc(),
14+
Pattern::EmptyList { .. } => "[]".to_doc(),
1515

1616
Pattern::Assign {
1717
name, pattern: p, ..
@@ -22,7 +22,7 @@ pub(super) fn to_doc<'a>(
2222
.append(env.next_local_var_name(name))
2323
}
2424

25-
Pattern::Cons { head, tail, .. } => pattern_list_cons(head, tail, vars, env),
25+
Pattern::ListCons { head, tail, .. } => pattern_list_cons(head, tail, vars, env),
2626

2727
Pattern::Discard { .. } => "_".to_doc(),
2828

@@ -117,11 +117,11 @@ fn pattern_list_cons<'a>(
117117

118118
fn categorise_element(expr: &TypedPattern) -> ListType<&TypedPattern, &TypedPattern> {
119119
match expr {
120-
Pattern::Cons { head, tail, .. } => ListType::Cons {
120+
Pattern::ListCons { head, tail, .. } => ListType::Cons {
121121
head: head.as_ref(),
122122
tail: tail.as_ref(),
123123
},
124-
Pattern::Nil { .. } => ListType::Nil,
124+
Pattern::EmptyList { .. } => ListType::Nil,
125125
other => ListType::NotList(other),
126126
}
127127
}

src/format.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,9 @@ impl<'comments> Formatter<'comments> {
694694
) -> Document<'a> {
695695
fn is_breakable(expr: &UntypedPattern) -> bool {
696696
match expr {
697-
Pattern::Tuple { .. } | Pattern::Cons { .. } | Pattern::BitString { .. } => true,
697+
Pattern::Tuple { .. } | Pattern::ListCons { .. } | Pattern::BitString { .. } => {
698+
true
699+
}
698700
Pattern::Constructor {
699701
arguments: args, ..
700702
} => !args.is_empty(),
@@ -1197,9 +1199,9 @@ impl<'comments> Formatter<'comments> {
11971199

11981200
Pattern::Discard { name, .. } => name.to_doc(),
11991201

1200-
Pattern::Nil { .. } => "[]".to_doc(),
1202+
Pattern::EmptyList { .. } => "[]".to_doc(),
12011203

1202-
Pattern::Cons { head, tail, .. } => {
1204+
Pattern::ListCons { head, tail, .. } => {
12031205
let (elems, tail) =
12041206
list_cons(head.as_ref(), tail.as_ref(), categorise_list_pattern);
12051207
let elems = concat(Itertools::intersperse(
@@ -1397,9 +1399,9 @@ fn categorise_list_expr(expr: &UntypedExpr) -> ListType<&UntypedExpr, &UntypedEx
13971399

13981400
fn categorise_list_pattern(expr: &UntypedPattern) -> ListType<&UntypedPattern, &UntypedPattern> {
13991401
match expr {
1400-
UntypedPattern::Nil { .. } => ListType::Nil,
1402+
UntypedPattern::EmptyList { .. } => ListType::Nil,
14011403

1402-
UntypedPattern::Cons { head, tail, .. } => ListType::Cons { head, tail },
1404+
UntypedPattern::ListCons { head, tail, .. } => ListType::Cons { head, tail },
14031405

14041406
other => ListType::NotList(other),
14051407
}

src/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ where
724724
}
725725
Some((start, Tok::ListNil, end)) => {
726726
let _ = self.next_tok();
727-
Pattern::Nil {
727+
Pattern::EmptyList {
728728
location: SrcSpan { start, end },
729729
}
730730
}
@@ -793,7 +793,7 @@ where
793793
name: "_".to_string(),
794794
},
795795
// No tail specified
796-
None => Pattern::Nil {
796+
None => Pattern::EmptyList {
797797
location: SrcSpan {
798798
start: rsqb_e - 1,
799799
end: rsqb_e,
@@ -804,7 +804,7 @@ where
804804
elems
805805
.into_iter()
806806
.rev()
807-
.fold(tail_pattern, |a, e| Pattern::Cons {
807+
.fold(tail_pattern, |a, e| Pattern::ListCons {
808808
location: e.location(),
809809
head: Box::new(e),
810810
tail: Box::new(a),

src/type_/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'a, 'b, 'c> ExprTyper<'a, 'b, 'c> {
292292
}
293293

294294
fn infer_nil(&mut self, location: SrcSpan) -> Result<TypedExpr, Error> {
295-
Ok(TypedExpr::ListNil {
295+
Ok(TypedExpr::EmptyList {
296296
location,
297297
typ: list(self.new_unbound_var(self.environment.level)),
298298
})

src/type_/pattern.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,15 @@ impl<'a, 'b, 'c> PatternTyper<'a, 'b, 'c> {
269269
Ok(Pattern::String { location, value })
270270
}
271271

272-
Pattern::Nil { location } => {
272+
Pattern::EmptyList { location } => {
273273
let typ2 = list(self.environment.new_unbound_var(self.level));
274274
self.environment
275275
.unify(typ, typ2)
276276
.map_err(|e| convert_unify_error(e, location))?;
277-
Ok(Pattern::Nil { location })
277+
Ok(Pattern::EmptyList { location })
278278
}
279279

280-
Pattern::Cons {
280+
Pattern::ListCons {
281281
location,
282282
head,
283283
tail,
@@ -286,7 +286,7 @@ impl<'a, 'b, 'c> PatternTyper<'a, 'b, 'c> {
286286
let head = Box::new(self.unify(*head, args[0].clone())?);
287287
let tail = Box::new(self.unify(*tail, typ)?);
288288

289-
Ok(Pattern::Cons {
289+
Ok(Pattern::ListCons {
290290
location,
291291
head,
292292
tail,

0 commit comments

Comments
 (0)