OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
evaluator.hpp
Go to the documentation of this file.
1//
2// Struct StackEvaluator
3//
4// Copyright (c) 2026, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General Public License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17#ifndef STACKEVALUATOR_HPP
18#define STACKEVALUATOR_HPP
19
20#include <cassert>
21#include <map>
22#include <string>
23
24#include "ast.hpp"
25#include "function.hpp"
26
27namespace client { namespace code_gen
28{
30
31 typedef bool result_type;
32
33 template <typename ErrorHandler>
34 StackEvaluator(ErrorHandler&)
35 {
36 }
37
38 double result() {
39 assert(evaluation_stack_.size() == 1);
41 double result = std::get<double>(res);
42 evaluation_stack_.pop_back();
43 return result;
44 }
45
46 void registerFunction(std::string name,
47 client::function::type callback) {
48 functions_.insert(client::function::named_t(name, callback));
49 }
50
52 std::map<std::string, client::function::type> functions) {
53 functions_.insert(functions.begin(), functions.end());
54 }
55
57 std::map<std::string, double> variableDictionary) {
58 variableDictionary_ = variableDictionary;
59 }
60
61 // visitor
62 bool operator()(ast::nil) { assert(false); return false; }
63 bool operator()(unsigned int x);
64 bool operator()(double x);
65 bool operator()(bool x);
66 bool operator()(ast::quoted_string const& x);
67 bool operator()(ast::identifier const& x);
68 bool operator()(ast::operation const& x);
69 bool operator()(ast::unary const& x);
70 bool operator()(ast::function_call const& x);
71 bool operator()(ast::expression const& x);
72
73 private:
74 std::map<std::string, double> variableDictionary_;
75 std::map<std::string, client::function::type> functions_;
76
77 //our stack is conform to function call arguments
79 };
80}}
81
82#endif
const std::string name
std::pair< std::string, type > named_t
Definition function.hpp:34
std::function< std::tuple< double, bool >(arguments_t)> type
Definition function.hpp:32
std::vector< argument_t > arguments_t
Definition function.hpp:30
std::variant< double, bool, std::string > argument_t
Definition function.hpp:28
void registerVariables(std::map< std::string, double > variableDictionary)
Definition evaluator.hpp:56
client::function::arguments_t evaluation_stack_
Definition evaluator.hpp:78
void registerFunction(std::string name, client::function::type callback)
Definition evaluator.hpp:46
std::map< std::string, client::function::type > functions_
Definition evaluator.hpp:75
void registerFunctions(std::map< std::string, client::function::type > functions)
Definition evaluator.hpp:51
std::map< std::string, double > variableDictionary_
Definition evaluator.hpp:74