From dac198d73ec6e0c1d5b966d6b6815aadbea73af9 Mon Sep 17 00:00:00 2001 From: Karn Kaul Date: Thu, 5 Oct 2023 17:54:09 -0700 Subject: [PATCH] Add comments. --- examples/quickstart/quickstart.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/quickstart/quickstart.cpp b/examples/quickstart/quickstart.cpp index 0e2a60c..d217a0e 100644 --- a/examples/quickstart/quickstart.cpp +++ b/examples/quickstart/quickstart.cpp @@ -8,13 +8,19 @@ namespace { auto run(std::string_view const text, bool verbose) -> bool { try { + // parse text into an expression auto expr = kalcy::Parser{text}.parse(); assert(expr != nullptr); + // evaluate parsed expression + // a custom Env can be used and passed, if desired std::cout << kalcy::evaluate(*expr) << "\n"; + // print AST if verbose if (verbose) { std::cout << std::format("expression\t: {}\nAST\t\t: {}\n", text, kalcy::to_string(*expr)); } return true; } catch (kalcy::Error const& err) { + // print error std::cerr << err.what() << "\n"; + // highlight error location under text std::cerr << " | " << text << "\n | " << err.build_highlight() << "\n"; return false; } @@ -22,20 +28,26 @@ auto run(std::string_view const text, bool verbose) -> bool { } // namespace auto main(int argc, char** argv) -> int { + // obtain arguments, skip exe name auto args = std::span{argv, static_cast(argc)}.subspan(1); bool verbose{}; + // check if verbose if (args.front() == std::string_view{"-v"}) { - args = args.subspan(1); verbose = true; + // advance args by 1 + args = args.subspan(1); } if (args.empty()) { + // print usage std::cout << std::format("usage: {} [-v] \"\"\n", *argv); return EXIT_SUCCESS; } + // run kalcy on input expression if (!run(args.front(), verbose)) { return EXIT_FAILURE; } + // print epilogue std::cout << std::format("\n^^ kalcy v{}\n", kalcy::version_v); }