Specific domain language (domain-specific languages,dsl) has become a hot topic; Many functional languages are popular, mainly because they can be used to build DSLs. In view of this, in the final article of the Scala Guide series for Java developers, Ted Neward continues to discuss a simple calculator DSL to demonstrate the power of functional languages in building an "external" DSL, and in this process to solve the question of converting text input to an AST for interpretation Problem. To parse the text input and convert it into the tree structure used by the interpreter in the previous article, Ted introduced the parser combo (parser combinator), a standard Scala library designed specifically for this task. (In the last article, we built a calculator parser and an AST).
Recall the plight of our hero: when trying to create a DSL (this is just a very simple calculator language), he creates a tree structure that contains the various options available for that language:
Binary Add/subtract/multiply/divide operator
Unary inverse operator
Numerical
The execution engine behind it knows how to perform those operations, and it even has an explicit optimization step to reduce the calculations needed to get results.
The final code is this:
Listing 1. Calculator Dsl:ast and Interpreter
Package COM.TEDNEWARD.CALCDSL
{
PRIVATE[CALCDSL] abstract class Expr
PRIVATE[CALCDSL] Case class Variable (name:string) extends Expr
PRIVATE[CALCDSL] Case class number (value:double) extends Expr
PRIVATE[CALCDSL] Case class UNARYOP (operator:string, arg:expr) extends Expr
PRIVATE[CALCDSL] Case Class BINARYOP (operator:string, left:expr, right:expr)
extends Expr
Object Calc
{
/**
* Function to simplify (a la mathematic terms) expressions
*/
def Simplify (e:expr): Expr =
{
e Match {
//Double negation returns the original value
Case UNARYOP ("-", Unaryop ("-", X)) => simplify (x)
//Positive Returns the original value
Case UNARYOP ("+", x) => simplify (x)
//Multiplying x by 1 returns the original value
Case BINARYOP ("*", X, Number (1)) => simplify (x)
//Multiplying 1 by x returns the original value
Case BINARYOP ("*", Number (1), x) => simplify (x)
Multiplying x by 0 returns zero
Case BINARYOP ("*", X, Number (0)) => number (0)
//Multiplying 0 by x returns zero
Case BINARYOP ("*", Number (0), X) => number (0)
//Dividing X by 1 returns the original value
Case BINARYOP ("/", X, Number (1)) => simplify (x)
//dividing x by x returns 1
Case BINARYOP ("/", X1, x2) If x1 = x2 => Number (1)
//Adding X to 0 returns the original value
Case BINARYOP ("+", x, Number (0)) => simplify (x)
//Adding 0 to X returns the original value
Case BINARYOP ("+", number (0), X) => simplify (x)
//Anything else cannot (yet) be simplified
Case _ => e
}
}
def evaluate (e:expr): Double =
{
simplify (e) match {
Case Number (x) => x
Case UNARYOP ("-", X) =>-(Evaluate (x))
Case BINARYOP ("+", X1, x2) => (Evaluate (x1) + Evaluate (x2))
Case BINARYOP ("-", X1, x2) => (Evaluate (x1)-Evaluate (x2))
Case BINARYOP ("*", X1, x2) => (Evaluate (x1) * Evaluate (x2))
Case BINARYOP ("/", X1, x2) => (Evaluate (x1)/Evaluate (x2))
}
}
}
}