Skip to content

Commit

Permalink
experimental option to dump JS from AST
Browse files Browse the repository at this point in the history
Summary:
Add a new module `AST2JS` which takes AST as input and generates
JavaScript source. It has an optional "pretty" mode where spaces,
newlines and indentation are inserted for readability. Long expressions
however are not split into multiple lines - that would require a whole
new level of complexity. The purpose here is not to implement a JS
auto-formatter, but simply to emit fairly readable code.

When "pretty" mode is disabled, no spaces and new lines are emitted (except where required).

It does not support all AST constructs yet - I had no time to add all of
them, but adding more is easy using the existing patterns. It crashes
when encountering an unsupported AST kind. It works well enough to
generate the `richards.js` benchmark.

The easiest way to implement all missing nodes is to delete
`void GenJS::visit(Node *n)`, which will cause compiler errors until
they are all added.

New command line options: `-dump-js`, `-dump-transformed-js`.

Reviewed By: avp

Differential Revision: D25295347

fbshipit-source-id: 201140c0465de87602a344d9e6a4a021238d94df
  • Loading branch information
tmikov authored and facebook-github-bot committed Dec 5, 2020
1 parent 0318eae commit e4ae97f
Show file tree
Hide file tree
Showing 13 changed files with 1,909 additions and 1 deletion.
28 changes: 28 additions & 0 deletions include/hermes/AST2JS/AST2JS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#ifndef HERMES_AST2JS_AST2JS_H
#define HERMES_AST2JS_AST2JS_H

namespace llvh {
class raw_ostream;
}

namespace hermes {
namespace ESTree {
class Node;
}

/// Output the supplied AST as JS.
/// \param OS the output stream
/// \param root the root AST node
/// \param pretty pretty print or not.
void generateJS(llvh::raw_ostream &OS, ESTree::Node *root, bool pretty);

} // namespace hermes

#endif
2 changes: 2 additions & 0 deletions include/hermes/Utils/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace hermes {
enum OutputFormatKind {
DumpAST,
DumpTransformedAST,
DumpJS,
DumpTransformedJS,
ViewCFG,
DumpIR,
DumpLIR,
Expand Down
Loading

0 comments on commit e4ae97f

Please sign in to comment.