aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/env.rs
blob: ad7cc2b922ab53a5a14e5d8ba1fa575220f02fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::{
    app::AppState,
    lisp::{error::LispError, expr::LispExpr, number::LispNumber, Environment},
};

pub fn is_bound<S: AsRef<str>>(env: &mut Environment, name: S) -> bool {
    env.get(name.as_ref()).is_some()
}

pub fn new_binding<S: AsRef<str>>(env: &mut Environment, name: S, value: LispExpr) {
    let _ = env.insert(name.as_ref().into(), value);
}

pub fn with_prelude() -> Environment {
    let mut env = Environment::new();
    new_binding(
        &mut env,
        "+",
        LispExpr::PrimitiveFunc(|args, _| {
            if args.iter().any(|arg| !matches!(arg, LispExpr::Number(_))) {
                Err(LispError::EvalError)
            } else {
                let result = args
                    .iter()
                    .map(|n| unwrap_number(n))
                    .fold(LispNumber::Integer(0), |acc, x| acc + *x);
                Ok(LispExpr::Number(result))
            }
        }),
    );
    env
}

pub fn eval(expr: &LispExpr, app: &mut AppState) -> Result<LispExpr, LispError> {
    let env = &mut app.lisp_env;
    match expr {
        LispExpr::Unit => Ok(expr.clone()),
        LispExpr::StringLit(_) => Ok(expr.clone()),
        LispExpr::Number(_) => Ok(expr.clone()),
        LispExpr::BoolLit(_) => Ok(expr.clone()),
        LispExpr::Ident(ref id) => env
            .get(id)
            .ok_or_else(|| LispError::EvalError)
            .map(Clone::clone),
        LispExpr::List(li) => {
            let head = &li[0];
            match head {
                LispExpr::Ident(func) => {
                    let func_expr = env
                        .get(func)
                        .map(Clone::clone)
                        .ok_or_else(|| LispError::EvalError)?;
                    match func_expr {
                        LispExpr::PrimitiveFunc(f) => {
                            let mut args = Vec::new();
                            for item in li[1..].iter() {
                                args.push(eval(item, app)?);
                            }
                            (f)(&args, None)
                        }
                        _ => Err(LispError::EvalError),
                    }
                }
                LispExpr::List(_) => {
                    // TODO
                    todo!();
                }
                _ => Err(LispError::EvalError),
            }
        }
        _ => Err(LispError::ParseError),
    }
}

pub fn unwrap_number(n: &LispExpr) -> &LispNumber {
    match n {
        LispExpr::Number(i) => i,
        _ => panic!("unwrap_number expected number"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn eval_primitive_call() {
        let mut env = Environment::new();
        new_binding(&mut env, "age", LispExpr::Number(LispNumber::Float(1.4)));
        new_binding(
            &mut env,
            "+",
            LispExpr::PrimitiveFunc(|args, _| {
                if args.iter().any(|arg| !matches!(arg, LispExpr::Number(_))) {
                    Err(LispError::EvalError)
                } else {
                    let result = args
                        .iter()
                        .map(|n| unwrap_number(n))
                        .fold(LispNumber::Integer(0), |acc, x| acc + *x);
                    Ok(LispExpr::Number(result))
                }
            }),
        );
        let mut numbers = (1..=3)
            .map(LispNumber::Integer)
            .map(LispExpr::Number)
            .collect::<Vec<_>>();
        let mut expr = Vec::new();
        expr.push(LispExpr::Ident("+".into()));
        expr.append(&mut numbers);
        // assert!(matches!(
        //     eval(&LispExpr::List(expr), ).unwrap(),
        //     LispExpr::Number(LispNumber::Integer(6))
        // ));
    }
}