1
+ import { FastShiftArray } from "../lib/fast-shift-array.mjs" ;
1
2
import { Expr } from "./expr.mjs" ;
2
3
import { Float } from "./float.mjs" ;
3
4
import { getIdStr } from "./get-id-str.mjs" ;
@@ -10,7 +11,7 @@ export class List extends Syntax {
10
11
readonly syntaxType = "list" ;
11
12
/** True when the list was defined by the user using parenthesis i.e. (hey, there) */
12
13
mayBeTuple ?: boolean ;
13
- value : Expr [ ] = [ ] ;
14
+ store : FastShiftArray < Expr > = new FastShiftArray ( ) ;
14
15
15
16
constructor (
16
17
opts : SyntaxMetadata & {
@@ -29,20 +30,24 @@ export class List extends Syntax {
29
30
}
30
31
}
31
32
33
+ get value ( ) {
34
+ return this . store . toArray ( ) ;
35
+ }
36
+
32
37
get hasChildren ( ) {
33
- return ! ! this . value . length ;
38
+ return ! ! this . store . length ;
34
39
}
35
40
36
41
get length ( ) {
37
- return this . value . length ;
42
+ return this . store . length ;
38
43
}
39
44
40
45
at ( index : number ) : Expr | undefined {
41
- return this . value . at ( index ) ;
46
+ return this . store . at ( index ) ;
42
47
}
43
48
44
49
exprAt ( index : number ) : Expr {
45
- const expr = this . value . at ( index ) ;
50
+ const expr = this . store . at ( index ) ;
46
51
if ( ! expr ) {
47
52
throw new Error ( `No expr at ${ index } ` ) ;
48
53
}
@@ -68,7 +73,7 @@ export class List extends Syntax {
68
73
set ( index : number , expr : Expr | string ) {
69
74
const result = typeof expr === "string" ? Identifier . from ( expr ) : expr ;
70
75
result . parent = this ;
71
- this . value [ index ] = result ;
76
+ this . store . set ( index , result ) ;
72
77
return this ;
73
78
}
74
79
@@ -82,48 +87,42 @@ export class List extends Syntax {
82
87
}
83
88
84
89
consume ( ) : Expr {
85
- const next = this . value . shift ( ) ;
90
+ const next = this . store . shift ( ) ;
86
91
if ( ! next ) throw new Error ( "No remaining expressions" ) ;
87
92
return next ;
88
93
}
89
94
90
- consumeRest ( ) : List {
91
- const newVal = this . slice ( 0 ) ;
92
- this . value = [ ] ;
93
- return newVal ;
94
- }
95
-
96
95
first ( ) : Expr | undefined {
97
- return this . value [ 0 ] ;
96
+ return this . store . at ( 0 ) ;
98
97
}
99
98
100
99
last ( ) : Expr | undefined {
101
- return this . value . at ( - 1 ) ;
100
+ return this . store . at ( - 1 ) ;
102
101
}
103
102
104
103
/** Returns all but the first element in an array */
105
104
rest ( ) : Expr [ ] {
106
- return this . value . slice ( 1 ) ;
105
+ return this . store . toArray ( ) . slice ( 1 ) ;
107
106
}
108
107
109
108
pop ( ) : Expr | undefined {
110
- return this . value . pop ( ) ;
109
+ return this . store . pop ( ) ;
111
110
}
112
111
113
112
push ( ...expr : ListValue [ ] ) {
114
113
expr . forEach ( ( ex ) => {
115
114
if ( typeof ex === "string" ) {
116
- this . value . push ( new Identifier ( { value : ex , parent : this } ) ) ;
115
+ this . store . push ( new Identifier ( { value : ex , parent : this } ) ) ;
117
116
return ;
118
117
}
119
118
120
119
if ( typeof ex === "number" && Number . isInteger ( ex ) ) {
121
- this . value . push ( new Int ( { value : ex , parent : this } ) ) ;
120
+ this . store . push ( new Int ( { value : ex , parent : this } ) ) ;
122
121
return ;
123
122
}
124
123
125
124
if ( typeof ex === "number" ) {
126
- this . value . push ( new Float ( { value : ex , parent : this } ) ) ;
125
+ this . store . push ( new Float ( { value : ex , parent : this } ) ) ;
127
126
return ;
128
127
}
129
128
@@ -139,11 +138,11 @@ export class List extends Syntax {
139
138
}
140
139
141
140
if ( ex . isList ( ) && ex . calls ( "splice_quote" ) ) {
142
- this . value . push ( ...ex . rest ( ) ) ;
141
+ this . store . push ( ...ex . rest ( ) ) ;
143
142
return ;
144
143
}
145
144
146
- this . value . push ( ex ) ;
145
+ this . store . push ( ex ) ;
147
146
} ) ;
148
147
149
148
return this ;
@@ -156,12 +155,12 @@ export class List extends Syntax {
156
155
insert ( expr : Expr | string , at = 0 ) {
157
156
const result = typeof expr === "string" ? Identifier . from ( expr ) : expr ;
158
157
result . parent = this ;
159
- this . value . splice ( at , 0 , result ) ;
158
+ this . store . splice ( at , 0 , result ) ;
160
159
return this ;
161
160
}
162
161
163
162
remove ( index : number , count = 1 ) {
164
- this . value . splice ( index , count ) ;
163
+ this . store . splice ( index , count ) ;
165
164
return this ;
166
165
}
167
166
@@ -200,16 +199,16 @@ export class List extends Syntax {
200
199
slice ( start ?: number , end ?: number ) : List {
201
200
return new List ( {
202
201
...super . getCloneOpts ( ) ,
203
- value : this . value . slice ( start , end ) ,
202
+ value : this . store . slice ( start , end ) ,
204
203
} ) ;
205
204
}
206
205
207
206
sliceAsArray ( start ?: number , end ?: number ) {
208
- return this . value . slice ( start , end ) ;
207
+ return this . store . slice ( start , end ) ;
209
208
}
210
209
211
210
toArray ( ) : Expr [ ] {
212
- return [ ... this . value ] ;
211
+ return this . value ;
213
212
}
214
213
215
214
toJSON ( ) {
0 commit comments