vexity 0.0.4

Tiny scripting language for hacking on abstractions of financial markets.
Documentation
use vexity::{
    interpreter::{Interpreter, VarValue},
    parser::parse,
};

#[test]
fn test_basic_hashmap_access() {
    let script = r#"
        let person = { name: "Alice", age: 30, active: true }

        let name = person.name
        let age = person.age
        let active = person.active
    "#;

    let statements = parse(script).expect("Parsing failed");
    let mut interpreter = Interpreter::new();
    interpreter.run(statements);

    let name = interpreter
        .vars
        .get("name")
        .and_then(|value| match value {
            VarValue::String(s) => Some(s),
            _ => None,
        })
        .expect("Variable `name` not found");
    assert_eq!(name.as_str(), "Alice");

    let age = interpreter
        .vars
        .get("age")
        .and_then(|value| match value {
            VarValue::Int(i) => Some(*i),
            _ => None,
        })
        .expect("Variable `age` not found");
    assert_eq!(age, 30);

    let active = interpreter
        .vars
        .get("active")
        .and_then(|value| match value {
            VarValue::Bool(b) => Some(*b),
            _ => None,
        })
        .expect("Variable `active` not found");
    assert!(active);
}

#[test]
fn test_nested_hashmap_access() {
    let script = r#"
        let user = {
            id: 42,
            profile: {
                username: "vex_user",
                location: "NYC"
            }
        }

        let username = user.profile.username
        let location = user.profile.location
    "#;

    let statements = parse(script).expect("Parsing failed");
    let mut interpreter = Interpreter::new();
    interpreter.run(statements);

    let username = interpreter
        .vars
        .get("username")
        .and_then(|value| match value {
            VarValue::String(s) => Some(s),
            _ => None,
        })
        .expect("Variable `username` not found");
    assert_eq!(username.as_str(), "vex_user");

    let location = interpreter
        .vars
        .get("location")
        .and_then(|value| match value {
            VarValue::String(s) => Some(s),
            _ => None,
        })
        .expect("Variable `location` not found");
    assert_eq!(location.as_str(), "NYC");
}