@@ -248,31 +248,31 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
248
248
doubleStack.push(node.text.toDouble())
249
249
}
250
250
if (type == KeyLangParser .ID ) {
251
-
251
+ val name = node.text.replace( " ` " , " " )
252
252
@Suppress(" DIVISION_BY_ZERO" )
253
253
when (val idType = idTypeStack.pop()) {
254
254
IDType .VARIABLE -> doubleStack.push(
255
- when (val name = node.text ) {
255
+ when (name) {
256
256
" PI" -> PI
257
257
else -> variables[name] ? : errorValue(" unresolved variable: '${name} '" , 0.0 / 0.0 )
258
258
}
259
259
)
260
260
261
261
IDType .FUNCTION0 -> {
262
262
val function: (DoubleArray ) -> Double =
263
- when (val candidate = node.text ) {
263
+ when (name ) {
264
264
" random" -> { _ -> Double .uniform(0.0 , 1.0 ) }
265
- else -> functions.functions0[candidate ]?.let { { _: DoubleArray -> it.invoke() } }
265
+ else -> functions.functions0[name ]?.let { { _: DoubleArray -> it.invoke() } }
266
266
? : errorValue(
267
- " unresolved function: '${candidate } ()'"
267
+ " unresolved function: '${name } ()'"
268
268
) { _ -> error(" this is the error function" ) }
269
269
}
270
270
functionStack.push(function)
271
271
}
272
272
273
273
IDType .FUNCTION1 -> {
274
274
val function: (DoubleArray ) -> Double =
275
- when (val candidate = node.text ) {
275
+ when (name ) {
276
276
" sqrt" -> { x -> sqrt(x[0 ]) }
277
277
" radians" -> { x -> Math .toRadians(x[0 ]) }
278
278
" degrees" -> { x -> Math .toDegrees(x[0 ]) }
@@ -287,60 +287,69 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
287
287
" floor" -> { x -> floor(x[0 ]) }
288
288
" ceil" -> { x -> ceil(x[0 ]) }
289
289
" saturate" -> { x -> x[0 ].coerceIn(0.0 , 1.0 ) }
290
- else -> functions.functions1[candidate ]?.let { { x: DoubleArray -> it.invoke(x[0 ]) } }
290
+ else -> functions.functions1[name ]?.let { { x: DoubleArray -> it.invoke(x[0 ]) } }
291
291
? : errorValue(
292
- " unresolved function: '${candidate } (x0)'"
292
+ " unresolved function: '${name } (x0)'"
293
293
) { _ -> error(" this is the error function" ) }
294
294
}
295
295
functionStack.push(function)
296
296
}
297
297
IDType .FUNCTION2 -> {
298
298
val function: (DoubleArray ) -> Double =
299
- when (val candidate = node.text ) {
299
+ when (name ) {
300
300
" max" -> { x -> max(x[0 ], x[1 ]) }
301
301
" min" -> { x -> min(x[0 ], x[1 ]) }
302
302
" pow" -> { x -> x[0 ].pow(x[1 ]) }
303
303
" atan2" -> { x -> atan2(x[0 ], x[1 ]) }
304
304
" random" -> { x -> Double .uniform(x[0 ], x[1 ]) }
305
305
" length" -> { x -> Vector2 (x[0 ], x[1 ]).length }
306
- else -> functions.functions2[candidate ]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ]) } }
306
+ else -> functions.functions2[name ]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ]) } }
307
307
? : errorValue(
308
- " unresolved function: '${candidate } (x0, x1)'"
308
+ " unresolved function: '${name } (x0, x1)'"
309
309
) { _ -> error(" this is the error function" ) }
310
310
}
311
311
functionStack.push(function)
312
312
}
313
313
IDType .FUNCTION3 -> {
314
314
val function: (DoubleArray ) -> Double =
315
- when (val candidate = node.text ) {
315
+ when (name ) {
316
316
" mix" -> { x -> mix(x[0 ], x[1 ], x[2 ]) }
317
+ " min" -> { x -> x.minOrNull()!! }
318
+ " max" -> { x -> x.maxOrNull()!! }
319
+ " sum" -> { x -> x.sum() }
317
320
" smoothstep" -> { x -> smoothstep(x[0 ], x[1 ], x[2 ]) }
318
321
" length" -> { x -> Vector3 (x[0 ], x[1 ], x[2 ]).length }
319
- else -> functions.functions3[candidate ]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ], x[2 ]) } }
322
+ else -> functions.functions3[name ]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ], x[2 ]) } }
320
323
? : errorValue(
321
- " unresolved function: '${candidate } (x0, x1, x2)'"
324
+ " unresolved function: '${name } (x0, x1, x2)'"
322
325
) { _ -> error(" this is the error function" ) }
323
326
}
324
327
functionStack.push(function)
325
328
}
326
329
IDType .FUNCTION4 -> {
327
330
val function: (DoubleArray ) -> Double =
328
- when (val candidate = node.text) {
329
- else -> functions.functions4[candidate]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ], x[2 ], x[3 ]) } }
331
+ when (name) {
332
+ " min" -> { x -> x.minOrNull()!! }
333
+ " max" -> { x -> x.maxOrNull()!! }
334
+ " sum" -> { x -> x.sum() }
335
+ else -> functions.functions4[name]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ], x[2 ], x[3 ]) } }
330
336
? : errorValue(
331
- " unresolved function: '${candidate } (x0, x1, x2, x3)'"
337
+ " unresolved function: '${name } (x0, x1, x2, x3)'"
332
338
) { _ -> error(" this is the error function" ) }
333
339
}
334
340
functionStack.push(function)
335
341
}
336
342
337
343
IDType .FUNCTION5 -> {
338
344
val function: (DoubleArray ) -> Double =
339
- when (val candidate = node.text) {
345
+ when (name) {
346
+ " min" -> { x -> x.minOrNull()!! }
347
+ " max" -> { x -> x.maxOrNull()!! }
348
+ " sum" -> { x -> x.sum() }
340
349
" map" -> { x -> map(x[0 ], x[1 ], x[2 ], x[3 ], x[4 ]) }
341
- else -> functions.functions5[candidate ]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ], x[2 ], x[3 ], x[4 ]) } }
350
+ else -> functions.functions5[name ]?.let { { x: DoubleArray -> it.invoke(x[0 ], x[1 ], x[2 ], x[3 ], x[4 ]) } }
342
351
? : errorValue(
343
- " unresolved function: '${candidate } (x0, x1, x2, x3, x4)'"
352
+ " unresolved function: '${name } (x0, x1, x2, x3, x4)'"
344
353
) { _ -> error(" this is the error function" ) }
345
354
}
346
355
functionStack.push(function)
@@ -374,7 +383,7 @@ fun evaluateExpression(
374
383
}
375
384
})
376
385
377
- val root = parser.miniCalcFile ()
386
+ val root = parser.keyLangFile ()
378
387
val listener = ExpressionListener (functions)
379
388
listener.variables.putAll(variables)
380
389
try {
0 commit comments