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...