aboutsummaryrefslogtreecommitdiffstats
path: root/src/builtins.rs
blob: 087e629c91a2e533dc1d8288cc4ebd4d164d435f (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::ast;
use super::runtime::{advance_v, Term, Value};
use std::fmt;
use std::rc::Rc;

#[derive(Debug, Clone, PartialEq)]
pub struct Builtin {
    pub identifier: &'static str,
    pub repr_name: &'static str,
    pub arguments: Vec<Value>,
}

impl Builtin {
    pub fn new(identifier: &'static str) -> Builtin {
        Builtin {
            identifier: identifier,
            repr_name: identifier,
            arguments: vec![],
        }
    }

    // pub fn bind_arg(self: &Builtin, arg: &Value) -> Builtin {
    //     let mut new_args = self.arguments.clone();
    //     new_args.push(arg.clone());

    //     Builtin {
    //         identifier: new_identifier,
    //         repr_name: self.repr_name,
    //         arguments: new_args,
    //     }
    // }
}

impl fmt::Display for Builtin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut arg_str = String::new();
        for argument in &self.arguments {
            arg_str.push_str(format!(" {}", argument).as_str());
        }

        write!(f, "Builtin({}){}", self.repr_name, arg_str)
    }
}

fn argument_error(builtin_id: &'static str, arg: &Value) -> Result<(Rc<Term>, usize), String> {
    Err(format!(
        "[runtime] cannot apply builtin {} to argument {}",
        builtin_id, arg
    ))
}

pub const B_INTEGER_EQ: &str = "int.eq?";
pub const B_INTEGER_EQ_1: &str = "##int.eq?_1";
pub const B_INTEGER_INCREMENT: &str = "int.increment";
pub const B_INTEGER_ADD: &str = "int.add";
pub const B_INTEGER_ADD_1: &str = "##int.add_1";

pub fn make_boolean_true_function() -> Term {
    let x = advance_v();
    let y = advance_v();
    Term::Abstraction(x, Rc::new(Term::Abstraction(y, Rc::new(Term::Variable(x)))))
}

pub fn make_boolean_false_function() -> Term {
    let x = advance_v();
    let y = advance_v();
    Term::Abstraction(x, Rc::new(Term::Abstraction(y, Rc::new(Term::Variable(y)))))
}

pub fn make_identity_function() -> Term {
    let v = advance_v();
    Term::Abstraction(v, Rc::new(Term::Variable(v)))
}

pub fn try_ast_symbol_to_builtin_term(symbol: &ast::Symbol) -> Option<Term> {
    let builtin = match symbol.as_str() {
        "true" => return Some(make_boolean_true_function()),
        "false" => return Some(make_boolean_false_function()),
        "id" => return Some(make_identity_function()),
        B_INTEGER_EQ => Builtin::new(B_INTEGER_EQ),
        B_INTEGER_INCREMENT => Builtin::new(B_INTEGER_INCREMENT),
        B_INTEGER_ADD => Builtin::new(B_INTEGER_ADD),
        _ => return None,
    };

    Some(Term::Builtin(builtin))
}

pub fn evaluate_builtin(builtin: &Builtin, rhs: Rc<Term>) -> Result<(Rc<Term>, usize), String> {
    let result_term = match builtin.identifier {
        B_INTEGER_EQ => match &*rhs {
            Term::Primitive(primitive) => match primitive {
                Value::Integer(_) => {
                    let new_builtin = Builtin {
                        identifier: B_INTEGER_EQ_1,
                        repr_name: builtin.identifier,
                        arguments: vec![primitive.clone()],
                    };
                    Term::Builtin(new_builtin)
                }
                other => return argument_error(builtin.identifier, other),
            },
            _ => return Ok((Rc::new(Term::Builtin(builtin.clone())), 0)),
        },
        B_INTEGER_EQ_1 => match &*rhs {
            Term::Primitive(primitive) => match primitive {
                Value::Integer(value) => {
                    assert_eq!(builtin.arguments.len(), 1);
                    let other = &builtin.arguments[0];
                    match other {
                        Value::Integer(other_value) => {
                            if value == other_value {
                                make_boolean_true_function()
                            } else {
                                make_boolean_false_function()
                            }
                        }
                        other => return argument_error(builtin.identifier, other),
                    }
                }
                other => return argument_error(builtin.identifier, other),
            },
            _ => return Ok((Rc::new(Term::Builtin(builtin.clone())), 0)),
        },
        B_INTEGER_INCREMENT => match &*rhs {
            Term::Primitive(primitive) => match primitive {
                Value::Integer(value) => Term::Primitive(Value::Integer(value + 1)),
                other => return argument_error(builtin.identifier, other),
            },
            _ => return Ok((Rc::new(Term::Builtin(builtin.clone())), 0)),
        },
        B_INTEGER_ADD => match &*rhs {
            Term::Primitive(primitive) => match primitive {
                Value::Integer(_) => {
                    let new_builtin = Builtin {
                        identifier: B_INTEGER_ADD_1,
                        repr_name: builtin.identifier,
                        arguments: vec![primitive.clone()],
                    };
                    Term::Builtin(new_builtin)
                }
                other => return argument_error(builtin.identifier, other),
            },
            _ => return Ok((Rc::new(Term::Builtin(builtin.clone())), 0)),
        },
        B_INTEGER_ADD_1 => match &*rhs {
            Term::Primitive(primitive) => match primitive {
                Value::Integer(value) => {
                    assert_eq!(builtin.arguments.len(), 1);
                    let summand = &builtin.arguments[0];
                    match summand {
                        Value::Integer(summand_value) => {
                            Term::Primitive(Value::Integer(summand_value + value))
                        }
                        other => return argument_error(builtin.identifier, other),
                    }
                }
                other => return argument_error(builtin.identifier, other),
            },
            _ => return Ok((Rc::new(Term::Builtin(builtin.clone())), 0)),
        },
        _ => {
            return Err(format!(
                "[runtime] invalid builtin evaluated: {}",
                builtin.identifier
            ))
        }
    };

    Ok((Rc::new(result_term), 1))
}