Create validation on midi() function

This commit is contained in:
2020-03-14 14:20:46 +01:00
parent d3f6138a8b
commit ba23545e1b
3 changed files with 23 additions and 15 deletions

View File

@@ -4,8 +4,8 @@ import io.smnp.dsl.ast.model.node.*
import io.smnp.environment.Environment
import io.smnp.error.EnvironmentException
import io.smnp.error.EvaluationException
import io.smnp.error.MethodInvocationException
import io.smnp.error.PositionException
import io.smnp.error.SmnpException
import io.smnp.evaluation.model.entity.EvaluatorOutput
class AccessOperatorEvaluator : Evaluator() {
@@ -36,9 +36,7 @@ class AccessOperatorEvaluator : Evaluator() {
(argsNode as FunctionCallArgumentsNode).items.map { evaluator.evaluate(it, environment).value }
try {
return EvaluatorOutput.value(environment.invokeMethod(lhs, identifier, arguments))
} catch(e: MethodInvocationException) {
throw PositionException(EnvironmentException(e, environment), identifierNode.position)
} catch(e: EvaluationException) {
} catch(e: SmnpException) {
throw PositionException(EnvironmentException(e, environment), identifierNode.position)
}
}

View File

@@ -6,9 +6,8 @@ import io.smnp.dsl.ast.model.node.IdentifierNode
import io.smnp.dsl.ast.model.node.Node
import io.smnp.environment.Environment
import io.smnp.error.EnvironmentException
import io.smnp.error.EvaluationException
import io.smnp.error.FunctionInvocationException
import io.smnp.error.PositionException
import io.smnp.error.SmnpException
import io.smnp.evaluation.model.entity.EvaluatorOutput
class FunctionCallEvaluator : Evaluator() {
@@ -22,10 +21,8 @@ class FunctionCallEvaluator : Evaluator() {
try {
return EvaluatorOutput.value(environment.invokeFunction(identifier, arguments))
} catch(e: FunctionInvocationException) {
throw PositionException(EnvironmentException(e, environment), node.position)
} catch(e: EvaluationException) {
throw PositionException(EnvironmentException(e, environment), node.position)
} catch(e: SmnpException) {
throw PositionException(EnvironmentException(e, environment), identifierNode.position)
}
}
}

View File

@@ -4,7 +4,7 @@ import io.smnp.callable.function.Function
import io.smnp.callable.function.FunctionDefinitionTool
import io.smnp.callable.signature.Signature.Companion.simple
import io.smnp.callable.signature.Signature.Companion.vararg
import io.smnp.error.EvaluationException
import io.smnp.error.CustomException
import io.smnp.ext.midi.MidiSequencer
import io.smnp.type.enumeration.DataType.*
import io.smnp.type.matcher.Matcher.Companion.anyType
@@ -21,7 +21,13 @@ class MidiFunction : Function("midi") {
listOf(NOTE, INT, STRING),
mapOfMatchers(ofType(STRING), anyType())
) body { _, (config, lines) ->
MidiSequencer.playLines(lines.unwrap() as List<List<Any>>, unwrapConfig(config))
val unwrappedLines = lines.unwrap() as List<List<Any>>
if (unwrappedLines.size > 16) {
throw CustomException("MIDI standard supports max to 16 channels and that number has been exceeded")
}
MidiSequencer.playLines(unwrappedLines, unwrapConfig(config))
Value.void()
}
@@ -29,7 +35,13 @@ class MidiFunction : Function("midi") {
mapOfMatchers(anyType(), anyType()),
mapOfMatchers(ofType(INT), listOfMatchers(listOf(NOTE, INT, STRING)))
) body { _, (config, channels) ->
MidiSequencer.playChannels(channels.unwrap() as Map<Int, List<List<Any>>>, unwrapConfig(config))
val unwrappedChannels = channels.unwrap() as Map<Int, List<List<Any>>>
if (unwrappedChannels.size > 16 || unwrappedChannels.any { (k) -> k > 16 }) {
throw CustomException("MIDI standard supports max to 16 channels and that number has been exceeded")
}
MidiSequencer.playChannels(unwrappedChannels, unwrapConfig(config))
Value.void()
}
}
@@ -37,8 +49,9 @@ class MidiFunction : Function("midi") {
private fun unwrapConfig(config: Value): Map<String, Any> {
return (config.unwrap() as Map<String, Any>)
.map { (key, value) ->
key to when(key) {
"bpm" -> value as? Int ?: throw EvaluationException("Invalid parameter type: 'bpm' is supposed to be of int type")
key to when (key) {
"bpm" -> value as? Int
?: throw CustomException("Invalid parameter type: 'bpm' is supposed to be of int type")
else -> value
}
}.toMap()