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

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