Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "Tree Structure Editor" for Lisp?

I've heard that S-expressions can be represented as trees; for example (f 1 (g 2) 3) as

      .
 .  .  .    .
f  1   .      3          [first level]
      g  2               [second level]

Is there an editor (preferably free) for editing this kind of structure directly? This would

  1. Avoid "all those parentheses"
  2. Exhibit the elegance I've been understanding of lisp.
like image 941
scarlet Avatar asked Oct 23 '11 20:10

scarlet


2 Answers

S-expressions represent a tree, not the other way around. Your example, formatted with newlines:

(f 1
   (g 2)
   3)

It represents the following cons tree:

    +---+---+   +---+---+   +---+---+   +---+---+
--->| f | ----->| 1 | ----->| ¦ | ----->| 3 |NIL|
    +---+---+   +---+---+   + ¦ +---+   +---+---+
                              v
                            +---+---+   +---+---+
                            | g | ----->| 2 |NIL|
                            +---+---+   +---+---+

This is at the same time the actual abstract syntax tree of the program—something that compilers for other language families have to construct from intricate rules.

For editing, the parentheses are all you and your editor need to operate on the tree level. In Emacs it is paredit-mode, but I guess that other editors have similar utilities or plugins.

like image 84
Svante Avatar answered Oct 18 '22 13:10

Svante


As far as I know (and as the Wikipedia article confirms), structure editing was used in Interlisp-D. I am not aware of any structure editor for Common Lisp widely in use today, but maybe there is something I don't know about. There is an example on Pascal J. Bourguignon 's site you may want to play around with. (I didn't take a closer look at it) I did, however use something similar for XML in Oxygen some time ago.

(Also, I don't think the reason Interlisp used this had anything to do with getting rid of the parentheses, and there might be problems with comments, for example.)

like image 32
danlei Avatar answered Oct 18 '22 13:10

danlei