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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use sandpit::{Gc, Mutator, Tag, Tagged, Trace};

use super::func::Func;
use super::hash_map::GcHashMap;
use super::list::List;
use super::native_func::NativeFunc;
use super::string::VMString;
use super::value::Value;

#[derive(Trace, Clone)]
pub struct TaggedValue<'gc> {
    pub ptr: Tagged<'gc, ValueTag>
}

#[derive(Debug, Tag, PartialEq)]
pub enum ValueTag {
    Packed,
    #[ptr(f64)]
    Float,
    #[ptr(i64)]
    Int,
    #[ptr(List<'gc>)]
    List,
    #[ptr(Func<'gc>)]
    Func,
    #[ptr(VMString<'gc>)]
    String,
    #[ptr(GcHashMap<'gc>)]
    Map,
    #[ptr(NativeFunc)]
    NativeFunc,
}

impl<'gc> From<&TaggedValue<'gc>> for Value<'gc> {
    fn from(value: &TaggedValue<'gc>) -> Self {
        let ptr = value.ptr.clone();
        match ptr.get_tag() {
            ValueTag::Float => {
                let v = ValueTag::get_float(ptr).unwrap();

                Value::Float(*v)
            }
            ValueTag::Func => {
                let v = ValueTag::get_func(ptr).unwrap();

                Value::Func(v)
            }
            ValueTag::Int => {
                let v = ValueTag::get_int(ptr).unwrap();

                Value::Int(*v)
            }
            ValueTag::List => {
                let v = ValueTag::get_list(ptr).unwrap();

                Value::List(v)
            }
            ValueTag::String => {
                let v = ValueTag::get_string(ptr).unwrap();

                Value::String(v)
            }
            ValueTag::Map => {
                let v = ValueTag::get_map(ptr).unwrap();

                Value::Map(v)
            }
            ValueTag::NativeFunc => {
                let v = ValueTag::get_nativefunc(ptr).unwrap();

                Value::NativeFunc(v)
            }
            ValueTag::Packed => {
                let raw = ptr.get_stripped_raw() as u64;

                TaggedValue::unpack(raw)
            }
        }
    }
}

// PACKED VALUE LAYOUT
// size = 8 bytes
// first 3 bits are used by the 'primary' tag, ValueTag::Packed
//
// There are 5 secondary tags meaning the next 3 bits after the primary tag
// are used for the secondary Tag
//
// Of the entire 64 bit value, 6 bits are used in tagged leaving 58 bits.
// The packed value is then stored in the 32 top bits. The value could use all 58 bits,
// specifically the i32 could be grown into a i58, but then we would need special
// overflow checking logic which I didn't feel like implementing.
//
// Value (32 bits)                                                  Secondary Tag (3 bits) => PackedTag::_
// |                                                                  |
// |                                                                  |  Primary Tag (3 bits) == ValueTag::PackedTag
// |                                                                  |   |
// V                                                                  V   V
// -----------------------------------                               --- ---
// 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00 000 000

enum PackedTag {
    SymId,
    Int,
    Bool,
    Null,
}

impl<'gc> TaggedValue<'gc> {
    pub fn __new(ptr: Tagged<'gc, ValueTag>) -> Self {
        Self { ptr }
    }

    pub fn __get_ptr(&self) -> Tagged<'gc, ValueTag> {
        self.ptr.clone()
    }

    pub fn new_null() -> Self {
        let raw: u64 = (PackedTag::Null as u64) << 3;
        let ptr = Tagged::from_raw(raw as usize, ValueTag::Packed);

        Self {
            ptr
        }
    }

    pub fn new_bool(b: bool) -> Self {
        let mut raw: u64 = (PackedTag::Bool as u64) << 3;

        if b {
            let value_mask: u64 = (u32::MAX as u64) ^ u64::MAX;
            raw ^= value_mask;
        }

        let ptr = Tagged::from_raw(raw as usize, ValueTag::Packed);

        Self { ptr }
    }

    pub fn from_value(value: Value<'gc>, mu: &'gc Mutator) -> Self {
        if let Some(tagged) = Self::try_packing(&value) {
            return tagged;
        }

        let ptr = 
        match value {
            Value::List(gc_list) => ValueTag::from_list(gc_list),
            Value::Func(func) => ValueTag::from_func(func),
            Value::Float(f) => ValueTag::from_float(Gc::new(mu, f)),
            Value::String(s) => ValueTag::from_string(s),
            Value::Map(c) => ValueTag::from_map(c),
            Value::NativeFunc(nf) => ValueTag::from_nativefunc(nf),
            Value::Int(i) => ValueTag::from_int(Gc::new(mu, i)),
            _ => panic!("failed to convert value into tagged value"),
        };

        Self { ptr }
    }

    pub fn set_null(&self) {
        let raw: u64 = (PackedTag::Null as u64) << 3;

        self.ptr.set_raw(raw as usize, ValueTag::Packed);
    }

    pub fn is_truthy(&self) -> bool {
        match self.ptr.get_tag() {
            ValueTag::Packed => {
                let raw = self.ptr.get_stripped_raw() as u64;

                match TaggedValue::unpack(raw) {
                    Value::Null | 
                    Value::Int(0) | 
                    Value::Bool(false) => false,
                    _ => true
                }
            }
            _ => {
                Value::from(self).is_truthy()
            }
        }
    }

    fn unpack(raw: u64) -> Value<'gc> {
        let packed_tag_mask: u64 = 7 << 3;
        let value_mask: u64 = (u32::MAX as u64) ^ u64::MAX;
        let packed_tag: u64 = (raw & packed_tag_mask) >> 3;
        let packed_value: u32 = u32::try_from((raw & value_mask) >> 32).unwrap();

        if (PackedTag::SymId as u64) == packed_tag {
            return Value::SymId(packed_value);
        }

        if (PackedTag::Null as u64) == packed_tag {
            return Value::Null;
        }

        if (PackedTag::Int as u64) == packed_tag {
            let packed_int = i32::from_ne_bytes(packed_value.to_ne_bytes());

            return Value::Int(packed_int as i64);
        }

        if (PackedTag::Bool as u64) == packed_tag {
            let packed_bool = packed_value != 0;

            return Value::Bool(packed_bool);
        }

        panic!("Bad packed value")
    }

    fn try_packing(value: &Value<'gc>) -> Option<TaggedValue<'gc>> {
        let tagged = match value {
            Value::Null => {
                return Some(TaggedValue::new_null())
            }
            Value::Bool(b) => {
                return Some(TaggedValue::new_bool(*b))
            }
            Value::SymId(id) => {
                let mut raw: u64 = (*id as u64) << 32;
                raw ^= (PackedTag::SymId as u64) << 3;

                Tagged::from_raw(raw as usize, ValueTag::Packed)
            }
            Value::Int(i) => match i32::try_from(*i) {
                Ok(i) => {
                    let mut raw = u32::from_ne_bytes(i32::to_ne_bytes(i)) as u64;
                    raw <<= 32;
                    raw ^= (PackedTag::Int as u64) << 3;

                    Tagged::from_raw(raw as usize, ValueTag::Packed)
                }
                Err(_) => return None,
            },
            _ => return None,
        };

        Some(TaggedValue { ptr: tagged })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sandpit::{Arena, Root};

    #[test]
    fn pack_and_unpack_null_value() {
        let _: Arena<Root![()]> = Arena::new(|_| {
            let tagged = TaggedValue::new_null();

            assert_eq!(tagged.ptr.get_tag(), ValueTag::Packed);

            let unpacked = Value::from(&tagged);

            if let Value::Null = unpacked {
                assert!(true);
            } else {
                assert!(false);
            }
        });
    }

    #[test]
    fn pack_and_unpack_bool_value() {
        let _: Arena<Root![()]> = Arena::new(|_| {
            let tagged = TaggedValue::new_bool(false);

            assert_eq!(tagged.ptr.get_tag(), ValueTag::Packed);

            let unpacked = Value::from(&tagged);

            if let Value::Bool(false) = unpacked {
                assert!(true);
            } else {
                assert!(false);
            }
        });
    }

    #[test]
    fn pack_and_unpack_sym_id() {
        let _: Arena<Root![()]> = Arena::new(|mu| {
            let v = Value::SymId(18);
            let tagged = v.as_tagged(mu);

            assert_eq!(tagged.ptr.get_tag(), ValueTag::Packed);

            let unpacked = Value::from(&tagged);

            if let Value::SymId(18) = unpacked {
                assert!(true);
            } else {
                assert!(false);
            }
        });
    }

    #[test]
    fn pack_and_unpack_small_float() {
        let _: Arena<Root![()]> = Arena::new(|mu| {
            let v = Value::Float(420.69);
            let tagged = v.as_tagged(mu);

            assert_eq!(tagged.ptr.get_tag(), ValueTag::Float);

            let unpacked = Value::from(&tagged);

            if let Value::Float(f) = unpacked {
                assert_eq!(420.69, f);
            } else {
                assert!(false);
            }
        });
    }

    #[test]
    fn pack_and_unpack_int() {
        let _: Arena<Root![()]> = Arena::new(|mu| {
            let v = Value::Int(-333);
            let tagged = v.as_tagged(mu);

            assert_eq!(tagged.ptr.get_tag(), ValueTag::Packed);

            let unpacked = Value::from(&tagged);

            if let Value::Int(-333) = unpacked {
                assert!(true);
            } else {
                assert!(false);
            }
        });
    }

    #[test]
    fn tagged_native_func_round_trip() {
        use sandpit::Gc;
        use crate::runtime::native_func::NativeFunc;
        use crate::runtime::error::RuntimeError;

        fn dummy_fn<'gc>(_args: &[Value<'gc>], _mu: &'gc Mutator) -> Result<Value<'gc>, RuntimeError> {
            Ok(Value::Null)
        }

        let _: Arena<Root![()]> = Arena::new(|mu| {
            let nf = NativeFunc { arity: 0, func: dummy_fn };
            let gc_nf = Gc::new(mu, nf);
            let v = Value::NativeFunc(gc_nf);

            let tagged = v.as_tagged(mu);
            assert_eq!(tagged.ptr.get_tag(), ValueTag::NativeFunc);

            let unpacked = Value::from(&tagged);
            if let Value::NativeFunc(recovered) = unpacked {
                assert_eq!(recovered.arity, 0);
            } else {
                panic!("expected NativeFunc variant after round-trip");
            }
        });
    }

    #[test]
    fn native_func_type_metadata() {
        use sandpit::Gc;
        use crate::runtime::native_func::NativeFunc;
        use crate::runtime::error::RuntimeError;
        use crate::symbol_map::FN_SYM;

        fn dummy_fn<'gc>(_args: &[Value<'gc>], _mu: &'gc Mutator) -> Result<Value<'gc>, RuntimeError> {
            Ok(Value::Null)
        }

        let _: Arena<Root![()]> = Arena::new(|mu| {
            let nf = NativeFunc { arity: 2, func: dummy_fn };
            let gc_nf = Gc::new(mu, nf);
            let v = Value::NativeFunc(gc_nf);

            assert_eq!(v.type_str(), "NativeFunc");
            assert_eq!(v.get_type_id(), FN_SYM);
            assert!(v.is_truthy());
        });
    }

    #[test]
    fn native_func_equality_is_identity() {
        use sandpit::Gc;
        use crate::runtime::native_func::NativeFunc;
        use crate::runtime::error::RuntimeError;

        fn dummy_fn<'gc>(_args: &[Value<'gc>], _mu: &'gc Mutator) -> Result<Value<'gc>, RuntimeError> {
            Ok(Value::Null)
        }

        let _: Arena<Root![()]> = Arena::new(|mu| {
            let nf = NativeFunc { arity: 0, func: dummy_fn };
            let gc_a = Gc::new(mu, nf);
            let gc_b = Gc::new(mu, nf);

            let val_a1 = Value::NativeFunc(gc_a.clone());
            let val_a2 = Value::NativeFunc(gc_a);
            let val_b = Value::NativeFunc(gc_b);

            // Same Gc pointer → equal
            assert!(val_a1.is_equal_to(&val_a2));
            // Different allocation → not equal
            assert!(!val_a1.is_equal_to(&val_b));
        });
    }

    #[test]
    fn native_func_direct_call() {
        use sandpit::Gc;
        use crate::runtime::native_func::NativeFunc;
        use crate::runtime::error::RuntimeError;

        fn add_ints<'gc>(args: &[Value<'gc>], _mu: &'gc Mutator) -> Result<Value<'gc>, RuntimeError> {
            if let (Value::Int(a), Value::Int(b)) = (&args[0], &args[1]) {
                Ok(Value::Int(a + b))
            } else {
                panic!("expected two ints");
            }
        }

        let _: Arena<Root![()]> = Arena::new(|mu| {
            let nf = NativeFunc { arity: 2, func: add_ints };
            let gc_nf = Gc::new(mu, nf);

            let args = vec![Value::Int(10), Value::Int(32)];
            let result = (gc_nf.func)(&args, mu).unwrap();

            if let Value::Int(42) = result {
                assert!(true);
            } else {
                panic!("expected Int(42)");
            }
        });
    }
}