diff options
author | Alon Zakai (kripken) <alonzakai@gmail.com> | 2017-01-31 16:08:36 -0800 |
---|---|---|
committer | Alon Zakai (kripken) <alonzakai@gmail.com> | 2017-01-31 16:47:00 -0800 |
commit | f2af5a9d587cbdeb8f164fb5d37d20daef527213 (patch) | |
tree | 1619ea1ce6bc51cf35380f3b0b7240291c9220f1 /src/emscripten-optimizer/simple_ast.cpp | |
parent | e8d6188499260d599b2002d1b17405220d22869a (diff) | |
download | binaryen-f2af5a9d587cbdeb8f164fb5d37d20daef527213.tar.gz binaryen-f2af5a9d587cbdeb8f164fb5d37d20daef527213.tar.bz2 binaryen-f2af5a9d587cbdeb8f164fb5d37d20daef527213.zip |
refactor asm.js ast to use an Assign node
Diffstat (limited to 'src/emscripten-optimizer/simple_ast.cpp')
-rw-r--r-- | src/emscripten-optimizer/simple_ast.cpp | 102 |
1 files changed, 101 insertions, 1 deletions
diff --git a/src/emscripten-optimizer/simple_ast.cpp b/src/emscripten-optimizer/simple_ast.cpp index 7379e9740..f28ef9b13 100644 --- a/src/emscripten-optimizer/simple_ast.cpp +++ b/src/emscripten-optimizer/simple_ast.cpp @@ -56,6 +56,106 @@ bool Ref::operator!() { GlobalMixedArena arena; +// Value + +Value& Value::setAssign(Ref target, Ref value) { + asAssign()->target() = target; + asAssign()->value() = value; + return *this; +} + +Assign* Value::asAssign() { + assert(isAssign()); + return static_cast<Assign*>(this); +} + +void Value::stringify(std::ostream &os, bool pretty) { + static int indent = 0; + #define indentify() { for (int i_ = 0; i_ < indent; i_++) os << " "; } + switch (type) { + case String: { + if (str.str) { + os << '"' << str.str << '"'; + } else { + os << "\"(null)\""; + } + break; + } + case Number: { + os << std::setprecision(17) << num; // doubles can have 17 digits of precision + break; + } + case Array: { + if (arr->size() == 0) { + os << "[]"; + break; + } + os << '['; + if (pretty) { + os << std::endl; + indent++; + } + for (size_t i = 0; i < arr->size(); i++) { + if (i > 0) { + if (pretty) os << "," << std::endl; + else os << ", "; + } + indentify(); + (*arr)[i]->stringify(os, pretty); + } + if (pretty) { + os << std::endl; + indent--; + } + indentify(); + os << ']'; + break; + } + case Null: { + os << "null"; + break; + } + case Bool: { + os << (boo ? "true" : "false"); + break; + } + case Object: { + os << '{'; + if (pretty) { + os << std::endl; + indent++; + } + bool first = true; + for (auto i : *obj) { + if (first) { + first = false; + } else { + os << ", "; + if (pretty) os << std::endl; + } + indentify(); + os << '"' << i.first.c_str() << "\": "; + i.second->stringify(os, pretty); + } + if (pretty) { + os << std::endl; + indent--; + } + indentify(); + os << '}'; + break; + } + case Assign_: { + os << "["; + ref->stringify(os, pretty); + os << ", "; + asAssign()->value()->stringify(os, pretty); + os << "]"; + break; + } + } +} + // dump void dump(const char *str, Ref node, bool pretty) { @@ -252,6 +352,6 @@ void traverseFunctions(Ref ast, std::function<void (Ref)> visit) { // ValueBuilder -IStringSet ValueBuilder::statable("assign call binary unary-prefix conditional dot new sub seq string object array"); +IStringSet ValueBuilder::statable("call binary unary-prefix conditional dot new sub seq string object array"); } // namespace cashew |