awk-rs 0.2.0

A 100% POSIX-compatible AWK implementation in Rust
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
use std::io::Write;

use crate::ast::Expr;
use crate::error::{Error, Result, SourceLocation};
use crate::value::Value;

use super::Interpreter;

impl<'a> Interpreter<'a> {
    /// Call a function with special handling for builtins that need AST access
    pub fn call_function<W: Write>(
        &mut self,
        name: &str,
        args: &[Expr],
        location: SourceLocation,
        output: &mut W,
    ) -> Result<Value> {
        // Check for built-in functions that need special argument handling
        match name {
            "sub" | "gsub" => return self.call_regex_sub(name, args, location),
            "match" => return self.call_match(args, location),
            "split" => return self.call_split(args, location),
            "patsplit" => return self.call_patsplit(args, location),
            "asort" | "asorti" => return self.call_asort(name == "asorti", args, location),
            "close" => return self.call_close(args, location),
            "fflush" => return self.call_fflush(args, location, output),
            "system" => return self.call_system(args, output),
            "length" => {
                // gawk extension: length(array) returns element count.
                // Only intercept when the sole argument names a known array;
                // otherwise fall through to the normal scalar handling below.
                if let Some(Expr::Var(var_name, _)) = args.first() {
                    // Resolve pass-by-reference aliases so length(a) works on
                    // arrays received as function parameters.
                    if let Some(arr) = self.arrays.get(self.resolve_array_name(var_name)) {
                        return Ok(Value::Number(arr.len() as f64));
                    }
                }
            }
            _ => {}
        }

        // Evaluate all arguments for other functions
        let arg_values: Result<Vec<Value>> = args.iter().map(|e| self.eval_expr(e)).collect();
        let arg_values = arg_values?;

        // Check for other built-in functions
        if let Some(result) = self.call_builtin(name, &arg_values)? {
            return Ok(result);
        }

        // Check for user-defined functions
        if let Some(func) = self.functions.get(name).cloned() {
            // Extract array names from arguments for pass-by-reference
            let array_refs: Vec<Option<String>> = args
                .iter()
                .map(|e| match e {
                    Expr::Var(name, _) if self.arrays.contains_key(name) => Some(name.clone()),
                    _ => None,
                })
                .collect();
            return self.call_user_function(func, arg_values, array_refs, output);
        }

        Err(Error::runtime_at(
            format!("undefined function: {}", name),
            location.line,
            location.column,
        ))
    }

    /// Extract regex pattern from an expression (handles both regex literals and strings)
    fn extract_pattern(&mut self, expr: &Expr) -> Result<String> {
        match expr {
            Expr::Regex(pattern, _) => Ok(pattern.clone()),
            other => Ok(self.eval_expr(other)?.to_string_val()),
        }
    }

    /// Call sub or gsub with proper regex and target handling
    fn call_regex_sub(
        &mut self,
        name: &str,
        args: &[Expr],
        _location: SourceLocation,
    ) -> Result<Value> {
        let global = name == "gsub";

        let pattern = args
            .first()
            .map(|e| self.extract_pattern(e))
            .transpose()?
            .unwrap_or_default();

        let replacement = args
            .get(1)
            .map(|e| self.eval_expr(e))
            .transpose()?
            .map(|v| v.to_string_val())
            .unwrap_or_default();

        // Get the target (third argument or $0)
        let (target_value, target_expr) = if let Some(target_arg) = args.get(2) {
            (
                self.eval_expr(target_arg)?.to_string_val(),
                Some(target_arg),
            )
        } else {
            (self.record.clone(), None)
        };

        let re = self.get_regex(&pattern)?.clone();

        let (new_str, count) = regex_sub_helper(&re, &replacement, &target_value, global);

        // Assign the result back to the target
        if let Some(target_arg) = target_expr {
            self.assign_to_lvalue(target_arg, Value::from_string(new_str))?;
        } else {
            self.set_record(&new_str);
        }

        Ok(Value::Number(count as f64))
    }

    /// Call match with proper regex handling
    fn call_match(&mut self, args: &[Expr], _location: SourceLocation) -> Result<Value> {
        let s = args
            .first()
            .map(|e| self.eval_expr(e))
            .transpose()?
            .map(|v| v.to_string_val())
            .unwrap_or_default();

        let pattern = args
            .get(1)
            .map(|e| self.extract_pattern(e))
            .transpose()?
            .unwrap_or_default();

        let re = self.get_regex(&pattern)?.clone();

        if let Some(m) = re.find(&s) {
            self.rstart = m.start() + 1;
            self.rlength = m.len() as i32;
            Ok(Value::Number(self.rstart as f64))
        } else {
            self.rstart = 0;
            self.rlength = -1;
            Ok(Value::Number(0.0))
        }
    }

    /// Call split with proper array name handling
    fn call_split(&mut self, args: &[Expr], location: SourceLocation) -> Result<Value> {
        let s = args
            .first()
            .map(|e| self.eval_expr(e))
            .transpose()?
            .map(|v| v.to_string_val())
            .unwrap_or_default();

        // Get array name from second argument (must be a variable name)
        let array_name = match args.get(1) {
            Some(Expr::Var(name, _)) => name.clone(),
            Some(Expr::ArrayAccess { array, .. }) => array.clone(),
            Some(_) => {
                return Err(Error::runtime_at(
                    "split: second argument must be an array",
                    location.line,
                    location.column,
                ));
            }
            None => {
                return Err(Error::runtime_at(
                    "split: missing array argument",
                    location.line,
                    location.column,
                ));
            }
        };

        // Get separator (third argument or default FS)
        let sep = if let Some(sep_expr) = args.get(2) {
            self.extract_pattern(sep_expr)?
        } else {
            self.fs.clone()
        };

        // Clear the array
        self.arrays.remove(&array_name);

        // Split and populate array
        let parts: Vec<&str> = if sep.is_empty() {
            // gawk extension: empty separator splits into one element per character
            s.char_indices()
                .map(|(start, c)| &s[start..start + c.len_utf8()])
                .collect()
        } else if sep == " " {
            s.split_whitespace().collect()
        } else if sep.len() == 1 {
            s.split(&sep).collect()
        } else {
            // Use regex split for multi-char separators (via the shared regex cache)
            let re = self.get_regex(&sep)?.clone();
            re.split(&s).collect()
        };

        for (i, part) in parts.iter().enumerate() {
            let key = (i + 1).to_string();
            self.set_array_element(&array_name, &key, Value::from_string(part.to_string()));
        }

        Ok(Value::Number(parts.len() as f64))
    }

    /// asort(source [, dest]) - sort array values
    /// asorti(source [, dest]) - sort array indices
    fn call_asort(
        &mut self,
        sort_indices: bool,
        args: &[Expr],
        location: SourceLocation,
    ) -> Result<Value> {
        // Get source array name
        let source_name = match args.first() {
            Some(Expr::Var(name, _)) => name.clone(),
            _ => {
                return Err(Error::runtime_at(
                    if sort_indices {
                        "asorti: first argument must be an array"
                    } else {
                        "asort: first argument must be an array"
                    },
                    location.line,
                    location.column,
                ));
            }
        };

        // Get optional destination array name
        let dest_name = match args.get(1) {
            Some(Expr::Var(name, _)) => Some(name.clone()),
            None => None,
            _ => {
                return Err(Error::runtime_at(
                    if sort_indices {
                        "asorti: second argument must be an array"
                    } else {
                        "asort: second argument must be an array"
                    },
                    location.line,
                    location.column,
                ));
            }
        };

        // Get values to sort
        let items: Vec<String> = if let Some(arr) = self.arrays.get(&source_name) {
            if sort_indices {
                arr.keys().cloned().collect()
            } else {
                arr.values().map(|v| v.to_string_val()).collect()
            }
        } else {
            Vec::new()
        };

        let mut sorted = items;
        sorted.sort();

        let count = sorted.len();

        // Store in destination (or source if no dest)
        let target = dest_name.unwrap_or_else(|| source_name.clone());
        self.arrays.remove(&target);

        for (i, item) in sorted.iter().enumerate() {
            let key = (i + 1).to_string();
            self.set_array_element(&target, &key, Value::from_string(item.clone()));
        }

        Ok(Value::Number(count as f64))
    }

    /// patsplit(string, array, fieldpat [, seps]) - split by pattern matches
    fn call_patsplit(&mut self, args: &[Expr], location: SourceLocation) -> Result<Value> {
        // Get string to split
        let s = args
            .first()
            .map(|e| self.eval_expr(e))
            .transpose()?
            .map(|v| v.to_string_val())
            .unwrap_or_default();

        // Get array name
        let array_name = match args.get(1) {
            Some(Expr::Var(name, _)) => name.clone(),
            _ => {
                return Err(Error::runtime_at(
                    "patsplit: second argument must be an array",
                    location.line,
                    location.column,
                ));
            }
        };

        // Get field pattern
        let fieldpat = if let Some(pat_expr) = args.get(2) {
            self.extract_pattern(pat_expr)?
        } else {
            return Err(Error::runtime_at(
                "patsplit: missing fieldpat argument",
                location.line,
                location.column,
            ));
        };

        // Optional separator array
        let seps_name = match args.get(3) {
            Some(Expr::Var(name, _)) => Some(name.clone()),
            None => None,
            _ => None,
        };

        // Clear destination arrays
        self.arrays.remove(&array_name);
        if let Some(ref name) = seps_name {
            self.arrays.remove(name);
        }

        // Compile regex and find all matches
        let re = self.get_regex(&fieldpat)?;
        let matches: Vec<regex::Match> = re.find_iter(&s).collect();

        // Store matches in array
        for (i, mat) in matches.iter().enumerate() {
            let key = (i + 1).to_string();
            self.set_array_element(
                &array_name,
                &key,
                Value::from_string(mat.as_str().to_string()),
            );
        }

        // Store separators if requested
        if let Some(ref name) = seps_name {
            let mut last_end = 0;
            for (i, mat) in matches.iter().enumerate() {
                let sep = &s[last_end..mat.start()];
                let key = i.to_string();
                self.set_array_element(name, &key, Value::from_string(sep.to_string()));
                last_end = mat.end();
            }
            // Final separator after last match
            let key = matches.len().to_string();
            self.set_array_element(name, &key, Value::from_string(s[last_end..].to_string()));
        }

        Ok(Value::Number(matches.len() as f64))
    }

    /// Call close to close a file or pipe
    fn call_close(&mut self, args: &[Expr], location: SourceLocation) -> Result<Value> {
        let filename = args
            .first()
            .map(|e| self.eval_expr(e))
            .transpose()?
            .map(|v| v.to_string_val())
            .unwrap_or_default();

        // Remove from any of our file/pipe collections
        let found = self.output_files.remove(&filename).is_some()
            || self.input_files.remove(&filename).is_some()
            || self.pipes.remove(&filename).is_some();

        let _ = location;
        Ok(Value::Number(if found { 0.0 } else { -1.0 }))
    }

    /// Call fflush to flush output
    fn call_fflush<W: Write>(
        &mut self,
        args: &[Expr],
        _location: SourceLocation,
        output: &mut W,
    ) -> Result<Value> {
        if args.is_empty() {
            // Flush all output
            output.flush().map_err(Error::Io)?;
            for file in self.output_files.values_mut() {
                let _ = file.flush();
            }
            Ok(Value::Number(0.0))
        } else {
            let filename = self.eval_expr(&args[0])?.to_string_val();
            if filename.is_empty() {
                output.flush().map_err(Error::Io)?;
                Ok(Value::Number(0.0))
            } else if let Some(file) = self.output_files.get_mut(&filename) {
                file.flush().map_err(Error::Io)?;
                Ok(Value::Number(0.0))
            } else {
                Ok(Value::Number(-1.0))
            }
        }
    }

    /// `system(cmd)`: POSIX requires all buffered output to be flushed before the
    /// command runs, so its output interleaves with ours in the expected order.
    fn call_system<W: Write>(&mut self, args: &[Expr], output: &mut W) -> Result<Value> {
        let cmd = match args.first() {
            Some(expr) => self.eval_expr(expr)?.to_string_val(),
            None => String::new(),
        };

        output.flush().map_err(Error::Io)?;
        for file in self.output_files.values_mut() {
            let _ = file.flush();
        }

        let status = std::process::Command::new("sh")
            .arg("-c")
            .arg(&cmd)
            .status()
            .map(|s| s.code().unwrap_or(-1))
            .unwrap_or(-1);
        Ok(Value::Number(status as f64))
    }

    fn call_builtin(&mut self, name: &str, args: &[Value]) -> Result<Option<Value>> {
        match name {
            // String functions
            "length" => {
                let s = args
                    .first()
                    .map(|v| v.to_string_val())
                    .unwrap_or_else(|| self.record.clone());
                // Use character count for UTF-8 support
                Ok(Some(Value::Number(s.chars().count() as f64)))
            }

            "substr" => {
                let s = args.first().map(|v| v.to_string_val()).unwrap_or_default();
                let start = args.get(1).map(|v| v.to_number() as usize).unwrap_or(1);
                let len = args.get(2).map(|v| v.to_number() as usize);

                // AWK uses 1-based indexing; ensure start is at least 1
                let start = start.max(1).saturating_sub(1);
                let result = if let Some(len) = len {
                    s.chars().skip(start).take(len).collect()
                } else {
                    s.chars().skip(start).collect()
                };
                Ok(Some(Value::from_string(result)))
            }

            "index" => {
                let s = args.first().map(|v| v.to_string_val()).unwrap_or_default();
                let target = args.get(1).map(|v| v.to_string_val()).unwrap_or_default();
                // Find byte position, then convert to character position
                let pos = s
                    .find(&target)
                    .map(|byte_idx| {
                        // Count characters before the byte index
                        s[..byte_idx].chars().count() + 1
                    })
                    .unwrap_or(0);
                Ok(Some(Value::Number(pos as f64)))
            }

            "sprintf" => {
                let format = args.first().map(|v| v.to_string_val()).unwrap_or_default();
                let rest = if args.len() > 1 { &args[1..] } else { &[] };
                let result = self.format_printf(&format, rest);
                Ok(Some(Value::from_string(result)))
            }

            "tolower" => {
                let s = args.first().map(|v| v.to_string_val()).unwrap_or_default();
                Ok(Some(Value::from_string(s.to_lowercase())))
            }

            "toupper" => {
                let s = args.first().map(|v| v.to_string_val()).unwrap_or_default();
                Ok(Some(Value::from_string(s.to_uppercase())))
            }

            // Math functions
            "sin" => {
                let n = args.first().map(|v| v.to_number()).unwrap_or(0.0);
                Ok(Some(Value::Number(n.sin())))
            }

            "cos" => {
                let n = args.first().map(|v| v.to_number()).unwrap_or(0.0);
                Ok(Some(Value::Number(n.cos())))
            }

            "atan2" => {
                let y = args.first().map(|v| v.to_number()).unwrap_or(0.0);
                let x = args.get(1).map(|v| v.to_number()).unwrap_or(0.0);
                Ok(Some(Value::Number(y.atan2(x))))
            }

            "exp" => {
                let n = args.first().map(|v| v.to_number()).unwrap_or(0.0);
                Ok(Some(Value::Number(n.exp())))
            }

            "log" => {
                let n = args.first().map(|v| v.to_number()).unwrap_or(0.0);
                Ok(Some(Value::Number(n.ln())))
            }

            "sqrt" => {
                let n = args.first().map(|v| v.to_number()).unwrap_or(0.0);
                Ok(Some(Value::Number(n.sqrt())))
            }

            "int" => {
                let n = args.first().map(|v| v.to_number()).unwrap_or(0.0);
                Ok(Some(Value::Number(n.trunc())))
            }

            "rand" => {
                // Use the internal RNG state
                let random = self.next_random();
                Ok(Some(Value::Number(random)))
            }

            "srand" => {
                let old_seed = self.rand_seed;
                if let Some(seed) = args.first() {
                    self.rand_seed = seed.to_number() as u64;
                } else {
                    use std::time::{SystemTime, UNIX_EPOCH};
                    self.rand_seed = SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .map(|d| d.as_secs())
                        .unwrap_or(0);
                }
                self.rand_state = self.rand_seed;
                Ok(Some(Value::Number(old_seed as f64)))
            }

            // System functions
            // === GAWK Extensions ===

            // Time functions
            "systime" => {
                // Return current time as seconds since epoch
                use std::time::{SystemTime, UNIX_EPOCH};
                let secs = SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .map(|d| d.as_secs())
                    .unwrap_or(0);
                Ok(Some(Value::Number(secs as f64)))
            }

            "mktime" => {
                // Parse "YYYY MM DD HH MM SS [DST]" into epoch timestamp
                let datespec = args.first().map(|v| v.to_string_val()).unwrap_or_default();
                let parts: Vec<i64> = datespec
                    .split_whitespace()
                    .filter_map(|s| s.parse().ok())
                    .collect();

                if parts.len() >= 6 {
                    // Simple implementation using chrono-like calculation
                    // This is a simplified version; for full accuracy we'd need chrono crate
                    let year = parts[0];
                    let month = parts[1];
                    let day = parts[2];
                    let hour = parts[3];
                    let min = parts[4];
                    let sec = parts[5];

                    // Simplified epoch calculation (not handling DST or timezones)
                    let epoch = simple_mktime(year, month, day, hour, min, sec);
                    Ok(Some(Value::Number(epoch as f64)))
                } else {
                    Ok(Some(Value::Number(-1.0)))
                }
            }

            "strftime" => {
                // Format timestamp
                let format = args
                    .first()
                    .map(|v| v.to_string_val())
                    .unwrap_or_else(|| "%a %b %e %H:%M:%S %Z %Y".to_string());
                use std::time::{SystemTime, UNIX_EPOCH};
                let timestamp = args
                    .get(1)
                    .map(|v| v.to_number() as u64)
                    .unwrap_or_else(|| {
                        SystemTime::now()
                            .duration_since(UNIX_EPOCH)
                            .map(|d| d.as_secs())
                            .unwrap_or(0)
                    });

                let result = format_strftime(&format, timestamp);
                Ok(Some(Value::from_string(result)))
            }

            // gensub - like gsub but returns the result instead of modifying in place
            "gensub" => {
                let pattern = args.first().map(|v| v.to_string_val()).unwrap_or_default();
                let replacement = args.get(1).map(|v| v.to_string_val()).unwrap_or_default();
                let how = args
                    .get(2)
                    .map(|v| v.to_string_val())
                    .unwrap_or_else(|| "g".to_string());
                let target = args
                    .get(3)
                    .map(|v| v.to_string_val())
                    .unwrap_or_else(|| self.record.clone());

                let re = self.get_regex(&pattern)?.clone();

                // "g" or "G" means global, otherwise it's the occurrence number
                let result = if how.eq_ignore_ascii_case("g") {
                    let mut result = String::new();
                    let mut last_end = 0;
                    for caps in re.captures_iter(&target) {
                        let m = caps.get(0).unwrap();
                        result.push_str(&target[last_end..m.start()]);
                        result.push_str(&apply_replacement(&replacement, &caps, true));
                        last_end = m.end();
                    }
                    result.push_str(&target[last_end..]);
                    result
                } else {
                    // Numeric occurrence (default to the 1st if unparseable, per gawk)
                    let n = how.parse::<usize>().unwrap_or(1).max(1);
                    let mut count = 0;
                    let mut last_end = 0;
                    let mut result = String::new();
                    let mut replaced = false;
                    for caps in re.captures_iter(&target) {
                        count += 1;
                        if count == n {
                            let m = caps.get(0).unwrap();
                            result.push_str(&target[last_end..m.start()]);
                            result.push_str(&apply_replacement(&replacement, &caps, true));
                            last_end = m.end();
                            replaced = true;
                            break;
                        }
                    }
                    if !replaced {
                        target.clone()
                    } else {
                        result.push_str(&target[last_end..]);
                        result
                    }
                };

                Ok(Some(Value::from_string(result)))
            }

            _ => Ok(None), // Not a built-in
        }
    }

    fn call_user_function<W: Write>(
        &mut self,
        func: &crate::ast::FunctionDef,
        args: Vec<Value>,
        array_refs: Vec<Option<String>>,
        output: &mut W,
    ) -> Result<Value> {
        // Save current variables for local scope
        let saved_vars: std::collections::HashMap<String, Value> = func
            .params
            .iter()
            .filter_map(|name| self.variables.get(name).map(|v| (name.clone(), v.clone())))
            .collect();

        // Save any arrays that share names with parameters (for local arrays)
        let saved_arrays: std::collections::HashMap<
            String,
            std::collections::HashMap<String, Value>,
        > = func
            .params
            .iter()
            .filter_map(|name| self.arrays.get(name).map(|a| (name.clone(), a.clone())))
            .collect();

        // Create array aliases for pass-by-reference
        // If an argument is an array reference, the parameter name should point to the same array
        let mut array_aliases: std::collections::HashMap<String, String> =
            std::collections::HashMap::new();
        for (i, param) in func.params.iter().enumerate() {
            if let Some(Some(array_name)) = array_refs.get(i) {
                // This parameter is an array reference
                // Create an alias: when we access param, we should access array_name
                if param != array_name {
                    array_aliases.insert(param.clone(), array_name.clone());
                }
            }
        }
        self.array_aliases = array_aliases;

        // Set scalar parameters
        for (i, param) in func.params.iter().enumerate() {
            // Skip if this is an array reference
            if let Some(Some(_)) = array_refs.get(i) {
                continue;
            }
            let value = args.get(i).cloned().unwrap_or(Value::Uninitialized);
            self.set_variable_value(param, value);
        }

        // Execute function body, passing the actual output
        let result = match self.execute_block(&func.body, output)? {
            super::stmt::StmtResult::Return(v) => v,
            _ => Value::Uninitialized,
        };

        // Clear array aliases
        self.array_aliases.clear();

        // Restore saved variables and remove parameters that weren't saved
        for param in &func.params {
            if let Some(value) = saved_vars.get(param) {
                self.set_variable_value(param, value.clone());
            } else {
                self.variables.remove(param);
            }
        }

        // Restore any saved arrays or remove local arrays
        for param in &func.params {
            if let Some(arr) = saved_arrays.get(param) {
                self.arrays.insert(param.clone(), arr.clone());
            } else if !array_refs
                .get(
                    func.params
                        .iter()
                        .position(|p| p == param)
                        .unwrap_or(usize::MAX),
                )
                .map(|r| r.is_some())
                .unwrap_or(false)
            {
                self.arrays.remove(param);
            }
        }

        Ok(result)
    }

    /// Generate a random number between 0 and 1 using xorshift64
    fn next_random(&mut self) -> f64 {
        let mut x = self.rand_state;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        self.rand_state = x;
        (x as f64) / (u64::MAX as f64)
    }
}

fn regex_sub_helper(
    re: &regex::Regex,
    replacement: &str,
    target: &str,
    global: bool,
) -> (String, usize) {
    // POSIX/gawk replacement semantics: `&` = matched text, `\&` = literal `&`,
    // `\\` = literal backslash. sub/gsub don't support `\N` group refs.
    let mut count = 0;

    if global {
        let result = re.replace_all(target, |caps: &regex::Captures| {
            count += 1;
            apply_replacement(replacement, caps, false)
        });
        (result.to_string(), count)
    } else {
        let result = re.replace(target, |caps: &regex::Captures| {
            count += 1;
            apply_replacement(replacement, caps, false)
        });
        (result.to_string(), count)
    }
}

/// Expand a sub/gsub/gensub replacement string against a regex match.
///
/// - `&` is replaced with the whole match text.
/// - `\&` is a literal `&`.
/// - `\\` is a literal backslash.
/// - When `allow_groups` is set (gensub only), `\1`..`\9` are replaced with
///   the corresponding capture group (empty string if the group didn't
///   participate in the match).
/// - Any other backslash sequence is passed through unchanged.
fn apply_replacement(replacement: &str, caps: &regex::Captures, allow_groups: bool) -> String {
    let mut result = String::new();
    let mut chars = replacement.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            '\\' => match chars.peek().copied() {
                Some('&') => {
                    result.push('&');
                    chars.next();
                }
                Some('\\') => {
                    result.push('\\');
                    chars.next();
                }
                Some(d) if allow_groups && d.is_ascii_digit() && d != '0' => {
                    chars.next();
                    let idx = d.to_digit(10).unwrap() as usize;
                    if let Some(m) = caps.get(idx) {
                        result.push_str(m.as_str());
                    }
                }
                _ => result.push('\\'),
            },
            '&' => {
                if let Some(m) = caps.get(0) {
                    result.push_str(m.as_str());
                }
            }
            other => result.push(other),
        }
    }

    result
}

/// Simplified mktime implementation (UTC-based)
fn simple_mktime(year: i64, month: i64, day: i64, hour: i64, min: i64, sec: i64) -> i64 {
    // Days in each month (non-leap year)
    const DAYS_IN_MONTH: [i64; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    fn is_leap_year(year: i64) -> bool {
        (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
    }

    fn days_in_year(year: i64) -> i64 {
        if is_leap_year(year) { 366 } else { 365 }
    }

    // Calculate days from epoch (1970-01-01)
    let mut days: i64 = 0;

    // Add days for complete years
    for y in 1970..year {
        days += days_in_year(y);
    }
    for y in year..1970 {
        days -= days_in_year(y);
    }

    // Add days for complete months in current year
    for m in 1..month {
        let m_idx = (m - 1) as usize;
        if m_idx < 12 {
            days += DAYS_IN_MONTH[m_idx];
            if m == 2 && is_leap_year(year) {
                days += 1;
            }
        }
    }

    // Add remaining days
    days += day - 1;

    // Convert to seconds
    days * 86400 + hour * 3600 + min * 60 + sec
}

/// Simplified strftime implementation
fn format_strftime(format: &str, timestamp: u64) -> String {
    // Break down timestamp into components
    let secs = timestamp as i64;

    // Calculate year, month, day, etc.
    let (year, month, day, hour, min, sec, wday, yday) = breakdown_time(secs);

    let weekday_names = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    let month_names = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];
    let weekday_full = [
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
    ];
    let month_full = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ];

    let mut result = String::new();
    let mut chars = format.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '%' {
            match chars.next() {
                Some('Y') => result.push_str(&format!("{:04}", year)),
                Some('y') => result.push_str(&format!("{:02}", year % 100)),
                Some('m') => result.push_str(&format!("{:02}", month)),
                Some('d') => result.push_str(&format!("{:02}", day)),
                Some('e') => result.push_str(&format!("{:2}", day)),
                Some('H') => result.push_str(&format!("{:02}", hour)),
                Some('M') => result.push_str(&format!("{:02}", min)),
                Some('S') => result.push_str(&format!("{:02}", sec)),
                Some('a') => result.push_str(weekday_names.get(wday as usize).unwrap_or(&"???")),
                Some('A') => result.push_str(weekday_full.get(wday as usize).unwrap_or(&"???")),
                Some('b') | Some('h') => {
                    result.push_str(month_names.get((month - 1) as usize).unwrap_or(&"???"))
                }
                Some('B') => {
                    result.push_str(month_full.get((month - 1) as usize).unwrap_or(&"???"))
                }
                Some('j') => result.push_str(&format!("{:03}", yday)),
                Some('u') => result.push_str(&format!("{}", if wday == 0 { 7 } else { wday })),
                Some('w') => result.push_str(&format!("{}", wday)),
                Some('Z') => result.push_str("UTC"),
                Some('z') => result.push_str("+0000"),
                Some('%') => result.push('%'),
                Some('n') => result.push('\n'),
                Some('t') => result.push('\t'),
                Some(c) => {
                    result.push('%');
                    result.push(c);
                }
                None => result.push('%'),
            }
        } else {
            result.push(ch);
        }
    }

    result
}

/// Break down epoch seconds into date/time components
fn breakdown_time(secs: i64) -> (i64, i64, i64, i64, i64, i64, i64, i64) {
    const DAYS_IN_MONTH: [i64; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    fn is_leap_year(year: i64) -> bool {
        (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
    }

    let sec = secs % 60;
    let min = (secs / 60) % 60;
    let hour = (secs / 3600) % 24;
    let mut days = secs / 86400;

    // wday: 0 = Sunday, 1970-01-01 was Thursday (4)
    let wday = ((days + 4) % 7 + 7) % 7;

    // Calculate year
    let mut year = 1970i64;
    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if days >= days_in_year {
            days -= days_in_year;
            year += 1;
        } else if days < 0 {
            year -= 1;
            let days_in_year = if is_leap_year(year) { 366 } else { 365 };
            days += days_in_year;
        } else {
            break;
        }
    }

    let yday = days + 1; // 1-based day of year

    // Calculate month and day
    let mut month = 1i64;
    for (m, &dim) in DAYS_IN_MONTH.iter().enumerate() {
        let mut days_in_month = dim;
        if m == 1 && is_leap_year(year) {
            days_in_month += 1;
        }
        if days < days_in_month {
            month = m as i64 + 1;
            break;
        }
        days -= days_in_month;
    }
    let day = days + 1;

    (year, month, day, hour, min, sec, wday, yday)
}