@@ -51,23 +51,26 @@ class QFuncInterpreter(private var inputParameters: Map<String, ContextValue<*>?
5151 fun interpret (
5252 code : String ,
5353 callContext : CallContext ,
54- completion : () -> Unit = {},
55- error : (Throwable ) -> Unit = {}
56- ) {
57- val lexer = QFuncLexer (code, " <dynamic>" )
54+ filename : String = "<dynamic >"
55+ ): ContextValue <* >? {
56+ val lexer = QFuncLexer (code, filename)
5857 val parser = QFuncParser (lexer)
5958
6059 this @QFuncInterpreter.inputParameters =
6160 callContext.paramValues + (" core" to ContextValue (ContextType .core, CoreUtils ))
6261 this @QFuncInterpreter.context = MainDispatcher
6362
6463 try {
65- runBlocking { visit(parser.file ? : return @runBlocking) }
66- completion()
64+ return runBlocking { visit(parser.file ? : return @runBlocking null ) as ? ContextValue <* > }
6765 } catch (e: QFuncSyntaxError ) {
6866 logger.error(e.toString())
69- error(e)
67+ } catch (e: QFuncParserException ) {
68+ logger.error(" Error parsing: " + e.message)
69+ } catch (e: Exception ) {
70+ logger.error(e.toString() + " \n " + e.stackTraceToString())
7071 }
72+
73+ return null
7174 }
7275
7376 suspend fun interpretAsync (
@@ -234,6 +237,7 @@ class QFuncInterpreter(private var inputParameters: Map<String, ContextValue<*>?
234237 throw QFuncInterpreterException (" Cannot NOT non-boolean value" , tree.filename, tree.line, tree.column)
235238 }
236239 }
240+
237241 QFuncTokenType .SUB -> {
238242 when (value) {
239243 is Int -> return ContextValue (ContextType .int, - value)
@@ -243,6 +247,7 @@ class QFuncInterpreter(private var inputParameters: Map<String, ContextValue<*>?
243247 else -> throw QFuncInterpreterException (" Cannot SUB non-number value" , tree.filename, tree.line, tree.column)
244248 }
245249 }
250+
246251 QFuncTokenType .ADD -> {
247252 when (value) {
248253 is Int -> return ContextValue (ContextType .int, abs(value))
@@ -252,31 +257,50 @@ class QFuncInterpreter(private var inputParameters: Map<String, ContextValue<*>?
252257 else -> throw QFuncInterpreterException (" Cannot ADD non-number value" , tree.filename, tree.line, tree.column)
253258 }
254259 }
260+
255261 QFuncTokenType .INCREMENT -> {
256262 when (value) {
257263 is Int -> return ContextValue (ContextType .int, value + 1 )
258264 is Long -> return ContextValue (ContextType .long, value + 1 )
259265 is Float -> return ContextValue (ContextType .float, value + 1 )
260266 is Double -> return ContextValue (ContextType .double, value + 1 )
261- else -> throw QFuncInterpreterException (" Cannot INCREMENT non-number value" , tree.filename, tree.line, tree.column)
267+ else -> throw QFuncInterpreterException (
268+ " Cannot INCREMENT non-number value" ,
269+ tree.filename,
270+ tree.line,
271+ tree.column
272+ )
262273 }
263274 }
275+
264276 QFuncTokenType .DECREMENT -> {
265277 when (value) {
266278 is Int -> return ContextValue (ContextType .int, value - 1 )
267279 is Long -> return ContextValue (ContextType .long, value - 1 )
268280 is Float -> return ContextValue (ContextType .float, value - 1 )
269281 is Double -> return ContextValue (ContextType .double, value - 1 )
270- else -> throw QFuncInterpreterException (" Cannot DECREMENT non-number value" , tree.filename, tree.line, tree.column)
282+ else -> throw QFuncInterpreterException (
283+ " Cannot DECREMENT non-number value" ,
284+ tree.filename,
285+ tree.line,
286+ tree.column
287+ )
271288 }
272289 }
290+
273291 QFuncTokenType .BITWISE_NOT -> {
274292 when (value) {
275293 is Int -> return ContextValue (ContextType .int, value.inv ())
276294 is Long -> return ContextValue (ContextType .long, value.inv ())
277- else -> throw QFuncInterpreterException (" Cannot BITWISE_NOT non-number or non-integer value" , tree.filename, tree.line, tree.column)
295+ else -> throw QFuncInterpreterException (
296+ " Cannot BITWISE_NOT non-number or non-integer value" ,
297+ tree.filename,
298+ tree.line,
299+ tree.column
300+ )
278301 }
279302 }
303+
280304 else -> throw QFuncInterpreterException (" Unknown operator: ${tree.type} " , tree.filename, tree.line, tree.column)
281305 }
282306 }
@@ -446,7 +470,12 @@ class QFuncInterpreter(private var inputParameters: Map<String, ContextValue<*>?
446470 throw QFuncInterpreterException (" Not a function" , tree.filename, tree.line, tree.column)
447471 }
448472 } catch (e: Exception ) {
449- throw QFuncInterpreterException (" Error calling function: ${e.message} " , tree.filename, tree.line, tree.column)
473+ throw QFuncInterpreterException (
474+ " Error calling function: ${e.message} " ,
475+ tree.filename,
476+ tree.line,
477+ tree.column
478+ )
450479 }
451480 }
452481 }
@@ -567,16 +596,7 @@ class QFuncInterpreter(private var inputParameters: Map<String, ContextValue<*>?
567596 break
568597 }
569598
570- try {
571- visit(tree.body)
572- } catch (e: QFuncInterpreterException ) {
573- logger.error(" Error in while loop at ${tree.filename} :${tree.line} :${tree.column} " , e)
574- throw e
575- } catch (e: Break ) {
576- break
577- } catch (e: Continue ) {
578- continue
579- }
599+ visit(tree.body)
580600 }
581601
582602 return null
@@ -594,18 +614,9 @@ class QFuncInterpreter(private var inputParameters: Map<String, ContextValue<*>?
594614 }
595615
596616 for (item in iterable) {
597- try {
598- this .loopValues.push(ContextValue (ContextType [(item!! )::class ] as ContextType <* >, item))
599- visit(tree.body)
600- this .loopValues.pop()
601- } catch (e: QFuncInterpreterException ) {
602- logger.error(" Error in for loop at ${tree.filename} :${tree.line} :${tree.column} " , e)
603- throw e
604- } catch (e: Break ) {
605- break
606- } catch (e: Continue ) {
607- continue
608- }
617+ this .loopValues.push(ContextValue (ContextType [(item!! )::class ] as ContextType <* >, item))
618+ visit(tree.body)
619+ this .loopValues.pop()
609620 }
610621
611622 return null
0 commit comments