From a82a9d7b79200c372bf2d317e6a2be05f6900a97 Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 21 Oct 2021 20:51:24 +0200 Subject: [PATCH] Moved Assignable creation functions --- ablescript/src/ast.rs | 33 +++++++++++++++++++++++++++++++++ ablescript/src/parser.rs | 32 +------------------------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/ablescript/src/ast.rs b/ablescript/src/ast.rs index 67cd235..fa30420 100644 --- a/ablescript/src/ast.rs +++ b/ablescript/src/ast.rs @@ -50,6 +50,39 @@ pub enum AssignableKind { Index { indices: Vec }, } +impl Assignable { + pub fn from_expr(expr: Expr) -> Result { + match expr.kind { + ExprKind::Variable(ident) => Ok(Assignable { + ident: Ident::new(ident, expr.span), + kind: AssignableKind::Variable, + }), + ExprKind::Index { expr, index } => Self::from_index(*expr, *index), + _ => Err(()), + } + } + + fn from_index(mut buf: Expr, index: Expr) -> Result { + let mut indices = vec![index]; + let ident = loop { + match buf.kind { + ExprKind::Variable(ident) => break ident, + ExprKind::Index { expr, index } => { + indices.push(*index); + buf = *expr; + } + _ => return Err(()), + } + }; + + indices.reverse(); + Ok(Assignable { + ident: Ident::new(ident, buf.span), + kind: AssignableKind::Index { indices }, + }) + } +} + #[derive(Debug, PartialEq, Clone, Hash)] pub struct Block { pub block: Vec, diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index d850f60..1aa4876 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -355,16 +355,7 @@ impl<'source> Parser<'source> { // Variable Assignment Token::Equal => { - let assignable = buf.take().and_then(|buf| match buf.kind { - ExprKind::Variable(ident) => Some(Assignable { - ident: Ident::new(ident, buf.span), - kind: AssignableKind::Variable, - }), - ExprKind::Index { expr, index } => self.cart_assignable_flow(*expr, *index), - _ => None, - }); - - if let Some(assignable) = assignable { + if let Some(Ok(assignable)) = buf.take().map(Assignable::from_expr) { break StmtKind::Assign { assignable, value: self.expr_flow(Token::Semicolon)?, @@ -395,27 +386,6 @@ impl<'source> Parser<'source> { Ok(r) } - /// Parse Cart Assignable flow - fn cart_assignable_flow(&mut self, mut buf: Expr, index: Expr) -> Option { - let mut indices = vec![index]; - let ident = loop { - match buf.kind { - ExprKind::Variable(ident) => break ident, - ExprKind::Index { expr, index } => { - indices.push(*index); - buf = *expr; - } - _ => return None, - } - }; - - indices.reverse(); - Some(Assignable { - ident: Ident::new(ident, buf.span), - kind: AssignableKind::Index { indices }, - }) - } - /// Parse If flow /// /// Consists of condition and block, there is no else