aboutsummaryrefslogtreecommitdiffstats
path: root/src/cexpr.rs
blob: bac316cf1a58ea5990079272bda9d92db48c3ba7 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Canonical Expressions
 *
 * AKA, simplified, normalized algebraic expressions.
 *
 */

use crate::sexpr::SExpr;

pub enum NumericConstant {
    Pi, // 3.141592...
    E,  // 2.718281...
    Infinity,
}

#[derive(Clone, PartialEq)]
pub enum CNumber {
    Integer(i64),
    Rational(i64, u64),
    // Float
    // Constant
}

impl CNumber {
    pub fn to_sexpr(&self) -> Result<SExpr, String> {
        match self {
            CNumber::Integer(v) => Ok(SExpr::SInteger(*v)),
            CNumber::Rational(a, b) => Ok(SExpr::SList(vec![
                SExpr::SBuiltin("/".to_string()),
                SExpr::SInteger(*a),
                SExpr::SInteger(*b as i64),
            ])),
        }
    }
}

#[derive(Clone, PartialEq)]
pub enum CExpr {
    Symbol(String),
    Number(CNumber),
    Sum(Option<CNumber>, Vec<CExpr>),
    Product(Option<CNumber>, Vec<CExpr>),
    Power(Box<CExpr>, Box<CExpr>),
    Factorial(Box<CExpr>),
    UnaryFunction(String, Box<CExpr>),

    // TODO: Infinity?
    // TODO: Limit?
    // TODO: Vector?
}

impl CExpr {

    pub fn from_sexpr(sexpr: &SExpr) -> Result<CExpr, String> {

        // not all cases are handled; some atoms are covered trivialy
        match sexpr {
            SExpr::SNull => Err("null not handled".to_string()),
            SExpr::STrue | SExpr::SFalse => Err("booleans not handled".to_string()),
            SExpr::SInteger(v) => Ok(CExpr::Number(CNumber::Integer(*v))),
            SExpr::SFloat(v) => Err("floats not handled".to_string()),
            SExpr::SBuiltin(v) => Ok(CExpr::Symbol(v.to_string())),
            SExpr::SSymbol(v) => Err(format!("symbols (quoted identifiers) not handled: {}", v)),
            SExpr::SIdentifier(v) => Ok(CExpr::Symbol(v.to_string())),
            SExpr::SString(_) => Err("null not handled".to_string()),
            SExpr::SList(_) => Err("null not handled".to_string()),
            //SExpr::SList(l) => CExpr::from_sexpr_list(l),
        }
    }

/*
    pub fn from_sexpr_list(list: &Vec<SExpr>) -> Result<CExpr, String> {
        if list.is_empty() {
            unimplemented!()
        }
        match list[0] {
            SExpr::SBuiltin("+") => {
                Ok(CExpr::Sum(
                    // XXX
                    None,
                    list[1..].iter().map(|v| CExpr::from_sexpr(v)).collect::<Result<Vec<SExpr>, String>>()?,
                ))
            },
            SExpr::SBuiltin("*") => {
            },
            SExpr::SBuiltin("^") => {
            },
            _ => {
                unimplemented!()
            }
        }
    }
*/

    pub fn new_sum(list: &Vec<SExpr>) -> Result<CExpr, String> {
        unimplemented!()
    }

    pub fn new_product(list: &Vec<SExpr>) -> Result<CExpr, String> {
        unimplemented!()
    }

    pub fn to_sexpr(&self) -> Result<SExpr, String> {
        match self {
            CExpr::Symbol(s) => Ok(SExpr::SIdentifier(s.to_string())),
            CExpr::Number(n) => n.to_sexpr(),
            CExpr::Sum(n, l) => Ok(SExpr::SList(vec![
                SExpr::SBuiltin("+".to_string()),
                SExpr::SList(l.iter().map(|v| v.to_sexpr()).collect::<Result<Vec<SExpr>, String>>()?),
                // XXX: n
            ])),
            CExpr::Product(n, l) => Ok(SExpr::SList(vec![
                SExpr::SBuiltin("*".to_string()),
                SExpr::SList(l.iter().map(|v| v.to_sexpr()).collect::<Result<Vec<SExpr>, String>>()?),
                // XXX: n
            ])),
            CExpr::Power(a, b) => Ok(SExpr::SList(vec![
                SExpr::SBuiltin("^".to_string()),
                a.to_sexpr()?,
                b.to_sexpr()?,
            ])),
            CExpr::Factorial(v) => Ok(SExpr::SList(vec![
                SExpr::SIdentifier("factorial".to_string()),
                v.to_sexpr()?,
            ])),
            CExpr::UnaryFunction(s, v) => Ok(SExpr::SList(vec![
                SExpr::SIdentifier(s.to_string()),
                v.to_sexpr()?,
            ])),
        }
    }
}