Ask HN: Designing a Parenscript alternative
Several projects already exist which compile established dialects of Lisp into JavaScript, including Parenscript [1], Scriptjure [2], Scheme2Js [3] and ArcScript [4]. I'm trying to determine if there'd be value in creating a new little Lisp dialect specifically designed for compiling into JavaScript. The goal would be to end up with something like CoffeeScript [5], only s-expression based so that Lisp-style macros could be supported.
I've started designing the language following this general process:
1. Imagine what JavaScript would look like if it were composed of s-expressions rather than C-style syntax
2. Permit irregularities where it would be a convenience boon to everyday JavaScript coding
I will post example code in the comment thread, and feedback is very welcome. Do you think this project has potential, and would you consider using it? Do you have any advice about writing or hosting the compiler?
---
[1] http://common-lisp.net/project/parenscript/
[2] https://github.com/arohner/scriptjure
[3] http://www-sop.inria.fr/indes/scheme2js/
13 comments
[ 5.1 ms ] story [ 42.7 ms ] threadFunctions:
We could build a simple unhygienic macro system using the traditional quote ('), quasiquote (`), unquote (,) and unquote-splicing (,@) operators, as well as rest parameters (&). Here's how you might define a macro called unless: More coming...The solution isn't special syntax, it's macros. Check out Parenscript's chain:
http://common-lisp.net/project/parenscript/reference.html#ch...
"For blocks we use a wrapper function rather than the curly brace block so that lexical scoping for variables can be ensured."
In most cases you can generate more efficient code by doing rudimentary control flow analysis. Look at how RETURN-FROM is implemented in the current Parenscript repository version.
Also, speaking of lexical scoping don't forget that in JS loop variables only get a single binding. Scheme2JS uses a neat trick with with to implement the proper (per-iteration) binding (see this paper: http://www-sop.inria.fr/indes/scheme2js/files/icfp2006.pdf). It's the only useful use of with I've ever encountered.
"The ternary operator"
Don't. The #1 goal of any new Lisp to JS translator should be to eliminate the statement/expression dichotomy.
"We could build a simple unhygienic macro system using the traditional quote ('), quasiquote (`), unquote (,) and unquote-splicing (,@) operators, as well as rest parameters (&)."
Take it one step further and make quasiquoting be able to build arrays at runtime. This is something that Daniel Gackle (gruseom) suggested for Parenscript and I need to implement. I actually want to take it a step further still and have a matching system like fare-matcher (http://www.cliki.net/fare-matcher) based around quasi-quoting. If you write all your code this way, you can go a long way towards eliminating dependence on the specific data structures you use for representing s-expressions. Right now I suspect you can go all the way, and that it's a viable strategy for making Parenscript self-hosting without adding runtime support for car/cdr to JavaScript.
Speaking of runtime libraries, I think it's an implicit assumption you made already, but if you want to be like Coffeescript or Parenscript, you obviously need to avoid those.
This won't loop but just return 6.
I've just added a piece about the dot operator to the examples. I don't think any of the existing compilers have tried this approach.
Yeah, we're still working on that (mostly because I'm stupid). The expression/statement dichotomy is a wily satan.