dasunpubudumal / rlox_lexer

Lox language lexer written in Rust https://craftinginterpreters.com/contents.html
0 stars 0 forks source link

Write a test that uses the program_1 fixture #3

Closed dasunpubudumal closed 2 months ago

dasunpubudumal commented 3 months ago

Write a test that asserts correct functionality of the lexer for tests/fixtures/program_1.lox file.

// If x is greater than 1, return x. Otherwise, return 0.
fun main() {
    var x = 1;
    if (x > 1) {
        return x;
    } else {
        return 0;
    }
}

This test asserts for an integrated behavior of the lexer as a whole. Therefore, it should be included in tests directory.

There are likely to be test failures. Report the issues within this ticket as comments.

dasunpubudumal commented 3 months ago

Note

Rust adds a [raw] type into the vector of tokens. So, if the actual program has got 27 tokens, with the raw type it would be 27 + 1 = 28 tokens.

Image

However, not in src/lib.rs:46, when we access the vector through last() function, it returns the correct token - not the [raw] type.

This is probably because we've converted the iterator that is returned to a vector, and a vector requires an allocation.