diff options
author | David Robillard <d@drobilla.net> | 2009-03-30 19:46:51 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2009-03-30 19:46:51 +0000 |
commit | b0a77c63d82a1b6e46f37c29e07a29047315cd63 (patch) | |
tree | 7212eaf55c44a5bbcc612198bbd0644a40176c4c | |
parent | fdc794f5dc9de5b55fff71939ee4374ea38efa8f (diff) | |
download | resp-b0a77c63d82a1b6e46f37c29e07a29047315cd63.tar.gz resp-b0a77c63d82a1b6e46f37c29e07a29047315cd63.tar.bz2 resp-b0a77c63d82a1b6e46f37c29e07a29047315cd63.zip |
Add really primitive pretty printer.
git-svn-id: http://svn.drobilla.net/resp/tuplr@110 ad02d1e2-f140-0410-9f75-f8b11f17cedd
-rw-r--r-- | tuplr.cpp | 30 | ||||
-rw-r--r-- | tuplr.hpp | 1 | ||||
-rw-r--r-- | write.cpp | 27 |
3 files changed, 53 insertions, 5 deletions
@@ -192,8 +192,9 @@ print_usage(char* name, bool error) os << endl; os << " -h Display this help and exit" << endl; os << " -r Enter REPL after evaluating files" << endl; + os << " -p Pretty-print input only" << endl; os << " -e EXPRESSION Evaluate EXPRESSION" << endl; - os << " -o FILE Write assembly output to FILE" << endl; + os << " -o FILE Write output to FILE" << endl; return error ? 1 : 0; } @@ -216,6 +217,8 @@ main(int argc, char** argv) files.push_back(argv[i]); } else if (!strncmp(argv[i], "-r", 3)) { args.insert(make_pair(argv[i], "")); + } else if (!strncmp(argv[i], "-p", 3)) { + args.insert(make_pair(argv[i], "")); } else if (i == argc-1 || argv[i+1][0] == '-') { return print_usage(argv[0], true); } else { @@ -225,7 +228,25 @@ main(int argc, char** argv) } int ret = 0; - map<string,string>::iterator a = args.find("-e"); + + string output; + map<string,string>::const_iterator a = args.find("-o"); + if (a != args.end()) + output = a->second; + + a = args.find("-p"); + if (a != args.end()) { + ifstream is(files.front().c_str()); + if (is.good()) { + Cursor loc; + SExp exp = readExpression(loc, is); + AST* ast = penv.parse(exp); + pprint(cout, ast); + } + return 0; + } + + a = args.find("-e"); if (a != args.end()) { istringstream is(a->second); ret = eval(*cenv, "(command line)", is); @@ -245,9 +266,8 @@ main(int argc, char** argv) if (args.find("-r") != args.end() || (files.empty() && args.find("-e") == args.end())) ret = repl(*cenv); - a = args.find("-o"); - if (a != args.end()) { - ofstream os(a->second.c_str()); + if (output != "") { + ofstream os(output.c_str()); if (os.good()) { cenv->write(os); } else { @@ -547,6 +547,7 @@ private: * EVAL/REPL/MAIN * ***************************************************************************/ +void pprint(std::ostream& out, const AST* ast); void initLang(PEnv& penv, TEnv& tenv); CEnv* newCenv(PEnv& penv, TEnv& tenv); int eval(CEnv& cenv, const string& name, istream& is); @@ -56,3 +56,30 @@ operator<<(ostream& out, const AST* ast) return out << "?"; } +void +pprint_internal(ostream& out, const AST* ast, unsigned indent) +{ + out << string().insert(0, indent, ' '); + const ATuple* tup = ast->to<const ATuple*>(); + if (tup) { + const string head = tup->at(0)->str(); + out << "(" << head; + if (tup->size() > 1) + out << " " << tup->at(1); + for (size_t i = 2; i != tup->size(); ++i) { + out << endl; + pprint_internal(out, tup->at(i), indent + head.length() + 2); + } + out << ")"; + } else { + out << ast; + } +} + +void +pprint(ostream& out, const AST* ast) +{ + pprint_internal(out, ast, 0); + out << endl; +} + |