lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
//! パフォーマンス最適化モジュール
//!
//! このモジュールは高性能なパーサー最適化を提供します:
//! - SIMD最適化(文字分類、パターンマッチング)
//! - 並列化(独立した部分の同時処理)
//! - キャッシュ最適化(メモ化、局所性改善)
//! - ゼロコピー最適化(不必要なアロケーション削除)

use super::types::*;
use super::combinator::*;
use super::scheme::*;
use crate::diagnostics::Span;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// SIMD最適化されたパーサープリミティブ
pub struct SIMDPrimitives;

#[cfg(target_arch = "x86_64")]
impl SIMDPrimitives {
    /// SIMD最適化された文字分類
    /// ASCII文字の分類を並列実行
    pub fn simd_classify_chars(input: &str) -> Vec<CharClass> {
        use std::arch::x86_64::*;
        
        let mut result = Vec::with_capacity(input.len());
        let bytes = input.as_bytes();
        
        // 16バイトずつ処理
        let chunks = bytes.chunks_exact(16);
        let remainder = chunks.remainder();
        
        unsafe {
            for chunk in chunks {
                // 16バイトを128bitレジスタにロード
                let data = _mm_loadu_si128(chunk.as_ptr() as *const __m128i);
                
                // 数字の判定 ('0'-'9')
                let is_digit = _mm_and_si128(
                    _mm_cmpgt_epi8(data, _mm_set1_epi8('0' as i8 - 1)),
                    _mm_cmplt_epi8(data, _mm_set1_epi8('9' as i8 + 1))
                );
                
                // 英字の判定 ('a'-'z', 'A'-'Z')
                let is_lower = _mm_and_si128(
                    _mm_cmpgt_epi8(data, _mm_set1_epi8('a' as i8 - 1)),
                    _mm_cmplt_epi8(data, _mm_set1_epi8('z' as i8 + 1))
                );
                let is_upper = _mm_and_si128(
                    _mm_cmpgt_epi8(data, _mm_set1_epi8('A' as i8 - 1)),
                    _mm_cmplt_epi8(data, _mm_set1_epi8('Z' as i8 + 1))
                );
                let is_alpha = _mm_or_si128(is_lower, is_upper);
                
                // 空白文字の判定
                let is_space = _mm_or_si128(
                    _mm_cmpeq_epi8(data, _mm_set1_epi8(' ' as i8)),
                    _mm_cmpeq_epi8(data, _mm_set1_epi8('\t' as i8))
                );
                
                // 結果を個別に処理
                for i in 0..16 {
                    let digit_mask = _mm_extract_epi8(is_digit, i) != 0;
                    let alpha_mask = _mm_extract_epi8(is_alpha, i) != 0;
                    let space_mask = _mm_extract_epi8(is_space, i) != 0;
                    
                    let class = if digit_mask {
                        CharClass::Digit
                    } else if alpha_mask {
                        CharClass::Alpha
                    } else if space_mask {
                        CharClass::Whitespace
                    } else {
                        CharClass::Other
                    };
                    
                    result.push(class);
                }
            }
        }
        
        // 残りをスカラー処理
        for &byte in remainder {
            let ch = byte as char;
            let class = if ch.is_ascii_digit() {
                CharClass::Digit
            } else if ch.is_alphabetic() {
                CharClass::Alpha
            } else if ch.is_whitespace() {
                CharClass::Whitespace
            } else {
                CharClass::Other
            };
            result.push(class);
        }
        
        result
    }
    
    /// SIMD最適化された文字列検索
    pub fn simd_find_pattern(haystack: &str, needle: &str) -> Option<usize> {
        if needle.len() > 16 || haystack.len() < 16 {
            // フォールバックしてスカラー処理
            return haystack.find(needle);
        }
        
        unsafe {
            use std::arch::x86_64::*;
            
            let needle_bytes = needle.as_bytes();
            let haystack_bytes = haystack.as_bytes();
            
            if needle_bytes.is_empty() {
                return Some(0);
            }
            
            let first_char = _mm_set1_epi8(needle_bytes[0] as i8);
            
            for i in 0..=(haystack_bytes.len().saturating_sub(16)) {
                let chunk = _mm_loadu_si128(haystack_bytes.as_ptr().add(i) as *const __m128i);
                let matches = _mm_cmpeq_epi8(chunk, first_char);
                let mask = _mm_movemask_epi8(matches);
                
                if mask != 0 {
                    // 最初の文字のマッチがある場合、完全一致を確認
                    for bit in 0..16 {
                        if (mask & (1 << bit)) != 0 {
                            let pos = i + bit;
                            if pos + needle.len() <= haystack.len() {
                                if &haystack_bytes[pos..pos + needle.len()] == needle_bytes {
                                    return Some(pos);
                                }
                            }
                        }
                    }
                }
            }
        }
        
        None
    }
}

/// 文字分類
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CharClass {
    Digit,
    Alpha,
    Whitespace,
    Other,
}

/// 並列パーサー - 独立した部分を並列処理
pub struct ParallelParser;

impl ParallelParser {
    /// 複数のS式を並列解析
    pub fn parallel_parse_expressions<'a>(
        inputs: Vec<&'a str>
    ) -> Vec<ParseResult<'a, SchemeSexp<'a>>> {
        use rayon::prelude::*;
        
        inputs.into_par_iter()
            .map(|input| {
                SchemeParser::s_expression().parse(input)
            })
            .collect()
    }
    
    /// 文字列リストの並列トークン化
    pub fn parallel_tokenize(inputs: Vec<String>) -> Vec<Vec<Token>> {
        use rayon::prelude::*;
        
        inputs.into_par_iter()
            .map(|input| {
                // 簡略化されたトークン化
                Self::simple_tokenize(&input)
            })
            .collect()
    }
    
    fn simple_tokenize(input: &str) -> Vec<Token> {
        let mut tokens = Vec::new();
        let mut chars = input.char_indices().peekable();
        
        while let Some((pos, ch)) = chars.next() {
            match ch {
                '(' => tokens.push(Token::LeftParen(pos)),
                ')' => tokens.push(Token::RightParen(pos)),
                '\'' => tokens.push(Token::Quote(pos)),
                '`' => tokens.push(Token::Quasiquote(pos)),
                ',' => {
                    if chars.peek() == Some(&(pos + 1, '@')) {
                        chars.next();
                        tokens.push(Token::UnquoteSplicing(pos));
                    } else {
                        tokens.push(Token::Unquote(pos));
                    }
                }
                c if c.is_whitespace() => {
                    // 空白をスキップ
                    continue;
                }
                _ => {
                    // 識別子や数値として処理
                    tokens.push(Token::Identifier(pos, ch.to_string()));
                }
            }
        }
        
        tokens
    }
}

/// 簡略化されたトークン型
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
    LeftParen(usize),
    RightParen(usize),
    Quote(usize),
    Quasiquote(usize),
    Unquote(usize),
    UnquoteSplicing(usize),
    Identifier(usize, String),
}

/// メモ化パーサー - 同じ入力位置での結果をキャッシュ
pub struct MemoizedParser<'a> {
    cache: Arc<RwLock<HashMap<(usize, String), ParseResult<'a, SchemeSexp<'a>>>>>,
}

impl<'a> MemoizedParser<'a> {
    pub fn new() -> Self {
        Self {
            cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }
    
    pub fn parse_with_memo(&self, input: Input<'a>, position: usize) -> ParseResult<'a, SchemeSexp<'a>> {
        let key = (position, input.to_string());
        
        // キャッシュから確認
        {
            let cache = self.cache.read().unwrap();
            if let Some(result) = cache.get(&key) {
                return result.clone();
            }
        }
        
        // 実際にパースを実行
        let result = SchemeParser::s_expression().parse(input);
        
        // 結果をキャッシュ
        {
            let mut cache = self.cache.write().unwrap();
            cache.insert(key, result.clone());
        }
        
        result
    }
    
    pub fn clear_cache(&self) {
        let mut cache = self.cache.write().unwrap();
        cache.clear();
    }
    
    pub fn cache_size(&self) -> usize {
        let cache = self.cache.read().unwrap();
        cache.len()
    }
}

/// ゼロコピー最適化パーサー
pub struct ZeroCopyParser;

impl ZeroCopyParser {
    /// 文字列スライスを使った効率的な識別子解析
    pub fn efficient_symbol<'a>(input: Input<'a>) -> ParseResult<'a, &'a str> {
        let start = input.as_ptr();
        let mut end = start;
        let mut bytes_iter = input.bytes();
        
        // 最初の文字をチェック
        if let Some(first_byte) = bytes_iter.next() {
            let first_char = first_byte as char;
            if !Self::is_scheme_identifier_start(first_char) {
                return Err(Box::new(ParseError::new(
                    "Invalid identifier start".to_string(),
                    Span::new(0, 1)
                )));
            }
            unsafe { end = end.add(1); }
        } else {
            return Err(Box::new(ParseError::new(
                "Empty input".to_string(),
                Span::new(0, 0)
            )));
        }
        
        // 続く文字を処理
        for byte in bytes_iter {
            let ch = byte as char;
            if Self::is_scheme_identifier_continue(ch) {
                unsafe { end = end.add(1); }
            } else {
                break;
            }
        }
        
        let symbol_len = unsafe { end.offset_from(start) } as usize;
        let symbol = &input[..symbol_len];
        let remaining = &input[symbol_len..];
        
        Ok((remaining, symbol))
    }
    
    fn is_scheme_identifier_start(ch: char) -> bool {
        ch.is_alphabetic() || "!$%&*+-./:<=>?@^_~".contains(ch)
    }
    
    fn is_scheme_identifier_continue(ch: char) -> bool {
        ch.is_alphanumeric() || "!$%&*+-./:<=>?@^_~".contains(ch)
    }
    
    /// バイト境界を意識した効率的な文字列分割
    pub fn split_at_char_boundary(s: &str, mid: usize) -> Option<(&str, &str)> {
        if mid == 0 {
            return Some(("", s));
        }
        if mid >= s.len() {
            return Some((s, ""));
        }
        
        // UTF-8バイト境界をチェック
        if s.is_char_boundary(mid) {
            Some(s.split_at(mid))
        } else {
            None
        }
    }
}

/// プロファイリング支援
pub struct ParserProfiler {
    parse_times: HashMap<String, Vec<f64>>,
    memory_usage: HashMap<String, usize>,
}

impl ParserProfiler {
    pub fn new() -> Self {
        Self {
            parse_times: HashMap::new(),
            memory_usage: HashMap::new(),
        }
    }
    
    pub fn time_parse<T, F>(&mut self, name: &str, parser: F) -> T
    where
        F: FnOnce() -> T,
    {
        let start = std::time::Instant::now();
        let result = parser();
        let elapsed = start.elapsed().as_secs_f64();
        
        self.parse_times.entry(name.to_string())
            .or_insert_with(Vec::new)
            .push(elapsed);
        
        result
    }
    
    pub fn record_memory_usage(&mut self, name: &str, bytes: usize) {
        self.memory_usage.insert(name.to_string(), bytes);
    }
    
    pub fn get_average_time(&self, name: &str) -> Option<f64> {
        self.parse_times.get(name).map(|times| {
            times.iter().sum::<f64>() / times.len() as f64
        })
    }
    
    pub fn get_memory_usage(&self, name: &str) -> Option<usize> {
        self.memory_usage.get(name).copied()
    }
    
    pub fn print_report(&self) {
        println!("Parser Performance Report");
        println!("========================");
        
        println!("\nParse Times (average):");
        for (name, times) in &self.parse_times {
            let avg = times.iter().sum::<f64>() / times.len() as f64;
            let min = times.iter().fold(f64::INFINITY, |a, &b| a.min(b));
            let max = times.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
            println!("  {}: avg={:.6}s, min={:.6}s, max={:.6}s, samples={}", 
                     name, avg, min, max, times.len());
        }
        
        println!("\nMemory Usage:");
        for (name, bytes) in &self.memory_usage {
            println!("  {}: {} bytes", name, bytes);
        }
    }
}

/// 最適化設定
#[derive(Debug, Clone)]
pub struct OptimizationConfig {
    pub enable_simd: bool,
    pub enable_parallel: bool,
    pub enable_memoization: bool,
    pub enable_zero_copy: bool,
    pub memoization_cache_size: usize,
    pub parallel_threshold: usize,
}

impl Default for OptimizationConfig {
    fn default() -> Self {
        Self {
            enable_simd: cfg!(target_arch = "x86_64"),
            enable_parallel: true,
            enable_memoization: true,
            enable_zero_copy: true,
            memoization_cache_size: 1000,
            parallel_threshold: 4,
        }
    }
}

/// 最適化済みパーサーファクトリー
pub struct OptimizedParserFactory {
    config: OptimizationConfig,
    profiler: ParserProfiler,
}

impl OptimizedParserFactory {
    pub fn new(config: OptimizationConfig) -> Self {
        Self {
            config,
            profiler: ParserProfiler::new(),
        }
    }
    
    pub fn create_s_expression_parser<'a>(&self) -> Box<dyn ParserCombinator<'a, SchemeSexp<'a>>> {
        if self.config.enable_memoization {
            Box::new(MemoizedSExpressionParser::new())
        } else {
            Box::new(StandardSExpressionParser::new())
        }
    }
    
    pub fn get_profiler(&self) -> &ParserProfiler {
        &self.profiler
    }
}

/// 標準S式パーサー
struct StandardSExpressionParser;

impl StandardSExpressionParser {
    fn new() -> Self {
        Self
    }
}

impl<'a> ParserCombinator<'a, SchemeSexp<'a>> for StandardSExpressionParser {
    fn parse(&self, input: Input<'a>) -> ParseResult<'a, SchemeSexp<'a>> {
        SchemeParser::s_expression().parse(input)
    }
}

/// メモ化S式パーサー
struct MemoizedSExpressionParser<'a> {
    memoizer: MemoizedParser<'a>,
}

impl<'a> MemoizedSExpressionParser<'a> {
    fn new() -> Self {
        Self {
            memoizer: MemoizedParser::new(),
        }
    }
}

impl<'a> ParserCombinator<'a, SchemeSexp<'a>> for MemoizedSExpressionParser<'a> {
    fn parse(&self, input: Input<'a>) -> ParseResult<'a, SchemeSexp<'a>> {
        // 入力位置を計算(簡略化)
        let position = input.as_ptr() as usize;
        self.memoizer.parse_with_memo(input, position)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[cfg(target_arch = "x86_64")]
    #[test]
    fn test_simd_char_classification() {
        let input = "hello123 world";
        let classes = SIMDPrimitives::simd_classify_chars(input);
        
        assert_eq!(classes.len(), input.len());
        assert_eq!(classes[0], CharClass::Alpha); // 'h'
        assert_eq!(classes[5], CharClass::Digit); // '1'
        assert_eq!(classes[8], CharClass::Whitespace); // ' '
    }
    
    #[cfg(target_arch = "x86_64")]
    #[test]
    fn test_simd_pattern_search() {
        let haystack = "hello world hello universe";
        let needle = "hello";
        
        let result = SIMDPrimitives::simd_find_pattern(haystack, needle);
        assert_eq!(result, Some(0));
        
        let result = SIMDPrimitives::simd_find_pattern(&haystack[6..], needle);
        assert_eq!(result, Some(6)); // "hello" at position 6 in the substring
    }
    
    #[test]
    fn test_parallel_parsing() {
        let inputs = vec!["(+ 1 2)", "(* 3 4)", "'hello"];
        let results = ParallelParser::parallel_parse_expressions(inputs);
        
        assert_eq!(results.len(), 3);
        for result in results {
            assert!(result.is_ok());
        }
    }
    
    #[test]
    fn test_memoized_parser() {
        let parser = MemoizedParser::new();
        
        let input = "(+ 1 2)";
        let result1 = parser.parse_with_memo(input, 0);
        let result2 = parser.parse_with_memo(input, 0);
        
        assert!(result1.is_ok());
        assert!(result2.is_ok());
        assert_eq!(parser.cache_size(), 1);
    }
    
    #[test]
    fn test_zero_copy_symbol_parsing() {
        let input = "hello-world";
        let result = ZeroCopyParser::efficient_symbol(input);
        
        assert!(result.is_ok());
        let (remaining, symbol) = result.unwrap();
        assert_eq!(symbol, "hello-world");
        assert_eq!(remaining, "");
    }
    
    #[test]
    fn test_parser_profiler() {
        let mut profiler = ParserProfiler::new();
        
        let result = profiler.time_parse("test_parse", || {
            std::thread::sleep(std::time::Duration::from_millis(1));
            42
        });
        
        assert_eq!(result, 42);
        let avg_time = profiler.get_average_time("test_parse");
        assert!(avg_time.is_some());
        assert!(avg_time.unwrap() > 0.0);
    }
    
    #[test]
    fn test_optimization_config() {
        let config = OptimizationConfig::default();
        assert!(config.enable_memoization);
        assert!(config.enable_zero_copy);
        assert_eq!(config.memoization_cache_size, 1000);
    }
    
    #[test]
    fn test_optimized_parser_factory() {
        let config = OptimizationConfig::default();
        let factory = OptimizedParserFactory::new(config);
        
        let parser = factory.create_s_expression_parser();
        let result = parser.parse("(+ 1 2)");
        assert!(result.is_ok());
    }
    
    #[test]
    fn test_char_boundary_split() {
        let s = "hello世界";
        
        let result = ZeroCopyParser::split_at_char_boundary(s, 5);
        assert!(result.is_some());
        let (left, right) = result.unwrap();
        assert_eq!(left, "hello");
        assert_eq!(right, "世界");
        
        // 無効なバイト境界
        let result = ZeroCopyParser::split_at_char_boundary(s, 6);
        assert!(result.is_none());
    }
}