This commit is contained in:
Erin 2021-04-29 19:19:35 +02:00 committed by ondra05
parent ca60f818eb
commit eef7ec16fa
4 changed files with 13 additions and 7 deletions

View file

@ -1 +1 @@
a(3,) 1 + 4

View file

@ -35,6 +35,12 @@ pub enum Expr {
iden: Iden, iden: Iden,
args: Vec<Expr>, args: Vec<Expr>,
}, },
Addition {
left: Box<Expr>,
right: Box<Expr>,
},
Literal(Value), Literal(Value),
Identifier(Iden), Identifier(Iden),
Melo(Iden), Melo(Iden),

View file

@ -1,8 +1,7 @@
use std::collections::HashMap;
use super::*; use super::*;
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
/// Parse operations (got identifier/value)
pub(super) fn parse_ops(&mut self, token: Token) -> Result<Expr, Error> { pub(super) fn parse_ops(&mut self, token: Token) -> Result<Expr, Error> {
let iden = if let Token::Identifier(i) = token { let iden = if let Token::Identifier(i) = token {
Iden(i) Iden(i)
@ -20,9 +19,11 @@ impl<'a> Parser<'a> {
Ok(buf[0].clone()) Ok(buf[0].clone())
} }
/// Parse function call
fn fn_call(&mut self, iden: Iden) -> Result<Expr, Error> { fn fn_call(&mut self, iden: Iden) -> Result<Expr, Error> {
self.lexer.next(); self.lexer.next();
let mut args: Vec<Expr> = Vec::new(); let mut args: Vec<Expr> = Vec::new();
while let Some(token) = self.lexer.peek() { while let Some(token) = self.lexer.peek() {
match token { match token {
Token::Identifier(id) => { Token::Identifier(id) => {
@ -38,9 +39,6 @@ impl<'a> Parser<'a> {
self.require(Token::Comma)?; self.require(Token::Comma)?;
} }
self.require(Token::RightParenthesis)?; self.require(Token::RightParenthesis)?;
Ok(Expr::FunctionCall { Ok(Expr::FunctionCall { iden, args })
iden,
args,
})
} }
} }

View file

@ -30,6 +30,7 @@ impl<'a> Parser<'a> {
} }
} }
/// Require an identifier on next and return it
pub(super) fn require_iden(&mut self) -> Result<String, Error> { pub(super) fn require_iden(&mut self) -> Result<String, Error> {
if let Some(Token::Identifier(id)) = self.lexer.next() { if let Some(Token::Identifier(id)) = self.lexer.next() {
Ok(id) Ok(id)
@ -41,6 +42,7 @@ impl<'a> Parser<'a> {
} }
} }
/// Throw unexpected token error (optionally what was expected)
pub(super) fn unexpected_token(&mut self, expected: Option<Token>) -> Error { pub(super) fn unexpected_token(&mut self, expected: Option<Token>) -> Error {
let error_msg = match expected { let error_msg = match expected {
Some(s) => format!( Some(s) => format!(