From b0a77c63d82a1b6e46f37c29e07a29047315cd63 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 30 Mar 2009 19:46:51 +0000 Subject: Add really primitive pretty printer. git-svn-id: http://svn.drobilla.net/resp/tuplr@110 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- tuplr.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'tuplr.cpp') diff --git a/tuplr.cpp b/tuplr.cpp index 3b7cb81..03c3d4e 100644 --- a/tuplr.cpp +++ b/tuplr.cpp @@ -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::iterator a = args.find("-e"); + + string output; + map::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 { -- cgit v1.2.1