3 ms·
It's true that Lisp syntax is simply a way of writing ASTs. But I think the real truth is deeper than that. Lisp feels powerful because you can manipulate pro
by haberman 16y ago
It's true that Lisp syntax is simply a way of writing ASTs. But I think the real truth is deeper than that.
Lisp feels powerful because you can manipulate programs as if they were data. This is easy in Lisp because your programs are already expressed as a tree, using a simple data model (cons cells).
In most other programming languages, the program first goes through this generally very hairy and complicated parser. The parser is not usually accessible from the language itself, which means you can't easily get your hands on an AST. Even when it is (like in Python: http://docs.python.org/library/ast.html http://docs.python.org/library/ast.html) the AST is a complicated data structure that takes a lot of work to make any sense of.
But this doesn't mean that the solution is to write programs as raw ASTs! Programming language syntax is nice because it lets us write things in ways that are convenient for humans. Most humans would rather read 1 + 2 - 3 * 4 than (+ 1 (- 2 (* 3 4))).
I strongly believe that the way to bring the power of Lisp to other programming languages is to provide much easier access to parsers and ASTs, and better APIs for them. If we could accomplish that, we could keep writing syntax that works for humans, but have easy access to data structures so that the AST can be very easily inspected, manipulated, generated, compiled, interpreted, or any of those things that Lispers love to do.
When I say "better APIs," I mean that you should have lots of flexibility for how you want to consume, process, and store the AST. You should have SAX-like stream processing and data structures like DOM (but something that binds more naturally to programming languages, so you can say foo.bar instead of foo.getElementById("bar")). There should be a convenient serialization format for the AST, and there should be a nice way to specify the schema of the AST (like a more generic version of http://scottmcpeak.com/elkhound/sources/ast/index.html http://scottmcpeak.com/elkhound/sources/ast/index.html).
You might sense this coming, but I've been working for a long time on what I consider a major solution to this problem. There are two parts to it.
upb (https://github.com/haberman/upb/wiki/ https://github.com/haberman/upb/wiki/) is a standalone and very minimalist implementation of Google's Protocol Buffers serialization format. I believe Protocol Buffers are an ideal data model for ASTs. Forget for a moment that Protocol Buffers is a serialization format and consider that you can use their schema language to specify something like:
message Expr {
// Both operands can be either a raw value or another expression.
double val1 = 1;
Expr expr1 = 2;
double val2 = 3;
Expr expr2 = 4;
// The operator that should be applied to the two values.
enum Operator {
PLUS = 1;
MINUS = 2;
MULTIPLY = 3;
DIVIDE = 4;
}
Operator operator = 5;
}
With that, you can generate data structures (an AST) that can be used in any language, with convenient syntax (expr.operator). Now Protocol Buffers are just a convenient way to create typed trees, just like the Elkhound "AST" project I linked above, but in a way that can be accessed from ANY programming language (that has protobuf support), not just one. upb also will support a streaming interface like SAX.
The second part is my somewhat dormant project Gazelle (http://www.gazelle-parser.org/ http://www.gazelle-parser.org/). I mean to resume it once upb is ready. Gazelle uses most of the same parsing theory as ANTLR, but instead of generating code for every parser it has a more VM-like approach. And as its output? You guessed it -- upb and Protocol Buffers. You can consume the parse tree in a streaming way (SAX), as a data structure (DOM), you can serialize the AST itself as a Protocol Buffer.
tl;dr: Programming languages are just a human-friendly serialization of an AST. Parsing is simply tree traversal. Protocol Buffers are simply a universal way of specifying and manipulating trees. And I'm working on an attempt to unify it all into a tiny but powerful library that aims to bring the power of Lisp to all programming languages.
- anamax 16y ago> 1 + 2 - 3 * 4 than (+ 1 (- 2 (* 3 4))). Most humans think that, but no humans know what a << b | c ? a : 5 && j *k < z ^ r % u means. Until they learn differently, most humans also think that training wheels help.