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
use crate::frame::FrameKind;
use crate::interpreter::Interpreter;
use crate::primitives::PrimitiveFn;
use crate::universe::Universe;
use crate::value::Value;
use crate::{expect_args, reverse};

/// Primitives for the **Block** and **Block1** class.
pub mod block1 {
    use super::*;

    pub static INSTANCE_PRIMITIVES: &[(&str, PrimitiveFn, bool)] = &[
        ("value", self::value, true),
        ("restart", self::restart, false),
    ];
    pub static CLASS_PRIMITIVES: &[(&str, PrimitiveFn, bool)] = &[];

    fn value(interpreter: &mut Interpreter, _: &mut Universe) {
        const SIGNATURE: &str = "Block1>>#value";

        expect_args!(SIGNATURE, interpreter, [
            Value::Block(block) => block,
        ]);

        let kind = FrameKind::Block {
            block: block.clone(),
        };

        interpreter.push_frame(kind);
    }

    fn restart(interpreter: &mut Interpreter, _: &mut Universe) {
        const SIGNATURE: &str = "Block>>#restart";

        expect_args!(SIGNATURE, interpreter, [Value::Block(_)]);

        let frame = interpreter.current_frame().expect("no current frame");
        frame.borrow_mut().bytecode_idx = 0;
    }

    /// 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)
    }
}

/// Primitives for the **Block2** class.
pub mod block2 {
    use super::*;

    pub static INSTANCE_PRIMITIVES: &[(&str, PrimitiveFn, bool)] = &[("value:", self::value, true)];
    pub static CLASS_PRIMITIVES: &[(&str, PrimitiveFn, bool)] = &[];

    fn value(interpreter: &mut Interpreter, _: &mut Universe) {
        const SIGNATURE: &str = "Block2>>#value:";

        expect_args!(SIGNATURE, interpreter, [
            Value::Block(block) => block,
            argument => argument,
        ]);

        let kind = FrameKind::Block {
            block: block.clone(),
        };

        let frame = interpreter.push_frame(kind);
        frame.borrow_mut().args.push(argument);
    }

    /// 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)
    }
}

/// Primitives for the **Block3** class.
pub mod block3 {
    use super::*;

    pub static INSTANCE_PRIMITIVES: &[(&str, PrimitiveFn, bool)] =
        &[("value:with:", self::value_with, true)];
    pub static CLASS_PRIMITIVES: &[(&str, PrimitiveFn, bool)] = &[];

    fn value_with(interpreter: &mut Interpreter, _: &mut Universe) {
        const SIGNATURE: &str = "Block3>>#value:with:";

        expect_args!(SIGNATURE, interpreter, [
            Value::Block(block) => block,
            argument1 => argument1,
            argument2 => argument2,
        ]);

        let kind = FrameKind::Block {
            block: block.clone(),
        };

        let frame = interpreter.push_frame(kind);
        frame.borrow_mut().args.push(argument1);
        frame.borrow_mut().args.push(argument2);
    }

    /// 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)
    }
}