@@ -9,7 +9,7 @@ import { BlockContext, Scope } from './scope';
99
1010export class Evaluator {
1111
12- static evalBlock ( ast : AstBlock , blockContext : BlockContext ) : unknown {
12+ evalBlock ( ast : AstBlock , blockContext : BlockContext ) : unknown {
1313 let lastResult = null ;
1414
1515 for ( let node of ast ?. funcs || [ ] ) {
@@ -19,14 +19,14 @@ export class Evaluator {
1919 const newScope = blockContext . blockScope ;
2020
2121 newScope . set ( funcDef . funcAst . name ,
22- ( ...args : unknown [ ] ) : unknown => Evaluator . jspyFuncInvoker ( funcDef , blockContext , ...args )
22+ ( ...args : unknown [ ] ) : unknown => this . jspyFuncInvoker ( funcDef , blockContext , ...args )
2323 ) ;
2424 }
2525
2626 for ( const node of ast . body ) {
2727 if ( node . type === 'comment' ) { continue ; }
2828 try {
29- lastResult = Evaluator . evalNode ( node , blockContext ) ;
29+ lastResult = this . evalNode ( node , blockContext ) ;
3030
3131 if ( blockContext . returnCalled ) {
3232 const res = blockContext . returnObject ;
@@ -50,7 +50,7 @@ export class Evaluator {
5050 throw err ;
5151 } else {
5252 const loc = node . loc ? node . loc : [ 0 , 0 ]
53- throw new JspyEvalError ( 'mainModule' , loc [ 0 ] , loc [ 1 ] , err . message )
53+ throw new JspyEvalError ( ast . name , loc [ 0 ] , loc [ 1 ] , err . message )
5454 }
5555 }
5656
@@ -59,13 +59,13 @@ export class Evaluator {
5959 return lastResult ;
6060 }
6161
62- static jspyFuncInvoker ( funcDef : FuncDefNode , context : BlockContext , ...args : unknown [ ] ) : unknown {
62+ jspyFuncInvoker ( funcDef : FuncDefNode , context : BlockContext , ...args : unknown [ ] ) : unknown {
6363
6464 const ast = Object . assign ( { } , funcDef . funcAst ) ;
6565 ast . type = 'func' ;
6666
6767 const blockContext = {
68- namelessFuncsCount : 0 ,
68+ moduleName : context . moduleName ,
6969 blockScope : context . blockScope . clone ( )
7070 } as BlockContext ;
7171
@@ -78,10 +78,10 @@ export class Evaluator {
7878 blockContext . blockScope . set ( funcDef . params [ i ] , args [ i ] ) ;
7979 }
8080
81- return Evaluator . evalBlock ( ast , blockContext ) ;
81+ return this . evalBlock ( ast , blockContext ) ;
8282 }
8383
84- private static invokeFunction ( func : ( ...args : unknown [ ] ) => unknown , fps : unknown [ ] ) : unknown {
84+ private invokeFunction ( func : ( ...args : unknown [ ] ) => unknown , fps : unknown [ ] ) : unknown {
8585 if ( fps . length === 0 ) { return func ( ) ; }
8686 if ( fps . length === 1 ) { return func ( fps [ 0 ] ) ; }
8787 if ( fps . length === 2 ) { return func ( fps [ 0 ] , fps [ 1 ] ) ; }
@@ -118,7 +118,7 @@ export class Evaluator {
118118 }
119119 }
120120
121- private static evalNode ( node : AstNode , blockContext : BlockContext ) : unknown {
121+ private evalNode ( node : AstNode , blockContext : BlockContext ) : unknown {
122122 if ( node . type === 'import' ) {
123123 // skip this for now. As modules are implemented externally
124124 return null ;
@@ -130,10 +130,10 @@ export class Evaluator {
130130
131131 if ( node . type === 'if' ) {
132132 const ifNode = node as IfNode ;
133- if ( Evaluator . evalNode ( ifNode . conditionNode , blockContext ) ) {
134- Evaluator . evalBlock ( { type : 'if' , body : ifNode . ifBody } as AstBlock , blockContext ) ;
133+ if ( this . evalNode ( ifNode . conditionNode , blockContext ) ) {
134+ this . evalBlock ( { type : 'if' , body : ifNode . ifBody } as AstBlock , blockContext ) ;
135135 } else if ( ifNode . elseBody ) {
136- Evaluator . evalBlock ( { type : 'if' , body : ifNode . elseBody } as AstBlock , blockContext ) ;
136+ this . evalBlock ( { type : 'if' , body : ifNode . elseBody } as AstBlock , blockContext ) ;
137137 }
138138
139139 return ;
@@ -143,7 +143,7 @@ export class Evaluator {
143143 const returnNode = node as ReturnNode ;
144144 blockContext . returnCalled = true ;
145145 blockContext . returnObject = returnNode . returnValue ?
146- Evaluator . evalNode ( returnNode . returnValue , blockContext )
146+ this . evalNode ( returnNode . returnValue , blockContext )
147147 : null ;
148148
149149 return blockContext . returnObject ;
@@ -162,11 +162,11 @@ export class Evaluator {
162162 if ( node . type === 'for' ) {
163163 const forNode = node as ForNode ;
164164
165- const array = Evaluator . evalNode ( forNode . sourceArray , blockContext ) as unknown [ ] | string ;
165+ const array = this . evalNode ( forNode . sourceArray , blockContext ) as unknown [ ] | string ;
166166
167167 for ( let item of array ) {
168168 blockContext . blockScope . set ( forNode . itemVarName , item ) ;
169- Evaluator . evalBlock ( { type : 'for' , body : forNode . body } as AstBlock , blockContext ) ;
169+ this . evalBlock ( { type : 'for' , body : forNode . body } as AstBlock , blockContext ) ;
170170 if ( blockContext . continueCalled ) { blockContext . continueCalled = false ; }
171171 if ( blockContext . breakCalled ) { break ; }
172172 }
@@ -177,8 +177,8 @@ export class Evaluator {
177177 if ( node . type === 'while' ) {
178178 const whileNode = node as WhileNode ;
179179
180- while ( Evaluator . evalNode ( whileNode . condition , blockContext ) ) {
181- Evaluator . evalBlock ( { type : 'while' , body : whileNode . body } as AstBlock , blockContext ) ;
180+ while ( this . evalNode ( whileNode . condition , blockContext ) ) {
181+ this . evalBlock ( { type : 'while' , body : whileNode . body } as AstBlock , blockContext ) ;
182182
183183 if ( blockContext . continueCalled ) { blockContext . continueCalled = false ; }
184184 if ( blockContext . breakCalled ) { break ; }
@@ -198,50 +198,50 @@ export class Evaluator {
198198
199199 if ( node . type === "binOp" ) {
200200 const binOpNode = ( node as BinOpNode ) ;
201- var left = Evaluator . evalNode ( binOpNode . left , blockContext ) ;
202- var right = Evaluator . evalNode ( binOpNode . right , blockContext ) ;
201+ var left = this . evalNode ( binOpNode . left , blockContext ) ;
202+ var right = this . evalNode ( binOpNode . right , blockContext ) ;
203203 return OperationFuncs [ binOpNode . op ] ( left as Primitive , right as Primitive ) ;
204204 }
205205
206206 if ( node . type === "arrowFuncDef" ) {
207207 const arrowFuncDef = node as ArrowFuncDefNode ;
208208
209- return ( ...args : unknown [ ] ) : unknown => Evaluator . jspyFuncInvoker ( arrowFuncDef , blockContext , ...args ) ;
209+ return ( ...args : unknown [ ] ) : unknown => this . jspyFuncInvoker ( arrowFuncDef , blockContext , ...args ) ;
210210 }
211211
212212 if ( node . type === "funcCall" ) {
213213 const funcCallNode = node as FunctionCallNode ;
214214 const func = blockContext . blockScope . get ( funcCallNode . name ) as ( ...args : unknown [ ] ) => unknown ;
215- const pms = funcCallNode . paramNodes ?. map ( n => Evaluator . evalNode ( n , blockContext ) ) || [ ]
215+ const pms = funcCallNode . paramNodes ?. map ( n => this . evalNode ( n , blockContext ) ) || [ ]
216216
217- return Evaluator . invokeFunction ( func , pms ) ;
217+ return this . invokeFunction ( func , pms ) ;
218218 }
219219
220220 if ( node . type === "assign" ) {
221221 const assignNode = node as AssignNode ;
222222
223223 if ( assignNode . target . type === 'getSingleVar' ) {
224224 const node = assignNode . target as SetSingleVarNode ;
225- blockContext . blockScope . set ( node . name , Evaluator . evalNode ( assignNode . source , blockContext ) ) ;
225+ blockContext . blockScope . set ( node . name , this . evalNode ( assignNode . source , blockContext ) ) ;
226226 } else if ( assignNode . target . type === 'dotObjectAccess' ) {
227227 const targetNode = assignNode . target as DotObjectAccessNode ;
228228
229229 // create a node for all but last property token
230230 // potentially it can go to parser
231231 const targetObjectNode = new DotObjectAccessNode ( targetNode . nestedProps . slice ( 0 , targetNode . nestedProps . length - 1 ) , targetNode . loc ) ;
232- const targetObject = Evaluator . evalNode ( targetObjectNode , blockContext ) as Record < string , unknown > ;
232+ const targetObject = this . evalNode ( targetObjectNode , blockContext ) as Record < string , unknown > ;
233233
234234 // not sure nested properties should be GetSingleVarNode
235235 // can be factored in the parser
236236 const lastPropertyName = ( targetNode . nestedProps [ targetNode . nestedProps . length - 1 ] as GetSingleVarNode ) . name
237237
238- targetObject [ lastPropertyName ] = Evaluator . evalNode ( assignNode . source , blockContext ) ;
238+ targetObject [ lastPropertyName ] = this . evalNode ( assignNode . source , blockContext ) ;
239239 } else if ( assignNode . target . type === 'bracketObjectAccess' ) {
240240 const targetNode = assignNode . target as BracketObjectAccessNode ;
241- const keyValue = Evaluator . evalNode ( targetNode . bracketBody , blockContext ) as string | number ;
241+ const keyValue = this . evalNode ( targetNode . bracketBody , blockContext ) as string | number ;
242242 const targetObject = blockContext . blockScope . get ( targetNode . propertyName as string ) as Record < string , unknown > ;
243243
244- targetObject [ keyValue ] = Evaluator . evalNode ( assignNode . source , blockContext ) ;
244+ targetObject [ keyValue ] = this . evalNode ( assignNode . source , blockContext ) ;
245245 } else {
246246 throw Error ( 'Not implemented Assign operation' ) ;
247247 // get chaining calls
@@ -252,15 +252,15 @@ export class Evaluator {
252252
253253 if ( node . type === 'bracketObjectAccess' ) {
254254 const sbNode = node as BracketObjectAccessNode ;
255- const key = Evaluator . evalNode ( sbNode . bracketBody , blockContext ) as string ;
255+ const key = this . evalNode ( sbNode . bracketBody , blockContext ) as string ;
256256 const obj = blockContext . blockScope . get ( sbNode . propertyName as string ) as Record < string , unknown > ;
257257 return ( obj [ key ] === undefined ) ? null : obj [ key ] ;
258258 }
259259
260260 if ( node . type === "dotObjectAccess" ) {
261261 const dotObject = node as DotObjectAccessNode ;
262262
263- let startObject = Evaluator . evalNode ( dotObject . nestedProps [ 0 ] , blockContext ) as any ;
263+ let startObject = this . evalNode ( dotObject . nestedProps [ 0 ] , blockContext ) as any ;
264264 for ( let i = 1 ; i < dotObject . nestedProps . length ; i ++ ) {
265265 const nestedProp = dotObject . nestedProps [ i ] ;
266266
@@ -273,13 +273,13 @@ export class Evaluator {
273273 } else if ( nestedProp . type === 'bracketObjectAccess' ) {
274274 const node = nestedProp as BracketObjectAccessNode ;
275275 startObject = startObject [ node . propertyName ] as unknown ;
276- startObject = startObject [ Evaluator . evalNode ( node . bracketBody , blockContext ) as string ] as unknown ;
276+ startObject = startObject [ this . evalNode ( node . bracketBody , blockContext ) as string ] as unknown ;
277277 } else if ( nestedProp . type === 'funcCall' ) {
278278 const funcCallNode = nestedProp as FunctionCallNode ;
279279 const func = startObject [ funcCallNode . name ] as ( ...args : unknown [ ] ) => unknown ;
280- const pms = funcCallNode . paramNodes ?. map ( n => Evaluator . evalNode ( n , blockContext ) ) || [ ]
280+ const pms = funcCallNode . paramNodes ?. map ( n => this . evalNode ( n , blockContext ) ) || [ ]
281281
282- startObject = Evaluator . invokeFunction ( func . bind ( startObject ) , pms ) ;
282+ startObject = this . invokeFunction ( func . bind ( startObject ) , pms ) ;
283283
284284 } else {
285285 throw Error ( "Can't resolve dotObjectAccess node" )
@@ -295,7 +295,7 @@ export class Evaluator {
295295 const obj = { } as Record < string , unknown > ;
296296
297297 for ( const p of createObjectNode . props ) {
298- obj [ Evaluator . evalNode ( p . name , blockContext ) as string ] = Evaluator . evalNode ( p . value , blockContext ) ;
298+ obj [ this . evalNode ( p . name , blockContext ) as string ] = this . evalNode ( p . value , blockContext ) ;
299299 }
300300
301301 return obj ;
@@ -306,7 +306,7 @@ export class Evaluator {
306306 const res = [ ] as unknown [ ] ;
307307
308308 for ( const item of arrayNode . items ) {
309- res . push ( Evaluator . evalNode ( item , blockContext ) ) ;
309+ res . push ( this . evalNode ( item , blockContext ) ) ;
310310 }
311311
312312 return res ;
0 commit comments