Schools of Thought – Compilers

It occurs to me that there are roughly two schools of compiler and language design, typified by the books The Structure and Interpretation of Computer programs (aka SICP) by Ableson and Sussman, and Compilers: Principles, Techniques, and Tools by Aho et al.

They’re also known as the Wizard Book and Dragon Book, respectively, named for their cover illustrations. The choices appear even more fitting in juxtaposition.

A little stack machine

Threw this together to show an example of computed goto. Computed goto is a handy tool for building little interpreters.

#include 

#define NEXT goto **ip++
int main()
{
  int fact = 6, fact_b0 = 16;
  void* program[] = { 
    &&push, (void*)5, &&call, (void*)fact, &&printi, &&end,
    &&beq0, (void*)fact_b0, &&dup, &&push, (void*)1, &&sub, &&call, (void*)fact, &&mult, &&ret, 
    &&pop, &&push, (void*)1, &&ret  };
  void** ip = program;
  void* stack[16] = {0}; void** sp = stack-1;
  void* cstack[16] = {0}; void** cp = cstack-1;
  NEXT;
call:
  *++cp = ip+1; ip = program + (int)*ip;
  NEXT;
ret:
  ip = *cp--;
  NEXT;
beq0:
  ip = *sp ? ip+1 : program + (int)*ip;
  NEXT;
push:
  *++sp = *ip++;
  NEXT;
pop:
  --sp;
  NEXT;
dup:
  sp[1] = sp[0]; sp++;
  NEXT;
mult:
  sp[-1] = (void*)((int)sp[-1] * (int)sp[0]); sp--;
  NEXT;
sub:
  sp[-1] = (void*)((int)sp[-1] - (int)sp[0]); sp--;
  NEXT;
printi:
  printf("%i\n", (int)*sp--);
  NEXT;
end:
  return 0;
}

On codepad

Repls, repls, everywhere

Have a new-years resolution to try out a new programming language, but in too much of a hurry to pick only one, or install anything?

Online REPs and REPLs

Today there’s 61 different languages on that list. That many, there’s gotta be at least one that strikes your fancy.

Best batch execution site: ideone.com with 50 unique languages.

Best interactive REPL site: repl.it with 16 unique languages.

Quickrefs for Python and Vim

More quick reference booklets:

I don’t really need the qr for Python, but I thought it might be good to have ready should a friend a coworker wish to start learning Python.

As for Vim, the reference is certainly terrible – I’ve barely used it at all. The qr is just a list of commands that looked useful. I’ll edit the qr as I go, perhaps using the last pages for notes.

Go: Defer, Panic, and Recover

It seems after much resistance to the idea, Go has added exception handling to the language in the form of defer, panic, and recover.

The Go Programming Language Blog: Defer, Panic, and Recover.

Don’t get me wrong, I think the’ve got something here.  Defer gives you the equivalent of C#’s finally/using; or a nice subset of C++’s destructors; or Lisp’s unwind-protect.  Panic = throw / raise.  Recover = catch.

The blog post gives the impression that you can’t programmatically access the value passed to panic — “recover returns nil” — but before judging that behavior I should try to confirm it in the Go documentation.

All in all, I’m giving this design a thumbs up. No element of it is new, but they seem to have brought exception handling in without compromising their mantra of practical and simple.

Best and Free Programming Ebooks

(updated / switched link to source article on stack overflow)

So, I wrote my list of some unique programming texts. But on the more practical side, there’s this:

Best and Free Programming Ebooks.

Free full texts, covering: Bash, C, C++, C#, Common Lisp, Haskell Java, JavaScript, Lua, Objective-C, Perl, PHP, PowerShell, Prolog, Python, Ruby, SQL, x86 assembly, algorithms, version control, and a couple other topics.  I’ll still be putting together my own preferred lists, but half of what I’ve got is already on this list.

Is it better to remain blissfully unaware…

I was once asked “why bother learning these other languages, if you can’t use them”. It hadn’t occurred to this person that I code off hours. I’ve also been asked “what if you find these other languages actually are better than C++? Won’t you find it depressing having to code in a normal language then?”

It’s a good question. I don’t know if there’s one answer, but I’ve found mine.

Learning Lisp, Haskell, Python, and others has given me a greater appreciation for computer science, for the theory and universe of knowledge that lies underneath the surface of programming. Now when I code, I don’t just solve the problem at hand. I see different ways to solve it. I see other remote problems its related to. I appreciate the aesthetic of it all.

It’s not for everyone, but I find the mathematics underlying it all very beautiful.

And as for my day job in old-school-C++? Well, I hack together a prototype in Python in a couple hours, then spend a week translating it to C++ (adding verbosity, manual error handling, unfolding metaprogramming, etc). My boss doesn’t necessarily understand the whole languages thing, but he’s happy when I get a 2 wk job done in one. I’m happy too; having gotten through the icky bits of the problem in Python, I spend less time working on the boring bits in C++, and can move on to the next project sooner.

There is one last question, that I’m not sure I really have a good answer to. “Won’t you be tempted to use features that don’t exist in a normal language?” I’m hoping this will evaporate. Templates have existed in C++ since ’88 (pdf), various people have hacked lambdas in, and VC10 has added them for real. Short those facilities, it’s not like I have forgotten how to write imperative code. It’s still an option – I just now realize the development expenses of that option.

~

I’m curious, other answers do you choose for these question?