satyr / coco

Unfancy CoffeeScript
http://satyr.github.com/coco/
MIT License
498 stars 48 forks source link

Idea: "Syntax literal" #206

Open akx opened 11 years ago

akx commented 11 years ago

A possible neat feature idea that occurred to me while out walking: a literal that would compile down to a Coco syntax tree object/JavaScript syntax tree object (if it's generated by Coco on the way to JS?)/worst case JavaScript source fragment as string (that could be parsed back to an AST by client code as required).

Use case (for the sake of discussion let's use the syntax ^( ... )):

dbQuery ^(5 < foo < 10) 

-->

dbQuery({
  "type":"Binary","op":"&&",
  "first":{"type":"Binary","op":"<","first":{"type":"Literal","value":"5"},"second":{"type":"Var","value":"foo"}},
  "second":{"type":"Binary","op":"<","first":{"type":"Var","value":"foo"},"second":{"type":"Literal","value":"10"}}
})

The AST above is adapted from coco -aj for (5 < foo && foo < 10) as the AST for 5 < foo < 10 (obviously) didn't have extended comparison transformed into an and expression. Point being the resulting AST should be as easy to use as possible by client code, with as much of Coco's expressiveness remaining as is sensible.

satyr commented 11 years ago

An obvious problem is that our AST is awful: not well-designed, prone to change, not transformable to JS AST, etc.

Something like this would be a must if we were to implement a macro system, though.