mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-09-17 14:47:21 +03:00
Add list comprehension
This commit is contained in:
parent
cd320af476
commit
e13e360118
@ -29,6 +29,8 @@ pub enum Term {
|
|||||||
Lst { els: Vec<Term> },
|
Lst { els: Vec<Term> },
|
||||||
// "(" ... ")"
|
// "(" ... ")"
|
||||||
Tup { els: Vec<Term> },
|
Tup { els: Vec<Term> },
|
||||||
|
// "[" {term} "for" {bind} "in" {iter} ("if" {cond})? "]"
|
||||||
|
Comprehension { term: Box<Term>, bind: Name, iter: Box<Term>, cond: Option<Box<Term>> },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -88,6 +88,7 @@ impl Term {
|
|||||||
rhs.order_kwargs(ctx);
|
rhs.order_kwargs(ctx);
|
||||||
}
|
}
|
||||||
Term::Lst { els } | Term::Tup { els } => els.iter_mut().for_each(|e| e.order_kwargs(ctx)),
|
Term::Lst { els } | Term::Tup { els } => els.iter_mut().for_each(|e| e.order_kwargs(ctx)),
|
||||||
|
Term::Comprehension { .. } => {}
|
||||||
Term::None | Term::Var { .. } | Term::Num { .. } | Term::Str { .. } => {}
|
Term::None | Term::Var { .. } | Term::Num { .. } | Term::Str { .. } => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,10 +65,7 @@ impl<'a> PyParser<'a> {
|
|||||||
head
|
head
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'[' => {
|
'[' => self.list_or_comprehension()?,
|
||||||
let els = self.list_like(|p| p.parse_term_py(), "[", "]", ",", true, 0)?;
|
|
||||||
Term::Lst { els }
|
|
||||||
}
|
|
||||||
'\"' => {
|
'\"' => {
|
||||||
let str = self.parse_quoted_string()?;
|
let str = self.parse_quoted_string()?;
|
||||||
let val = STRINGS.get(str);
|
let val = STRINGS.get(str);
|
||||||
@ -90,6 +87,28 @@ impl<'a> PyParser<'a> {
|
|||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn list_or_comprehension(&mut self) -> Result<Term, String> {
|
||||||
|
self.consume("[")?;
|
||||||
|
let head = self.parse_term_py()?;
|
||||||
|
if self.try_consume_keyword("for") {
|
||||||
|
let bind = self.parse_hvml_name()?;
|
||||||
|
self.consume("in")?;
|
||||||
|
let iter = self.parse_term_py()?;
|
||||||
|
let mut cond = None;
|
||||||
|
if self.try_consume_keyword("if") {
|
||||||
|
cond = Some(Box::new(self.parse_term_py()?));
|
||||||
|
}
|
||||||
|
self.consume("]")?;
|
||||||
|
Ok(Term::Comprehension { term: Box::new(head), bind, iter: Box::new(iter), cond })
|
||||||
|
} else {
|
||||||
|
let mut head = vec![head];
|
||||||
|
self.try_consume(",");
|
||||||
|
let tail = self.list_like(|p| p.parse_term_py(), "", "]", ",", true, 0)?;
|
||||||
|
head.extend(tail);
|
||||||
|
Ok(Term::Lst { els: head })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_term_py(&mut self) -> Result<Term, String> {
|
fn parse_term_py(&mut self) -> Result<Term, String> {
|
||||||
self.skip_trivia();
|
self.skip_trivia();
|
||||||
if self.try_consume_keyword("lambda") {
|
if self.try_consume_keyword("lambda") {
|
||||||
@ -562,6 +581,9 @@ def inc(n):
|
|||||||
n += 1;
|
n += 1;
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
|
def inc_list(list):
|
||||||
|
return [x+1 for x in list];
|
||||||
|
|
||||||
def lam():
|
def lam():
|
||||||
return lambda x, y: return x;;
|
return lambda x, y: return x;;
|
||||||
|
|
||||||
|
@ -109,6 +109,7 @@ impl Term {
|
|||||||
tag: lang::Tag::Static,
|
tag: lang::Tag::Static,
|
||||||
els: els.into_iter().map(Self::to_lang).collect(),
|
els: els.into_iter().map(Self::to_lang).collect(),
|
||||||
},
|
},
|
||||||
|
Term::Comprehension { .. } => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user