-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProgramSequence.js
More file actions
85 lines (69 loc) · 2.53 KB
/
Copy pathProgramSequence.js
File metadata and controls
85 lines (69 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// @flow
import type { Program } from './types';
export default class ProgramSequence {
program: Program;
programCounter: number;
constructor(program: Program, programCounter: number) {
this.program = program;
this.programCounter = programCounter;
}
getProgram(): Program {
return this.program;
}
getProgramLength(): number {
return this.program.length;
}
getProgramCounter(): number {
return this.programCounter;
}
getCurrentProgramStep(): string {
return this.program[this.programCounter];
}
getProgramStepAt(index: number): string {
return this.program[index];
}
updateProgram(program: Program): ProgramSequence {
return new ProgramSequence(program, this.programCounter);
}
updateProgramCounter(programCounter: number): ProgramSequence {
return new ProgramSequence(this.program, programCounter);
}
updateProgramAndProgramCounter(program: Program, programCounter: number): ProgramSequence {
return new ProgramSequence(program, programCounter);
}
incrementProgramCounter(): ProgramSequence {
return new ProgramSequence(this.program, this.programCounter + 1);
}
overwriteStep(index: number, command: string): ProgramSequence {
const program = this.program.slice();
program[index] = command;
return this.updateProgram(program);
}
insertStep(index: number, command: string): ProgramSequence {
const program = this.program.slice();
program.splice(index, 0, command);
if (index <= this.programCounter) {
return this.updateProgramAndProgramCounter(program, this.programCounter + 1);
} else {
return this.updateProgram(program);
}
}
deleteStep(index: number): ProgramSequence {
const program = this.program.slice();
program.splice(index, 1);
if (index < this.programCounter && this.program.length > 1) {
return this.updateProgramAndProgramCounter(program, this.programCounter - 1);
} else {
return this.updateProgram(program);
}
}
swapStep(indexFrom: number, indexTo: number): ProgramSequence {
const program = this.program.slice();
if (program[indexFrom] != null && program[indexTo] != null) {
const currentStep = program[indexFrom];
program[indexFrom] = program[indexTo];
program[indexTo] = currentStep;
}
return this.updateProgram(program);
}
}