Conversational C ++

zhaozj2021-02-17  53

Conversational C by Steve Donovan, The Author Of C by Example: "Underc" Learning EditionMar 15, 2002

Interpreted languages ​​usually have an interactive mode in which expressions and statements can be evaluated in a conversational way, and there is no technical reason why C can not be used like this. According to Steve Donovan, C turns out to be a very interesting conversational language. This Article is provided courtesy of que.

The secret weapon of most popular scripting languages ​​is that they support an interactive mode. LISP and BASIC systems became popular because expressions could be typed in and immediately evaluated. (Obviously, they have other virtues, but they would probably remained unappreciated if they were not so interactive.) This, of course, makes most sense for languages ​​such as LISP, in which everything has a value-even if it is nil, but nearly any programming language can be used interactively. Such an implementation supports a "conversational" style of programming, in which the feedback from the computer is practically instantaneous. Programmers were very productive while working with interactive systems such as LISP, APL or FORTH, despite the limitations of these languages. The only requirement is that the language be function-based. SO, Even Pascal Can Be Interpreted Interactively1, Eiffel or Java Would Present Problems Because The isy class-based-there is no free-standing functions. AN interactive implementation of Java would necessarily do violence to the language. The big difference between a batch and an interactive environment is that batch environments have to keep compilation going, whereas interactive environments have to keep evaluation going. Typically, interactive systems stop attempting to compile at the first error. C compilers get confused easily, and this in turn confuses the programmer, who really needs to sort out the reported problem first. This is particularly true for the novice programmer. I first started working on conversational C

systems because I felt they would be very useful tools for the beginner learning the language. Freedom to explore seems an essential part of the learning process, and human language acquisition is bound up initially with talking, not writing and reading. C is, curiously enough , better suited to interactive work than BASIC because expressions can be valid statements. For example, you may type an assignment and you will be shown the value. Similarly, variables are valid (if silly) C statements. What I hope to show you in this article is the practicality and the power of using C in this interactive mode, and how this can potentially vastly improve programmer productivity. Interactive C needs a slightly relaxed syntax. I'll use UnderC2 for these examples because CINT does not allow function or macro DEFINITIONS in Interactive Mode. in Standard C , Declarations Can Be Mixed With Other Statements, But Only In Class or Function Bodies. in Interactive C , Function Definitions Can Be Mixed with statements at global scope:;> Double SQR (double x) {return x * x;};> SQR (2);

(Double) 4.0

;> Double X = 2;

;> x;

(Double) x = 2.0 These are all valid C statements, except there's no scope restriction on their use. Our old and disreputable friend-the preprocessor-becomes very useful, indeed. For example, I commonly define a macro to do the usual for -statement:;> #define for (i, n) for (int i = 0; i <(n); i )

;> For (i, 5) cout << i << Endl;

0

1

2

3

4 Here is another reason why C is a better interactive language than Pascal (or even BASIC);. It is a terse language ({for "begin," etc.) that can be made even more compact with macros Conversational C is often throwaway code, and you do not want to waste keystrokes. Now, making up your own control statements is generally not a good idea in formal C (can we say written C ?), but in a conversational context, you can afford to use a more informal style. in a formal legal document in English, you would not use contractions (such as "do not"). I'm going to make a case for (limited) control statement macros in another article, but during an interactive Session you may pretty much do anything syntactically legal in the pursuit of faster interactions:;> #define all (ls) .begin () () .end () .end ().

;> Copy (ALL (ls), arr);! I would certainly hesitate to do this in production code C was regarded as being too complex to use in an interpretive fashion, but this is an historical problem that has to do with the way the language was initially implemented: preprocessor, translator (to C), compiler (to assembly), assembler (to object code), linker (to executable) A modern interpreter, such as UnderC, pulls input through a built-in preprocessing tokenizer. , and compiles directly to pcode-which then is immediately executed. Obviously, this code will be slower than the result of a true compiler, but interactive systems only have to be faster than human reaction time. Generally, if you can do something in less than 150 ms, then you are considered instantaneous. A myth exists that there is a complexity / speed trade-off with interactive implementations, which simply is not true. CINT and UnderC run rings around Tcl (which is about as simple as a useful Language Can Get. C is Terse Enough, But perhaps it is too syntactically involved to use interactively I agree that the for-statement does not trip easily off the fingers, which is why I suggested defining FOR to do all that typing for you Using FOR is also much less prone to errors..; i have been caught more than once in nested loops by the tendency to substitute an i for a j. People will not routinely define functions and classes at the prompt. (If they do, then facilities exist for copying the transcript to a file or to The clipboard.) Complex Stuff Will Be Typed Into A File, And Loaded As Needed. A More Interesting Question IS WHETHER C

is expressive enough. Pascal is certainly not (as well as not being terse enough). People use application-specific languages ​​because all the tools of their particular trade are a few keystrokes away. An application that illustrates this point is prototyping signal processing algorithms. Years ago, I used a system called Asyst, which was an array-based Forth dialect. Despite being backward in many ways (not just syntactically), it was a tremendously powerful way to experiment with algorithms because the language made things such as Fast Fourier Transforms and data plots available as primitives, which you then could use interactively. But it was not a good applications platform. One of my colleagues uses IDL (a kind of interactive Fortran on steroids, with rich data display features and extended support for vectors) For prototyping algorithms, and again the conversational model overweighs the disadvantage of working with a niche language. Recently, CH3, A C-Language C99 compatibility), is been marketed for the same purpose. C is well-suited to this kind of style due to its capability to precisely define language semantics to fit the desired syntax. Functions may be passed vectors, and they may return vectors. Arithmetic . with complex numbers uses the usual notation For example, an interactive C system could allow the prototypes to view the power spectrum of the differences between two data vectors with a line like this:;> plot (abs (fft (x y)) This isn't An Appropriate style for real high-perform-performance C Number-Crunching (See Blitz C

4), but that is not crucial here. In fact, the interpreter is being used to glue functional components together. The components themselves would be implemented as shared libraries built by an optimizing compiler. I'm hoping to find the time to make such libraries available this year. This kind of engineering application may seem specialized, but most kinds of projects can benefit from interactive prototyping. At the core of most large projects are a few design unknowns that need to be explored. "Exploratory programming" seems to be the very antithesis of careful software engineering process, but it can contribute to design and implementation in several ways. According to BA Sheil5, exploratory programming is about amplifying programmers, whereas structured methods are designed to restrain programmers. Both have their place in the software development Process. Another Place That The INTERACTIVE MODE HAS in Rigorous Development Is Testing. I Find That Classes Reveal THEIR PROBLEMS IN C onversation. Obviously, you need to generate static tests for further verification, but ... Conversational C is one way around the header problem described in my article titled "Scripting with C ." There seems no standard way around the fact that large amounts of header information must be included, and these headers make up the largest part of the program text that the compiler must deal with. A little 30-line standard C program involving iostreams and strings involves the compiler having to chew nearly 9,000 lines of header files. Invariably, Dependencies Develope Between Parts of Large Programs Until Building Becomes a Tedious Process, And We Have The "Nightly Build"

. Phenomenon In an interactive C session, headers can be loaded up front, and much recompiling can be done very quickly because these headers are already present in the system There is no link phase;. All UnderC methods have indirect references to the actual pcode, so there is no need to go through all function references patching addresses. This works particularly well when the headers are for an imported system that rarely changes. In the "Scripting with C " article, I discuss using C as the glue language for VTK applications , in which the problem was loading (and parsing!) 300 class headers each time a script was to be run. But once the headers are loaded into an interactive session, then VTK test programs in C can be recompiled within milliseconds. This suggests To Me A Way To Efficiently Organize A C

scripting environment: The interpreter remains resident as server, and a small client script launcher communicates with it and uses it to execute scripts The compiler state remains persistent, and headers would only need to be included on the first execution This would be an excellent.. implementation for a CGI scripting application, for instance. The scripts themselves could be small main programs that call the rest of the system, and state would automatically be maintained between individual connections. The dependency problem also becomes more manageable. for instance, a common change to a class is adding another method. Although that class definition may be needed throughout the system (perhaps hundreds of thousands of lines or more) we really need to recompile only the class itself because the class layout has not changed. In fact, you would Have to Compile Only The Method Itself. Providing You Are Not Adding A First Virtual Method (and Thereby Implicitly Add a Hidden Pointer T o the class layout) you should be able to avoid an otherwise tedious build. (If you are getting nervous at this point, remember that dangerous "lazy builds" could, in principle, be detected by the system.) So, it's possible to create an environment in which large C systems can often be modified in milliseconds, rather than minutes or even hours. A criticism at this point is that this "encourages hacking," but by the same token it encourages whole-system testing. Exploratory programming encourages A Shift of Thinking Away from Monolithic Executables; There ISn't Really A "Program" That IS "launched."

转载请注明原文地址:https://www.9cbs.com/read-30093.html

New Post(0)