File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -126,42 +126,45 @@ export function tokenize(ctx: Context, code: string): Token[] {
126126 return str ;
127127 }
128128
129+ function matchType < T > ( type : T , ...others : T [ ] ) {
130+ return others . includes ( type ) ;
131+ }
132+
129133 function expandImplicitMultiplication ( ) {
130134 if ( position >= code . length ) return ;
131135
132136 const prev = tokens . at ( - 2 ) ;
133137 const curr = tokens . at ( - 1 ) ;
134138 if ( ! prev || ! curr ) return ;
135139
136- let expand = false ;
137-
138- // expand: 2x
139- if ( prev . type === TOKEN . NUMBER && curr . type === TOKEN . IDENTIFIER ) {
140- expand = true ;
141- }
142- // expand: 2(x+1)
143- else if ( prev . type === TOKEN . NUMBER && curr . type === TOKEN . LPAREN ) {
144- expand = true ;
145- }
146- // expand: (x+1)y or (x+1)2
147- else if (
140+ const expand = [
141+ // )2
142+ // )x
143+ // )sin
144+ // )(
148145 prev . type === TOKEN . RPAREN &&
149- ( curr . type === TOKEN . IDENTIFIER || curr . type === TOKEN . NUMBER )
150- ) {
151- expand = true ;
152- }
153- // expand: x(x+1) - not function call
154- else if (
155- prev . type === TOKEN . IDENTIFIER &&
156- curr . type === TOKEN . LPAREN &&
157- ! isFunction ( prev . value )
158- ) {
159- expand = true ;
160- }
161- // expand: (x+1)(x+2)
162- else if ( prev . type === TOKEN . RPAREN && curr . type === TOKEN . LPAREN ) {
163- expand = true ;
164- }
146+ matchType (
147+ curr . type ,
148+ TOKEN . IDENTIFIER ,
149+ TOKEN . FUNCTION ,
150+ TOKEN . NUMBER ,
151+ TOKEN . LPAREN
152+ ) ,
153+
154+ // 2sin
155+ // 2x
156+ // 2(
157+ prev . type === TOKEN . NUMBER &&
158+ matchType (
159+ curr . type ,
160+ TOKEN . LPAREN ,
161+ TOKEN . IDENTIFIER ,
162+ TOKEN . FUNCTION
163+ ) ,
164+
165+ // x(
166+ prev . type === TOKEN . IDENTIFIER && curr . type === TOKEN . LPAREN
167+ ] . some ( ( x ) => ! ! x ) ;
165168
166169 if ( ! expand ) return ;
167170
You can’t perform that action at this time.
0 commit comments