seq-runtime 5.4.0

Runtime library for the Seq programming language
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Float operations for Seq
//!
//! These functions are exported with C ABI for LLVM codegen to call.
//! All float operations use the `f.` prefix to distinguish from integer operations.

use crate::seqstring::global_string;
use crate::stack::{Stack, pop, pop_two, push};
use crate::value::Value;

// =============================================================================
// Push Float
// =============================================================================

/// Push a float value onto the stack
///
/// # Safety
/// Stack pointer must be valid or null
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_push_float(stack: Stack, value: f64) -> Stack {
    unsafe { push(stack, Value::Float(value)) }
}

// =============================================================================
// Arithmetic Operations
// =============================================================================

/// Float addition: ( Float Float -- Float )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_add(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.add") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe { push(rest, Value::Float(x + y)) },
        _ => panic!("f.add: expected two Floats on stack"),
    }
}

/// Float subtraction: ( Float Float -- Float )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_subtract(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.subtract") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe { push(rest, Value::Float(x - y)) },
        _ => panic!("f.subtract: expected two Floats on stack"),
    }
}

/// Float multiplication: ( Float Float -- Float )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_multiply(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.multiply") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe { push(rest, Value::Float(x * y)) },
        _ => panic!("f.multiply: expected two Floats on stack"),
    }
}

/// Float division: ( Float Float -- Float )
///
/// Division by zero returns infinity (IEEE 754 behavior)
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_divide(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.divide") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe { push(rest, Value::Float(x / y)) },
        _ => panic!("f.divide: expected two Floats on stack"),
    }
}

// =============================================================================
// Comparison Operations (return Int 0 or 1)
// =============================================================================
//
// Note: Float comparisons return Int (0/1) for Forth-style boolean semantics,
// while integer comparisons in arithmetic.rs return Bool. Both work with
// conditionals and test.assert since they accept both Int and Bool.

/// Float equality: ( Float Float -- Int )
///
/// **Warning:** Direct float equality can be surprising due to IEEE 754
/// rounding. For example, `0.1 0.2 f.add 0.3 f.=` may return 0.
/// Consider using epsilon-based comparison for tolerances.
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_eq(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.=") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe {
            push(rest, Value::Int(if x == y { 1 } else { 0 }))
        },
        _ => panic!("f.=: expected two Floats on stack"),
    }
}

/// Float less than: ( Float Float -- Int )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_lt(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.<") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe {
            push(rest, Value::Int(if x < y { 1 } else { 0 }))
        },
        _ => panic!("f.<: expected two Floats on stack"),
    }
}

/// Float greater than: ( Float Float -- Int )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_gt(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.>") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe {
            push(rest, Value::Int(if x > y { 1 } else { 0 }))
        },
        _ => panic!("f.>: expected two Floats on stack"),
    }
}

/// Float less than or equal: ( Float Float -- Int )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_lte(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.<=") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe {
            push(rest, Value::Int(if x <= y { 1 } else { 0 }))
        },
        _ => panic!("f.<=: expected two Floats on stack"),
    }
}

/// Float greater than or equal: ( Float Float -- Int )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_gte(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.>=") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe {
            push(rest, Value::Int(if x >= y { 1 } else { 0 }))
        },
        _ => panic!("f.>=: expected two Floats on stack"),
    }
}

/// Float not equal: ( Float Float -- Int )
///
/// # Safety
/// Stack must have two Float values on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_f_neq(stack: Stack) -> Stack {
    let (rest, a, b) = unsafe { pop_two(stack, "f.<>") };
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => unsafe {
            push(rest, Value::Int(if x != y { 1 } else { 0 }))
        },
        _ => panic!("f.<>: expected two Floats on stack"),
    }
}

// =============================================================================
// Type Conversions
// =============================================================================

/// Convert Int to Float: ( Int -- Float )
///
/// # Safety
/// Stack must have an Int value on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_int_to_float(stack: Stack) -> Stack {
    assert!(!stack.is_null(), "int->float: stack is empty");
    let (stack, val) = unsafe { pop(stack) };

    match val {
        Value::Int(i) => unsafe { push(stack, Value::Float(i as f64)) },
        _ => panic!("int->float: expected Int on stack"),
    }
}

/// Convert Float to Int: ( Float -- Int )
///
/// Truncates toward zero. Values outside 63-bit signed range are clamped:
/// - Values >= 2^62-1 become 2^62-1 (4611686018427387903)
/// - Values <= -(2^62) become -(2^62) (-4611686018427387904)
/// - NaN becomes 0
///
/// # Safety
/// Stack must have a Float value on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_float_to_int(stack: Stack) -> Stack {
    assert!(!stack.is_null(), "float->int: stack is empty");
    let (stack, val) = unsafe { pop(stack) };

    match val {
        Value::Float(f) => {
            // Clamp to i64 range to avoid undefined behavior
            // 63-bit signed integer range: -(2^62) to (2^62 - 1)
            const INT63_MAX: i64 = (1i64 << 62) - 1;
            const INT63_MIN: i64 = -(1i64 << 62);
            let i = if f.is_nan() {
                0
            } else if f >= INT63_MAX as f64 {
                INT63_MAX
            } else if f <= INT63_MIN as f64 {
                INT63_MIN
            } else {
                f as i64
            };
            unsafe { push(stack, Value::Int(i)) }
        }
        _ => panic!("float->int: expected Float on stack"),
    }
}

/// Convert Float to String: ( Float -- String )
///
/// # Safety
/// Stack must have a Float value on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_float_to_string(stack: Stack) -> Stack {
    assert!(!stack.is_null(), "float->string: stack is empty");
    let (stack, val) = unsafe { pop(stack) };

    match val {
        Value::Float(f) => {
            let s = f.to_string();
            unsafe { push(stack, Value::String(global_string(s))) }
        }
        _ => panic!("float->string: expected Float on stack"),
    }
}

/// Convert String to Float: ( String -- Float Int )
/// Returns the parsed float and 1 on success, or 0.0 and 0 on failure
///
/// # Safety
/// Stack must have a String value on top
#[unsafe(no_mangle)]
pub unsafe extern "C" fn patch_seq_string_to_float(stack: Stack) -> Stack {
    assert!(!stack.is_null(), "string->float: stack is empty");
    let (stack, val) = unsafe { pop(stack) };

    match val {
        Value::String(s) => match s.as_str().parse::<f64>() {
            Ok(f) => {
                let stack = unsafe { push(stack, Value::Float(f)) };
                unsafe { push(stack, Value::Bool(true)) }
            }
            Err(_) => {
                let stack = unsafe { push(stack, Value::Float(0.0)) };
                unsafe { push(stack, Value::Bool(false)) }
            }
        },
        _ => panic!("string->float: expected String on stack"),
    }
}

// =============================================================================
// Public re-exports with short names
// =============================================================================

pub use patch_seq_f_add as f_add;
pub use patch_seq_f_divide as f_divide;
pub use patch_seq_f_eq as f_eq;
pub use patch_seq_f_gt as f_gt;
pub use patch_seq_f_gte as f_gte;
pub use patch_seq_f_lt as f_lt;
pub use patch_seq_f_lte as f_lte;
pub use patch_seq_f_multiply as f_multiply;
pub use patch_seq_f_neq as f_neq;
pub use patch_seq_f_subtract as f_subtract;
pub use patch_seq_float_to_int as float_to_int;
pub use patch_seq_float_to_string as float_to_string;
pub use patch_seq_int_to_float as int_to_float;
pub use patch_seq_push_float as push_float;
pub use patch_seq_string_to_float as string_to_float;

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_push_float() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push_float(stack, 3.5);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Float(3.5));
        }
    }

    #[test]
    fn test_f_add() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(1.5));
            let stack = push(stack, Value::Float(2.5));

            let stack = f_add(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Float(4.0));
        }
    }

    #[test]
    fn test_f_subtract() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(5.0));
            let stack = push(stack, Value::Float(2.0));

            let stack = f_subtract(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Float(3.0));
        }
    }

    #[test]
    fn test_f_multiply() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(3.0));
            let stack = push(stack, Value::Float(4.0));

            let stack = f_multiply(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Float(12.0));
        }
    }

    #[test]
    fn test_f_divide() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(10.0));
            let stack = push(stack, Value::Float(4.0));

            let stack = f_divide(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Float(2.5));
        }
    }

    #[test]
    fn test_f_divide_by_zero() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(1.0));
            let stack = push(stack, Value::Float(0.0));

            let stack = f_divide(stack);

            let (_stack, result) = pop(stack);
            match result {
                Value::Float(f) => assert!(f.is_infinite()),
                _ => panic!("Expected Float"),
            }
        }
    }

    #[test]
    fn test_f_eq_true() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(3.5));
            let stack = push(stack, Value::Float(3.5));

            let stack = f_eq(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(1));
        }
    }

    #[test]
    fn test_f_eq_false() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(3.5));
            let stack = push(stack, Value::Float(2.5));

            let stack = f_eq(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(0));
        }
    }

    #[test]
    fn test_f_lt() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(1.5));
            let stack = push(stack, Value::Float(2.5));

            let stack = f_lt(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(1)); // 1.5 < 2.5
        }
    }

    #[test]
    fn test_f_gt() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(2.5));
            let stack = push(stack, Value::Float(1.5));

            let stack = f_gt(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(1)); // 2.5 > 1.5
        }
    }

    #[test]
    fn test_f_lte() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(2.5));
            let stack = push(stack, Value::Float(2.5));

            let stack = f_lte(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(1)); // 2.5 <= 2.5
        }
    }

    #[test]
    fn test_f_gte() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(2.5));
            let stack = push(stack, Value::Float(2.5));

            let stack = f_gte(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(1)); // 2.5 >= 2.5
        }
    }

    #[test]
    fn test_f_neq() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(1.0));
            let stack = push(stack, Value::Float(2.0));

            let stack = f_neq(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(1)); // 1.0 <> 2.0
        }
    }

    #[test]
    fn test_int_to_float() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Int(42));

            let stack = int_to_float(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Float(42.0));
        }
    }

    #[test]
    fn test_float_to_int() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(3.7));

            let stack = float_to_int(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(3)); // Truncates toward zero
        }
    }

    #[test]
    fn test_float_to_int_negative() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(-3.7));

            let stack = float_to_int(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(-3)); // Truncates toward zero
        }
    }

    #[test]
    fn test_float_to_int_nan() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(f64::NAN));

            let stack = float_to_int(stack);

            let (_stack, result) = pop(stack);
            assert_eq!(result, Value::Int(0)); // NaN becomes 0
        }
    }

    #[test]
    fn test_float_to_int_clamps_to_63bit_max() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(1e20)); // Much larger than 63-bit max

            let stack = float_to_int(stack);

            let (_stack, result) = pop(stack);
            let int63_max = (1i64 << 62) - 1;
            assert_eq!(result, Value::Int(int63_max));
        }
    }

    #[test]
    fn test_float_to_int_clamps_to_63bit_min() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(-1e20)); // Much smaller than 63-bit min

            let stack = float_to_int(stack);

            let (_stack, result) = pop(stack);
            let int63_min = -(1i64 << 62);
            assert_eq!(result, Value::Int(int63_min));
        }
    }

    #[test]
    fn test_float_to_int_infinity_clamps() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(f64::INFINITY));

            let stack = float_to_int(stack);

            let (_stack, result) = pop(stack);
            let int63_max = (1i64 << 62) - 1;
            assert_eq!(result, Value::Int(int63_max));
        }
    }

    #[test]
    fn test_float_to_string() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(3.5));

            let stack = float_to_string(stack);

            let (_stack, result) = pop(stack);
            match result {
                Value::String(s) => assert_eq!(s.as_str(), "3.5"),
                _ => panic!("Expected String"),
            }
        }
    }

    #[test]
    fn test_float_to_string_whole_number() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(42.0));

            let stack = float_to_string(stack);

            let (_stack, result) = pop(stack);
            match result {
                Value::String(s) => assert_eq!(s.as_str(), "42"),
                _ => panic!("Expected String"),
            }
        }
    }

    #[test]
    fn test_nan_propagation() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(f64::NAN));
            let stack = push(stack, Value::Float(1.0));

            let stack = f_add(stack);

            let (_stack, result) = pop(stack);
            match result {
                Value::Float(f) => assert!(f.is_nan()),
                _ => panic!("Expected Float"),
            }
        }
    }

    #[test]
    fn test_infinity() {
        unsafe {
            let stack = crate::stack::alloc_test_stack();
            let stack = push(stack, Value::Float(f64::INFINITY));
            let stack = push(stack, Value::Float(1.0));

            let stack = f_add(stack);

            let (_stack, result) = pop(stack);
            match result {
                Value::Float(f) => assert!(f.is_infinite()),
                _ => panic!("Expected Float"),
            }
        }
    }
}