53#include <boost/foreach.hpp>
54#include <boost/variant/apply_visitor.hpp>
55#include <boost/assert.hpp>
56#include <boost/lexical_cast.hpp>
57#include <boost/algorithm/string.hpp>
63 if (idx >=
fValue.size()) {
64 std::cerr <<
"**ERROR** SetValue: idx=" << idx <<
" is out-of-range." << std::endl;
72 if (idx >=
fError.size()) {
73 std::cerr <<
"**ERROR** SetError: idx=" << idx <<
" is out-of-range." << std::endl;
92 if (!(*
this)(x.
rhs)) {
93 std::cout <<
"**SEM ERROR** in var assignment of var: " << x.
lhs.
name << std::endl;
94 std::cout <<
" rhs is missing or has an error." << std::endl;
103 if (!boost::apply_visitor(*
this, x.
first))
139 if (!(*
this)(x.
arg)) {
140 std::cout <<
"**SEM ERROR** in function: arg is missing or has an error." << std::endl;
149 if (!boost::apply_visitor(*
this, x.
operand_))
175 if (!(*
this)(x.
base)) {
176 std::cout <<
"**SEM ERROR** in power: base argument is missing or has an error." << std::endl;
180 if (!(*
this)(x.
pow)) {
181 std::cout <<
"**SEM ERROR** in power: power argument is missing or has an error." << std::endl;
190 return boost::apply_visitor(*
this, x);
204 if (!boost::apply_visitor(*
this, x.
operand_))
223 std::string var_name;
247 bool r = (*this)(*x.
rhs);
251 std::cout <<
"**SEM ERROR** rhs of var decl, name: " << x.
lhs.
name <<
", failed" << std::endl;
262 std::vector<double> &val,
263 std::vector<double> &err)
267 std::cerr <<
"**ERROR** couldn't find pre-def variable : " << name << std::endl;
281 std::size_t n =
fVarPos.size();
302 for (
unsigned int i=0; i<
fVariable.size(); i++) {
315 std::string result(
"??");
320 result = name.substr(1);
326 int num = strtol(result.c_str(), &p, 10);
340 std::vector<double> result;
343 for (
unsigned int i=0; i<result.size(); i++)
352 std::vector<double> result;
355 for (
unsigned int i=0; i<result.size(); i++)
363 std::vector<double> result;
366 result = (*this)(x.
rhs);
372 bool errorVal(
false);
375 std::size_t pos = name.find(
"Err");
376 if (pos != std::string::npos) {
377 name = name.substr(0, pos);
392 std::vector<double> vec = boost::apply_visitor(*
this, x.
first);
395 vec = (*this)(oper, vec);
402 std::vector<double> vec = (*this)(x.
arg);
408 for (
unsigned int i=0; i<vec.size(); i++) {
412 for (
unsigned int i=0; i<vec.size(); i++)
417 for (
unsigned int i=0; i<vec.size(); i++) {
421 for (
unsigned int i=0; i<vec.size(); i++)
425 for (
unsigned int i=0; i<vec.size(); i++)
426 vec[i] = fabs(vec[i]);
429 for (
unsigned int i=0; i<vec.size(); i++)
430 vec[i] = sin(vec[i]);
433 for (
unsigned int i=0; i<vec.size(); i++)
434 vec[i] = cos(vec[i]);
437 for (
unsigned int i=0; i<vec.size(); i++)
438 vec[i] = tan(vec[i]);
441 for (
unsigned int i=0; i<vec.size(); i++)
442 vec[i] = sinh(vec[i]);
445 for (
unsigned int i=0; i<vec.size(); i++)
446 vec[i] = cosh(vec[i]);
449 for (
unsigned int i=0; i<vec.size(); i++)
450 vec[i] = tanh(vec[i]);
453 for (
unsigned int i=0; i<vec.size(); i++)
454 vec[i] = asin(vec[i]);
457 for (
unsigned int i=0; i<vec.size(); i++)
458 vec[i] = acos(vec[i]);
461 for (
unsigned int i=0; i<vec.size(); i++)
462 vec[i] = atan(vec[i]);
465 for (
unsigned int i=0; i<vec.size(); i++)
466 vec[i] = exp(vec[i]);
469 for (
unsigned int i=0; i<vec.size(); i++) {
474 vec[i] = log10(vec[i]);
479 for (
unsigned int i=0; i<vec.size(); i++) {
484 vec[i] = log(vec[i]);
489 for (
unsigned int i=0; i<vec.size(); i++) {
494 vec[i] = sqrt(vec[i]);
508 std::vector<double> rhs = boost::apply_visitor(*
this, x.
operand_);
511 if (lhs.size() != rhs.size()) {
518 for (
unsigned int i=0; i<lhs.size(); i++)
519 lhs[i] = lhs[i] + rhs[i];
522 for (
unsigned int i=0; i<lhs.size(); i++)
523 lhs[i] = lhs[i] - rhs[i];
526 for (
unsigned int i=0; i<lhs.size(); i++)
527 lhs[i] = lhs[i] * rhs[i];
530 for (
unsigned int i=0; i<lhs.size(); i++) {
535 lhs[i] = lhs[i] / rhs[i];
549 std::vector<double> baseV = (*this)(x.
base);
550 std::vector<double> powV = (*this)(x.
pow);
552 if (baseV.size() != powV.size()) {
557 std::vector<double> result;
558 result.resize(baseV.size());
559 for (
unsigned int i=0; i<baseV.size(); i++)
560 result[i] = pow(baseV[i], powV[i]);
568 std::vector<double> result;
571 for (
unsigned int i=0; i<result.size(); i++)
574 boost::apply_visitor(*
this, x);
582 std::vector<double> result;
585 for (
unsigned int i=0; i<result.size(); i++)
597 std::vector<double> vec = boost::apply_visitor(*
this, x.
operand_);
600 for (
unsigned int i=0; i<vec.size(); i++) {
609 std::string name = x.
name;
610 bool errorVal(
false);
613 std::size_t pos = name.find(
"Err");
614 if (pos != std::string::npos) {
615 name = name.substr(0, pos);
629 std::vector<double> result;
636 bool errorVal(
false);
639 std::size_t pos = name.find(
"Err");
640 if (pos != std::string::npos) {
641 name = name.substr(0, pos);
648 result = (*this)(*x.
rhs);
675 for (
unsigned int i=0; i<
fVariable.size(); i++) {
690 for (
unsigned int i=0; i<
fVariable.size(); i++) {
703 std::vector<double> val, err;
704 for (
unsigned int i=0; i<
fVariable.size(); i++) {
705 if (!strstr(
fVariable[i].GetName().c_str(),
"Err")) {
706 std::cout <<
"+++++" << std::endl;
707 std::cout <<
"var Name: " <<
fVariable[i].GetName() << std::endl;
710 for (
unsigned int j=0; j<val.size(); j++) {
712 std::cout <<
" " << j <<
": " << val[j] <<
" +- " << err[j] << std::endl;
714 std::cout <<
" " << j <<
": " << val[j] << std::endl;
718 std::cout <<
"+++++" << std::endl;
The PVarHandler class manages variable data during evaluation.
void SetName(std::string name)
Sets the variable name.
std::vector< double > fError
Vector of error values (one per run)
void SetError(std::vector< double > &dval)
Sets all error values for this variable.
std::vector< double > fValue
Vector of values (one per run)
void SetValue(std::vector< double > &dval)
Sets all values for this variable.
std::list< statement > statement_list
Type alias for a list of statements forming a program.
@ op_minus
Subtraction operator (-)
@ op_positive
Unary plus operator (+x)
@ op_divide
Division operator (/)
@ op_plus
Addition operator (+)
@ op_times
Multiplication operator (*)
@ op_negative
Unary minus operator (-x)
boost::variant< variable_declaration, assignment > statement
Variant type representing a single statement.
@ fun_sqrt
Square root function.
@ fun_atan
Arctangent function.
@ fun_acos
Arccosine function.
@ fun_exp
Exponential function.
@ fun_min
Minimum value function.
@ fun_cos
Cosine function.
@ fun_cosh
Hyperbolic cosine function.
@ fun_max
Maximum value function.
@ fun_ln
Natural logarithm function.
@ fun_log
Base-10 logarithm function.
@ fun_tanh
Hyperbolic tangent function.
@ fun_asin
Arcsine function.
@ fun_sinh
Hyperbolic sine function.
@ fun_tan
Tangent function.
@ fun_abs
Absolute value function.
Represents an assignment statement.
variable lhs
The left-hand side variable being assigned to.
expression rhs
The right-hand side expression to evaluate.
Represents a complete expression with operator precedence.
operand first
The first operand in the expression.
std::list< operation > rest
Sequence of operations applied left-to-right.
Represents a function call with a single argument.
expression arg
The argument expression.
funid func_id
The function identifier.
Represents an empty/null AST node.
Represents a binary operation with an operator and right operand.
optoken operator_
The binary operator (+, -, *, /)
operand operand_
The right-hand operand.
Represents a power operation.
expression base
The base expression.
expression pow
The exponent expression.
Represents a unary operation applied to an operand.
operand operand_
The operand the operator is applied to.
optoken operator_
The unary operator (positive or negative)
Represents a variable declaration with optional initialization.
variable lhs
The variable being declared.
boost::optional< expression > rhs
Optional initialization expression.
Represents a variable in an expression.
std::string name
Variable name without the '$' prefix.
unsigned int find_var(std::string const &name)
Finds a variable index by name.
std::vector< double > operator()(ast::nil)
Evaluates a nil node (should never be called).
void print_result()
Prints all variable results to standard output (debugging).
std::vector< PVarHandler > fVariable
Variable table with values and errors.
PVarHandler getVar(const std::string name, bool &ok)
Retrieves a variable by name after evaluation.
bool find_var(std::string const &name)
Checks if a variable exists in the symbol table.
std::vector< PVarHandler > fVariable
Symbol table of declared variables.
std::string pos_to_var(std::string const &name, bool &ok)
Converts position-based variable reference to variable name.
void add_predef_var_values(const std::string &name, std::vector< double > &val, std::vector< double > &err)
Injects predefined variable values from collection data.
std::map< int, std::string > fVarPos
Map from position index to variable name.
boost::function< void(int tag, std::string const &what)> error_handler
Error handler function for reporting semantic errors.
void add_var(std::string const &name)
Adds a variable to the symbol table.
bool operator()(ast::nil)
Visitor for nil AST nodes (should never be called).