diff --git a/src/scripts/interpreter.ts b/src/scripts/interpreter.ts index 93520c5..d2d19f9 100644 --- a/src/scripts/interpreter.ts +++ b/src/scripts/interpreter.ts @@ -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; + } } \ No newline at end of file diff --git a/src/scripts/main.ts b/src/scripts/main.ts index 3ec1ddd..4c69ea3 100644 --- a/src/scripts/main.ts +++ b/src/scripts/main.ts @@ -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,