Added variable assignment to parser

- Fixed parse error, that `rlyeh` returned HopBack
This commit is contained in:
Erin 2021-06-11 16:59:40 +02:00 committed by ondra05
parent 851f04c653
commit 6f16d2594b
3 changed files with 27 additions and 4 deletions

View file

@ -53,6 +53,10 @@ pub enum StmtKind {
iden: Iden, iden: Iden,
init: Option<Expr>, init: Option<Expr>,
}, },
Assign {
iden: Iden,
value: Expr,
},
Functio { Functio {
iden: Iden, iden: Iden,

View file

@ -203,6 +203,7 @@ impl ExecEnv {
self.decl_var(&iden.iden, init); self.decl_var(&iden.iden, init);
} }
StmtKind::Assign { .. } => todo!(),
StmtKind::Functio { StmtKind::Functio {
iden: _, iden: _,
args: _, args: _,

View file

@ -74,7 +74,7 @@ impl<'source> Parser<'source> {
start..self.lexer.span().end, start..self.lexer.span().end,
)), )),
Token::Rlyeh => Ok(Stmt::new( Token::Rlyeh => Ok(Stmt::new(
self.semi_terminated(StmtKind::HopBack)?, self.semi_terminated(StmtKind::Rlyeh)?,
start..self.lexer.span().end, start..self.lexer.span().end,
)), )),
@ -284,12 +284,16 @@ impl<'source> Parser<'source> {
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or(Error::unexpected_eof(self.lexer.span().start))?
{ {
// Print to stdout
Token::Print => { 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), ErrorKind::UnexpectedToken(Token::Print),
self.lexer.span(), self.lexer.span(),
))?) ))?);
break self.semi_terminated(stmt)?;
} }
// Functio call
Token::LeftParen => { Token::LeftParen => {
if let Some(Expr { if let Some(Expr {
kind: ExprKind::Variable(iden), kind: ExprKind::Variable(iden),
@ -299,10 +303,23 @@ impl<'source> Parser<'source> {
break self.functio_call_flow(Iden::new(iden, span))?; 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)?), t => buf = Some(self.parse_expr(t, &mut buf)?),
} }
}; };
self.require(Token::Semicolon)?;
Ok(r) Ok(r)
} }
@ -397,6 +414,7 @@ impl<'source> Parser<'source> {
} }
} }
self.require(Token::Semicolon)?;
Ok(StmtKind::Call { iden, args }) Ok(StmtKind::Call { iden, args })
} }