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
use crate::context::Context;
use crate::value::Value;

pub trait Builtin {
    fn run(&mut self, name: &str, ctx: &mut Context);
    fn print(&mut self, v: Value);
    fn new_line(&mut self);
    fn wait(&mut self);
}

impl<'a, B: Builtin> Builtin for &'a mut B {
    #[inline(always)]
    fn run(&mut self, name: &str, ctx: &mut Context) {
        (**self).run(name, ctx);
    }
    #[inline(always)]
    fn print(&mut self, v: Value) {
        (**self).print(v);
    }
    #[inline(always)]
    fn new_line(&mut self) {
        (**self).new_line();
    }
    #[inline(always)]
    fn wait(&mut self) {
        (**self).wait();
    }
}

pub struct DummyBuiltin;

impl Builtin for DummyBuiltin {
    #[inline(always)]
    fn run(&mut self, _name: &str, _ctx: &mut Context) {}
    #[inline(always)]
    fn print(&mut self, _v: Value) {}
    #[inline(always)]
    fn new_line(&mut self) {}
    #[inline(always)]
    fn wait(&mut self) {}
}

pub struct RecordBuiltin(String);

impl RecordBuiltin {
    #[inline(always)]
    pub fn new() -> Self {
        Self(String::with_capacity(8196))
    }

    #[inline(always)]
    pub fn text(&self) -> &str {
        &self.0
    }
}

impl Builtin for RecordBuiltin {
    #[inline(always)]
    fn run(&mut self, name: &str, _ctx: &mut Context) {
        self.0.push_str(name);
    }
    #[inline(always)]
    fn print(&mut self, v: Value) {
        use std::fmt::Write;
        write!(self.0, "{}", v).unwrap();
    }
    #[inline(always)]
    fn new_line(&mut self) {
        self.0.push('@');
    }
    #[inline(always)]
    fn wait(&mut self) {
        self.0.push('#');
    }
}