Kapitel 12

Programmiersprache


In the preceding sections, you were introduced to the LISP programming language, program development and how to use AutoLisp to extend and customize a commercial CAD software like AutoCad. As part of this process, all the work was done at the programming level using LISP. Expressions and programs which you developed were executed by AutoLisp interpreter that runs in the background.

In this section, an overview of how program instructions in a high-level language like LISP really get executed by the machine is presented. We also introduce the concepts of interpreted and compiled programming languages, symbolic and procedural languages, and the kinds of choices you can make whenever you undertake programming tasks. Finally, we also demonstrate how AutoCad and AutoLisp environment can be extended by combining it with an entirely different kind of a programming language like C.

12.1 Computer Organization

Computers process instructions- also called programs, and carry out various operations like addition, subtraction, etc. The electronic circuits of computers can recognize and directly carry out only a limited number of simple instructions. The most basic instructions are as simple as 'add two numbers', 'check if a number is set to 1', 'move one number from one part of computer memory to another'. This language which the computer really understands is called a machine language. Since the basic set of instructions in a machine language is so simple and so small, we may find it difficult to express many tasks in the machine language.

This problem can be addressed by designing another language (L2) that is more extensive and easy to use. But we still need a way to communicate every expression in L2 to the machine. This process can be done in two different ways. In one approach, every statement in L2 is replaced by an equivalent statement in the machine language, which the computer can execute. This is known as translation.

In the second approach, a program in the machine language reads each statement written in L2, and immediately carries out an equivalent instruction in the machine language. This is the process of interpretation.

What if the language L2 is also found to be complicated to use? We can design another language (L3) that is easier to use. Everything written in L3 will either get translated or interpreted into one of the lower level languages and then get executed by the computer. We can continue designing like this, inventing new languages till we find one that is appropriate for our task. Thus we can think of computers as multi-level machines; at the highest level, most sophisticated languages are acceptable; at the bottom most level, only the simplest- machine language, is acceptable. As a programmer, you may select to work on a level at which you find the language most comfortable. You do not need to be concerned with precise details of how your programming language gets either translated or interpreted by lower level languages, including the machine language. But if you have a conceptual understanding of this process, your programming ability will improve significantly.

Most modern computers are multi-level machines in the sense described above. Some of the most common levels found in modern computers are shown in the following figure; they include: device level, digital logic level, microprogramming level, conventional machine level, operating system machine level, assembly language level, and problem-oriented language level. 

Hierarchy of various levels in computer organization

The device level is the lowest level at which electronic transistors operate on electrical current. At the digital logic level, there are digital gates which take as input signals representing 0 or 1, and compute as output some simple function of the input. The microprogramming level consists of a small number of instructions (e.g. adding two numbers, logical comparisons of numbers) which are turned into input for the digital logic level. If different computer manufacturers use different microprograms in their computers, a program written for one computer brand may not work for another brand. At the conventional machine level, the instructions become more extensive in scope but they are still defined in terms of microprogramming instructions. At the next level- operating system machine level, a new set of instructions, and features like different memory organization, and others are available. Some of these instructions are directly interpreted by the microprogramming level, others are interpreted by an operating system. In the levels described so far, (except for the device level), the languages used are numeric; programs written in these languages consist of long series of numbers (which in turn are made up of 0's and 1's).

At the next level- assembly language level, programs contain symbolic forms- words and abbreviations that are meaningful so that it is easier to write programs. A program called assembler translates assembly language into one of the other languages described above. The next level up in this hierarchy uses problem-oriented languages. Examples of such languages are: FORTRAN, C, Pascal, Basic, and others including LISP. Programs written in one of these languages are either executed by: an interpreter (which takes one instruction at a time, turns it into one of the lower level instructions, executes it, takes the next instruction, and so on) or a compiler (which takes the whole program and turns it into an equivalent program at one of the lower levels, and executes it).

12.2 Programming Languages

Many programming languages are available and widely used today. In order to use a given programming language, you only need to get a software package consisting of the programming editor, compiler, and some other utilities for a specific brand of computer that you have. Some programming languages are more popular than others since some languages are better for certain tasks than others. Following are some of the major distinguishing characteristics of programming languages.

12.2.1 Data typing

Computer programs consist of instructions that operate on data, and most common data types are numbers- integers and reals, characters (and strings made up of characters), and logicals (e.g. t or nil in LISP). From these simple data types, more complex data types can be developed.

Some programming languages, e.g. Pascal, are strongly typed, i.e. you need to declare the type of value that a variable can take before that variable is used or assigned a value. After doing a type declaration, it is not permissible to assign a value of any other type to the variable, otherwise your program will not be compiled or even crash during execution. Other languages, e.g. C, are weakly typed, i.e. you still need to do variable declaration before you use them but you can assign a value to a variable that may be different from what you declared. In such cases, predefined conventions for changing values from one type to another (e.g. characters to integers) are used. If you are not aware of these conventions, the program will do unexpected things when executed. A language like LISP gives you even more freedom- a variable and the type of value it will be assigned, need not be declared before you use it. The same variable may be assigned an integer, a character, a real or a character string, a list that contains multiple values of various types, and entire function definitions may be assigned to one symbol.

12.2.2 Interpreted vs. Compiled

As described previously, some languages are interpreted, i.e. each statement in a program is taken, interpreted and executed one at a time. This implies that you can incrementally develop your program one line at a time, test it, debug it and then extend it. Many implementations of LISP and Basic are interpreted.

For languages that are compiled, you need to write a complete program. After it is compiled, it can be tested, debugged, extended- but for each step, you need to go through the whole cycle even if the only mistake in your program is a missing comma. Many implementations of Pascal, C, FORTRAN are compiled.

Interpreted languages are good for program development but may take longer to execute. Compiled languages may take longer in program development and debugging but may take less time for actual execution of a compiled program.

12.2.3 Symbolic vs. Procedural

This is a more abstract distinction better appreciated after learning a few programming languages. In general terms, if a programming task involves a well understood series of operations, or numerical computations, or the necessity of maintaining precise data types, it is better to use procedural languages like Pascal, C, FORTAN, and others. On the other hand, if a programming task involves manipulation of abstract concepts and associations between them and data sizes or types are not known in advance before the program is executed, it is better to work with a symbolic language like LISP.

12.3 AutoCad and C

AutoCad software provides a way of extending and customizing its features by using AutoLisp. In many cases, a modelling software like AutoCad will be used to do numerical computations, e.g. calculating the mass properties of solids created in AutoCad. It is possible to write programs in AutoLisp which perform these calculations. A more efficient- in terms of space and time, alternative is to write programs to do such calculations in a procedural language like C and interface it with AutoCad environment.

With the latest version of AutoCad, it is possible for external programs to communicate with AutoCad environment and the drawing data created in it. The programming language C may be used to write such external programs. In order to communicate and share data with AutoCad, certain conventions are defined. Once these conventions are followed in programs written in C, then all the external programs can be compiled and are available from within AutoCad.

12.4 Uebung 12

In this exercise, you will get a first-hand experience of executing the same program that is interpreted as well as compiled. This exercise will give you some indication of where to employ which kind of programming language.

You can use the program you wrote for bung 7 (Iteration and Recursion) for this exercise. In order to compare the amount of time it takes to run your program in interpreted LISP and in compiled LISP, you need do the following with your program file.

First, copy a file 12_name.lsp from the directory /homes2/prog/ausgabe into your account. This file contains a timer function that you will need to use. It also contains instructions about where and how you should paste your program code for bung 6 into this file.

Once you have completed the above process, do the following:

You should submit both 12_name.lsp and 12_name.bi4 by copying them in the directory: /homes2/prog/abgabe.


To the next chapter

Prog Content Vorwort ..1.. ..2.. ..3.. ..4.. ..5.. ..6.. ..7.. ..8.. ..9.. ..10.. ..11.. ..12.. ..13.. Appendix


@ by Architektur und CAAD 1994.......... The Teacher Team