Skip to content

Commit 192398d

Browse files
committed
Fixed a bug. Parsing was not in a try block
1 parent 7eb9edb commit 192398d

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/machine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class Machine {
3030
this.lines = new Lines();
3131
let instructions = program.split(/\n/);
3232
instructions.forEach((instruction, index) => {
33-
let { lineNumber, command, args, nonExecutableLine } = parse(instruction);
34-
if (nonExecutableLine) return;
3533
let line;
3634
try {
35+
let { lineNumber, command, args, nonExecutableLine } = parse(instruction);
36+
if (nonExecutableLine) return;
3737
line = Line.create(lineNumber, command, args, index + 1, instruction);
3838
} catch (e) {
3939
e.setLineNumber(index + 1);

test/testMachine.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ describe('Machine loading', function() {
1717
const program = ['10 STAR'];
1818
assert.throws(() => machine.load(stitch(program)));
1919
});
20+
21+
it('should throw an exception when there is a parse error in the arguments',() => {
22+
const machine = new Machine();
23+
const missingArgumentProg = ['10 MOV A,'];
24+
const missingQuoteProg = ['10 PRN "HELLO'];
25+
assert.throws(() => machine.load(stitch(missingArgumentProg)), InvalidInstructionException);
26+
assert.throws(() => machine.load(stitch(missingQuoteProg)), InvalidInstructionException);
27+
});
2028

2129
it('should ignore empty lines', function() {
2230
const machine = new Machine();

test/testParser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ describe('should not parse illegal forms', function() {
7171
assert.throws(() => parse('10 ADD A,-2'), InvalidInstructionException);
7272
assert.throws(() => parse('10 ADD A -2'), InvalidInstructionException);
7373
assert.throws(() => parse('-10 ADD A -2'), InvalidInstructionException);
74+
assert.throws(() => parse('10 MOV A,'), InvalidInstructionException);
7475
});
7576
});

0 commit comments

Comments
 (0)