Implemented Len in parser

pull/2/head
ondra05 2022-01-22 20:37:44 +01:00
parent 9e6cc36135
commit 622ce45435
3 changed files with 24 additions and 4 deletions

View File

@ -190,6 +190,7 @@ pub enum ExprKind {
expr: Box<Expr>,
index: Box<Expr>,
},
Len(Box<Expr>),
Variable(String),
}

View File

@ -175,6 +175,7 @@ impl ExecEnv {
.map(|x| x.borrow().clone())
.unwrap_or(Value::Nul)
}
Len(_) => todo!("length"),
// TODO: not too happy with constructing an artificial
// Ident here.

View File

@ -195,10 +195,7 @@ impl<'source> Parser<'source> {
Token::LeftBracket => match buf.take() {
Some(buf) => Ok(Expr::new(
ExprKind::Index {
expr: Box::new(buf),
index: Box::new(self.expr_flow(Token::RightBracket)?),
},
self.index_flow(buf)?,
start..self.lexer.span().end,
)),
None => Ok(Expr::new(self.cart_flow()?, start..self.lexer.span().end)),
@ -279,6 +276,27 @@ impl<'source> Parser<'source> {
Ok(ExprKind::Cart(cart))
}
/// Flow for indexing operations
///
/// Indexing with empty index resolves to length of expression, else it indexes
fn index_flow(&mut self, expr: Expr) -> Result<ExprKind, Error> {
let mut buf = None;
Ok(loop {
match self.checked_next()? {
Token::RightBracket => match buf {
Some(index) => {
break ExprKind::Index {
expr: Box::new(expr),
index: Box::new(index),
}
}
None => break ExprKind::Len(Box::new(expr)),
},
token => buf = Some(self.parse_expr(token, &mut buf)?),
}
})
}
/// Flow for operators
///
/// Generates operation from LHS buffer and next expression as RHS