From ca60f818eb79df626a063e9ef581221604780e03 Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 29 Apr 2021 18:50:51 +0200 Subject: [PATCH] Implemented function calls --- able-script-test/parse_test.able | 2 +- src/parser/item.rs | 11 +++++++++-- src/parser/mod.rs | 3 --- src/parser/ops.rs | 17 ++++++++++++++++- src/variables.rs | 1 + 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/able-script-test/parse_test.able b/able-script-test/parse_test.able index f3c8d37..4848030 100644 --- a/able-script-test/parse_test.able +++ b/able-script-test/parse_test.able @@ -1 +1 @@ -a() \ No newline at end of file +a(3,) \ No newline at end of file diff --git a/src/parser/item.rs b/src/parser/item.rs index 63dafa0..eabea92 100644 --- a/src/parser/item.rs +++ b/src/parser/item.rs @@ -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 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, + args: Vec, }, Literal(Value), + Identifier(Iden), Melo(Iden), } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 2461252..b5a3c4f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -142,9 +142,6 @@ impl<'a> Parser<'a> { } }; - if token == Token::RightBrace { - break; - } body.push_str(match token { Token::OpGt | Token::OpLt diff --git a/src/parser/ops.rs b/src/parser/ops.rs index fb22c30..0e8bb5d 100644 --- a/src/parser/ops.rs +++ b/src/parser/ops.rs @@ -22,10 +22,25 @@ impl<'a> Parser<'a> { fn fn_call(&mut self, iden: Iden) -> Result { self.lexer.next(); + let mut args: Vec = 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, }) } } diff --git a/src/variables.rs b/src/variables.rs index cb476b9..fd4a57c 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -23,6 +23,7 @@ pub enum Value { Int(i32), Bool(bool), Abool(Abool), + Nul, } #[derive(Debug)]