jfecher / ante

A safe, easy systems language
http://antelang.org
MIT License
1.9k stars 79 forks source link

Do nothing if the input file is empty #118

Closed mtshiba closed 2 years ago

mtshiba commented 2 years ago

This is a trivial problem, but the compiler crashed when I tried to compile an empty file.

$ ante empty.an
empty.an:1:1     error: failed trying to parse a term
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src\error\mod.rs:203:79
stack backtrace:
...

In languages that do not have or require entry points, such as the main function, an empty file is often valid input and nothing is done as a result of execution.

So I've modified the compiler to immediately terminate if the input is an empty file.

jfecher commented 2 years ago

I don't think a hard coded check for if the file length is 0 is the right way to solve this problem. The crash occurs on an unwrap while reporting an error, so I pushed commit ea67b72 which solves it by changing it to unwrap_or("").

There remains the separate issue of running the compiler on an empty file (or one only filled with whitespace or comments) producing a parser error:

empty.an:1:1    error: failed trying to parse a term

I consider this a separate issue, so I'm closing this PR as the core issue is fixed.