Implemented function calls

This commit is contained in:
Erin 2021-04-29 18:50:51 +02:00 committed by ondra05
parent 17a32a8df7
commit ca60f818eb
5 changed files with 27 additions and 7 deletions

View file

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

View file

@ -2,9 +2,15 @@ use std::collections::HashMap;
use crate::variables::Value;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Iden(pub String);
impl From<Iden> for Expr {
fn from(iden: Iden) -> Self {
Self::Identifier(iden)
}
}
#[derive(Debug, Clone)]
pub enum Expr {
VariableDeclaration {
@ -27,8 +33,9 @@ pub enum Expr {
FunctionCall {
iden: Iden,
args: HashMap<Iden, Value>,
args: Vec<Expr>,
},
Literal(Value),
Identifier(Iden),
Melo(Iden),
}

View file

@ -142,9 +142,6 @@ impl<'a> Parser<'a> {
}
};
if token == Token::RightBrace {
break;
}
body.push_str(match token {
Token::OpGt
| Token::OpLt

View file

@ -22,10 +22,25 @@ impl<'a> Parser<'a> {
fn fn_call(&mut self, iden: Iden) -> Result<Expr, Error> {
self.lexer.next();
let mut args: Vec<Expr> = Vec::new();
while let Some(token) = self.lexer.peek() {
match token {
Token::Identifier(id) => {
args.push(Iden(id.clone()).into());
self.lexer.next();
}
Token::RightParenthesis => break,
_ => {
let next = self.lexer.next();
args.push(self.parse_expr(next)?)
}
}
self.require(Token::Comma)?;
}
self.require(Token::RightParenthesis)?;
Ok(Expr::FunctionCall {
iden,
args: HashMap::new(),
args,
})
}
}

View file

@ -23,6 +23,7 @@ pub enum Value {
Int(i32),
Bool(bool),
Abool(Abool),
Nul,
}
#[derive(Debug)]