top | item 7051877

A Lisp interpeter in a thousand lines of Bash

97 points| Morgawr | 12 years ago |github.com | reply

29 comments

order
[+] VLM|12 years ago|reply
As a nostalgic trip, a little over 30 years ago I was playing with Randall Beer's LISP interpreter which ran in MS Basic on a TRS-80 model III, very slowly, as seen on page 176 at this link (this is the first in a multiple article series)

https://archive.org/details/80-microcomputing-magazine-1983-...

I distinctly remember as a kid it was very slow indeed, but interesting.

I got lost reading the ads. In retrospect, computing used to be a much more expensive hobby than it is today. Not just relative terms, but absolute terms. Then again, people are much poorer now, so its required.

Anyway, since 1983, he became a neuroscience prof and mentions his BASIC LISP on his homepage

http://mypage.iu.edu/~rdbeer/

The line numbers are not consecutive, but I think he's well under a thousand lines of BASIC, there just aren't enough pages of code in the listing to exceed that.

And yes, this was considered reasonable coding style back then. That is why this generation never shrank in terror at the sight of bad Perl code. Why yes, this is a bit hard to read, but I've certainly seen worse...

[+] Morgawr|12 years ago|reply
Slightly related to the "let's implement Lisp on weird languages/platform" theme, here's awklisp, a project that apparently helped/inspired Dipert to write Gherkin: https://github.com/darius/awklisp
[+] breadbox|12 years ago|reply
I also took the time to type in Randall Beer's Lisp from 80 micro. In fact, I'm pretty sure that that was my first time accessing a working Lisp interpreter. (Mostly working, anyway. I think I had a typo that I never managed to track down.) Unfortunately I didn't really know what to do with it. I didn't really learn how to use Lisp until I spent several days trying to write Lisp programs in college, and finally started to understand the functional paradigm. But yeah, that may have been one of the longest magazine programs that I ever typed in.
[+] wfn|12 years ago|reply
> since 1983, [Randall Beer] became a neuroscience prof and mentions his BASIC LISP on his homepage

> http://mypage.iu.edu/~rdbeer/

Just wanted to say thanks for linking to this fine chap. He seems to have a cross-section of interests such that I can really relate to and draw from. In particular, some people here might be interested about the intersection of cellular automata theory and (what it has to say about) cognition, autonomous systems, etc. (see e.g. [1, 2]) This is one of his approaches to understanding dynamic systems and how coordinated bahaviour can arise in them. See his publication list, too. [3]

Apparently formal treatments / approaches to autopoiesis have been developed for quite some time (currently recommended books seem to be from e.g. 1980 and onward.) Interesting indeed! :)

[1]: http://pubs.cogs.indiana.edu/pubspdf/34233/34233_varela.pdf

[2]: http://mypage.iu.edu/~rdbeer/Papers/Beer2014.pdf

[3]: http://mypage.iu.edu/~rdbeer/pubs.html

[+] mikeash|12 years ago|reply
This is fascinating. I assumed it was yet another ridiculous attempt to build something in an environment completely unsuited for it, but it seems that they are serious. But they're also sufficiently aware of the craziness of the project that the first thing they do is explain just why the heck they're doing it:

https://github.com/alandipert/gherkin/wiki/Why-gherkin%3F

The short version is that bash is the closest thing to being universally available on every UNIXoid system no matter what, and so by writing stuff in bash, you make it so that it can run everywhere. But because bash sucks to program in, this is a minimalist interpreter for a sane language. You can then write programs in that language, and they will only depend on bash and on this interpreter, and the interpreter is simple enough not to need any sort of complex installation.

I can't quite think of a use case for this where it's not worth e.g. installing Python first, but it's an interesting project all the same.

[+] lloeki|12 years ago|reply
> I can't quite think of a use case for this where it's not worth e.g. installing Python first

Whereever you get Bash, you can reasonably assume Perl5 (unless in an initrd or something). Even on some old AIX 4 I had a readily available Perl 5.005.

Nonetheless I wish there were more actual shells that were not sh descendants.

[+] chubot|12 years ago|reply
Yeah, I guess bash is technically more available than a C compiler. But I think for the overwhelming number of use cases, you could just do "cc -o lisp single-file-lisp-interpreter.c" (there are many compact options). Not to mention just installing Python :)

Also I wonder if awk would have been a better language than bash. I think awk is more available across various Unixes.

If anything, the length that they are willing to go to do this points to the brokenness of package managers. There are still a lot of disadvantages to a package manager versus cp-ing some code.

[+] parfe|12 years ago|reply
Written for Bash 4.0 so OS X will not run without additional work because Apple refuses to ship GPL 3.0. Otherwise really cool project. I love bash software (when other people do the coding).
[+] gaius|12 years ago|reply
I started a project like this about 10 years ago, but then I discovered that you could just compile Lisp on your own workstation and upload it to prod with a .sh extension and no-one would actually check, they would just blindly run it. Not even the size was suspicious. Used the same trick abit later with OCaml and Haskell, you just compile them as whatever.py and no-one's any the wiser.
[+] cbsw|12 years ago|reply
+ - * / even doesn't support multi-data. (+ 1 2 3) would be 3,stupid
[+] crnixon|12 years ago|reply
Cool, awesome, great point. I tried Googling for your implementation and couldn't find it. Could you drop a link?
[+] wooby|12 years ago|reply
Yes, the arithmetic primitives don't support variadic arguments. You can see how + works - and that it only deals with the first two arguments - here: https://github.com/alandipert/gherkin/blob/278354246aebf14b8.... A pull request fixing this would be welcome.

In the meantime, for variadic addition, one can do:

(load-file "core.gk") (reduce + 0 '(1 2 3)) ;=> 6

[+] mzs|12 years ago|reply
There a UUOC in strmap_file, in fact all those uses head, tr, and tail could likely be just handled by sed.
[+] finin|12 years ago|reply
interpeter => interpreter