Defining a real programming language
Real programming brings up more challenges than the simple calc language from the previous chapter. To have a look at the details, we will be using a tiny subset of Modula-2 in this and the following chapters. Modula-2 is well-designed and optionally supports generics and object-orientated programming (OOP). However, we are not going to create a complete Modula-2 compiler in this book. Therefore, we will call the subset tinylang.
Let’s begin with an example of what a program in tinylang looks like. The following function computes the greatest common divisor using the Euclidean algorithm:
MODULE Gcd; PROCEDURE GCD(a, b: INTEGER) : INTEGER; VAR t: INTEGER; BEGIN Â Â IF b = 0 THEN Â Â Â Â RETURN a; Â Â END; Â Â WHILE b # 0 DO Â Â Â Â t := a MOD b; Â Â Â Â a := b; Â Â Â Â b := t; Â Â END; Â Â RETURN a; END GCD; END Gcd.
Now that we...