This commit is contained in:
Erin 2021-05-03 23:02:19 +02:00 committed by ondra05
parent 5bdbc2bbb0
commit 1618262948
2 changed files with 14 additions and 7 deletions

View file

@ -99,7 +99,7 @@ impl<'a> Parser<'a> {
} }
_ => Err(Error { _ => Err(Error {
kind: ErrorKind::SyntaxError("Unexpected identifier".to_owned()), kind: ErrorKind::SyntaxError("Unexpected token".to_owned()),
position: start..self.lexer.span().end, position: start..self.lexer.span().end,
}), }),
} }
@ -153,6 +153,7 @@ impl<'a> Parser<'a> {
/// `functio [iden] ([expr], [expr]) { ... } /// `functio [iden] ([expr], [expr]) { ... }
fn function_declaration(&mut self) -> ParseResult { fn function_declaration(&mut self) -> ParseResult {
let iden = self.require_iden()?; let iden = self.require_iden()?;
self.require(Token::LeftParenthesis)?; self.require(Token::LeftParenthesis)?;
let mut args = vec![]; let mut args = vec![];
loop { loop {
@ -163,7 +164,9 @@ impl<'a> Parser<'a> {
_ => return Err(self.unexpected_token(None)), _ => return Err(self.unexpected_token(None)),
} }
} }
self.require(Token::LeftBrace)?; self.require(Token::LeftBrace)?;
// Parse function body // Parse function body
let body = self.parse_body()?; let body = self.parse_body()?;

View file

@ -20,6 +20,7 @@ macro_rules! gen_infix {
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
pub(super) fn parse_ops(&mut self, token: Token) -> ParseResult { pub(super) fn parse_ops(&mut self, token: Token) -> ParseResult {
// Statements
match self.lexer.peek() { match self.lexer.peek() {
Some(Token::LeftParenthesis) => return self.fn_call(token), Some(Token::LeftParenthesis) => return self.fn_call(token),
Some(Token::Assignment) => return self.parse_assignment(token), Some(Token::Assignment) => return self.parse_assignment(token),
@ -31,12 +32,15 @@ impl<'a> Parser<'a> {
loop { loop {
let peek = self.lexer.peek().clone(); let peek = self.lexer.peek().clone();
buf = match peek { buf = match peek {
// Print statement
Some(Token::Print) => { Some(Token::Print) => {
self.lexer.next(); self.lexer.next();
self.require(Token::Semicolon)?; self.require(Token::Semicolon)?;
return Ok(Stmt::Print(buf).into()); return Ok(Stmt::Print(buf).into());
} }
None => return Ok(buf.into()), None => return Ok(buf.into()),
// An expression
_ => self.parse_operation(peek, buf)?, _ => self.parse_operation(peek, buf)?,
} }
} }
@ -65,6 +69,8 @@ impl<'a> Parser<'a> {
fn parse_assignment(&mut self, token: Token) -> ParseResult { fn parse_assignment(&mut self, token: Token) -> ParseResult {
self.lexer.next(); self.lexer.next();
// Extract identifier
let iden = if let Token::Identifier(i) = token { let iden = if let Token::Identifier(i) = token {
Iden(i) Iden(i)
} else { } else {
@ -76,6 +82,7 @@ impl<'a> Parser<'a> {
let next = self.lexer.next(); let next = self.lexer.next();
let mut value = self.parse_expr(next)?; let mut value = self.parse_expr(next)?;
loop { loop {
let peek = self.lexer.peek().clone(); let peek = self.lexer.peek().clone();
value = match peek { value = match peek {
@ -89,6 +96,7 @@ impl<'a> Parser<'a> {
Some(t) => self.parse_operation(Some(t), value)?, Some(t) => self.parse_operation(Some(t), value)?,
}; };
} }
self.lexer.next(); self.lexer.next();
Ok(Stmt::VarAssignment { iden, value }.into()) Ok(Stmt::VarAssignment { iden, value }.into())
@ -143,12 +151,8 @@ impl<'a> Parser<'a> {
let next = self.lexer.next(); let next = self.lexer.next();
let mut buf = self.parse_expr(next)?; let mut buf = self.parse_expr(next)?;
loop { loop {
let next = self.lexer.peek().clone().ok_or(Error { let peek = self.lexer.peek().clone();
kind: ErrorKind::EndOfTokenStream, buf = match peek {
position: self.lexer.span(),
})?;
buf = match Some(next) {
Some(Token::RightParenthesis) => { Some(Token::RightParenthesis) => {
self.lexer.next(); self.lexer.next();
return Ok(buf); return Ok(buf);