qanglang-core 0.1.0

Core library for QangLang
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use crate::{
    BoundIntrinsicHandle, BoundMethodHandle, ClassHandle, HashMapHandle, HeapAllocator,
    InstanceHandle, NativeFunctionHandle,
    arena::Index,
    memory::{ArrayHandle, ClosureHandle, FunctionHandle, StringHandle},
};
use rustc_hash::FxHasher;
use std::hash::{Hash, Hasher};

// keywords
pub const NIL_TYPE_STRING: &str = "nil";
pub const BOOLEAN_TYPE_STRING: &str = "boolean";
pub const NUMBER_TYPE_STRING: &str = "number";
pub const STRING_TYPE_STRING: &str = "string";
pub const FUNCTION_TYPE_STRING: &str = "function";
pub const CLASS_TYPE_STRING: &str = "class";
pub const OBJECT_TYPE_STRING: &str = "object";
pub const ARRAY_TYPE_STRING: &str = "array";
pub const MODULE_TYPE_STRING: &str = "module";

const NAN_BASE: u64 = 0x7FF8_0000_0000_0000;
const TYPE_MASK: u64 = 0x0000_F000_0000_0000; // 4 bits for type
const PAYLOAD_MASK: u64 = 0x0000_0FFF_FFFF_FFFF; // 44 bits for data

// Type tags (in the high nibble of mantissa)
const NIL_TAG: u64 = 0x0000_1000_0000_0000;
const BOOL_TAG: u64 = 0x0000_2000_0000_0000; // +1 for false, +2 for true
const STRING_TAG: u64 = 0x0000_3000_0000_0000;
const CLOSURE_TAG: u64 = 0x0000_4000_0000_0000;
const NATIVE_TAG: u64 = 0x0000_5000_0000_0000;
const FUNC_TAG: u64 = 0x0000_6000_0000_0000;
const CLASS_TAG: u64 = 0x0000_7000_0000_0000;
const INSTANCE_TAG: u64 = 0x0000_8000_0000_0000;
const BOUND_METHOD_TAG: u64 = 0x0000_9000_0000_0000;
const BOUND_INTRINSIC_TAG: u64 = 0x0000_A000_0000_0000;
const ARRAY_TAG: u64 = 0x0000_B000_0000_0000;
const OBJECT_TAG: u64 = 0x0000_C000_0000_0000;
const MODULE_TAG: u64 = 0x0000_D000_0000_0000;
// Tags E and F still available

// Usage
const NIL_NAN: u64 = NAN_BASE | NIL_TAG | 1;
const TRUE_NAN: u64 = NAN_BASE | BOOL_TAG | 2;
const FALSE_NAN: u64 = NAN_BASE | BOOL_TAG | 1;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ValueKind {
    Nil,
    True,
    False,
    Number(f64),
    String(u32),
    Closure(Index),
    NativeFunction(u32),
    FunctionDecl(u32),
    Class(Index),
    Instance(Index),
    BoundMethod(Index),
    BoundIntrinsic(Index),
    Array(Index),
    ObjectLiteral(Index),
    Module(Index),
}

#[derive(Debug, Clone, Copy)]
pub struct Value(f64);

impl Value {
    pub fn nil() -> Self {
        Self(f64::from_bits(NIL_NAN))
    }

    pub fn is_nil(&self) -> bool {
        self.0.to_bits() == NIL_NAN
    }

    pub fn boolean(value: bool) -> Self {
        Self(f64::from_bits(if value { TRUE_NAN } else { FALSE_NAN }))
    }

    pub fn as_boolean(&self) -> Option<bool> {
        match self.0.to_bits() {
            TRUE_NAN => Some(true),
            FALSE_NAN => Some(false),
            _ => None,
        }
    }

    pub fn number(value: f64) -> Self {
        Self(value)
    }

    pub fn as_number(&self) -> Option<f64> {
        if !self.0.is_nan() { Some(self.0) } else { None }
    }

    pub fn string(handle: StringHandle) -> Self {
        let bits = NAN_BASE | STRING_TAG | (handle as u64 & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_string(&self) -> Option<StringHandle> {
        let bits = self.0.to_bits();
        if bits & TYPE_MASK == STRING_TAG {
            Some((bits & PAYLOAD_MASK) as u32)
        } else {
            None
        }
    }

    pub fn function(handle: FunctionHandle) -> Self {
        let bits = NAN_BASE | FUNC_TAG | (handle as u64 & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_function(&self) -> Option<FunctionHandle> {
        let bits = self.0.to_bits();
        if bits & TYPE_MASK == FUNC_TAG {
            Some((bits & PAYLOAD_MASK) as u32)
        } else {
            None
        }
    }

    pub fn native_function(handle: NativeFunctionHandle) -> Self {
        let bits = NAN_BASE | NATIVE_TAG | (handle as u64 & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_native_function(&self) -> Option<NativeFunctionHandle> {
        let bits = self.0.to_bits();
        if bits & TYPE_MASK == NATIVE_TAG {
            Some((bits & PAYLOAD_MASK) as u32)
        } else {
            None
        }
    }

    pub fn closure(handle: ClosureHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | CLOSURE_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_closure(&self) -> Option<ClosureHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == CLOSURE_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn class(handle: ClassHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | CLASS_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_class(&self) -> Option<ClassHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == CLASS_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn instance(handle: InstanceHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | INSTANCE_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_instance(&self) -> Option<InstanceHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == INSTANCE_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn bound_method(handle: BoundMethodHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | BOUND_METHOD_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_bound_method(&self) -> Option<BoundMethodHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == BOUND_METHOD_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn bound_intrinsic(handle: BoundIntrinsicHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | BOUND_INTRINSIC_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_bound_intrinsic(&self) -> Option<BoundIntrinsicHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == BOUND_INTRINSIC_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn array(handle: ArrayHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | ARRAY_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_array(&self) -> Option<ArrayHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == ARRAY_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn object_literal(handle: HashMapHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | OBJECT_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_object_literal(&self) -> Option<HashMapHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == OBJECT_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn module(handle: HashMapHandle) -> Self {
        let packed = handle.pack_for_nan();
        let bits = NAN_BASE | MODULE_TAG | (packed & PAYLOAD_MASK);
        Self(f64::from_bits(bits))
    }

    pub fn as_module(&self) -> Option<HashMapHandle> {
        let bits = self.0.to_bits();
        if (bits & TYPE_MASK) == MODULE_TAG {
            let packed = bits & PAYLOAD_MASK;
            Some(Index::unpack_from_nan(packed))
        } else {
            None
        }
    }

    pub fn kind(self) -> ValueKind {
        if !self.0.is_nan() {
            return ValueKind::Number(self.0);
        }

        let bits = self.0.to_bits();
        match bits & TYPE_MASK {
            NIL_TAG => ValueKind::Nil,
            BOOL_TAG => {
                let payload = bits & PAYLOAD_MASK;
                if payload == 2 {
                    ValueKind::True
                } else {
                    ValueKind::False
                }
            }
            STRING_TAG => {
                let handle = (bits & PAYLOAD_MASK) as u32;
                ValueKind::String(handle)
            }
            FUNC_TAG => {
                let handle = (bits & PAYLOAD_MASK) as u32;
                ValueKind::FunctionDecl(handle)
            }
            NATIVE_TAG => {
                let handle = (bits & PAYLOAD_MASK) as u32;
                ValueKind::NativeFunction(handle)
            }
            CLOSURE_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::Closure(index)
            }
            CLASS_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::Class(index)
            }
            INSTANCE_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::Instance(index)
            }
            BOUND_METHOD_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::BoundMethod(index)
            }
            BOUND_INTRINSIC_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::BoundIntrinsic(index)
            }
            ARRAY_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::Array(index)
            }
            OBJECT_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::ObjectLiteral(index)
            }
            MODULE_TAG => {
                let packed = bits & PAYLOAD_MASK;
                let index = Index::unpack_from_nan(packed);
                ValueKind::Module(index)
            }
            _ => unreachable!("Invalid NaN pattern"),
        }
    }

    pub fn is_truthy(&self) -> bool {
        match self.as_boolean() {
            Some(b) => b,
            None => !self.is_nil(), // Everything except nil and false is truthy
        }
    }

    pub fn print(&self, allocator: &HeapAllocator) {
        print!("{}", self.to_display_string(allocator));
    }

    pub fn to_display_string(&self, allocator: &HeapAllocator) -> String {
        match self.kind() {
            ValueKind::Nil => "nil".to_string(),
            ValueKind::Number(number) => number.to_string(),
            ValueKind::String(handle) => allocator.strings.get_string(handle).to_string(),
            ValueKind::True => "true".to_string(),
            ValueKind::False => "false".to_string(),
            ValueKind::FunctionDecl(function_handle) => {
                let function = allocator.get_function(function_handle);
                let identifier = allocator.strings.get_string(function.name);
                format!("{}<function>", identifier)
            }
            ValueKind::Closure(handle) => {
                let closure = allocator.closures.get_closure(handle);
                let function = allocator.get_function(closure.function);
                let identifier = allocator.strings.get_string(function.name);
                format!("{}<function>", identifier)
            }
            ValueKind::NativeFunction(handle) => {
                let function = allocator.get_native_function(handle);
                let identifier = allocator.strings.get_string(function.name_handle);
                format!("{}<function>", identifier)
            }
            ValueKind::Class(handle) => {
                let clazz = allocator.get_class(handle);
                let identifier = allocator.strings.get_string(clazz.name);
                format!("{}<class>", identifier)
            }
            ValueKind::Instance(handle) => {
                let instance = allocator.get_instance(handle);
                let clazz = allocator.get_class(instance.clazz);
                let identifier = allocator.strings.get_string(clazz.name);
                format!("instanceof {}", identifier)
            }
            ValueKind::BoundMethod(handle) => {
                let method_binding = allocator.get_bound_method(handle);
                format!(
                    "{}.{}",
                    method_binding.receiver.to_display_string(allocator),
                    Value::closure(method_binding.closure).to_display_string(allocator)
                )
            }
            ValueKind::BoundIntrinsic(handle) => {
                let intrinsic_binding = allocator.get_bound_intrinsic(handle);
                format!(
                    "{}.{}",
                    intrinsic_binding.receiver.to_type_string(),
                    Value::string(intrinsic_binding.name_handle).to_display_string(allocator)
                )
            }
            ValueKind::Array(handle) => {
                let mut string = String::from("[");
                for item in allocator.arrays.iter(handle) {
                    string.push_str(&item.to_display_string(allocator));
                    string.push(',');
                }
                string.push(']');
                string
            }
            ValueKind::ObjectLiteral(handle) => {
                let mut string = String::from("{{");
                for (key, value) in allocator.tables.iter(handle) {
                    string.push(' ');
                    string.push_str(&key.to_display_string(allocator));
                    string.push('=');
                    string.push_str(&value.to_display_string(allocator));
                    string.push(',');
                    string.push(' ');
                }
                string.push_str("}}");
                string
            }
            ValueKind::Module(handle) => {
                format!("module#{}", handle)
            }
        }
    }

    pub fn to_type_string(&self) -> &'static str {
        match self.kind() {
            ValueKind::Nil => NIL_TYPE_STRING,
            ValueKind::True => BOOLEAN_TYPE_STRING,
            ValueKind::False => BOOLEAN_TYPE_STRING,
            ValueKind::Number(_) => NUMBER_TYPE_STRING,
            ValueKind::String(_) => STRING_TYPE_STRING,
            ValueKind::Closure(_) => FUNCTION_TYPE_STRING,
            ValueKind::NativeFunction(_) => FUNCTION_TYPE_STRING,
            ValueKind::FunctionDecl(_) => FUNCTION_TYPE_STRING,
            ValueKind::Class(_) => CLASS_TYPE_STRING,
            ValueKind::Instance(_) => OBJECT_TYPE_STRING,
            ValueKind::BoundMethod(_) => FUNCTION_TYPE_STRING,
            ValueKind::BoundIntrinsic(_) => FUNCTION_TYPE_STRING,
            ValueKind::Array(_) => ARRAY_TYPE_STRING,
            ValueKind::ObjectLiteral(_) => OBJECT_TYPE_STRING,
            ValueKind::Module(_) => MODULE_TYPE_STRING,
        }
    }

    pub fn hash(&self) -> u64 {
        let mut hasher = FxHasher::default();

        self.0.to_bits().hash(&mut hasher);

        hasher.finish()
    }
}

impl From<f64> for Value {
    fn from(num: f64) -> Self {
        Value::number(num)
    }
}

impl From<u64> for Value {
    fn from(num: u64) -> Self {
        Value::number(num as f64)
    }
}

impl From<usize> for Value {
    fn from(num: usize) -> Self {
        Value::number(num as f64)
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Value::boolean(value)
    }
}

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        // For regular numbers (not NaN), use direct comparison
        if !self.0.is_nan() && !other.0.is_nan() {
            return self.0 == other.0;
        }

        // For NaN patterns, compare the bit representation directly
        // This ensures different NaN values (nil, booleans, strings, etc.) compare correctly
        self.0.to_bits() == other.0.to_bits()
    }
}

impl Default for Value {
    fn default() -> Self {
        Value::nil()
    }
}

impl std::fmt::Display for Index {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.index)
    }
}