score:0

This is just a copy&paste solution, but I am sure you get the idea:

case class VarArgsFunction(Name: String, Args: Action*) extends Action {
  def visit: Double = {
    typeOf[M].member(Name: TermName) match {
      case NoSymbol     => throw new Error(s"Unknown function '$Name'")
      case func: Symbol => {
        runtimeMirror(getClass.getClassLoader).reflect(new M).reflectMethod(func.asMethod)(
          (for { arg <- Args } yield arg.visit).toList).asInstanceOf[Double]
      }
    }
  }
}

object Parser {
  def main(args: Array[String]) {
    // Prints 2.718281828459045
    println(Function("e").visit)

    // Prints 7.0
    println(Function("add", Number(3), Number(4)).visit)

    // Prints 1.2246467991473532E-16
    println(Function("sin", Number(180)).visit)

    // Throws IllegalArgumentException: wrong number of arguments
    println(VarArgsFunction("max", Number(1), Number(2.5), Number(50), Number(-3)).visit)
  }
}

Related Query

More Query from same tag