You are going to run into some troubles making this feel like a natural language since the natural form that Scala wants to parse is class-instance method argument method argument method argument ...
, which is rather unlike English.
However, here is a framework to get you started, with lots of extra boilerplate syntax to make the parsing work out right.
object Now {
class Value(val please: Double) {
def plus(d: Double) = new Value(please + d)
def minus(d: Double) = new Value(please - d)
override def toString = please.toString
}
def calculate(d: Double): Value = new Value(d)
}
And here it is working (after an import language.postfixOps
):
scala> Now calculate 4 plus 6 please
res1: Double = 10.0
Incidentally, there's already a very good natural way to ask for 4+6...it's 4+6
. It works for speakers of many different languages, and for almost all computer languages too. So this DSL might be fun for a toy, but I'm not sure what the practical utility is.