![]() |
|
Navigation |
Synopsis A version of Exp based on abstract syntax.
Description The abstract syntax
![]()
Examples The abstract syntax for Exp looks like this:
module demo::lang::Exp::Abstract::Syntax data Exp = con(int n)
module demo::lang::Exp::Abstract::Eval import demo::lang::Exp::Abstract::Syntax; public int eval(con(int n)) = n;Here we see Rascal's pattern-directed invocation in action (see Rascal:Function). The essence is this: in other languages the formal parameters in a function declaration are just that: formal parameters, i.e., single names that can be used inside the function and that are bound when the function is called. In Rascal, however, the formal parameters are actually a pattern and functions can have arbitrarily complex patterns as (single) formal parameter. These patterns may bind variables and thus introduce variables that can be used in tthe function body. The big advantage of pattern-directed invocation is modularity and extensibility:
rascal>import demo::lang::Exp::Abstract::Syntax; ok rascal>import demo::lang::Exp::Abstract::Eval; ok rascal>eval(mul(con(7), con(3))); int: 21 rascal>eval(add(con(3), mul(con(4), con(5)))); int: 23Entering expressions in abstract syntax form is no fun, and this is where concrete syntax comes to the rescue. ![]() |