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
use std::cell::RefCell;
use std::rc::Rc;

use crate::class::Class;
use crate::expect_args;
use crate::instance::Instance;
use crate::invokable::Return;
use crate::primitives::PrimitiveFn;
use crate::universe::Universe;
use crate::value::Value;
use crate::SOMRef;

pub static INSTANCE_PRIMITIVES: &[(&str, PrimitiveFn, bool)] = &[
    ("new", self::new, true),
    ("name", self::name, true),
    ("fields", self::fields, true),
    ("methods", self::methods, true),
    ("superclass", self::superclass, true),
];
pub static CLASS_PRIMITIVES: &[(&str, PrimitiveFn, bool)] = &[];

fn superclass(_: &mut Universe, args: Vec<Value>) -> Return {
    const SIGNATURE: &str = "Class>>#superclass";

    expect_args!(SIGNATURE, args, [
        Value::Class(class) => class,
    ]);

    let super_class = class.borrow().super_class();
    Return::Local(super_class.map(Value::Class).unwrap_or(Value::Nil))
}

fn new(_: &mut Universe, args: Vec<Value>) -> Return {
    const SIGNATURE: &str = "Class>>#new";

    expect_args!(SIGNATURE, args, [
        Value::Class(class) => class,
    ]);

    let instance = Instance::from_class(class);
    let instance = Rc::new(RefCell::new(instance));
    Return::Local(Value::Instance(instance))
}

fn name(universe: &mut Universe, args: Vec<Value>) -> Return {
    const SIGNATURE: &str = "Class>>#name";

    expect_args!(SIGNATURE, args, [
        Value::Class(class) => class,
    ]);

    let sym = universe.intern_symbol(class.borrow().name());
    Return::Local(Value::Symbol(sym))
}

fn methods(_: &mut Universe, args: Vec<Value>) -> Return {
    const SIGNATURE: &str = "Class>>#methods";

    expect_args!(SIGNATURE, args, [
        Value::Class(class) => class,
    ]);

    let methods = class
        .borrow()
        .methods
        .values()
        .map(|invokable| Value::Invokable(invokable.clone()))
        .collect();

    Return::Local(Value::Array(Rc::new(RefCell::new(methods))))
}

fn fields(universe: &mut Universe, args: Vec<Value>) -> Return {
    const SIGNATURE: &str = "Class>>#fields";

    expect_args!(SIGNATURE, args, [
        Value::Class(class) => class,
    ]);

    fn gather_locals(universe: &mut Universe, class: SOMRef<Class>) -> Vec<Value> {
        let mut fields = match class.borrow().super_class() {
            Some(super_class) => gather_locals(universe, super_class),
            None => Vec::new(),
        };
        fields.extend(
            class
                .borrow()
                .locals
                .keys()
                .map(|field| Value::Symbol(universe.intern_symbol(field))),
        );
        fields
    }

    let fields = gather_locals(universe, class);

    Return::Local(Value::Array(Rc::new(RefCell::new(fields))))
}

/// Search for an instance primitive matching the given signature.
pub fn get_instance_primitive(signature: &str) -> Option<PrimitiveFn> {
    INSTANCE_PRIMITIVES
        .iter()
        .find(|it| it.0 == signature)
        .map(|it| it.1)
}

/// Search for a class primitive matching the given signature.
pub fn get_class_primitive(signature: &str) -> Option<PrimitiveFn> {
    CLASS_PRIMITIVES
        .iter()
        .find(|it| it.0 == signature)
        .map(|it| it.1)
}