nilang 0.4.1

A scripting language interpreter for Advent of Code
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use sandpit::{Gc, Mutator};

use crate::runtime::string::VMString;
use crate::symbol_map::{
    SymID, SymbolMap, ARGS_SYM, BOOL_SYM, FLOAT_SYM, FN_SYM, INT_SYM, LIST_SYM, MAP_SYM, NULL_SYM, PATCH_SYM, STR_SYM, SYM_SYM
};

use super::hash_map::GcHashMap;
use super::instruction_stream::InstructionStream;
use super::list::List;
use super::stack::Stack;
use super::type_objects::TypeObjects;
use super::value::Value;
use super::error::RuntimeErrorKind;
use super::{ByteCode};

pub fn call_intrinsic<'gc>(
    stack: &Stack<'gc>,
    args: &mut InstructionStream<'gc>,
    supplied_args: usize,
    sym_id: SymID,
    symbol_map: &mut SymbolMap,
    mu: &'gc Mutator<'gc>,
    type_objects: &TypeObjects<'gc>,
) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    match sym_id {
        FLOAT_SYM => {
            expect_arg_count(1, supplied_args)?;
            let arg = extract_arg(args, stack)?;
            float(arg)
        }
        INT_SYM => {
            expect_arg_count(1, supplied_args)?;
            let arg = extract_arg(args, stack)?;
            int(arg)
        }
        STR_SYM => {
            expect_arg_count(1, supplied_args)?;
            let arg = extract_arg(args, stack)?;
            str(arg, mu, symbol_map)
        }
        SYM_SYM => {
            expect_arg_count(1, supplied_args)?;
            let arg = extract_arg(args, stack)?;
            sym(arg, symbol_map)
        }
        BOOL_SYM => {
            expect_arg_count(1, supplied_args)?;
            let arg = extract_arg(args, stack)?;
            bbool(arg)
        }
        NULL_SYM => {
            Ok(Value::Null)
        }
        LIST_SYM => {
            match supplied_args {
                0 => Ok(Value::List(Gc::new(mu, List::alloc(mu)))),
                1 => {
                    let arg = extract_arg(args, stack)?;
                    match arg {
                        Value::List(_) => Ok(arg),
                        Value::String(vm_string) => {
                            let gc_list = Gc::new(mu, List::alloc(mu));

                            for c in 0.. vm_string.len() {
                                let c = vm_string.at(c).unwrap();
                                let char_string =  VMString::alloc([c].into_iter(), mu);
                                let value = Value::String(Gc::new(mu, char_string));

                                gc_list.push(value.as_tagged(mu), mu);
                            }

                            Ok(Value::List(gc_list))
                        }
                        Value::Map(vm_map) => {
                            let gc_list = vm_map.as_list(mu);

                            Ok(Value::List(gc_list))
                        }
                        _ => {
                            let gc_list = Gc::new(mu, List::alloc(mu));
                            gc_list.push(arg.as_tagged(mu), mu);
                            Ok(Value::List(gc_list))
                        }
                    }
                },
                _ => {
                    let gc_list = Gc::new(mu, List::alloc(mu));

                    for _ in 0..supplied_args {
                        let arg = extract_arg(args, stack)?;
                        gc_list.push(arg.as_tagged(mu), mu);
                    }

                    Ok(Value::List(gc_list))
                }
            }
        }
        FN_SYM => {
            // fn(value) → returns a 0-arg function that returns value
            expect_arg_count(1, supplied_args)?;
            let arg = extract_arg(args, stack)?;
            Ok(generate_fn_intrinsic(arg, mu))
        }
        MAP_SYM => generate_map_intrinsic(supplied_args, args, stack, mu),
       
        ARGS_SYM => get_program_args(mu), // Maybe store in a global?
                                          //
        PATCH_SYM => {
            expect_arg_count(3, supplied_args)?;
            let arg1 = extract_arg(args, stack)?;
            let arg2 = extract_arg(args, stack)?;
            let arg3 = extract_arg(args, stack)?;
            patch(arg1, arg2, arg3, type_objects, mu)
        }

        _ => {
            let intrinsic_name = symbol_map.get_str(sym_id);
            Err((
                RuntimeErrorKind::TypeError,
                format!("Unknown intrinsic: ${}", intrinsic_name)
            ))
        }
    }
}

fn extract_arg<'a, 'gc>(
    instr_stream: &mut InstructionStream<'gc>,
    stack: &Stack<'gc>
) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    if let ByteCode::StoreArg { src } = instr_stream.advance() {
        let tagged_val = stack.get_reg(src)
            .map_err(|e| (e.kind, e.message.unwrap_or_else(|| "Register access failed".to_string())))?;
        Ok(Value::from(&tagged_val))
    } else {
        Err((
            RuntimeErrorKind::InvalidByteCode,
            format!("Expected StoreArg instruction, found {:?}", instr_stream.prev())
        ))
    }
}

fn expect_arg_count(
    expected_args: usize,
    given_args: usize,
) -> Result<(), (RuntimeErrorKind, String)> {
    if expected_args != given_args {
        let msg = format!("Expected {} args, was given {}", expected_args, given_args);

        Err((RuntimeErrorKind::WrongNumArgs, msg))
    } else {
        Ok(())
    }
}

fn bbool<'gc>(arg: Value<'gc>) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    Ok(Value::Bool(arg.is_truthy()))
}

fn sym<'gc>(
    arg: Value<'gc>,
    syms: &mut SymbolMap,
) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    match arg {
        Value::String(s) => Ok(Value::SymId(syms.get_id(&s.as_string()))),
        Value::SymId(_) => Ok(arg),
        _ => {
            Err((
                RuntimeErrorKind::TypeError,
                format!("$sym() cannot convert {} to Symbol", arg.type_str()),
            ))
        }
    }
}

fn str<'gc>(
    arg: Value<'gc>,
    mu: &'gc Mutator,
    syms: &mut SymbolMap,
) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    let chars = match arg {
        Value::String(_) => return Ok(arg),
        Value::Null => "".chars(),
        Value::Bool(false) => "false".chars(),
        Value::Bool(true) => "true".chars(),
        Value::Int(i) => {
            return Ok(Value::String(Gc::new(
                mu,
                VMString::alloc(i.to_string().chars(), mu),
            )));
        }
        Value::Float(f) => {
            return Ok(Value::String(Gc::new(
                mu,
                VMString::alloc(f.to_string().chars(), mu),
            )));
        }
        Value::SymId(id) => syms.get_str(id).chars(),
        Value::Map(_) | Value::List(_) => {
            let string_repr = arg.to_string(syms, true);
            return Ok(Value::String(Gc::new(
                mu,
                VMString::alloc(string_repr.chars(), mu),
            )));
        }
        _ => {
            return Err((
                RuntimeErrorKind::TypeError,
                format!("$str() cannot convert {} to String", arg.type_str()),
            ))
        }
    };

    Ok(Value::String(Gc::new(mu, VMString::alloc(chars, mu))))
}

fn get_program_args<'gc>(mu: &'gc Mutator) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    let gc_list = Gc::new(mu, List::alloc(mu));
    let str_args: Vec<String> = std::env::args().collect();

    let mut flag = false;
    for arg in str_args.iter() {
        if flag {
            let vm_str = Value::String(Gc::new(mu, VMString::alloc(arg.chars(), mu)));

            gc_list.push(vm_str.as_tagged(mu), mu);
        } else if arg == "--" {
            flag = true;
        }
    }

    Ok(Value::List(gc_list))
}

fn float<'gc>(arg: Value<'gc>) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    match arg {
        Value::Float(_) => Ok(arg),
        Value::Int(i) => Ok(Value::Float(i as f64)),
        Value::Null => Ok(Value::Float(0.0)),
        Value::Bool(false) => Ok(Value::Float(0.0)),
        Value::Bool(true) => Ok(Value::Float(1.0)),
        Value::String(vm_str) => {
            let s = vm_str.as_string();

            if let Ok(f) = s.trim().parse::<f64>() {
                return Ok(Value::Float(f));
            }

            Ok(Value::Null)
        }
        _ => Err((
            RuntimeErrorKind::TypeError,
            format!("$float() cannot convert {} to Float", arg.type_str()),
        )),
    }
}

fn int<'gc>(arg: Value<'gc>) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    match arg {
        Value::Int(_) => Ok(arg),
        Value::Float(f) => Ok(Value::Int(f as i64)),
        Value::Null => Ok(Value::Int(0)),
        Value::Bool(false) => Ok(Value::Int(0)),
        Value::Bool(true) => Ok(Value::Int(1)),
        Value::String(vm_str) => {
            let s = vm_str.as_string();

            if let Ok(int) = s.trim().parse::<i64>() {
                return Ok(Value::Int(int));
            }

            Ok(Value::Null)
        }
        _ => Err((
            RuntimeErrorKind::TypeError,
            format!("$int() cannot convert {} to Int", arg.type_str()),
        )),
    }
}

fn convert_list_to_map<'gc>(
    list: Gc<'gc, List<'gc>>,
    mu: &'gc Mutator,
) -> Result<Gc<'gc, GcHashMap<'gc>>, (RuntimeErrorKind, String)> {
    let new_map = GcHashMap::alloc(mu);

    for i in 0..list.len() {
        let item = list.at(i);

        // Each item must be a 2-element list [key, value]
        match item {
            Value::List(pair) => {
                if pair.len() != 2 {
                    return Err((
                        RuntimeErrorKind::TypeError,
                        format!("$map() expects list of 2-element pairs, found list of length {}", pair.len())
                    ));
                }
                let key = pair.at(0);
                let val = pair.at(1);
                GcHashMap::insert(new_map.clone(), key.as_tagged(mu), val.as_tagged(mu), mu);
            }
            _ => {
                return Err((
                    RuntimeErrorKind::TypeError,
                    format!("$map() expects list of pairs, found {} in list", item.type_str())
                ));
            }
        }
    }

    Ok(new_map)
}

fn generate_map_intrinsic<'gc>(
    supplied_args: usize,
    args: &mut InstructionStream<'gc>,
    stack: &Stack<'gc>,
    mu: &'gc Mutator,
) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    match supplied_args {
        0 => {
            // map() → {}
            Ok(Value::Map(GcHashMap::alloc(mu)))
        }
        1 => {
            let arg = extract_arg(args, stack)?;
            match arg {
                Value::Map(_) => {
                    // map({a:1}) → {a:1} (identity)
                    Ok(arg)
                }
                Value::List(list) => {
                    // map([[a,1], [b,2]]) → {a:1, b:2}
                    let new_map = convert_list_to_map(list, mu)?;
                    Ok(Value::Map(new_map))
                }
                _ => {
                    Err((
                        RuntimeErrorKind::TypeError,
                        format!("$map() cannot convert {} to Map", arg.type_str())
                    ))
                }
            }
        }
        _ => {
            Err((
                RuntimeErrorKind::TypeError,
                format!("map() expects 0 or 1 argument, got {}", supplied_args)
            ))
        }
    }
}

fn generate_fn_intrinsic<'gc>(
    arg: Value<'gc>,
    mu: &'gc Mutator,
) -> Value<'gc> {
    let tagged_arg = arg.as_tagged(mu);

    // Create a function with one upvalue containing the wrapped value
    let upvalues_array = mu.alloc_array_from_fn(1, |_| tagged_arg.clone());
    let upvalues = sandpit::GcOpt::from(upvalues_array);

    // Create bytecode: load upvalue 0 into register 0, then return it
    let code = mu.alloc_array_from_fn(2, |idx| {
        match idx {
            0 => ByteCode::LoadUpvalue { dest: 0, id: 0 },
            1 => ByteCode::Return { src: 0 },
            _ => unreachable!()
        }
    });

    // Create the function
    use super::func::{Func, LoadedLocal};
    let func = Func::new(
        0,                                      // id (doesn't matter for synthetic functions)
        false,                                  // auto_binds
        0,                                      // arity (0 args)
        1,                                      // max_clique (1 register needed)
        mu.alloc_array_from_fn(0, |_| LoadedLocal::Int(0)), // no locals
        code,
        upvalues,
        sandpit::GcOpt::new_none(),            // no bound_args
        None,                                   // no spans
        None,                                   // no file_path
        false                                   // not top_level
    );

    Value::Func(sandpit::Gc::new(mu, func))
}

fn patch<'gc>(
    primitive_sym: Value<'gc>,
    key: Value<'gc>,
    value: Value<'gc>,
    type_objects: &TypeObjects<'gc>,
    mu: &'gc Mutator,
) -> Result<Value<'gc>, (RuntimeErrorKind, String)> {
    if let Value::SymId(sym_id) = primitive_sym {
        if let Some(type_obj) = type_objects.get_type_obj(sym_id) {
            if let Value::SymId(_) = key {
                GcHashMap::insert(type_obj, key.as_tagged(mu), value.as_tagged(mu), mu);

                return Ok(Value::Null);
            } else {
                return Err((
                    RuntimeErrorKind::TypeError,
                    format!("$patch() expects Symbol as key, received {}", key.type_str()),
                ));
            }
        } 
    }

    Err((
        RuntimeErrorKind::TypeError,
        "$patch() can only patch primitive type symbols".to_string(),
    ))
}