File tree Expand file tree Collapse file tree 16 files changed +127
-127
lines changed Expand file tree Collapse file tree 16 files changed +127
-127
lines changed Original file line number Diff line number Diff line change @@ -24,5 +24,5 @@ emily.setNext(william).setNext(amelia).setNext(joseph);
24
24
25
25
// Various troubles occurred.
26
26
for ( let i = 0 ; i < 10 ; i ++ ) {
27
- emily . support ( new Trouble ( i ) ) ;
27
+ emily . support ( new Trouble ( i ) ) ;
28
28
}
Original file line number Diff line number Diff line change @@ -21,10 +21,10 @@ export class Action extends Node {
21
21
22
22
parse ( context ) {
23
23
// ˅
24
- const currentToken = context . getToken ( ) ;
25
- if ( currentToken !== 'forward' && currentToken !== 'right' && currentToken !== 'left' ) {
26
- throw new Error ( currentToken + ' is unknown' ) ;
27
- }
24
+ const currentToken = context . getToken ( ) ;
25
+ if ( currentToken !== 'forward' && currentToken !== 'right' && currentToken !== 'left' ) {
26
+ throw new Error ( currentToken + ' is unknown' ) ;
27
+ }
28
28
29
29
this . name = currentToken ;
30
30
@@ -34,7 +34,7 @@ export class Action extends Node {
34
34
35
35
toString ( ) {
36
36
// ˅
37
- return this . name ;
37
+ return this . name ;
38
38
// ˄
39
39
}
40
40
Original file line number Diff line number Diff line change @@ -16,33 +16,33 @@ export class CommandList extends Node {
16
16
constructor ( ) {
17
17
// ˅
18
18
super ( ) ;
19
- this . nodes = new Array ( ) ;
19
+ this . nodes = new Array ( ) ;
20
20
// ˄
21
21
}
22
22
23
23
parse ( context ) {
24
24
// ˅
25
- while ( true ) {
26
- if ( context . getToken ( ) == null ) {
27
- throw new Error ( 'Missing "end"' ) ;
28
- }
29
- else if ( context . getToken ( ) === 'end' ) {
30
- context . slideToken ( 'end' ) ;
31
- break ;
32
- }
33
- else {
34
- const aNode = new Command ( ) ;
35
- aNode . parse ( context ) ;
36
-
37
- this . nodes . push ( aNode ) ; // Hold the parsed node
38
- }
39
- }
25
+ while ( true ) {
26
+ if ( context . getToken ( ) == null ) {
27
+ throw new Error ( 'Missing "end"' ) ;
28
+ }
29
+ else if ( context . getToken ( ) === 'end' ) {
30
+ context . slideToken ( 'end' ) ;
31
+ break ;
32
+ }
33
+ else {
34
+ const aNode = new Command ( ) ;
35
+ aNode . parse ( context ) ;
36
+
37
+ this . nodes . push ( aNode ) ; // Hold the parsed node
38
+ }
39
+ }
40
40
// ˄
41
41
}
42
42
43
43
toString ( ) {
44
44
// ˅
45
- return '[' + this . nodes . join ( ', ' ) + ']' ;
45
+ return '[' + this . nodes . join ( ', ' ) + ']' ;
46
46
// ˄
47
47
}
48
48
Original file line number Diff line number Diff line change @@ -24,22 +24,22 @@ export class Command extends Node {
24
24
parse ( context ) {
25
25
// ˅
26
26
let aNode ;
27
- if ( context . getToken ( ) === 'repeat' ) {
28
- aNode = new Repeat ( ) ;
29
- aNode . parse ( context ) ;
30
- }
31
- else {
32
- aNode = new Action ( ) ;
33
- aNode . parse ( context ) ;
34
- }
27
+ if ( context . getToken ( ) === 'repeat' ) {
28
+ aNode = new Repeat ( ) ;
29
+ aNode . parse ( context ) ;
30
+ }
31
+ else {
32
+ aNode = new Action ( ) ;
33
+ aNode . parse ( context ) ;
34
+ }
35
35
36
36
this . node = aNode ; // Hold the parsed node
37
37
// ˄
38
38
}
39
39
40
40
toString ( ) {
41
41
// ˅
42
- return this . node . toString ( ) ;
42
+ return this . node . toString ( ) ;
43
43
// ˄
44
44
}
45
45
Original file line number Diff line number Diff line change @@ -15,45 +15,45 @@ export class Context {
15
15
16
16
constructor ( text ) {
17
17
// ˅
18
- this . tokens = text . split ( / \s + / ) ;
19
- this . currentIndex = 0 ;
18
+ this . tokens = text . split ( / \s + / ) ;
19
+ this . currentIndex = 0 ;
20
20
// ˄
21
21
}
22
22
23
23
nextToken ( ) {
24
24
// ˅
25
- if ( this . currentIndex < this . tokens . length ) {
26
- return this . tokens [ this . currentIndex ++ ] ;
27
- }
28
- else {
29
- return null ;
30
- }
25
+ if ( this . currentIndex < this . tokens . length ) {
26
+ return this . tokens [ this . currentIndex ++ ] ;
27
+ }
28
+ else {
29
+ return null ;
30
+ }
31
31
// ˄
32
32
}
33
33
34
34
getToken ( ) {
35
35
// ˅
36
- return this . tokens [ this . currentIndex ] ;
36
+ return this . tokens [ this . currentIndex ] ;
37
37
// ˄
38
38
}
39
39
40
40
slideToken ( token ) {
41
41
// ˅
42
- if ( token !== this . getToken ( ) ) {
43
- throw new Error ( 'WARNING: ' + token + ' is expected but ' + this . getToken ( ) + ' was found.' ) ;
44
- }
45
- this . nextToken ( ) ;
42
+ if ( token !== this . getToken ( ) ) {
43
+ throw new Error ( 'WARNING: ' + token + ' is expected but ' + this . getToken ( ) + ' was found.' ) ;
44
+ }
45
+ this . nextToken ( ) ;
46
46
// ˄
47
47
}
48
48
49
49
getNumber ( ) {
50
50
// ˅
51
- try {
52
- return parseInt ( this . getToken ( ) ) ;
53
- }
54
- catch {
55
- throw new Error ( 'WARNING: ' + this . getToken ( ) ) ;
56
- }
51
+ try {
52
+ return parseInt ( this . getToken ( ) ) ;
53
+ }
54
+ catch {
55
+ throw new Error ( 'WARNING: ' + this . getToken ( ) ) ;
56
+ }
57
57
// ˄
58
58
}
59
59
Original file line number Diff line number Diff line change @@ -41,8 +41,8 @@ import readline from 'readline';
41
41
const stream = fs . createReadStream ( process . cwd ( ) + '/program.txt' , 'utf8' ) ;
42
42
const reader = readline . createInterface ( { input : stream } ) ;
43
43
reader . on ( 'line' , ( line ) => {
44
- console . log ( 'Before parsing : ' + line ) ;
45
- const node = new Program ( ) ;
46
- node . parse ( new Context ( line ) ) ;
47
- console . log ( 'After parsing : ' + node ) ;
44
+ console . log ( 'Before parsing : ' + line ) ;
45
+ const node = new Program ( ) ;
46
+ node . parse ( new Context ( line ) ) ;
47
+ console . log ( 'After parsing : ' + node ) ;
48
48
} ) ;
Original file line number Diff line number Diff line change @@ -23,18 +23,18 @@ export class Program extends Node {
23
23
24
24
parse ( context ) {
25
25
// ˅
26
- context . slideToken ( 'program' ) ;
26
+ context . slideToken ( 'program' ) ;
27
27
28
28
const aCommandList = new CommandList ( ) ;
29
- aCommandList . parse ( context ) ;
29
+ aCommandList . parse ( context ) ;
30
30
31
- this . commandList = aCommandList ; // Hold the parsed command list
31
+ this . commandList = aCommandList ; // Hold the parsed command list
32
32
// ˄
33
33
}
34
34
35
35
toString ( ) {
36
36
// ˅
37
- return ( '[program ' + this . commandList + ']' ) ;
37
+ return ( '[program ' + this . commandList + ']' ) ;
38
38
// ˄
39
39
}
40
40
Original file line number Diff line number Diff line change @@ -19,28 +19,28 @@ export class Repeat extends Node {
19
19
constructor ( ) {
20
20
// ˅
21
21
super ( ) ;
22
- this . number = 0 ;
23
- this . commandList = null ;
22
+ this . number = 0 ;
23
+ this . commandList = null ;
24
24
// ˄
25
25
}
26
26
27
27
parse ( context ) {
28
28
// ˅
29
- context . slideToken ( 'repeat' ) ;
29
+ context . slideToken ( 'repeat' ) ;
30
30
31
- this . number = context . getNumber ( ) ;
32
- context . slideToken ( String ( this . number ) ) ;
31
+ this . number = context . getNumber ( ) ;
32
+ context . slideToken ( String ( this . number ) ) ;
33
33
34
34
const aCommandList = new CommandList ( ) ;
35
- aCommandList . parse ( context ) ;
35
+ aCommandList . parse ( context ) ;
36
36
37
37
this . commandList = aCommandList ; // Hold the parsed command list
38
38
// ˄
39
39
}
40
40
41
41
toString ( ) {
42
42
// ˅
43
- return 'repeat ' + this . number + ' ' + this . commandList ;
43
+ return 'repeat ' + this . number + ' ' + this . commandList ;
44
44
// ˄
45
45
}
46
46
Original file line number Diff line number Diff line change @@ -17,22 +17,22 @@ export class BookShelfIterator extends Iterator {
17
17
constructor ( bookShelf ) {
18
18
// ˅
19
19
super ( ) ;
20
- this . index = 0 ;
21
- this . bookShelf = bookShelf ;
20
+ this . index = 0 ;
21
+ this . bookShelf = bookShelf ;
22
22
// ˄
23
23
}
24
24
25
25
hasNext ( ) {
26
26
// ˅
27
- return this . index < this . bookShelf . numberOfBooks ;
27
+ return this . index < this . bookShelf . numberOfBooks ;
28
28
// ˄
29
29
}
30
30
31
31
next ( ) {
32
32
// ˅
33
- const book = this . bookShelf . getAt ( this . index ) ;
34
- this . index ++ ;
35
- return book ;
33
+ const book = this . bookShelf . getAt ( this . index ) ;
34
+ this . index ++ ;
35
+ return book ;
36
36
// ˄
37
37
}
38
38
Original file line number Diff line number Diff line change @@ -18,27 +18,27 @@ export class BookShelf extends Aggregate {
18
18
constructor ( maxsize ) {
19
19
// ˅
20
20
super ( ) ;
21
- this . books = new Array ( maxsize ) ;
22
- this . _numberOfBooks = 0 ;
21
+ this . books = new Array ( maxsize ) ;
22
+ this . _numberOfBooks = 0 ;
23
23
// ˄
24
24
}
25
25
26
26
iterator ( ) {
27
27
// ˅
28
- return new BookShelfIterator ( this ) ;
28
+ return new BookShelfIterator ( this ) ;
29
29
// ˄
30
30
}
31
31
32
32
getAt ( index ) {
33
33
// ˅
34
- return this . books [ index ] ;
34
+ return this . books [ index ] ;
35
35
// ˄
36
36
}
37
37
38
38
add ( book ) {
39
39
// ˅
40
- this . books [ this . _numberOfBooks ] = book ;
41
- this . _numberOfBooks ++ ;
40
+ this . books [ this . _numberOfBooks ] = book ;
41
+ this . _numberOfBooks ++ ;
42
42
// ˄
43
43
}
44
44
You can’t perform that action at this time.
0 commit comments