devalang_core/core/parser/handler/identifier/
call.rs1use crate::core::{
2 lexer::token::{Token, TokenKind},
3 parser::{
4 driver::Parser,
5 statement::{Statement, StatementKind},
6 },
7 shared::value::Value,
8 store::global::GlobalStore,
9};
10
11pub fn parse_call_token(
12 parser: &mut Parser,
13 current_token: Token,
14 _global_store: &mut GlobalStore,
15) -> Statement {
16 parser.advance(); let name_token = match parser.peek_clone() {
20 Some(t) => t,
21 None => {
22 return Statement::error(
23 current_token,
24 "Expected function name after 'call'".to_string(),
25 );
26 }
27 };
28
29 if name_token.kind != TokenKind::Identifier {
30 return Statement::error(
31 name_token,
32 "Expected function name to be an identifier".to_string(),
33 );
34 }
35
36 let func_name = name_token.lexeme.clone();
37 parser.advance(); let mut args: Vec<Value> = Vec::new();
41 if let Some(open_paren) = parser.peek_clone() {
42 if open_paren.kind == TokenKind::LParen {
43 parser.advance(); while let Some(token) = parser.peek_clone() {
47 if token.kind == TokenKind::RParen {
48 parser.advance(); break;
50 }
51
52 match token.kind {
53 TokenKind::Number => {
54 if let Ok(num) = token.lexeme.parse::<f32>() {
55 args.push(Value::Number(num));
56 }
57 parser.advance();
58 }
59 TokenKind::String => {
60 args.push(Value::String(token.lexeme.clone()));
61 parser.advance();
62 }
63 TokenKind::Identifier => {
64 args.push(Value::Identifier(token.lexeme.clone()));
65 parser.advance();
66 }
67 TokenKind::Comma => {
68 parser.advance(); }
70 _ => {
71 return Statement::error(
72 token,
73 "Unexpected token in call arguments".to_string(),
74 );
75 }
76 }
77 }
78 }
79 }
80
81 Statement {
82 kind: StatementKind::Call {
83 name: func_name,
84 args,
85 },
86 value: Value::Null,
87 indent: current_token.indent,
88 line: current_token.line,
89 column: current_token.column,
90 }
91}