From 4d4ab243822dffe59e788c44808bdcab8a0ab745 Mon Sep 17 00:00:00 2001 From: Goren Barak Date: Sun, 26 Nov 2023 10:17:09 -0500 Subject: [PATCH] Made `parse_fun_call()` work. --- src/main.rs | 7 ++++--- src/parse/parse.rs | 34 +++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 56d6099..fc7ef6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,8 +45,9 @@ fn main() { // println!("{}", fc); + let parsed = "hello(hi)"; - let mut lexer = Token::lexer("let a = b;"); - println!("{:?}", lex_str("let a = b;")); - println!("{:?}", parse_var_declaration(lexer)); + let mut lexer = Token::lexer(parsed); + println!("{:?}", lex_str(parsed)); + println!("{:?}", parse_fun_call(lexer)); } diff --git a/src/parse/parse.rs b/src/parse/parse.rs index d964f3d..d12a7f1 100644 --- a/src/parse/parse.rs +++ b/src/parse/parse.rs @@ -17,7 +17,6 @@ pub fn parse_var_declaration(mut tokens: Lexer) -> Option { println!("{:?}", name); if unwrap!(tokens) == Equal { let temp_token = unwrap!(tokens); - if let Number(n) = temp_token { let value = Value::Number(n); println!("{:?}", value); @@ -32,13 +31,20 @@ pub fn parse_var_declaration(mut tokens: Lexer) -> Option { tok } -pub fn parse_value(token: (Token, &str)) -> Option> { - let mut value = None; +pub fn parse_value<'a>(token: (Option>, &'a str)) -> Option> { + if let Some(Ok(tt)) = token.0 { + let mut value = None; - if let Number(n) = token.0 { - value = Some(Value::Number(n)); - } else if token.0 == Identifier { - value = Some(Value::Var(VarReference { name: token.1 })); + if let Number(n) = tt { + value = Some(Value::Number(n)); + } else if tt == Identifier { + value = Some(Value::Var(VarReference { name: token.1 })); + } + + value + + } else { + return None; } } @@ -46,4 +52,18 @@ pub fn parse_fun_call(mut tokens: Lexer) -> Option { // Is it an Ident? → Is it a LeftParen? → Is it a value (I should really make a function to parse that) or is it a RightParen? // ↓ ↓ // If it's a value, push that to `params`. Otherwise, params will just be a `Vec::new()`. + let mut tok = None; + + if unwrap!(tokens) == Identifier { + let name = tokens.slice(); + if unwrap!(tokens) == LeftParen { + let mut params = Vec::new(); + while let Some(value) = parse_value((tokens.next(), tokens.slice())) { + params.push(value); + } + tok = Some(Expr::FunCall(FunCall {name, params: params.clone()})); + } + } + + tok }