mupp 1.1.0
Loading...
Searching...
No Matches
PProgram.hpp
Go to the documentation of this file.
1/***************************************************************************
2
3 PProgram.hpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8 Based on Joel de Guzman example on calc7,
9 see https://github.com/boostorg/spirit
10
11***************************************************************************/
12
13/***************************************************************************
14 * Copyright (C) 2023 by Andreas Suter *
15 * andreas.suter@psi.ch *
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
32
33#ifndef _PPROGRAM_HPP_
34#define _PPROGRAM_HPP_
35
36#include <string>
37#include <vector>
38#include <map>
39
40#include "PAst.hpp"
41#include "PErrorHandler.hpp"
42
43#include <boost/function.hpp>
44#include <boost/shared_ptr.hpp>
45#include <boost/phoenix/core.hpp>
46#include <boost/phoenix/function.hpp>
47#include <boost/phoenix/operator.hpp>
48
49namespace mupp { namespace prog {
51
65 {
66 public:
70 PVarHandler() : fName("") {}
71
76 void SetName(std::string name) { fName = name; }
77
82 void SetValue(std::vector<double> &dval) { fValue = dval; }
83
89 void SetValue(double dval, unsigned idx);
90
95 void SetError(std::vector<double> &dval) { fError = dval; }
96
102 void SetError(double dval, unsigned idx);
103
108 std::string GetName() { return fName; }
109
114 unsigned int GetSize() { return (fValue.size() == fError.size()) ? fValue.size() : 0; }
115
120 std::vector<double> GetValue() { return fValue; }
121
127 double GetValue(unsigned int idx) { return (idx < fValue.size()) ? fValue[idx] : 0; }
128
133 std::vector<double> GetError() { return fError; }
134
140 double GetError(unsigned int idx) { return (idx < fError.size()) ? fError[idx] : 0; }
141
142 private:
143 std::string fName;
144 std::vector<double> fValue;
145 std::vector<double> fError;
146 };
147
149
164 struct PProgram
165 {
166 typedef bool result_type;
167
173 template <typename PErrorHandler>
174 PProgram(PErrorHandler& error_handler_)
175 {
176 using namespace boost::phoenix::arg_names;
177 namespace phx = boost::phoenix;
178 using boost::phoenix::function;
179 error_handler = function<PErrorHandler>(error_handler_)(
180 "**ERROR** ", _2, phx::cref(error_handler_.iters)[_1]);
181 }
182
187 bool operator()(ast::nil) { BOOST_ASSERT(0); return false; }
188
194 bool operator()(double x);
195
201 bool operator()(ast::assignment const &x);
202
208 bool operator()(ast::expression const &x);
209
215 bool operator()(ast::function const &x);
216
222 bool operator()(ast::operation const &x);
223
229 bool operator()(ast::power const &x);
230
236 bool operator()(ast::statement const &x);
237
243 bool operator()(ast::statement_list const &x);
244
250 bool operator()(ast::unary const &x);
251
257 bool operator()(ast::variable const &x);
258
265
272 void add_predef_var_values(const std::string &name,
273 std::vector<double> &val,
274 std::vector<double> &err);
275
280 void add_var(std::string const& name);
281
287 bool find_var(std::string const &name);
288
295 bool find_var(std::string const &name, unsigned int &idx);
296
305 std::string pos_to_var(std::string const &name, bool &ok);
306
311 std::vector<PVarHandler> getVars() { return fVariable; }
312
313 private:
314 std::vector<PVarHandler> fVariable;
315 std::map<int, std::string> fVarPos;
316
317 boost::function<
318 void(int tag, std::string const& what)>
320 };
321
323
341 {
342 typedef std::vector<double> result_type;
343
348 PProgEval(std::vector<PVarHandler> var) : fVariable(var) {}
349
354 std::vector<double> operator()(ast::nil);
355
361 std::vector<double> operator()(double x);
362
368 std::vector<double> operator()(ast::assignment const &x);
369
375 std::vector<double> operator()(ast::expression const &x);
376
382 std::vector<double> operator()(ast::function const &x);
383
390 std::vector<double> operator()(ast::operation const &x, std::vector<double> lhs);
391
397 std::vector<double> operator()(ast::power const &x);
398
404 std::vector<double> operator()(ast::statement const &x);
405
411 std::vector<double> operator()(ast::statement_list const &x);
412
418 std::vector<double> operator()(ast::unary const &x);
419
425 std::vector<double> operator()(ast::variable const &x);
426
432 std::vector<double> operator()(ast::variable_declaration const &x);
433
440 PVarHandler getVar(const std::string name, bool &ok);
441
445 void print_result();
446
447 private:
448 std::vector<PVarHandler> fVariable;
449
455 unsigned int find_var(std::string const &name);
456 };
457}}
458
459#endif // _PPROGRAM_HPP_
The PVarHandler class manages variable data during evaluation.
Definition PProgram.hpp:65
void SetName(std::string name)
Sets the variable name.
Definition PProgram.hpp:76
std::string GetName()
Gets the variable name.
Definition PProgram.hpp:108
std::vector< double > fError
Vector of error values (one per run)
Definition PProgram.hpp:145
std::vector< double > GetError()
Gets all error values.
Definition PProgram.hpp:133
std::string fName
Variable name.
Definition PProgram.hpp:143
std::vector< double > GetValue()
Gets all values.
Definition PProgram.hpp:120
PVarHandler()
Default constructor initializing an unnamed variable.
Definition PProgram.hpp:70
void SetError(std::vector< double > &dval)
Sets all error values for this variable.
Definition PProgram.hpp:95
double GetValue(unsigned int idx)
Gets a single value at a specific index.
Definition PProgram.hpp:127
unsigned int GetSize()
Gets the size of the variable data.
Definition PProgram.hpp:114
double GetError(unsigned int idx)
Gets a single error value at a specific index.
Definition PProgram.hpp:140
std::vector< double > fValue
Vector of values (one per run)
Definition PProgram.hpp:144
void SetValue(std::vector< double > &dval)
Sets all values for this variable.
Definition PProgram.hpp:82
std::list< statement > statement_list
Type alias for a list of statements forming a program.
Definition PAst.hpp:268
boost::variant< variable_declaration, assignment > statement
Variant type representing a single statement.
Definition PAst.hpp:260
The PErrorHandler struct handles parsing and semantic errors.
std::vector< Iterator > iters
Vector mapping AST node IDs to source positions (used by PAnnotation)
Represents an assignment statement.
Definition PAst.hpp:234
Represents a complete expression with operator precedence.
Definition PAst.hpp:198
Represents a function call with a single argument.
Definition PAst.hpp:209
Represents an empty/null AST node.
Definition PAst.hpp:123
Represents a binary operation with an operator and right operand.
Definition PAst.hpp:185
Represents a power operation.
Definition PAst.hpp:221
Represents a unary operation applied to an operand.
Definition PAst.hpp:173
Represents a variable declaration with optional initialization.
Definition PAst.hpp:247
Represents a variable in an expression.
Definition PAst.hpp:139
std::vector< double > result_type
Return type for all visitor methods (vector of values)
Definition PProgram.hpp:342
PProgEval(std::vector< PVarHandler > var)
Constructor that initializes the evaluator with variables.
Definition PProgram.hpp:348
unsigned int find_var(std::string const &name)
Finds a variable index by name.
Definition PProgram.cpp:665
std::vector< double > operator()(ast::nil)
Evaluates a nil node (should never be called).
Definition PProgram.cpp:338
void print_result()
Prints all variable results to standard output (debugging).
Definition PProgram.cpp:701
std::vector< PVarHandler > fVariable
Variable table with values and errors.
Definition PProgram.hpp:448
PVarHandler getVar(const std::string name, bool &ok)
Retrieves a variable by name after evaluation.
Definition PProgram.cpp:685
bool find_var(std::string const &name)
Checks if a variable exists in the symbol table.
Definition PProgram.cpp:285
std::vector< PVarHandler > fVariable
Symbol table of declared variables.
Definition PProgram.hpp:314
bool result_type
Return type for all visitor methods.
Definition PProgram.hpp:166
std::string pos_to_var(std::string const &name, bool &ok)
Converts position-based variable reference to variable name.
Definition PProgram.cpp:313
PProgram(PErrorHandler &error_handler_)
Constructor that sets up the error handler.
Definition PProgram.hpp:174
std::vector< PVarHandler > getVars()
Gets all variables from the symbol table.
Definition PProgram.hpp:311
void add_predef_var_values(const std::string &name, std::vector< double > &val, std::vector< double > &err)
Injects predefined variable values from collection data.
Definition PProgram.cpp:261
std::map< int, std::string > fVarPos
Map from position index to variable name.
Definition PProgram.hpp:315
boost::function< void(int tag, std::string const &what)> error_handler
Error handler function for reporting semantic errors.
Definition PProgram.hpp:319
void add_var(std::string const &name)
Adds a variable to the symbol table.
Definition PProgram.cpp:275
bool operator()(ast::nil)
Visitor for nil AST nodes (should never be called).
Definition PProgram.hpp:187