devalang_core/core/preprocessor/resolver/
synth.rs1use crate::{
2 core::{
3 parser::statement::{ Statement, StatementKind },
4 preprocessor::{ module::Module, resolver::driver::resolve_statement },
5 shared::value::Value,
6 store::global::GlobalStore,
7 },
8 utils::logger::{ LogLevel, Logger },
9};
10
11pub fn resolve_synth(
12 stmt: &Statement,
13 module: &Module,
14 path: &str,
15 global_store: &mut GlobalStore
16) -> Statement {
17 let logger = Logger::new();
18
19 let Value::Map(synth_map) = &stmt.value else {
20 return type_error(&logger, module, stmt, "Expected a map in synth statement".to_string());
21 };
22
23 let mut resolved_map = synth_map.clone();
24
25 if let Some(Value::Block(body)) = synth_map.get("body") {
26 let resolved_body = body.iter()
27 .map(|s| resolve_statement(s, module, path, global_store))
28 .collect::<Vec<_>>();
29 resolved_map.insert("body".to_string(), Value::Block(resolved_body));
30 } else {
31 logger.log_message(LogLevel::Warning, "synth without a body");
32 }
33
34 Statement {
35 kind: StatementKind::Synth,
36 value: Value::Map(resolved_map),
37 ..stmt.clone()
38 }
39}
40
41fn type_error(logger: &Logger, module: &Module, stmt: &Statement, message: String) -> Statement {
42 let stacktrace = format!("{}:{}:{}", module.path, stmt.line, stmt.column);
43 logger.log_error_with_stacktrace(&message, &stacktrace);
44
45 Statement {
46 kind: StatementKind::Error { message },
47 value: Value::Null,
48 ..stmt.clone()
49 }
50}