Skip to content

Commit

Permalink
lib/vm: reuse work from run_str in advance_cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
clarete committed Oct 3, 2023
1 parent 098a303 commit 5de56a1
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions langlang_lib/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,20 +389,13 @@ impl<'a> VM<'a> {
fn advance_cursor(&mut self) -> Result<(), Error> {
let c = &self.source[self.cursor];
self.cursor += 1;
self.column += 1;

match c {
value::Value::Char(c) if c.value == '\n' => {
self.column = 0;
self.line += 1;
}
_ => {}
}

// re-use work done in run_str()
let start = c.span().start;
self.line = start.line;
self.column = start.column;
if self.cursor > self.ffp {
self.ffp = self.cursor;
}

Ok(())
}

Expand Down Expand Up @@ -498,7 +491,7 @@ impl<'a> VM<'a> {

pub fn run_str(&mut self, input: &str) -> Result<Option<Value>, Error> {
let mut line = 0;
let mut column = 0;
let mut column = 1;
let source = input
.chars()
.enumerate()
Expand All @@ -507,7 +500,7 @@ impl<'a> VM<'a> {
let end = Position::new(i + 1, line, column + 1);
column += 1;
if c == '\n' {
column = 0;
column = 1;
line += 1;
}
value::Char::new_val(Span::new(start, end), c)
Expand Down

0 comments on commit 5de56a1

Please sign in to comment.