rustleaf 0.1.0

A simple programming language interpreter written 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
#[cfg(test)]
mod tests {
    use crate::lexer::{Lexer, Token, TokenType};

    #[test]
    fn test_keywords() {
        let source = "var fn if else while for loop return break continue class static self";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Var),
                Token::simple(TokenType::Fn),
                Token::simple(TokenType::If),
                Token::simple(TokenType::Else),
                Token::simple(TokenType::While),
                Token::simple(TokenType::For),
                Token::simple(TokenType::Loop),
                Token::simple(TokenType::Return),
                Token::simple(TokenType::Break),
                Token::simple(TokenType::Continue),
                Token::simple(TokenType::Class),
                Token::simple(TokenType::Static),
                Token::with_text(TokenType::Ident, "self"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_additional_keywords() {
        let source = "pub use raise import match try catch macro";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Pub),
                Token::simple(TokenType::Use),
                Token::with_text(TokenType::Ident, "raise"),
                Token::simple(TokenType::Import),
                Token::simple(TokenType::Match),
                Token::simple(TokenType::Try),
                Token::simple(TokenType::Catch),
                Token::simple(TokenType::Macro),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_identifiers() {
        let source = "hello world _private camelCase snake_case CONSTANT x123";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::Ident, "hello"),
                Token::with_text(TokenType::Ident, "world"),
                Token::with_text(TokenType::Ident, "_private"),
                Token::with_text(TokenType::Ident, "camelCase"),
                Token::with_text(TokenType::Ident, "snake_case"),
                Token::with_text(TokenType::Ident, "CONSTANT"),
                Token::with_text(TokenType::Ident, "x123"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_integers() {
        let source = "42 0 123 1_000_000 0xFF 0xff 0o77 0b1010 0b1111_0000";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::Int, "42"),
                Token::with_text(TokenType::Int, "0"),
                Token::with_text(TokenType::Int, "123"),
                Token::with_text(TokenType::Int, "1_000_000"),
                Token::with_text(TokenType::Int, "0xFF"),
                Token::with_text(TokenType::Int, "0xff"),
                Token::with_text(TokenType::Int, "0o77"),
                Token::with_text(TokenType::Int, "0b1010"),
                Token::with_text(TokenType::Int, "0b1111_0000"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_floats() {
        let source = "3.14159 1.0 0.1 1e10 2.5e-4 1E+6";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::Float, "3.14159"),
                Token::with_text(TokenType::Float, "1.0"),
                Token::with_text(TokenType::Float, "0.1"),
                Token::with_text(TokenType::Float, "1e10"),
                Token::with_text(TokenType::Float, "2.5e-4"),
                Token::with_text(TokenType::Float, "1E+6"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_strings() {
        let source = r#""hello" "world\n" r"raw\string""#;
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::String, "hello"),
                Token::with_text(TokenType::String, "world\n"), // \n should become actual newline
                Token::with_text(TokenType::RawString, "raw\\string"), // raw strings keep backslashes
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_escape_sequences() {
        let source = r#""line1\nline2" "tab\there" "quote\"test" "unicode\u{1F604}""#;
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::String, "line1\nline2"),
                Token::with_text(TokenType::String, "tab\there"),
                Token::with_text(TokenType::String, "quote\"test"),
                Token::with_text(TokenType::String, "unicode😄"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_all_string_types() {
        let source = r#""regular" r"raw""#;
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::String, "regular"),
                Token::with_text(TokenType::RawString, "raw"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_raw_string_multiline() {
        let source = r#"r"hello
world""#;
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::RawString, "hello\nworld"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_booleans_and_null() {
        let source = "true false null";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::True),
                Token::simple(TokenType::False),
                Token::simple(TokenType::Null),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_operators() {
        let source = "+ - * / % ** = += -= *= /= %= == != < > <= >= | .. ..=";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Plus),
                Token::simple(TokenType::Minus),
                Token::simple(TokenType::Star),
                Token::simple(TokenType::Slash),
                Token::simple(TokenType::Percent),
                Token::simple(TokenType::StarStar),
                Token::simple(TokenType::Equal),
                Token::simple(TokenType::PlusEqual),
                Token::simple(TokenType::MinusEqual),
                Token::simple(TokenType::StarEqual),
                Token::simple(TokenType::SlashEqual),
                Token::simple(TokenType::PercentEqual),
                Token::simple(TokenType::EqualEqual),
                Token::simple(TokenType::BangEqual),
                Token::simple(TokenType::Less),
                Token::simple(TokenType::Greater),
                Token::simple(TokenType::LessEqual),
                Token::simple(TokenType::GreaterEqual),
                Token::simple(TokenType::Pipe),
                Token::simple(TokenType::DotDot),
                Token::simple(TokenType::DotDotEqual),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_punctuation() {
        let source = "( ) { } [ ] , ; . : :: #";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::LeftParen),
                Token::simple(TokenType::RightParen),
                Token::simple(TokenType::LeftBrace),
                Token::simple(TokenType::RightBrace),
                Token::simple(TokenType::LeftBracket),
                Token::simple(TokenType::RightBracket),
                Token::simple(TokenType::Comma),
                Token::simple(TokenType::Semicolon),
                Token::simple(TokenType::Dot),
                Token::simple(TokenType::Colon),
                Token::simple(TokenType::DoubleColon),
                Token::simple(TokenType::Hash),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_logical_keywords() {
        let source = "and or xor not in is";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::And),
                Token::simple(TokenType::Or),
                Token::simple(TokenType::Xor),
                Token::simple(TokenType::NotIn), // "not in" gets rewritten to NotIn
                Token::simple(TokenType::Is),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_macro_tokens() {
        let source = "# macro";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Hash),
                Token::simple(TokenType::Macro),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_macro_annotation_syntax() {
        let source = "#[test]";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Hash),
                Token::simple(TokenType::LeftBracket),
                Token::with_text(TokenType::Ident, "test"),
                Token::simple(TokenType::RightBracket),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_comments_ignored() {
        let source = "var x = 42; // this is a comment\n/* multi\nline */ var y = 24;";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Var),
                Token::with_text(TokenType::Ident, "x"),
                Token::simple(TokenType::Equal),
                Token::with_text(TokenType::Int, "42"),
                Token::simple(TokenType::Semicolon),
                Token::simple(TokenType::Var),
                Token::with_text(TokenType::Ident, "y"),
                Token::simple(TokenType::Equal),
                Token::with_text(TokenType::Int, "24"),
                Token::simple(TokenType::Semicolon),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_whitespace_ignored() {
        let source = "  var   x   =   42  ;  ";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Var),
                Token::with_text(TokenType::Ident, "x"),
                Token::simple(TokenType::Equal),
                Token::with_text(TokenType::Int, "42"),
                Token::simple(TokenType::Semicolon),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_complex_expression() {
        let source = r#"var greeting = "Hello, " + name + "!";"#;
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Var),
                Token::with_text(TokenType::Ident, "greeting"),
                Token::simple(TokenType::Equal),
                Token::with_text(TokenType::String, "Hello, "),
                Token::simple(TokenType::Plus),
                Token::with_text(TokenType::Ident, "name"),
                Token::simple(TokenType::Plus),
                Token::with_text(TokenType::String, "!"),
                Token::simple(TokenType::Semicolon),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_function_definition() {
        let source = "fn calculate(x, y) { return x * y; }";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Fn),
                Token::with_text(TokenType::Ident, "calculate"),
                Token::simple(TokenType::LeftParen),
                Token::with_text(TokenType::Ident, "x"),
                Token::simple(TokenType::Comma),
                Token::with_text(TokenType::Ident, "y"),
                Token::simple(TokenType::RightParen),
                Token::simple(TokenType::LeftBrace),
                Token::simple(TokenType::Return),
                Token::with_text(TokenType::Ident, "x"),
                Token::simple(TokenType::Star),
                Token::with_text(TokenType::Ident, "y"),
                Token::simple(TokenType::Semicolon),
                Token::simple(TokenType::RightBrace),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_error_unexpected_character() {
        let source = "var x = $;"; // $ is not a valid token
        let result = Lexer::tokenize(source);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Unexpected character"));
        assert!(error_msg.contains("$"));
    }

    #[test]
    fn test_longest_match_wins() {
        // Test that >= is tokenized as GreaterEqual, not Greater + Equal
        let source = "x >= y";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::Ident, "x"),
                Token::simple(TokenType::GreaterEqual),
                Token::with_text(TokenType::Ident, "y"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_keyword_vs_identifier() {
        // Test that "variable" is an identifier, not "var" + "iable"
        let source = "variable variance";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::Ident, "variable"),
                Token::with_text(TokenType::Ident, "variance"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_empty_input() {
        let source = "";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(tokens, vec![Token::simple(TokenType::Eof)]);
    }

    #[test]
    fn test_only_whitespace() {
        let source = "   \t  \n  \r\n  ";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(tokens, vec![Token::simple(TokenType::Eof)]);
    }

    #[test]
    fn test_utf8_bom_handling() {
        // Test that BOM is ignored
        let source_with_bom = "\u{FEFF}var x = 42;";
        let tokens = Lexer::tokenize(source_with_bom).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::simple(TokenType::Var),
                Token::with_text(TokenType::Ident, "x"),
                Token::simple(TokenType::Equal),
                Token::with_text(TokenType::Int, "42"),
                Token::simple(TokenType::Semicolon),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_range_operators() {
        let source = "0..10 0..=10 1..n";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::Int, "0"),
                Token::simple(TokenType::DotDot),
                Token::with_text(TokenType::Int, "10"),
                Token::with_text(TokenType::Int, "0"),
                Token::simple(TokenType::DotDotEqual),
                Token::with_text(TokenType::Int, "10"),
                Token::with_text(TokenType::Int, "1"),
                Token::simple(TokenType::DotDot),
                Token::with_text(TokenType::Ident, "n"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_doc_comments() {
        let source = "/// This is a doc comment\n//! This is an inner doc comment";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::DocComment, " This is a doc comment"),
                Token::with_text(TokenType::InnerDocComment, " This is an inner doc comment"),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_doc_comment_blocks() {
        let source = "/** Block doc comment */\n/*! Inner block doc comment */";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::DocCommentBlock, " Block doc comment "),
                Token::with_text(TokenType::InnerDocCommentBlock, " Inner block doc comment "),
                Token::simple(TokenType::Eof),
            ]
        );
    }

    #[test]
    fn test_doc_comments_vs_regular_comments() {
        let source = "/// Doc comment\n// Regular comment\n/** Block doc */\n/* Regular block */";
        let tokens = Lexer::tokenize(source).unwrap();

        assert_eq!(
            tokens,
            vec![
                Token::with_text(TokenType::DocComment, " Doc comment"),
                Token::with_text(TokenType::DocCommentBlock, " Block doc "),
                Token::simple(TokenType::Eof),
            ]
        );
    }
}