
Little Lisp interpreter - astdb
https://maryrosecook.com/blog/post/little-lisp-interpreter
======
llimllib
Neat! I did my own javascript lisp after Norvig's too:
[https://twitter.com/llimllib/status/857410209102299136](https://twitter.com/llimllib/status/857410209102299136)

Then, instead of adding variables, I wrapped most of the functions on a canvas
in the env and used it to generate my talk's slides:
[https://github.com/llimllib/adhocteam.club/blob/master/slide...](https://github.com/llimllib/adhocteam.club/blob/master/slides.py#L15)

Then I let my coworkers graffiti the slides with the same lisp as I gave my
talk, as it was an open web server where you submitted them. A tiny demo is
left running at [https://www.adhocteam.club/](https://www.adhocteam.club/) ,
but I should write it all up.

~~~
Edmond
checkout letzcode.com, it uses scheme...meant to be a social way to introduce
students to programming.

------
falcolas
I don't want to take away from this, but any time a home built lisp
interpreter comes up with a "let me show you how it works", I always have to
reference back to the MAL (make a lisp) project:

[https://github.com/kanaka/mal](https://github.com/kanaka/mal)

I find it's better to write it yourself than just have it explained to you. It
really doesn't take that long to do, and it's broken into nice chunks of
discrete work.

------
flavio81
I love these articles (plus the "make a lisp" project as well). They give me a
lot of hope in the future: Next time a customer insists that its software
should be delivered in Java, i will first write my own informal, bug-ridden
implementation of Lisp on top of it, and write the rest of the software in
such Lisp.

Greenspun 10th rule rules!!

------
escherize
I'm unclear about function invocation here... FTA:

    
    
        A function invocation. This comprises a list where the first element
        is the function and the rest of the elements are the arguments. first
        takes one argument, (1 2), and returns 1.
    

My problem is, the same way in every lisp I've seen, (1 2) qualifies as a
function invocation. I'm not sure how the interpreter would know not to invoke
1 on 2, in these examples:

    
    
        (1 2)
    

or

    
    
        (first (1 2))

~~~
hencq
In most (all?) lisps that's actually exactly what would happen. It would try
to call the function '1' (and throw an error since 1 is not a valid function).
If you want to have a list you'd typically have something like (list 1 2) or
you could quote it '(1 2).

In this case it seems to actually check if the first element of the list is a
function and only treat it as a function call in that case.

~~~
ambulancechaser
but you're not passing it the argument '(1 2). Lisps are usually (always?)
strict evaluation, so the form `(list 1 2)` or `(quote (1 2))` would return
the form (1 2). First applied to this form would return 1. The specification
is correct that first applied to (1 2) would return 1. The fact that this
means most invocations would look like `(first '(1 2))` is irrelevant since
the argument to first here is not '(1 2) but is actually (1 2).

~~~
asdlllkasdasd
You are actually in violent agreement with the parent.

The grandparent poster was confused why the syntax (foo (1 2)) can be used to
apply FOO to (1 2). As the parent points out, for typical Lisps this would
actually give an error; instead, (foo '(1 2)) would be the appropriate syntax
to apply FOO to the form (1 2). Indeed, when strictly evaluating (foo '(1 2)),
first the arguments are evaluated. Since functions self-evaluate, FOO
evaluates to itself, while '(1 2) evaluates to (1 2). Then, FOO is applied to
(1 2). This is in complete agreement with what you said and what the
specification says.

However, the Lisp interpreter at hand actually self-evaluates lists whose head
is not a function. Thus (1 2) self-evaluates and (foo (1 2)) has the same
effect as (foo '(1 2)).

~~~
lispm
> Since functions self-evaluate, FOO evaluates to itself

No, in Lisp FOO is a name of a function, not a function itself.

Thus FOO evaluates to a function (otherwise it would be an error) in a Lisp-1
like Scheme. In a Lisp-2 like Common Lisp, one would say that the function
value of FOO is retrieved.

> Indeed, when strictly evaluating (foo '(1 2)), first the arguments are
> evaluated

Actually not. In Lisp the first item needs to be looked at first. If it is
determined to name a function, then we can evaluate the arguments, of which
there is only one in this case.

~~~
asdlllkasdasd
You are right about both points!

PS: The second you quoted out of order. If you reread, you will see that I was
referring to FOO as the first "argument" (a typo for "element"..).

------
piinbinary
I also have made one based on Norvig's:
[https://gist.github.com/jmikkola/b7c6c644dff1c07891c698f0a52...](https://gist.github.com/jmikkola/b7c6c644dff1c07891c698f0a527a890)

------
bsder
Careful: any time someone says they built a lisp quickly, somehow it _always_
manages to leave out the whole (a . b) cons notation.

It's amazing just how difficult that little problem makes everything.

~~~
kazinator
They also leave out _unwind-protect_ and any means to recover from an error.

------
shruubi
It seems almost a right of passage for a programmer to build/attempt a
lisp/pseudo-lisp implementation. Quite honestly, when I made my (very poor)
attempt, it was one of the most fun, and most frustrating projects I had done
at the time, and I would highly recommend every developer have shot at
building some kind of lisp.

One thing I did notice in this implementation, is the lack of quoting and cons
notation. Although, cons is a weird one in the lisp world given it is the only
infix operator in the language.

------
laszlokorte
(test "this ) will not ( work")

~~~
laszlokorte
But in the linked repository the tokenizer has been adjusted:
[https://github.com/maryrosecook/littlelisp/blob/master/littl...](https://github.com/maryrosecook/littlelisp/blob/master/littlelisp.js#L112-L128)

------
RodgerTheGreat
This is an interpreter, not a compiler. The page title itself, "Little Lisp
interpreter" is more accurate.

~~~
sctb
Thanks, we've updated the title from “A lisp compiler in JavaScript”.

