tcalc 0.2.0
 
Loading...
Searching...
No Matches
eval.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <cstddef>
15#include <string>
16#include <unordered_map>
17#include <utility>
18#include <vector>
19
20#include "tcalc/builtins.hpp"
21#include "tcalc/common.hpp"
22#include "tcalc/error.hpp"
23#include "tcalc/parser.hpp"
24
25namespace tcalc {
26
31class TCALC_PUBLIC EvalContext
32{
33public:
34 constexpr static std::size_t MAX_CALL_DEPTH = 1000;
35
36private:
37 std::unordered_map<std::string, double> _vars;
38 std::unordered_map<std::string, builtins::Function> _funcs;
39
40 std::size_t _call_depth{ 0 };
41
42public:
49 EvalContext(std::unordered_map<std::string, double> vars,
50 std::unordered_map<std::string, builtins::Function> funcs,
51 std::size_t call_depth = 0)
52 : _vars{ std::move(vars) }
53 , _funcs{ std::move(funcs) }
54 , _call_depth{ call_depth }
55 {
56 }
57
58 EvalContext() = default;
59 ~EvalContext() = default;
60
66 [[nodiscard]] TCALC_INLINE auto& vars() noexcept { return _vars; }
67
74 [[nodiscard]] TCALC_INLINE auto& funcs() noexcept { return _funcs; }
75
82 error::Result<double> var(const std::string& name) const;
83
90 TCALC_INLINE void var(const std::string& name, double value) noexcept
91 {
92 _vars[name] = value;
93 }
94
101 error::Result<builtins::Function> func(const std::string& name) const;
102
109 TCALC_INLINE void func(const std::string& name,
110 builtins::Function func) noexcept
111 {
112 _funcs[name] = std::move(func);
113 }
114
120 [[nodiscard]] TCALC_INLINE auto call_depth() const noexcept
121 {
122 return _call_depth;
123 }
124
130 TCALC_INLINE void call_depth(std::size_t depth) { _call_depth = depth; }
131
136 TCALC_INLINE void increment_call_depth() noexcept { ++_call_depth; }
137
143 void update_with(const EvalContext& ctx);
144};
145
150class TCALC_PUBLIC Evaluator
151{
152private:
153 EvalContext _ctx;
154 ast::Parser _parser{};
155
156public:
162 explicit Evaluator(const EvalContext& ctx);
163
169 : Evaluator{ EvalContext{ builtins::BUILTIN_VARIABLES,
170 builtins::BUILTIN_FUNCTIONS } }
171 {
172 }
173
174 ~Evaluator() = default;
175
181 [[nodiscard]] TCALC_INLINE auto& ctx() const noexcept { return _ctx; }
182
189 error::Result<double> eval(std::string_view input);
190
197 error::Result<std::vector<double>> eval_prog(std::string_view input);
198};
199
200}
Built-in functions and variables.
std::function< error::Result< double >(const std::vector< double > &, const EvalContext &)> Function
Built-in function type.
Definition builtins.hpp:38
Evaluation context which stores variables and built-in functions.
Definition eval.hpp:32
EvalContext(std::unordered_map< std::string, double > vars, std::unordered_map< std::string, builtins::Function > funcs, std::size_t call_depth=0)
Construct a new Eval Context object.
Definition eval.hpp:49
TCALC_INLINE auto & funcs() noexcept
Get built-in functions.
Definition eval.hpp:74
TCALC_INLINE void increment_call_depth() noexcept
Increment the call depth.
Definition eval.hpp:136
TCALC_INLINE void var(const std::string &name, double value) noexcept
Set a variable.
Definition eval.hpp:90
TCALC_INLINE auto call_depth() const noexcept
Get the call depth.
Definition eval.hpp:120
TCALC_INLINE void call_depth(std::size_t depth)
Set the call depth.
Definition eval.hpp:130
TCALC_INLINE void func(const std::string &name, builtins::Function func) noexcept
Set a built-in function.
Definition eval.hpp:109
TCALC_INLINE auto & vars() noexcept
Get variables.
Definition eval.hpp:66
Evaluator for tcalc.
Definition eval.hpp:151
TCALC_INLINE auto & ctx() const noexcept
Get the evaluation context.
Definition eval.hpp:181
Evaluator()
Construct a default Evaluator object with default context.
Definition eval.hpp:168
AST parser.
Definition parser.hpp:97
tcalc common header.
tcalc error definition.
_TCALC_EXPECTED_NS::expected< T, Error > Result
Result type.
Definition error.hpp:153
AST parser.