coco icon indicating copy to clipboard operation
coco copied to clipboard

Idea: "Syntax literal"

Open akx opened this issue 12 years ago • 1 comments

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.

akx avatar Mar 10 '13 23:03 akx

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.

satyr avatar Mar 11 '13 01:03 satyr