Skip to content

Commit

Permalink
Remove Kalcy, add Env::get_default().
Browse files Browse the repository at this point in the history
  • Loading branch information
karnkaul committed Oct 6, 2023
1 parent cc7fef6 commit 0bb2a88
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 58 deletions.
9 changes: 5 additions & 4 deletions examples/quickstart/quickstart.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include <kalcy/kalcy.hpp>
#include <cassert>
#include <format>
#include <iomanip>
#include <iostream>
#include <span>

namespace {
auto run(std::string_view const text, bool verbose) -> bool {
auto kalcy = kalcy::Kalcy{};
auto ast = std::string{};
try {
std::cout << kalcy.evaluate(text, &ast) << "\n";
if (verbose) { std::cout << "expression\t: " << text << "\nAST\t\t: " << ast << "\n"; }
auto expr = kalcy::Parser{text}.parse();
assert(expr != nullptr);
std::cout << kalcy::evaluate(*expr) << "\n";
if (verbose) { std::cout << std::format("expression\t: {}\nAST\t\t: {}\n", text, kalcy::to_string(*expr)); }
return true;
} catch (kalcy::Error const& err) {
std::cerr << err.what() << "\n";
Expand Down
1 change: 0 additions & 1 deletion kalcy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ target_sources(${PROJECT_NAME} PRIVATE
src/error.cpp
src/eval.cpp
src/expr.cpp
src/kalcy.cpp
src/parser.cpp
src/scanner.cpp
)
11 changes: 11 additions & 0 deletions kalcy/include/kalcy/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class Env {
///
struct Mismatch {};

///
/// \brief Get a default instance (singleton).
/// \returns const reference to a static default instance.
///
static auto get_default() -> Env const&;

///
/// \brief Populate environment with builtins.
///
explicit Env();

///
/// \brief Define a function.
///
Expand Down
2 changes: 2 additions & 0 deletions kalcy/include/kalcy/eval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ struct Eval {
///
[[nodiscard]] auto operator()(Expr const& expr) const noexcept(false) -> double;
};

inline auto evaluate(Expr const& expr, Env const& env = Env::get_default()) { return Eval{env}(expr); }
} // namespace kalcy
30 changes: 2 additions & 28 deletions kalcy/include/kalcy/kalcy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,5 @@
#include <kalcy/build_version.hpp>
#include <kalcy/env.hpp>
#include <kalcy/error.hpp>

namespace kalcy {
///
/// \brief Expression evaluator with an owned environment.
///
class Kalcy {
public:
///
/// \brief Populate environment with builtins.
///
explicit Kalcy();

///
/// \brief Evaluate an expression.
/// \param expression expression to evaluate.
/// \param out_ast (optional) out parameter to build stringified AST to.
/// \throws sub-type of Error.
///
[[nodiscard]] auto evaluate(std::string_view expression, std::string* out_ast = {}) const noexcept(false) -> double;

///
/// \brief Execution environment.
///
/// The environment is read-only during evaluation of expressions.
///
Env environment{};
};
} // namespace kalcy
#include <kalcy/eval.hpp>
#include <kalcy/parser.hpp>
15 changes: 15 additions & 0 deletions kalcy/src/env.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
#include <kalcy/env.hpp>
#include <kalcy/error.hpp>
#include <cassert>
#include <cmath>
#include <numbers>

namespace kalcy {
auto Env::get_default() -> Env const& {
static auto const ret{Env{}};
return ret;
}

Env::Env() {
define("pi", std::numbers::pi_v<double>);
define("sqrt", [](std::span<double const> args) {
if (args.size() != 1) { throw Mismatch{}; }
return std::sqrt(args.front());
});
}

void Env::define(std::string_view name, Func func) {
if (name.empty() || !func) { return; }
m_table.insert_or_assign(name, std::move(func));
Expand Down
2 changes: 1 addition & 1 deletion kalcy/src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ParseError::ParseError(Token const token, Token::Type const expected)
UndefinedSymbol::UndefinedSymbol(Token const token) : Error(token, std::format("undefined symbol: '{}'", token.lexeme)) {}

ArgsMismatch::ArgsMismatch(Token const token, std::size_t argument_count)
: Error(token, std::format("{} does not take {} arguments", token.lexeme, argument_count)), argument_count(argument_count) {}
: Error(token, std::format("{} does not take {} argument(s)", token.lexeme, argument_count)), argument_count(argument_count) {}

InvalidOperaor::InvalidOperaor(Token const token) : Error(token, std::format("invalid operator: '{}'", token.lexeme)) {}
} // namespace kalcy
24 changes: 0 additions & 24 deletions kalcy/src/kalcy.cpp

This file was deleted.

0 comments on commit 0bb2a88

Please sign in to comment.