6 ms·
30 minutes Lisp in Ruby (direct link)
- JoelMcCracken 16y agoLisp's simplicity is one of its most interesting features. As far as I know, Lisp is the only language that can achieve such great power while managing to be so simple. Lisp has staying power.
- kingkilr 16y agoIt's kind of cheating to not even include a lexer/parser.
- Goladus 16y agoIf your platform provides one, why bother?
- shaunxcode 16y agoso that you can read lisp source instead of [:+, 6, 7] However the parsing is probably the easiest part so punting makes sense as you can get down to the stuff that matters and add that in later.
- akkartik 16y agoI wish somebody would build a toy lisp with some sort of macro. That's the part of the implementation that is still opaque to me. Last time this came up: http://news.ycombinator.com/item?id=1468345 http://news.ycombinator.com/item?id=1468345
- EdiX 16y agoI don't know what's the proper way of implementing macros but I can tell you how I did it in my toy lisp implementation: I compiled defmacro as a normal function but I also added it to a macro table, then in my compile-form function I would use this table to decide if the form is a) a special form b) a function call c) a macro (a) and (b) are obvious, (c) means I would call the compiled macro "function" on the rest of the form and the execute compile-form on the result. It's unhygenic and there's probably some fatal flaw I didn't catch.
- bitsai 16y agoQuestion about (a): How do you handle special forms? I wrote a toy Lisp interpreter in Clojure, but couldn't figure out how to implement ', which I assumed should be implemented as a special form.
- abecedarius 16y ago'X is expanded by the reader into (QUOTE X), a special form like any other.
- bitsai 16y agoRighto. I had a working implementation that could expand 'x to (quote a) and '(1 2 3) to (quote (1 2 3)), but the case of expanding '''a to (quote (quote (quote a))) stumped me. I realize that ' is a special form like any other... except I haven't figured out how to implement any other special forms either :)
- EdiX 16y agoI handled them by generating the assembly code to implement them and setting up the symbol tables as required, I implemented ' as abecedarius said and then quote was a special form that compiled into something that "returned" the requested symbol object. PS similar deal with ` it would be expanded into a bunch of normal forms by the parser ("reader" in lisp-speak) and from there it's implemented with macros and functions, I copied Guy Steele example implementation, you can find it somewhere.
- bitsai 16y agoThanks for the tips. It's probably time for me to stop messing around and start reading the Lambda Papers in earnest...
- abecedarius 16y agoThat's the standard way to do it, except DEFMACRO is not a function. So define a function called SET-MACROEXPANDER or something, and define DEFMACRO as a macro using it.
- abecedarius 16y agohttp://github.com/darius/awklisp http://github.com/darius/awklisp does it the usual way. It's an order of magnitude bigger than the OP, and not focused on How To Do Macros, but on the plus side, it is just a toy.
- jcoglan 16y agoI wrote a Scheme in Ruby that has macros last year: http://github.com/jcoglan/heist http://github.com/jcoglan/heist. It's an interpreter, not a compiler, and is extremely dynamic: all functions, macros, special forms are just treated as function calls by the parser. Macros are just a subtype of functions that accept and return syntax nodes before eval-ing the output, and this is done at runtime while running all your regular code.