From 9af3670d16f6d6bfc42f76a1e20a41701e63ea67 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 25 Jan 2020 20:56:44 -0500 Subject: [PATCH] Handle function calls inside `Value` --- src/value.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/value.rs b/src/value.rs index c601bd8..a744135 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,8 +1,10 @@ #![allow(dead_code, unused_variables)] use std::fmt::{self, Display}; use std::iter::{Iterator, Peekable}; -use std::ops::{Add, Div, Mul, Rem, Sub}; +use std::ops::Add; +use crate::args::eat_call_args; +use crate::builtin::GLOBAL_FUNCTIONS; use crate::color::Color; use crate::common::{Keyword, Op, Scope, Symbol}; use crate::units::Unit; @@ -159,7 +161,7 @@ impl Value { None => return Some(left), }; match next.kind { - TokenKind::Symbol(Symbol::SemiColon) => return Some(left), + TokenKind::Symbol(Symbol::SemiColon) => Some(left), TokenKind::Symbol(Symbol::Comma) => { toks.next(); devour_whitespace_or_comment(toks); @@ -181,7 +183,7 @@ impl Value { TokenKind::Symbol(Symbol::Mul) => Op::Mul, TokenKind::Symbol(Symbol::Div) => Op::Div, TokenKind::Symbol(Symbol::Percent) => Op::Rem, - _ => unsafe { std::hint::unreachable_unchecked() } + _ => unsafe { std::hint::unreachable_unchecked() }, }; toks.next(); devour_whitespace_or_comment(toks); @@ -267,7 +269,24 @@ impl Value { _ => break, } } - Some(Value::Ident(s, QuoteKind::None)) + match toks.peek() { + Some(Token { + kind: TokenKind::Symbol(Symbol::OpenParen), + .. + }) => { + toks.next(); + let args = eat_call_args(toks); + let func = match scope.functions.get(&s) { + Some(f) => f, + None => match GLOBAL_FUNCTIONS.get(&s) { + Some(f) => f, + None => todo!("called undefined function"), + }, + }; + Some(func.clone().args(&args).call()) + } + _ => Some(Value::Ident(s, QuoteKind::None)), + } } TokenKind::Symbol(Symbol::DoubleQuote) => { let mut s = String::new();