From 588f69b710a53fc3dd6336a0afbcbde919b93960 Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 11 Jun 2021 16:59:40 +0200 Subject: [PATCH] Added variable assignment to parser - Fixed parse error, that `rlyeh` returned HopBack --- src/ast.rs | 4 ++++ src/interpret.rs | 1 + src/parser.rs | 26 ++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 69c2570..1aa3453 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -53,6 +53,10 @@ pub enum StmtKind { iden: Iden, init: Option, }, + Assign { + iden: Iden, + value: Expr, + }, Functio { iden: Iden, diff --git a/src/interpret.rs b/src/interpret.rs index fc17a60..9bdfcb0 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -203,6 +203,7 @@ impl ExecEnv { self.decl_var(&iden.iden, init); } + StmtKind::Assign { .. } => todo!(), StmtKind::Functio { iden: _, args: _, diff --git a/src/parser.rs b/src/parser.rs index b52663a..19294c3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -74,7 +74,7 @@ impl<'source> Parser<'source> { start..self.lexer.span().end, )), Token::Rlyeh => Ok(Stmt::new( - self.semi_terminated(StmtKind::HopBack)?, + self.semi_terminated(StmtKind::Rlyeh)?, start..self.lexer.span().end, )), @@ -284,12 +284,16 @@ impl<'source> Parser<'source> { .next() .ok_or(Error::unexpected_eof(self.lexer.span().start))? { + // Print to stdout Token::Print => { - break StmtKind::Print(buf.take().ok_or(Error::new( + let stmt = StmtKind::Print(buf.take().ok_or(Error::new( ErrorKind::UnexpectedToken(Token::Print), self.lexer.span(), - ))?) + ))?); + break self.semi_terminated(stmt)?; } + + // Functio call Token::LeftParen => { if let Some(Expr { kind: ExprKind::Variable(iden), @@ -299,10 +303,23 @@ impl<'source> Parser<'source> { break self.functio_call_flow(Iden::new(iden, span))?; } } + + // Variable Assignment + Token::Equal => { + if let Some(Expr { + kind: ExprKind::Variable(iden), + span, + }) = buf + { + break StmtKind::Assign { + iden: Iden::new(iden, span), + value: self.expr_flow(Token::Semicolon)?, + }; + } + } t => buf = Some(self.parse_expr(t, &mut buf)?), } }; - self.require(Token::Semicolon)?; Ok(r) } @@ -397,6 +414,7 @@ impl<'source> Parser<'source> { } } + self.require(Token::Semicolon)?; Ok(StmtKind::Call { iden, args }) }