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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! The standard library implementation for the debris language
//!
//! Currently experimental.
//! There are not yet specific plans how it will look like.
//!
//! However, I plan to add at least a wrapper for every minecraft command.
use std::{collections::HashMap, rc::Rc};

use debris_common::Ident;
use debris_error::{LangErrorKind, LangResult};
use debris_llir::{
    class::{Class, ClassKind},
    function_interface::{DowncastArray, ToFunctionInterface, ValidReturnType},
    json_format::{FormattedText, JsonFormatComponent},
    llir_nodes::{
        Call, ExecuteRaw, ExecuteRawComponent, FastStore, FastStoreFromResult, Node, WriteMessage,
        WriteTarget,
    },
    minecraft_utils::ScoreboardValue,
    objects::{
        obj_bool::ObjBool,
        obj_bool_static::ObjStaticBool,
        obj_class::{HasClass, ObjClass},
        obj_format_string::{FormatStringComponent, ObjFormatString},
        obj_function::{FunctionContext, ObjFunction},
        obj_function_ref::ObjFunctionRef,
        obj_int::ObjInt,
        obj_int_static::ObjStaticInt,
        obj_module::ObjModule,
        obj_native_function::ObjNativeFunction,
        obj_null::ObjNull,
        obj_string::ObjString,
    },
    type_context::TypeContext,
    ObjectRef, Type, ValidPayload,
};

// Helper macro that can be used to match on the type of function parameters
macro_rules! match_parameters {
    ($ctx:ident, $args:ident, ($($name:ident),+): ($($type:path),+) => $cmd:expr, $($rest:tt)*) => {{
        match_parameters! { impl, [], $ctx, $args, ($($name),+): ($($type),+) => $cmd, $($rest)* }
    }};
    (error, $ctx:ident, $data:ident, ) => {};
    (error, $ctx:ident, $data:ident, [$($expected:tt)*], $($rest:tt)*) => {
        $data.push(vec![$(<$expected>::static_class($ctx.type_ctx()).to_string()),*]);
        match_parameters!(error, $ctx, $data, $($rest)*)
    };
    (impl, [$($expected:tt)*], $ctx:ident, $args:ident,) => {
        #[allow(clippy::vec_init_then_push)]
        {
            let mut data = Vec::new();
            match_parameters!{error, $ctx, data, $($expected)*}

            Err(LangErrorKind::UnexpectedOverload {
                parameters: $args.iter().map(|obj| obj.class.to_string()).collect(),
                expected: data,
                function_definition_span: None,
            })
        }
    };
    (impl, [$($expected:tt)*], $ctx:ident, $args:ident, ($($name:ident),+): ($($type:path),+) => $cmd:expr, $($rest:tt)*) => {
        if let Some(($($name),*,)) = DowncastArray::<($(&$type),*,)>::downcast_array($args) {
            Ok($cmd)
        } else {
            match_parameters! { impl, [[$($type),+], $($expected)*], $ctx, $args, $($rest)* }
        }
    };
}

fn function_for<Params, Return, T>(name: &'static str, function: T) -> ObjFunction
where
    T: ToFunctionInterface<Params, Return> + 'static,
    Return: ValidReturnType,
{
    ObjFunction::new(name, Rc::new(function.to_normalized_function().into()))
}

pub fn load_all(ctx: &TypeContext) -> HashMap<Ident, ObjectRef> {
    load(ctx).members.into_iter().collect()
}

/// Loads the standard library module
pub fn load(ctx: &TypeContext) -> ObjModule {
    let mut module = ObjModule::new("builtins");

    register_primitives(ctx, &mut module);

    module.register_function(ctx, function_for("execute", execute));
    module.register_function(ctx, function_for("print", print));
    module.register_function(ctx, function_for("dyn_int", dyn_int));
    module.register_function(ctx, function_for("on_tick", on_tick));
    module.register_function(ctx, function_for("export", export));
    module.register_function(ctx, function_for("dbg", dbg));
    module.register_function(ctx, function_for("type", get_type));

    module
}

macro_rules! register_primitives {
    ($ctx:ident, $module:ident, $($ident:literal => $class:ident),*) => {{
        $(
            $module.register($ident, ObjClass::from($class::static_class($ctx)).into_object($ctx));
        )*
    }}
}

/// Registers all primitive types
fn register_primitives(ctx: &TypeContext, module: &mut ObjModule) {
    register_primitives! {ctx, module,
        "Bool" => ObjBool,
        "ComptimeBool" => ObjStaticBool,
        "FormatString" => ObjFormatString,
        "FunctionRef" => ObjFunctionRef,
        "Int" => ObjInt,
        "ComptimeInt" => ObjStaticInt,
        "Null" => ObjNull,
        "String" => ObjString,
        "Type" => ObjClass
    };

    module.register(
        "Any",
        ObjClass::new(Class::new_empty(ClassKind::Type(Type::Any)).into()).into_object(ctx),
    );
}

fn execute(ctx: &mut FunctionContext, args: &[ObjectRef]) -> LangResult<ObjInt> {
    match_parameters! {ctx, args,
        (value): (ObjString) => execute_string(ctx, value),
        (value): (ObjFormatString) => execute_format_string(ctx, value),
    }
}

/// Executes a string as a command and returns the result
fn execute_string(ctx: &mut FunctionContext, string: &ObjString) -> ObjInt {
    let string_value = string.value();
    let return_value = ctx.item_id;

    let execute_command =
        Node::Execute(ExecuteRaw(vec![ExecuteRawComponent::String(string_value)]));
    ctx.emit(Node::FastStoreFromResult(FastStoreFromResult {
        command: execute_command.into(),
        id: return_value,
    }));

    return_value.into()
}

fn execute_format_string(ctx: &mut FunctionContext, format_string: &ObjFormatString) -> ObjInt {
    let return_value = ctx.item_id;

    let mut components = Vec::with_capacity(format_string.components.len());
    for component in &format_string.components {
        match component {
            FormatStringComponent::String(string) => {
                components.push(JsonFormatComponent::RawText(string.clone()));
            }
            FormatStringComponent::Value(obj) => obj.payload.json_fmt(&mut components),
        }
    }

    // Just convert the (possibly styled) json text into simple execute compatible components
    let components = components
        .into_iter()
        .map(|json_component| match json_component {
            JsonFormatComponent::RawText(text) => ExecuteRawComponent::String(text),
            JsonFormatComponent::Score(score) => ExecuteRawComponent::ScoreboardValue(score),
            JsonFormatComponent::Function(function) => {
                ExecuteRawComponent::Node(Node::Call(Call { id: function }))
            }
        })
        .collect();

    let execute_command = Node::Execute(ExecuteRaw(components));
    ctx.emit(Node::FastStoreFromResult(FastStoreFromResult {
        command: execute_command.into(),
        id: return_value,
    }));

    return_value.into()
}

fn print(ctx: &mut FunctionContext, parameters: &[ObjectRef]) {
    let mut components = Vec::with_capacity(parameters.len());
    match parameters {
        [] => {}
        [single] => single.payload.json_fmt(&mut components),
        [first, rest @ ..] => {
            first.payload.json_fmt(&mut components);
            let sep = ", ".into();
            for obj in rest {
                components.push(JsonFormatComponent::RawText(Rc::clone(&sep)));
                obj.payload.json_fmt(&mut components);
            }
        }
    }
    let node = Node::Write(WriteMessage {
        target: WriteTarget::Chat,
        message: FormattedText { components },
    });
    ctx.emit(node);
}

fn dyn_int(ctx: &mut FunctionContext, args: &[ObjectRef]) -> LangResult<ObjInt> {
    match_parameters! {ctx, args,
        (value): (ObjStaticInt) => static_int_to_int(ctx, value),
        (value): (ObjInt) => int_to_int(value),
        (value): (ObjStaticBool) => static_bool_to_int(ctx, value),
        (value): (ObjBool) => bool_to_int(value),
    }
}

fn static_int_to_int(ctx: &mut FunctionContext, x: &ObjStaticInt) -> ObjInt {
    ctx.emit(Node::FastStore(FastStore {
        id: ctx.item_id,
        value: ScoreboardValue::Static(x.value),
    }));

    ObjInt::from(ctx.item_id)
}

fn int_to_int(x: &ObjInt) -> ObjInt {
    x.clone()
}

fn static_bool_to_int(ctx: &mut FunctionContext, x: &ObjStaticBool) -> ObjInt {
    let value = i32::from(x.value);
    static_int_to_int(ctx, &value.into())
}

fn bool_to_int(x: &ObjBool) -> ObjInt {
    ObjInt::new(x.id)
}

/// Registers a function to run on load
fn on_tick(ctx: &mut FunctionContext, function: &ObjNativeFunction) {
    let span = ctx.span;
    ctx.runtime_mut()
        .register_ticking_function(function.function_id, span);
}

/// Exports a function to a specific path
fn export(ctx: &mut FunctionContext, parameters: &[ObjectRef]) -> LangResult<ObjectRef> {
    match_parameters!(ctx, parameters,
        (value): (ObjString) => export_impl(ctx, value.value()),
    )
}

fn export_impl(ctx: &FunctionContext, exported_path: Rc<str>) -> ObjectRef {
    let export = move |ctx: &mut FunctionContext, function: &ObjNativeFunction| {
        let span = ctx.span;
        ctx.runtime_mut()
            .export(function.function_id, exported_path.to_string(), span);
    };
    let export_function = function_for("export_inner", export);
    export_function.into_object(ctx.type_ctx())
}

fn dbg(ctx: &mut FunctionContext, parameters: &[ObjectRef]) {
    if let [param, ..] = parameters {
        let string = format!("{param:?}");
        ctx.emit(Node::Write(WriteMessage {
            target: WriteTarget::Chat,
            message: FormattedText {
                components: vec![JsonFormatComponent::RawText(string.into())],
            },
        }));
    }
}

fn get_type(_: &mut FunctionContext, params: &[ObjectRef]) -> LangResult<ObjClass> {
    if let [param] = params {
        let class = param.class.clone();
        Ok(ObjClass::new(class))
    } else {
        Err(LangErrorKind::UnexpectedOverload {
            parameters: params.iter().map(ToString::to_string).collect(),
            expected: vec![vec!["Any".to_string()]],
            function_definition_span: None,
        })
    }
}