Skip to content

Commit

Permalink
Add comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
karnkaul committed Oct 6, 2023
1 parent 0bb2a88 commit dac198d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion examples/quickstart/quickstart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,46 @@
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;
}
}
} // namespace

auto main(int argc, char** argv) -> int {
// obtain arguments, skip exe name
auto args = std::span{argv, static_cast<std::size_t>(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] \"<expression>\"\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);
}

0 comments on commit dac198d

Please sign in to comment.