File tree Expand file tree Collapse file tree 4 files changed +29
-8
lines changed Expand file tree Collapse file tree 4 files changed +29
-8
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ <h1>Easy Forth</h1>
3434 < script src ="javascripts/jquery-2.1.4.js "> </ script >
3535 < script src ="javascripts/toc.js "> </ script >
3636 < script src ="javascripts/scale.fix.js "> </ script >
37+ < script src ="javascripts/shared.js "> </ script >
3738 < script src ="javascripts/stack.js "> </ script >
3839 < script src ="javascripts/dictionary.js "> </ script >
3940 < script src ="javascripts/memory.js "> </ script >
Original file line number Diff line number Diff line change 11'use strict' ;
22
3- var FALSE = 0 ;
4- var TRUE = - 1 ;
5-
63function Forth ( ) {
74 // Core structures
85 var context = {
@@ -168,13 +165,14 @@ function Forth() {
168165 }
169166
170167 function readLines ( codeLines , callbacks ) {
171- // Use reduce to execute promises in sequence
172- return codeLines . reduce ( function ( promise , codeLine ) {
173- return promise . then ( function ( ) {
168+ var promiseFunctions = codeLines . map ( function ( codeLine ) {
169+ return function ( ) {
174170 callbacks && callbacks . lineCallback ( codeLine ) ;
175171 return readLine ( codeLine , callbacks && callbacks . outputCallback ) ;
176- } ) ;
177- } , Promise . resolve ( ) ) ;
172+ } ;
173+ } ) ;
174+
175+ return executeInSequence ( promiseFunctions ) ; // defined in shared.js
178176 }
179177
180178 // because readLines is async, addPredefinedWords is async too
Original file line number Diff line number Diff line change 1+ var FALSE = 0 ;
2+ var TRUE = - 1 ;
3+
4+ // Takes an array of nullary functions that return promises and executes them in series,
5+ // collecting the output from each, and finally resolving with the combined output
6+ function executeInSequence ( promiseFunctions ) {
7+ var output = [ ] ;
8+
9+ function addOutput ( promise ) {
10+ return promise . then ( function ( o ) {
11+ output . push ( o ) ;
12+ return output . join ( "" ) ;
13+ } ) ;
14+ }
15+
16+ return promiseFunctions . reduce ( function ( promise , promiseFunction ) {
17+ return promise . then ( function ( ) {
18+ return addOutput ( promiseFunction ( ) ) ;
19+ } ) ;
20+ } , Promise . resolve ( ) ) ;
21+ }
Original file line number Diff line number Diff line change 1212 < script src ="lib/jasmine-2.3.4/boot.js "> </ script >
1313
1414 <!-- include source files here... -->
15+ < script src ="../javascripts/shared.js "> </ script >
1516 < script src ="../javascripts/stack.js "> </ script >
1617 < script src ="../javascripts/dictionary.js "> </ script >
1718 < script src ="../javascripts/memory.js "> </ script >
You can’t perform that action at this time.
0 commit comments