Skip to content

Commit

Permalink
fix bug for empty rows
Browse files Browse the repository at this point in the history
  • Loading branch information
mousetail committed Apr 8, 2024
1 parent dc4a6cf commit b7f5074
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/scripts/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,18 @@ export function step(o: ProgramState) {
} else {
throw new Error("Unexpected token: " + token + " Note: all symbols must be lower case");
}
o.cursor[0] += o.cursor_direction[0];
o.cursor[1] += o.cursor_direction[1];

o.cursor[1] = (o.cursor[1] + o.program.length) % o.program.length;
o.cursor[0] = (o.cursor[0] + o.program[o.cursor[1]].length) % o.program[o.cursor[1]].length;
if (o.cursor_direction[0] != 0) {
o.cursor[0] += o.cursor_direction[0];

const row_length = o.program[o.cursor[1]].length;
if (row_length != 0) {
o.cursor[0] = (o.cursor[0] + row_length) % row_length;
}
}

if (o.cursor_direction[1] != 0) {
o.cursor[1] += o.cursor_direction[1];
o.cursor[1] = (o.cursor[1] + o.program.length) % o.program.length;
}
}
5 changes: 4 additions & 1 deletion src/scripts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ function text_with_cursor(program: number[][], cursor: [number, number]): (strin
let text_before = text.slice(0, cursor[1]).map(i => i + '\n').join('') + text[cursor[1]].substring(0, cursor[0]);
let text_after = text[cursor[1]].substring(cursor[0] + 1) + '\n' + text.slice(cursor[1] + 1).map(i => i + '\n').join('');

let cursor_content = text[cursor[1]][cursor[0]];
let span = document.createElement('span');
span.innerText = text[cursor[1]][cursor[0]];
if (cursor_content != undefined) {
span.innerText = cursor_content;
}
span.classList.add('cursor');
return [
text_before,
Expand Down

0 comments on commit b7f5074

Please sign in to comment.