AST in JSON format
Kevin Curtis
kevinc1846 at googlemail.com
Mon Nov 16 16:58:51 PST 2009
The previous example was pretty printed. A production version would
offer a minified alternative without newlines and indentation. e.g:
["Program",["ExprStmt",["Assign",{"op":"="},["Id",{"name":"x"}], ...
Perhaps there could be in addition an alternative compact format where
the element strings are replaced with integers:
[44,[20,[21,{"op":"="},[25,{"name":"x"}], ...
And the attribute name string with an integer or maybe the first letter:
[44,[20,[21,{1:"="},[25,{3:"x"}], ...
[44,[20,[21,{"o":"="},[25,{n:"x"}], ...
It's compact and as the integers match the underlying enum no hash
lookup for the element/attribute string names is required. Kind of a
half way house between the AST and bytecode. e.g:
var ast = JSON.AST.parse(source, true); // true for compact mode
var result = JSON.AST.evaluate(ast); // No special mode required
The evaluate() function would simply test if the element value was a
string or integer and hash lookup or atoi.
More information about the es-discuss
mailing list