bcomp 0.1.0

A compiler for a subset of the BASIC language
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
//! The lexer will take the main source code and turn it into tokens
//!
//! The lexer is the first module called at entry, and is the first thing to touch the code being modified
//! TODO: Fill out the rest of the required rust documentation like the examples and such

use std::{cell::Cell, vec};

// We'll need to estable some kind of token system
pub mod token;

/// Lexer struct
///
/// We need a lifetime for the input string.
/// I wonder if it would be better to create a shared reference the main program is responsible for deleting
/// Maybe with like RC<str> or something
pub struct Lexer<'a> {
    input: &'a str,
    size: Cell<usize>,
    location: Cell<usize>,
}

#[allow(dead_code, unused_variables)]
impl<'a> Lexer<'a> {
    pub fn new(input: &'a str) -> Self {
        Self {
            input,
            size: input.chars().count().into(),
            location: 0.into(),
        }
    }

    /// Parses the tokens of the input into a vector of tokens
    pub fn parse_tokens(&self) -> Vec<token::Token> {
        let mut tokens = vec![];

        while self.location.get() < self.size.get() {
            let tok = self.next_token();
            tokens.push(tok);
        }

        tokens
    }

    /// Reads the next token and increments the current location
    ///
    /// If used while the location is out of bounds, the function returns 0x00
    fn read_char(&self) -> char {
        if self.location.get() < self.size.get() {
            let char = self.input.chars().nth(self.location.get()).unwrap();
            self.location.set(self.location.get() + 1);
            char // returns char
        } else {
            '\0'
        }
    }

    /// Read the next token
    ///
    #[expect(
        clippy::too_many_lines,
        reason = "token dispatch is intentionally kept together while the lexer is small"
    )]
    pub fn next_token(&self) -> token::Token {
        // Skip whitespace
        while self.peek_char().is_whitespace() {
            self.read_char();
        }
        while self.peek_char() == '\n' {
            self.read_char();
        }

        let ch = self.peek_char();

        match ch {
            // Example: single-character tokens
            ';' => {
                self.read_char();
                token::Token::Semicolon
            }
            ',' => {
                self.read_char();
                token::Token::Comma
            }
            ':' => {
                self.read_char();
                token::Token::Colon
            }
            '=' => {
                self.read_char();
                token::Token::Equal
            }
            '<' => {
                self.read_char();
                match self.peek_char() {
                    '=' => {
                        self.read_char();
                        token::Token::LessEqual
                    }
                    '>' => {
                        self.read_char();
                        token::Token::NotEqual
                    }
                    _ => token::Token::Less,
                }
            }
            '>' => {
                self.read_char();
                if self.peek_char() == '=' {
                    self.read_char();
                    token::Token::GreaterEqual
                } else {
                    token::Token::Greater
                }
            }
            '+' => {
                self.read_char();
                token::Token::Plus
            }
            '-' => {
                self.read_char();
                token::Token::Minus
            }
            '*' => {
                self.read_char();
                token::Token::Asterisk
            }
            '/' => {
                self.read_char();
                token::Token::Slash
            }
            '(' => {
                self.read_char();
                token::Token::LParen
            }
            ')' => {
                self.read_char();
                token::Token::RParen
            }
            '\0' => token::Token::Eof,
            '\"' => {
                let mut str_lit_char = self.read_char();
                let mut str_literal: String = String::new();
                str_literal.push(str_lit_char);
                str_lit_char = self.read_char();
                while str_lit_char != '\"' {
                    str_literal.push(str_lit_char);
                    if self.size == self.location {
                        return token::Token::InvalidLit(str_literal);
                    }
                    str_lit_char = self.read_char();
                }
                str_literal.push(str_lit_char);
                token::Token::String(str_literal)
            }
            '\'' => {
                let mut ch_lit_char = self.read_char();
                let mut ch_literal: String = String::new();
                ch_literal.push(ch_lit_char);
                ch_lit_char = self.read_char();
                while ch_lit_char != '\'' {
                    ch_literal.push(ch_lit_char);
                    if self.size == self.location {
                        return token::Token::InvalidLit(ch_literal);
                    }
                    ch_lit_char = self.read_char();
                }
                ch_literal.push(ch_lit_char);
                if ch_literal.chars().count() > 3 || ch_literal.chars().count() < 3 {
                    return token::Token::InvalidLit(ch_literal);
                }
                token::Token::Char(ch_literal.chars().nth(1).unwrap())
            }
            _ => {
                // Example: identifier or number
                if ch.is_alphabetic() {
                    let ident: String = self.read_identifier();
                    //checks if the token is a keyword
                    match ident.as_str() {
                        "print" => token::Token::Print,
                        "let" => token::Token::Let,
                        "if" => token::Token::If,
                        "then" => token::Token::Then,
                        "else" => token::Token::Else,
                        "for" => token::Token::For,
                        "to" => token::Token::To,
                        "next" => token::Token::Next,
                        "goto" => token::Token::Goto,
                        "gosub" => token::Token::Gosub,
                        "return" => token::Token::Return,
                        "end" => token::Token::End,
                        "rem" => token::Token::Rem,
                        _ => token::Token::Identifier(ident),
                    }
                } else if ch.is_numeric() {
                    let num = self.read_number();
                    token::Token::Integer(num) // ARCHER: For now we'll just support integers, you should fix this
                } else {
                    self.read_char();
                    token::Token::Invalid(ch)
                }
            }
        }
    }

    /// Reads an identifier (e.g., variable/function name)
    fn read_identifier(&self) -> String {
        let mut ident = String::new();
        while self.peek_char().is_alphanumeric() || self.peek_char() == '_' {
            ident.push(self.read_char());
        }
        ident
    }

    /// Reads a number (integer only for now)
    fn read_number(&self) -> i64 {
        let mut num = String::new();
        while self.peek_char().is_numeric() {
            num.push(self.read_char());
        }
        num.parse().unwrap()
    }

    /// Take a look at the next token without incrementing the location.
    pub fn peek(&self) -> token::Token {
        let location = self.location.get();
        let token = self.next_token();
        self.location.set(location);
        token
    }

    /// Take a look at the next character without incrimenting the location
    ///
    /// If used while the location is out of bounds, the function returns 0x00
    fn peek_char(&self) -> char {
        if self.location.get() < self.size.get() {
            self.input.chars().nth(self.location.get()).unwrap()
        } else {
            '\0'
        }
    }
}

#[cfg(test)]
mod test {
    use crate::lexer::{Lexer, token};
    use ntest::timeout;

    #[test]
    #[timeout(100)]
    fn test_peek_char() {
        let lex: Lexer<'_> = Lexer::new("Alphabet");

        assert_eq!(lex.peek_char(), 'A');
        assert_eq!(lex.peek_char(), 'A');
        assert_eq!(lex.peek_char(), 'A');
        assert_eq!(lex.read_char(), 'A');

        assert_eq!(lex.peek_char(), 'l');
    }

    #[test]
    #[timeout(100)]
    fn test_peek_token() {
        let lex: Lexer<'_> = Lexer::new("print \"HELLO\";");

        assert_eq!(lex.peek(), token::Token::Print);
        assert_eq!(lex.peek(), token::Token::Print);
        assert_eq!(lex.next_token(), token::Token::Print);
        assert_eq!(lex.peek(), token::Token::String("\"HELLO\"".to_string()));
        assert_eq!(
            lex.next_token(),
            token::Token::String("\"HELLO\"".to_string())
        );
        assert_eq!(lex.next_token(), token::Token::Semicolon);
    }

    #[test]
    #[timeout(100)]
    fn test_peek_token_skips_whitespace_without_consuming() {
        let lex: Lexer<'_> = Lexer::new("   <= 10");

        assert_eq!(lex.peek(), token::Token::LessEqual);
        assert_eq!(lex.peek(), token::Token::LessEqual);
        assert_eq!(lex.next_token(), token::Token::LessEqual);
        assert_eq!(lex.next_token(), token::Token::Integer(10));
    }

    #[test]
    #[timeout(100)]
    fn test_read_char() {
        let lex: Lexer<'_> = Lexer::new("Alphabet");

        assert_eq!(lex.read_char(), 'A');
        assert_eq!(lex.read_char(), 'l');
        assert_eq!(lex.read_char(), 'p');
        assert_eq!(lex.read_char(), 'h');
        assert_eq!(lex.read_char(), 'a');
        assert_eq!(lex.read_char(), 'b');
        assert_eq!(lex.read_char(), 'e');
        assert_eq!(lex.read_char(), 't');
        assert_eq!(lex.read_char(), '\0');
        assert_eq!(lex.read_char(), '\0');
        assert_eq!(lex.read_char(), '\0');
    }

    #[test]
    #[timeout(100)]
    fn test_main_fn() {
        let lex = Lexer::new("main()");
        let tokens = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Identifier("main".to_string()));
        assert_eq!(tokens[1], token::Token::LParen);
        assert_eq!(tokens[2], token::Token::RParen);
    }

    #[test]
    #[timeout(100)]
    fn test_next_token() {
        let lex: Lexer<'_> = Lexer::new("ALPHA + GAMMA");

        assert_eq!(
            lex.next_token(),
            token::Token::Identifier("ALPHA".to_string())
        );
        assert_eq!(lex.next_token(), token::Token::Plus);
        assert_eq!(
            lex.next_token(),
            token::Token::Identifier("GAMMA".to_string())
        );
    }

    #[test]
    #[timeout(100)]
    fn test_parse_tokens() {
        let lex: Lexer<'_> = Lexer::new("ALPHA + GAMMA");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Identifier("ALPHA".to_string()));
        assert_eq!(tokens[1], token::Token::Plus);
        assert_eq!(tokens[2], token::Token::Identifier("GAMMA".to_string()));
    }

    #[test]
    #[timeout(100)]
    fn test_parse_separators() {
        let lex: Lexer<'_> = Lexer::new("A,B:C;");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Identifier("A".to_string()));
        assert_eq!(tokens[1], token::Token::Comma);
        assert_eq!(tokens[2], token::Token::Identifier("B".to_string()));
        assert_eq!(tokens[3], token::Token::Colon);
        assert_eq!(tokens[4], token::Token::Identifier("C".to_string()));
        assert_eq!(tokens[5], token::Token::Semicolon);
    }

    #[test]
    #[timeout(100)]
    fn test_parse_comparison_operators() {
        let lex: Lexer<'_> = Lexer::new("= < > <= >= <>");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Equal);
        assert_eq!(tokens[1], token::Token::Less);
        assert_eq!(tokens[2], token::Token::Greater);
        assert_eq!(tokens[3], token::Token::LessEqual);
        assert_eq!(tokens[4], token::Token::GreaterEqual);
        assert_eq!(tokens[5], token::Token::NotEqual);
    }

    #[test]
    #[timeout(100)]
    fn test_string_token() {
        let lex: Lexer<'_> = Lexer::new("\"ABCD\" \"Sheep\" \"LITERAL\"");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::String(String::from("\"ABCD\"")));
        assert_eq!(tokens[1], token::Token::String(String::from("\"Sheep\"")));
        assert_eq!(tokens[2], token::Token::String(String::from("\"LITERAL\"")));
    }

    #[test]
    #[timeout(100)]
    fn test_char_lit_token() {
        let lex: Lexer<'_> = Lexer::new("'a' 'ab'");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Char('a'));
        assert_eq!(tokens[1], token::Token::InvalidLit("'ab'".to_string()));
    }

    #[test]
    #[timeout(100)]
    fn test_invalid_str_literal() {
        let lex: Lexer<'_> = Lexer::new("\"aaaaaaaa");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(
            tokens[0],
            token::Token::InvalidLit("\"aaaaaaaa".to_string())
        );
    }

    #[test]
    #[timeout(100)]
    fn test_invalid_ch_literal() {
        let lex: Lexer<'_> = Lexer::new("'aaaaaaaa");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(
            tokens[0],
            token::Token::InvalidLit("\'aaaaaaaa".to_string())
        );
    }

    #[test]
    #[timeout(100)]
    fn test_parse_keywords() {
        let lex: Lexer<'_> =
            Lexer::new("print let if then else for to next goto gosub return end rem");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Print);
        assert_eq!(tokens[1], token::Token::Let);
        assert_eq!(tokens[2], token::Token::If);
        assert_eq!(tokens[3], token::Token::Then);
        assert_eq!(tokens[4], token::Token::Else);
        assert_eq!(tokens[5], token::Token::For);
        assert_eq!(tokens[6], token::Token::To);
        assert_eq!(tokens[7], token::Token::Next);
        assert_eq!(tokens[8], token::Token::Goto);
        assert_eq!(tokens[9], token::Token::Gosub);
        assert_eq!(tokens[10], token::Token::Return);
        assert_eq!(tokens[11], token::Token::End);
        assert_eq!(tokens[12], token::Token::Rem);
    }

    #[test]
    #[timeout(100)]
    fn test_invalid_num() {
        let lex: Lexer<'_> = Lexer::new("65e");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Integer(65));
        assert_eq!(tokens[1], token::Token::Identifier("e".to_string()));
    }

    #[test]
    #[timeout(100)]
    fn lex_example_code() {
        let lex: Lexer<'_> = Lexer::new("1;\n2;");
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Integer(1));
        assert_eq!(tokens[1], token::Token::Semicolon);
        assert_eq!(tokens[2], token::Token::Integer(2));
        assert_eq!(tokens[3], token::Token::Semicolon);
    }

    #[test]
    #[timeout(100)]
    fn test_print() {
        let file_path = "./tests/input/print.bsc";

        // Attempt to read the file contents into a String
        let contents = std::fs::read_to_string(file_path).unwrap();
        let lex: Lexer<'_> = Lexer::new(&contents);
        let tokens: Vec<token::Token> = lex.parse_tokens();

        assert_eq!(tokens[0], token::Token::Print);
        assert_eq!(
            tokens[1],
            token::Token::String("\"HELLO WORLD\"".to_string())
        );
        assert_eq!(tokens[2], token::Token::Semicolon);
    }
}