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
use std::collections::HashMap;

use std::rc::Rc;

use environment::Environment;

#[derive(Debug, PartialEq)]
pub struct RispError(String);

pub type RispResult = Result<RispType, RispError>;


#[derive(Debug, PartialEq, Clone)]
pub enum RispType {
    Nil,
    Bool(bool),
    Int(i64),
    Str(String),
    List(Vec<RispType>),
    Vector(Vec<RispType>),
    Map(HashMap<String, RispType>),
    Keyword(String),
    Symbol(String),
    Function(fn(Vec<RispType>) -> RispResult),
    RispFunction(RispFunc),
}

#[derive(Debug, PartialEq, Clone)]
pub struct RispFunc {
    pub args: Vec<RispType>,
    pub variadic_arg: Option<String>,
    pub body: Rc<RispType>,
    pub env: Environment
}


pub fn error<S: Into<String>>(message: S) -> RispError {
    RispError(message.into())
}

pub fn error_result<S: Into<String>>(message: S) -> RispResult {
    Err(error(message))
}

pub fn symbol<S: Into<String>>(s: S) -> RispType {
    RispType::Symbol(s.into())
}

pub fn keyword<S: Into<String>>(s: S) -> RispType {
    RispType::Keyword(s.into())
}

pub fn string<S: Into<String>>(s: S) -> RispType {
    RispType::Str(s.into())
}

#[allow(dead_code)]
pub fn map<S: Into<String>>(pairs: Vec<(S, RispType)>) -> RispType {
    let result: HashMap<String, RispType> = pairs.into_iter()
        .map(|(s, r)| (s.into(), r))
        .collect();
    RispType::Map(result)
}