Common Lisp As an Embedded Extension Language
Total Page:16
File Type:pdf, Size:1020Kb
Common Lisp as an Embedded Extension Language A large part of HP PE/SolidDesigner’s user interface is written in Common Lisp. Common Lisp is also used as a user-accessible extension language. by Jens Kilian and HeinzĆPeter Arndt HP's PE/ME10 and PE/ME30 CAD systems contain anLisp extenĆ was invented by John McCarthy in 1956 during the sion language based on the macro expansion paradigm.Dartmouth The Summer Research Project on Artificial IntelliĆ user's input (commands and data) is separated intogence. single The first commonly used dialect was Lisp 1.5, but tokens, each of which denotes a command, function,unlike variĆ FORTRAN (or any other imperative language) Lisp is able, macro name, number, string, operator, or otherso syntacĆ easy to modify and extend that over time it acquired tic element. Commands, functions, and arithmetical expresĆcountless different dialects. For a long time, most Lisp sysĆ sions are evaluated by the language interpreter. Eachtems macro belonged to one of two major families, Interlisp and name is associated with a macro definition, whichMacLisp, is another but still differed in details. In 1981, discussions token sequence (either predefined by the system orabout defined a common Lisp language were begun. The goal was to by the user). When the language interpreter encountersdefine a a core language to be used as a base for future Lisp macro name, it substitutes the corresponding tokensystems. seĆ In 1984, the release of Common Lisp:1 The Language quence (this process isexpanding called the macro) and provided a first reference for the new language. An ANSI continues with the first token of the expansion. Technical Committee (X3J13) began to work on a formal standardization in 1985 and delivered a draft standard for Macro expansion languages are easy to implement and have Common Lisp in April 1992. This draft standard includes been used in many applications where one would hardly objectĆoriented programming features (the Common Lisp expect to find an embedded language. For example,X the T E Object System, or CLOS). For a more detailed account on the typesetting system contains a macro interpreter. evolution of Lisp, see McCarthy2 and Steele and Gabriel.3 The HP PE/ME10 and PE/ME30 macro language includes HCL, the implementation of Common Lisp used in HP PE/ powerful control constructs (suchIF/THEN/ELSE as andLOOP/ SolidDesigner, is derived from Austin Kyoto Common Lisp EXIT_IF/END_LOOP), local variables, and a mechanism for (itself descended from Kyoto Common Lisp). It corresponds passing parameters to a macro when it is being expanded. to the version of the language described in reference 1, but These constructs make it possible to solve general programĆ already incorporates some of the extensions from reference ming problems. Because the HP PE/ME10 and PE/ME30 4 and the draft standard. macro language is interpreted, programs can be developed in an interactive fashion and modifications can immediately Applications of Extension Languages be tried out. However, the resulting program is slower than a Adding extension languages to large application programs program written in a compiled language like C. HP PE/ME10 has become a standard practice. It provides many advanĆ and PE/ME30 macros can be compiled to an intermediate tages, some of which may be not as obvious as others. For form which executes faster than the pure interpreted verĆ the normal user of a system, an embedded programming sion, but which is still slower than an equivalent C program. language makes it possible to automate repetitive or tedious One disadvantage of the HP PE/ME10 and PE/ME30tasks. macro An inexperienced user can set it up as a simple reĆ language is that it is nonstandard. No other applicationcord/playback uses mechanism, while power users" can use it to the same language, and programs written in it havecreate to additional be functionality. If the extension language has ported when the user switches to another CAD system.ties to the application's user interface, userĆdefined functionĆ ality can be integrated as if it were part of the original Common Lisp application. Common Lisp was chosen as an extension language for HP If the application provides an API for adding extensions on a PE/SolidDesigner because it is nonproprietary and widely lower level, the extension language can itself be extended. used. This enables makers of valueĆadded software to integrate Surprising as it may be, Lisp is the second oldesttheir highĆlevel products seamlessly into the main application. As an programming language still in common use. The onlyexample, older the HP PE/SheetAdvisor application has been imĆ one is FORTRAN. Lisp is to researchers in artificialplemented intelliĆ within HP PE/ME30, offering a user interface gence what FORTRAN is to scientists and engineers.consistent with the rest of the program. October 1995 HewlettĆPackard Journal69 As a final step, portions of the application can themselvesThe article be on page 14 describes, from a user interface deĆ implemented in the embedded language. An exampleveloper's would perspective, how action routines are written and be the popular GNU Emacs text editor, a large parthow of menus which and dialogs are created. The mechanisms used is written in its embedded Lisp dialect. there are not part of the Common Lisp standard but are extensions provided by the HCL dialect. A large part of HP PE/SolidDesigner, too, is written in its own extension languageĊabout 30 percent at the time of Action Routines writing. Most of this 30 percent is in HP PE/SolidDesigner's Action routines implement the commands that a user types user interface. or issues via user interface elements to HP PE/SolidDesigner. Commands are identified by their names, which are Lisp Lisp in HP PE/SolidDesigner symbols evaluated in a special manner (similarSYM- to the Fig. 1 shows the major components of HP PE/SolidDesigner. BOL-MACROLET facility in the Common Lisp Object System). The Lisp subsystem is at the very core, together with the Each action routine is actually an interpreter for a small lanĆ Frame (operating system interface) and DSM (data structure guage, similar in syntax to the command language used in manager, see article, page 51) modules. All other compoĆ HP PE/ME10 and PE/ME30. Like HP PE/ME10 and PE/ME30 nents including Frame and DSM are embedded into the Lisp commands, action routines can be described by their syntax subsystem. This indicates that each component provides an diagrams. Fig. 2 contains the syntax diagram for a simplified interface through which its operations can be accessed by version of HP PE/SolidDesigner's exit command. Below the Lisp programs. syntax diagram is a state transition graph which shows how The introduction of new functionality into HP PE/SolidDeĆthe command will be processed. signer is usually done in the following steps: The definition of an action routine corresponds closely to its • Implement new data structures and operations in C++ syntax diagram. The defining Lisp expression, when evaluĆ • Add Lisp primitives (C++ functions callable from Lisp) for ated, generates a normal Lisp function that will traverse the accessing the new operations transition graph of the state machine when the action rouĆ • Add action routines to implement new userĆvisible tine is run. For example, the following is an action routine commands, using the Lisp interface to carry out the actual corresponding to the syntax diagram of Fig. 2: operations • Add menus, dialog boxes, or other graphical user(defaction interface simple_exit objects to access the new commands. (flag) ; local variable As long as the Lisp interfaceĊthe primitive functionsĊis (; state descriptions agreed to in advance, this process can be parallelized. A user interface specialist can work on the action routines (start and nil menus, calling dummy versions of the interface functions. “Terminate PE/SolidDesigner?” nil (:yes (setq flag t) answer-yes end) User Interface (:no (setq flag nil) answer-no end) (otherwise (display_error “Enter either :YES or :NO.”) nil start)) Links (end (do-it) (IGES, STEP, nil ...) nil)) K2 (; local functions Frame (do-it () Advanced (when flag HCL Blending (quit))))) DSM As can be seen in this example, an action routine can have local variables and functions. Local variables serve to carry PPG information from state to state. Local functions can reduce the amount of code present in the state descriptions, Advanced Local enhancing readability. Operations When HCL translates this action routine definition, it proĆ duces a Lisp function which, when run, traverses the state transition graph shown in Fig. 2b. If a state description conĆ tains a prompt string, asstart in state the in the example, the Fig. 1. HP PE/SolidDesigner system architecture. HCL is thetranslator automatically adds code for issuing the prompt Common Lisp subsystem. All components including Frame and reading user input. Effectively, the translator converts (operating system interface) and DSM (data structure manager)the simple syntax diagram into the more detailed form. have interfaces to Lisp. K2 is the solid modeling kernel. PPG is the planar profile generator. 70October 1995 HewlettĆPackard Journal simple_exit :YES ;; exit from action routine :NO (return)))))) (a) Transitions in the state machine are transformedgoto into :YES statements within the function's body. The conditional conĆ set flag = t structcond represents decisions, like the threeĆway branch in issue prompt wait for statestart. Before each state transition, the code can trigger start input end :NO an external event to enable graphical feedback in menus or set flag = nil dialogs. other input issue error message The actual translation is somewhat more complicated beĆ cause errors and other exceptional events must be taken into (b) account. The translator also adds code to support debugging Fig. 2.(a) Simplified syntax of the exit command. (b) Stateand profiling of an action routine.