mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-11-05 04:51:40 +03:00
Implement list resugar
This commit is contained in:
parent
ebd469a4ae
commit
20558e7fdf
@ -230,6 +230,14 @@ impl Tag {
|
||||
pub fn string_scons_head() -> Self {
|
||||
Self::Named(Name::new("String.SCons.head"))
|
||||
}
|
||||
|
||||
pub fn list() -> Self {
|
||||
Self::Named(Name::new("List"))
|
||||
}
|
||||
|
||||
pub fn list_lcons_head() -> Self {
|
||||
Self::Named(Name::new("List.LCons.head"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Book {
|
||||
|
@ -297,6 +297,34 @@ impl<'a> Reader<'a> {
|
||||
go(term, &mut s, self);
|
||||
Term::Str { val: s }
|
||||
}
|
||||
|
||||
fn decode_list(&mut self, term: &mut Term) -> Term {
|
||||
let mut els = Vec::new();
|
||||
fn go(t: &mut Term, els: &mut Vec<Term>, rd: &mut Reader<'_>) {
|
||||
match t {
|
||||
Term::Lam { tag, bod, .. } if *tag == Tag::list() => go(bod, els, rd),
|
||||
Term::App { tag, arg, .. } if *tag == Tag::list_lcons_head() => {
|
||||
if let Term::Lam { tag, bod, .. } = &mut **arg {
|
||||
if *tag == Tag::list() {
|
||||
els.push(rd.decode_list(bod));
|
||||
} else {
|
||||
els.push(*arg.clone());
|
||||
}
|
||||
} else {
|
||||
go(arg, els, rd)
|
||||
}
|
||||
}
|
||||
Term::App { fun, arg, .. } => {
|
||||
go(fun, els, rd);
|
||||
go(arg, els, rd);
|
||||
}
|
||||
Term::Var { .. } => {}
|
||||
other => els.push(other.clone()),
|
||||
}
|
||||
}
|
||||
go(term, &mut els, self);
|
||||
Term::List { els }
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents `let (fst, snd) = val` if `tag` is `None`, and `dup#tag fst snd = val` otherwise.
|
||||
@ -492,6 +520,7 @@ impl<'a> Reader<'a> {
|
||||
fn resugar_adts(&mut self, term: &mut Term) {
|
||||
match term {
|
||||
Term::Lam { tag, bod, .. } if *tag == Tag::string() => *term = self.decode_str(bod),
|
||||
Term::Lam { tag, bod, .. } if *tag == Tag::list() => *term = self.decode_list(bod),
|
||||
Term::Lam { tag: Tag::Named(adt_name), bod, .. } | Term::Chn { tag: Tag::Named(adt_name), bod, .. } => {
|
||||
let Some((adt_name, adt)) = self.book.adts.get_key_value(adt_name) else {
|
||||
return self.resugar_adts(bod);
|
||||
|
1
tests/golden_tests/run_file/list_resugar.hvm
Normal file
1
tests/golden_tests/run_file/list_resugar.hvm
Normal file
@ -0,0 +1 @@
|
||||
Main = (LCons 42 (LCons (LCons @x x LNil) LNil))
|
5
tests/snapshots/run_file__list_resugar.hvm.snap
Normal file
5
tests/snapshots/run_file__list_resugar.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/run_file/list_resugar.hvm
|
||||
---
|
||||
[42, [λd d]]
|
@ -2,4 +2,4 @@
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/run_file/list_take.hvm
|
||||
---
|
||||
(LCons 3 (LCons 2 LNil))
|
||||
[3, 2]
|
||||
|
Loading…
Reference in New Issue
Block a user