Create scaffolding for custom functions

This commit is contained in:
2020-03-11 19:59:29 +01:00
parent eb4fb1e980
commit e7bf085f58
10 changed files with 101 additions and 5 deletions

View File

@@ -30,7 +30,7 @@ abstract class Function(val name: String) {
.firstOrNull { (_, args) -> args.signatureMatched }
?: throw FunctionInvocationException(this, arguments)
return definition.body(environment, arguments.toList())
return definition.body(environment, args.arguments)
}
val signature: String

View File

@@ -2,7 +2,7 @@ package io.smnp.data.entity
import io.smnp.data.enumeration.Pitch
class Note private constructor(val pitch: Pitch, val octave: Int, val duration: Int, val dot: Boolean) {
data class Note (val pitch: Pitch, val octave: Int, val duration: Int, val dot: Boolean) {
data class Builder(var pitch: Pitch = Pitch.A, var octave: Int = 4, var duration: Int = 4, var dot: Boolean = false) {
fun pitch(pitch: Pitch) = apply { this.pitch = pitch }
fun octave(octave: Int) = apply { this.octave = octave }

View File

@@ -1,5 +1,7 @@
package io.smnp.environment
import io.smnp.callable.function.Function
import io.smnp.callable.method.Method
import io.smnp.type.model.Value
interface Environment {
@@ -8,4 +10,6 @@ interface Environment {
fun invokeFunction(name: String, arguments: List<Value>): Value
fun invokeMethod(obj: Value, name: String, arguments: List<Value>): Value
fun printCallStack()
fun defineFunction(function: Function)
fun defineMethod(method: Method)
}

View File

@@ -32,8 +32,14 @@ class Module(
}
}
fun attachTo(module: Module) {
module.addSubmodule(this)
fun addFunction(function: Function) {
function.module = this
functions.add(function)
}
fun addMethod(method: Method) {
method.module = this
methods.add(method)
}
val canonicalName: String