devalang_wasm/engine/audio/evaluator/
mod.rs1use crate::language::syntax::ast::Value;
3use std::collections::HashMap;
4
5pub fn evaluate_condition(condition: &Value, _context: &HashMap<String, Value>) -> bool {
7 match condition {
8 Value::Boolean(b) => *b,
9 Value::Number(n) => *n != 0.0,
10 Value::String(s) | Value::Identifier(s) => !s.is_empty(),
11 Value::Null => false,
12 _ => true,
13 }
14}
15
16pub fn evaluate_number(expr: &Value, context: &HashMap<String, Value>) -> f32 {
18 match expr {
19 Value::Number(n) => *n,
20 Value::Identifier(name) => {
21 if let Some(val) = context.get(name) {
22 evaluate_number(val, context)
23 } else {
24 0.0
25 }
26 }
27 Value::String(s) => s.parse::<f32>().unwrap_or(0.0),
28 Value::Boolean(b) => {
29 if *b {
30 1.0
31 } else {
32 0.0
33 }
34 }
35 _ => 0.0,
36 }
37}
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42
43 #[test]
44 fn test_evaluate_condition_boolean() {
45 let ctx = HashMap::new();
46 assert!(evaluate_condition(&Value::Boolean(true), &ctx));
47 assert!(!evaluate_condition(&Value::Boolean(false), &ctx));
48 }
49
50 #[test]
51 fn test_evaluate_number() {
52 let mut ctx = HashMap::new();
53 ctx.insert("x".to_string(), Value::Number(42.0));
54
55 let result = evaluate_number(&Value::Identifier("x".to_string()), &ctx);
56 assert!((result - 42.0).abs() < f32::EPSILON);
57 }
58}