~tim/scheme-vm

9d7f73bddaee39fb35db795c8d5b549ffdb72bda — Tim Morgan 2 years ago eee4616
Fix error when it's on line 1
2 files changed, 21 insertions(+), 1 deletions(-)

M spec/program_spec.rb
M src/atom.rs
M spec/program_spec.rb => spec/program_spec.rb +17 -0
@@ 213,6 213,23 @@ describe Program do
      end
    end

    context 'when an undefined variable is referenced on line 1 with weird spacing' do
      let(:code) do
        ' ( foo)'
      end

      it 'shows the cursor at the proper position' do
        subject.run
        stdout.rewind
        expect(stdout.read).to eq(
          "Error: foo is not defined\n\n" \
            "#{__FILE__}#1\n\n" \
            "   ( foo)\n" \
            "     ^ foo is not defined\n"
        )
      end
    end

    context 'exception in macro in another file' do
      let(:code) do
        '(include "./fixtures/bad-macro") ' \

M src/atom.rs => src/atom.rs +4 -1
@@ 4,7 4,10 @@ use rb::{Value, RB_NIL};
pub fn atom(name: &str, filename: &str, offset: usize, newlines: &Vec<usize>) -> Value {
    let lines_before: Vec<usize> = newlines.iter().take_while(|i| *i < &offset).map(|i| *i).collect();
    let line = int2rbnum!(lines_before.len() + 1);
    let column = int2rbnum!(offset - lines_before.last().unwrap_or(&0));
    let column = match lines_before.last() {
        Some(i) => int2rbnum!(offset - i),
        None => int2rbnum!(offset + 1)
    };
    let name = rb::str_new(&name.to_string());
    let filename = rb::str_new(&filename.to_string());
    let vm = rb::const_get("VM", &RB_NIL);