Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for rewriting Crenshaw's "let's build a compiler"?

I am rewriting Jack Crenshaw's "let's build a compiler" from Turbo Pascal 4.0 to JAVA. This is motivating because the classic book does not have yet an O-O version

Is there a more modern, OO version of "Let's Build a Compiler"?

The book contains 15 chapters. The compiler is presented in an incremental manner: Chapter I provides the boilerplate codes for the whole compiler, then each chapter adds some lines to the Pascal procedures of the precedent chapter.

I have already translated the first 2 chapters, each chapter as a package, each Pascal procedure as a static protected method in JAVA, the procedures of one chapter are gathered in a single class that extends the class translated from its precedent chapter.

package td1;
public class Cradle {
    protected final static char TAB='\t';
    protected static char look;

    protected static void getChar() throws IOException {
        look=(char) System.in.read();
        }       
 ...
}

package td2;

public class Cradle extends td1.Cradle{

    protected static void factor() throws IOException {
    ...
    }

...
}

However, when I come to td3, I have to update the factor() of td2.Cradle, but I do not want to change in td2.Cradle the factor(), because that will make the factor() in td2 do more than it should have presented in td2. I thought of "extend" td2.Cradle(), however, it seems impossible to extend a static class.

My related question here

Maybe should I change every static method to non-static one?

I certainly need some design pattern here, any one can help? I hope I was clear. In summary, this project incrementally presents more and more instructions for each procedure and I do hope to record the intermediate steps using some JAVA mechanism like inheritance.

The Pascal code is the classic book is here LBC. I feel attempted to use inheritance because

  1. Each chapter calls/adds a bit more lines to the procedures defined in precedent chapters
  2. I hope to make my JAVA source code usable to everyone who wants to follow LBC step by step. So it is not appropriate to use a single class to put in the final source code of the author's compiler. It is essential to split the codes by chapters and increment them gradually as Crenshaw did.

My actual solution is to keep the methods of tp1.Cradle as static. The methods in tp2.Cradle, tp3.Cradle, ... , until tp15.Cradle will be non-static, and they all statically import tp1.Cradle.* . Furthermore, for every integer i greater than 2, tp[i].Cradle extends tp[i-1].Cradle.

Don't hesitate to tell me better solution.

like image 301
zell Avatar asked May 05 '12 15:05

zell


1 Answers

It sounds like you are on the right track. In order to be able to override these methods, they should be instance methods. And so you should move away from a model that relies on these "global methods", to an instance based model, where you create an instance of the Cradle class and call the appropriate methods on that instance.

like image 172
JesusFreke Avatar answered Oct 14 '22 20:10

JesusFreke