Skip to content

Commit 2c6aaa4

Browse files
committed
Reset now clears the stack
1 parent 7aa6ec0 commit 2c6aaa4

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/machine.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ const Lines = require('./lines.js');
55

66
class Machine {
77
constructor() {
8-
this._reset();
98
this.lines = new Lines();
109
this.stack = new Stack();
10+
this._reset();
1111
}
1212

1313
_reset() {
1414
this._resetRegisters();
1515
this._resetFlags();
16+
this.stack.clear();
1617
this.prn = [];
1718
this.table = [];
1819
}

src/stack.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ class Stack {
88
* @constructor
99
*/
1010
constructor() {
11-
this.stack = [];
11+
this._stack = [];
1212
}
1313

1414
/**
1515
* Pushes the given value onto the stack
1616
* @param {number} value - The value to be pushed onto a stack.
1717
*/
1818
push(value) {
19-
this.stack.push(value);
19+
this._stack.push(value);
2020
}
2121

2222
/**
@@ -25,16 +25,20 @@ class Stack {
2525
* @throws {StackUnderflowException} Will throw an error if you try to pop an empty stack
2626
*/
2727
pop() {
28-
if (this.stack.length == 0) throw new StackUnderflowException();
29-
return this.stack.pop();
28+
if (this._stack.length == 0) throw new StackUnderflowException();
29+
return this._stack.pop();
3030
}
3131

3232
/**
3333
* Returns an array representation of the stack
3434
* @returns {number[]} The stack represented as an array with the last number pushed at the tail of the array and the first number pushed at the head of the array.
3535
*/
3636
asArray() {
37-
return this.stack.map(a => a);
37+
return this._stack.map(a => a);
38+
}
39+
40+
clear() {
41+
this._stack = [];
3842
}
3943
}
4044

test/testMachine.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,19 @@ describe('Machine with stack', () => {
118118
assert.throws(() => machine.execute());
119119
});
120120

121-
it.skip('should clear the stack between multiple executions', () => {
122-
// Code
121+
it('should clear the stack between multiple executions', () => {
122+
const machine = new Machine();
123+
const program = [
124+
'10 START',
125+
'20 MOV A, 5',
126+
'30 PUSH A',
127+
'40 STOP'
128+
];
129+
machine.load(stitch(program));
130+
machine.execute();
131+
assert.deepEqual([5],machine.getStack());
132+
machine.execute();
133+
assert.deepEqual([5],machine.getStack());
123134
});
124135
});
125136

test/testStack.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ describe('Stack ', () => {
3535
let stack = new Stack();
3636
assert.throws(() => stack.pop());
3737
});
38+
39+
it('should clear the stack',() => {
40+
let stack = new Stack();
41+
stack.push(5);
42+
assert.deepEqual([5], stack.asArray());
43+
stack.clear();
44+
assert.deepEqual([], stack.asArray());
45+
});
3846
});

0 commit comments

Comments
 (0)