File tree Expand file tree Collapse file tree 3 files changed +22
-11
lines changed
Expand file tree Collapse file tree 3 files changed +22
-11
lines changed Original file line number Diff line number Diff 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 } ) ;
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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 ( [
You can’t perform that action at this time.
0 commit comments