devalang_core/core/parser/handler/identifier/
synth.rs

1use std::collections::HashMap;
2
3use crate::core::{
4    lexer::token::Token,
5    parser::{
6        driver::Parser,
7        statement::{Statement, StatementKind},
8    },
9    shared::value::Value,
10    store::global::GlobalStore,
11};
12
13pub fn parse_synth_token(
14    parser: &mut Parser,
15    _current_token: Token,
16    _global_store: &mut GlobalStore,
17) -> Statement {
18    parser.advance(); // consume 'synth'
19
20    let Some(synth_token) = parser.previous_clone() else {
21        return Statement::unknown();
22    };
23
24    // Expect an identifier (synth waveform)
25    let Some(identifier_token) = parser.peek_clone() else {
26        return Statement::error(synth_token, "Expected identifier after 'synth'".to_string());
27    };
28
29    let synth_waveform = identifier_token.lexeme.clone();
30
31    parser.advance(); // consume identifier
32
33    // Expect synth optional parameters map
34    let parameters = if let Some(params) = parser.parse_map_value() {
35        // If parameters are provided, we expect a map
36        if let Value::Map(map) = params {
37            map
38        } else {
39            return Statement::error(
40                synth_token,
41                "Expected a map for synth parameters".to_string(),
42            );
43        }
44    } else {
45        // If no parameters are provided, we can still create the statement with an empty map
46        HashMap::new()
47    };
48
49    Statement {
50        kind: StatementKind::Synth,
51        value: Value::Map(HashMap::from([
52            ("entity".to_string(), Value::String("synth".to_string())),
53            (
54                "value".to_string(),
55                Value::Map(HashMap::from([
56                    ("waveform".to_string(), Value::String(synth_waveform)),
57                    ("parameters".to_string(), Value::Map(parameters)),
58                ])),
59            ),
60        ])),
61        indent: synth_token.indent,
62        line: synth_token.line,
63        column: synth_token.column,
64    }
65}