C++ By Example

At the end of the Twentieth Century I developed an obsession; I was a big C++ fan at the time, and was following this thought: What if people learn programming languages best in interactive, ‘conversational’ contexts, just like natural language? I had been fascinated by the interactive prompt ever since I found a LISP 1.5 interpreter on the university mainframe – I didn’t actually become a Lisp person but the power of evaluating an expression in a rich context was obvious. You can be right inside a running program. The contrast is with ‘batch’ processing where it’s like writing and sending a letter to the computer, which then compiles and links an executable; you get your mistakes back by return mail. 1

There seemed only one way to test this hypothesis: write a C++ interpreter, how hard could that be? In retrospect, my lack of formal Computer Science education beyond first year made me overconfident, and the resulting code was not a thing of beauty2. But, it did the job – there was an interactive prompt!

I had to loosen the grammar so that top-level expressions could exist outside functions, but the interactivity mostly just worked.

I pitched the book proposal to Que with the provisional title ‘Conversational C++’, though they already had a ‘by Example’ series and so C++ By Example happened. (You can view part of it in Google Books)

  1. This was a tedious process with the mainframe when lots of anxious students are waiting for a CPU core. The WITS (Witwatersrand Interactive Terminal System) had a Fortran IV interpreter which was convenient (but not interactive)
    ↩︎
  2. In particular the YACC/BISON grammar was a tangled mess of lexical tie-ins, because C++ cannot be parsed by a regular grammar. The ‘foreign function interface’ was entirely hand-coded in 32-bit Assembly, which aged poorly in the transition to 64-bit. Recently Louis Höfler has done a rewrite using libffi to do the interfacing with DLLs. (Code at GitHub) ↩︎