Skip to content

Commit dd4d931

Browse files
committed
FIX engine -- add "here" and "create" words
A word created with `create` was not added to `memory.variables` and was not found by `memory.getVariable`
1 parent bdc909a commit dd4d931

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

javascripts/forth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function Forth(next) {
6565
}
6666

6767
function createCreate(name) {
68-
var pointer = context.memory.here();
68+
var pointer = context.memory.addCreate(name, 0);
6969
addToDictionary(name, function (context) {
7070
context.stack.push(pointer);
7171
});

javascripts/memory.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@ function Memory() {
33
var memArray = [];
44
var _memPointer = 1000;
55

6+
function here() { // -- addr
7+
return _memPointer;
8+
}
9+
10+
function allot(cells) {
11+
_memPointer += cells;
12+
}
13+
614
function newMemPointer() {
715
return _memPointer++;
816
}
917

10-
function addVariable(name) {
11-
var address = newMemPointer();
18+
function addCreate( name, size_cells ) {
19+
const address = here();
20+
allot( size_cells );
1221
variables[name.toLowerCase()] = address;
1322
memArray[address];
1423
return getVariable(name);
1524
}
1625

26+
function addVariable(name) {
27+
return addCreate(name, 1);
28+
}
29+
1730
function getVariable(name) {
1831
return variables[name.toLowerCase()];
1932
}
@@ -26,15 +39,8 @@ function Memory() {
2639
return memArray[address] || 0;
2740
}
2841

29-
function here() { // -- addr
30-
return _memPointer;
31-
}
32-
33-
function allot(cells) {
34-
_memPointer += cells;
35-
}
36-
3742
return {
43+
addCreate: addCreate,
3844
addVariable: addVariable,
3945
getVariable: getVariable,
4046
setValue: setValue,

test/spec/forth_spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,11 @@ describe('Forth', function () {
837837
function () {
838838
collectOutput(forth.readLine, 'create foo 4 cells allot', this);
839839
},
840+
function () {
841+
const m = forth.q('context').memory ;
842+
expect( m.here() - m.getVariable('foo') ).toBe( 4 );
843+
done();
844+
},
840845
function (output) {
841846
expect(output).toBe(" ok");
842847
forth.readLines([

0 commit comments

Comments
 (0)