Fork me on GitHub

Language Definition with SpoofaxΒΆ

In this chapter we make a complete tour of language definition in Spoofax using Calc, a small calculator language, as example. In the sections of this chapter we define all aspects of the Calc language, including its concrete syntax, static semantics, dynamic semantics, testing, and configuration of the IDE. The source code of the language definition is available on github. You can follow along by forking the project and building it in Spoofax.

The Calc language supports the following features

  • Arithmetic with integer and floating point numbers with arbitrary precision
  • Boolean values and boolean operators
  • First-class (but non-recursive) polymorphic functions
  • Variable bindings through top-level definitions and let bindings in expressions
  • Type inference provides static type checking without explicit type annotations

The following Calc program calculates the monthly payments for a mortgage (according to Wikipedia):

monthly = \r Y P.
  let N = Y * 12 in // number of months
   if(r == 0) // no interest
     P / N
   else
     let rM = r / (100 * 12) in // monthly interest rate
     let f = (1 + rM) ^ N in
       (rM * P * f) / (f - 1);

r = 1.7;     // yearly interest rate (percentage)
Y = 30;      // number of years
P = 385,000; // principal

monthly r Y P;

At the end of the chapter we give a list of exercises with ideas for extending the language. Those may be a good way to further explore the capabilities of Spoofax before venturing out on your own. If you do make one of those extensions (or another one for that matter), we would welcome pull requests for branches of the project.

Note that this is not an introduction to formal language theory, compiler construction, concepts of programming languages. Nor is this chapter an exhaustive introduction to all features of Spoofax.