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
//! A mini language that extends `tinylang` with more types and stdlib
//! functions.

#[cfg(feature = "parse")]
pub(crate) mod parser;
pub mod stdlib;
pub mod types;
pub use super::tinylang;
pub use super::tinylang::TinyLang;
use crate::Interpreter;

/// Extends `TinyLang` with more stdlib functions and types.
#[derive(Clone)]
pub struct MiniLang;

impl Interpreter for MiniLang {
    type Value = <TinyLang as Interpreter>::Value;
    type Error = <TinyLang as Interpreter>::Error;
    type Env = <TinyLang as Interpreter>::Env;
    type Expr = <TinyLang as Interpreter>::Expr;

    fn parse(code: &str) -> Result<Self::Expr, Self::Error> {
        #[cfg(feature = "parse")]
        {
            parser::parse(code)
        }
        #[cfg(not(feature = "parse"))]
        {
            let _ = code;
            Err("parse() requires 'parse' feature".into())
        }
    }

    fn global_env() -> Self::Env {
        let env = TinyLang::global_env();
        stdlib::register(&env);
        env
    }

    fn evaluate(env: &Self::Env, expr: &Self::Expr) -> Result<Self::Value, Self::Error> {
        TinyLang::evaluate(env, expr)
    }
}