Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

How to build an interpreter in Java, Part 1: The BASICs

For complex applications requiring a scripting language, Java can be used to implement the interpreter, adding scripting abilities to any Java app

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
When I told a friend that I had written a BASIC interpreter in Java, he laughed so hard he nearly spilled the soda he was holding all over his clothes. "Why in the world would you build a BASIC interpreter in Java?" was the predictable first question out of his mouth. The answer is both simple and complex. The simple response is that it was fun to write an interpreter in Java, and if I were going to write an interpreter, I might as well write one about which I have fond memories from the early days of personal computing. On the complex side, I've noticed that many people using Java today have gotten past the point of creating tumbling Duke applets and are moving on to serious applications. Often, in building an application, you would like it to be configurable. The mechanism of choice for re-configuration is some sort of dynamic execution engine.

Known as macro languages, or configuration languages, dynamic execution is the feature that allows an application to be "programmed" by the user. The benefit of having a dynamic execution engine is that tools and applications can be customized to perform complex tasks without replacing the tool. The Java platform offers a wide variety of dynamic execution engine options.

HotJava and other hot options

Let's explore briefly some of the dynamic execution engine options available and then look at the implementation of my interpreter in depth. A dynamic execution engine is an embedded interpreter. An interpreter requires three facilities in order to operate:

  1. A means of being loaded with instructions
  2. A module format, for storing instructions to be executed
  3. A model or environment for interacting with the host program


HotJava
The most famous embedded interpreter has to be the HotJava "applet" environment that has completely reshaped the way people look at Web browsers.

The HotJava "applet" model was based on the notion that a Java application could create a generic base class with a known interface, and then dynamically load subclasses of that class and execute them at run time. These applets provided new capabilities and, within the confines of the base class, provided dynamic execution. This dynamic execution capability is a fundamental part of the Java environment and one of the things that makes it so special. We'll look at this particular environment in depth in a later column.

GNU EMACS
Before HotJava arrived, perhaps the most successful application with dynamic execution was GNU EMACS. This editor's LISP-like macro language has become a staple for many programmers. Briefly, the EMACS LISP environment consists of a LISP interpreter and many editing-type functions that can be used to compose the most complex macros. It should not be considered surprising that the EMACS editor originally was written in macros designed for an editor called TECO. Thus, the availability of a rich (if unreadable) macro language in TECO allowed an entirely new editor to be constructed. Today, GNU EMACS is the base editor, and entire games have been written in nothing more than the EMACS LISP code, known as el-code. This configuration ability has made GNU EMACS a mainstay editor, while the VT-100 terminals it was designed to run on have become mere footnotes in a writer's column.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources