mupp 1.1.0
Loading...
Searching...
No Matches
mupp::prog::PProgram Struct Reference

The PProgram struct performs semantic analysis on the AST. More...

#include <PProgram.hpp>

Public Types

typedef bool result_type
 Return type for all visitor methods.
 

Public Member Functions

template<typename PErrorHandler>
 PProgram (PErrorHandler &error_handler_)
 Constructor that sets up the error handler.
 
bool operator() (ast::nil)
 Visitor for nil AST nodes (should never be called).
 
bool operator() (double x)
 Visitor for numeric literal nodes.
 
bool operator() (ast::assignment const &x)
 Visitor for assignment statements.
 
bool operator() (ast::expression const &x)
 Visitor for expression nodes.
 
bool operator() (ast::function const &x)
 Visitor for function call nodes.
 
bool operator() (ast::operation const &x)
 Visitor for binary operation nodes.
 
bool operator() (ast::power const &x)
 Visitor for power operation nodes.
 
bool operator() (ast::statement const &x)
 Visitor for statement nodes (variant wrapper).
 
bool operator() (ast::statement_list const &x)
 Visitor for statement lists (program entry point).
 
bool operator() (ast::unary const &x)
 Visitor for unary operation nodes.
 
bool operator() (ast::variable const &x)
 Visitor for variable reference nodes.
 
bool operator() (ast::variable_declaration const &x)
 Visitor for variable declaration nodes.
 
void add_predef_var_values (const std::string &name, std::vector< double > &val, std::vector< double > &err)
 Injects predefined variable values from collection data.
 
void add_var (std::string const &name)
 Adds a variable to the symbol table.
 
bool find_var (std::string const &name)
 Checks if a variable exists in the symbol table.
 
bool find_var (std::string const &name, unsigned int &idx)
 Finds a variable and returns its index.
 
std::string pos_to_var (std::string const &name, bool &ok)
 Converts position-based variable reference to variable name.
 
std::vector< PVarHandlergetVars ()
 Gets all variables from the symbol table.
 

Private Attributes

std::vector< PVarHandlerfVariable
 Symbol table of declared variables.
 
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.
 

Detailed Description

The PProgram struct performs semantic analysis on the AST.

This visitor pattern struct traverses the AST generated by the parser and performs semantic checks:

  • Verifies that variables are declared before use
  • Prevents duplicate variable declarations
  • Validates function and operator usage
  • Resolves variable references and position-based lookups

After successful semantic analysis, the variable table can be used by PProgEval for expression evaluation. Semantic errors are reported via the error handler with source position information.

Definition at line 164 of file PProgram.hpp.

Member Typedef Documentation

◆ result_type

Return type for all visitor methods.

Definition at line 166 of file PProgram.hpp.

Constructor & Destructor Documentation

◆ PProgram()

template<typename PErrorHandler>
mupp::prog::PProgram::PProgram ( PErrorHandler & error_handler_)
inline

Constructor that sets up the error handler.

Template Parameters
PErrorHandlerthe error handler type
Parameters
error_handler_reference to the error handler for reporting semantic errors

Definition at line 174 of file PProgram.hpp.

Member Function Documentation

◆ add_predef_var_values()

void mupp::prog::PProgram::add_predef_var_values ( const std::string & name,
std::vector< double > & val,
std::vector< double > & err )

Injects predefined variable values from collection data.

Parameters
namethe variable name
valvector of values for this variable
errvector of error values for this variable

Definition at line 261 of file PProgram.cpp.

◆ add_var()

void mupp::prog::PProgram::add_var ( std::string const & name)

Adds a variable to the symbol table.

Parameters
namethe variable name to add

Definition at line 275 of file PProgram.cpp.

◆ find_var() [1/2]

bool mupp::prog::PProgram::find_var ( std::string const & name)

Checks if a variable exists in the symbol table.

Parameters
namethe variable name to find (with or without leading '$')
Returns
true if variable exists, false otherwise

Definition at line 285 of file PProgram.cpp.

◆ find_var() [2/2]

bool mupp::prog::PProgram::find_var ( std::string const & name,
unsigned int & idx )

Finds a variable and returns its index.

Parameters
namethe variable name to find (with or without leading '$')
idxoutput parameter for the variable index
Returns
true if variable exists, false otherwise

Definition at line 291 of file PProgram.cpp.

◆ getVars()

std::vector< PVarHandler > mupp::prog::PProgram::getVars ( )
inline

Gets all variables from the symbol table.

Returns
vector of all variable handlers

Definition at line 311 of file PProgram.hpp.

◆ operator()() [1/12]

bool mupp::prog::PProgram::operator() ( ast::assignment const & x)

Visitor for assignment statements.

Parameters
xthe assignment AST node
Returns
true if variable exists and RHS is valid, false otherwise

Definition at line 84 of file PProgram.cpp.

◆ operator()() [2/12]

bool mupp::prog::PProgram::operator() ( ast::expression const & x)

Visitor for expression nodes.

Parameters
xthe expression AST node
Returns
true if all operands and operations are valid, false otherwise

Definition at line 101 of file PProgram.cpp.

◆ operator()() [3/12]

bool mupp::prog::PProgram::operator() ( ast::function const & x)

Visitor for function call nodes.

Parameters
xthe function AST node
Returns
true if function is valid and argument is valid, false otherwise

Definition at line 114 of file PProgram.cpp.

◆ operator()() [4/12]

bool mupp::prog::PProgram::operator() ( ast::nil )
inline

Visitor for nil AST nodes (should never be called).

Returns
always false (assertion failure)

Definition at line 187 of file PProgram.hpp.

◆ operator()() [5/12]

bool mupp::prog::PProgram::operator() ( ast::operation const & x)

Visitor for binary operation nodes.

Parameters
xthe operation AST node
Returns
true if operation and operand are valid, false otherwise

Definition at line 147 of file PProgram.cpp.

◆ operator()() [6/12]

bool mupp::prog::PProgram::operator() ( ast::power const & x)

Visitor for power operation nodes.

Parameters
xthe power AST node
Returns
true if base and exponent are valid, false otherwise

Definition at line 173 of file PProgram.cpp.

◆ operator()() [7/12]

bool mupp::prog::PProgram::operator() ( ast::statement const & x)

Visitor for statement nodes (variant wrapper).

Parameters
xthe statement AST node
Returns
result of visiting the underlying statement type

Definition at line 188 of file PProgram.cpp.

◆ operator()() [8/12]

bool mupp::prog::PProgram::operator() ( ast::statement_list const & x)

Visitor for statement lists (program entry point).

Parameters
xthe statement list AST node
Returns
true if all statements are valid, false if any statement fails

Definition at line 193 of file PProgram.cpp.

◆ operator()() [9/12]

bool mupp::prog::PProgram::operator() ( ast::unary const & x)

Visitor for unary operation nodes.

Parameters
xthe unary AST node
Returns
true if operator and operand are valid, false otherwise

Definition at line 202 of file PProgram.cpp.

◆ operator()() [10/12]

bool mupp::prog::PProgram::operator() ( ast::variable const & x)

Visitor for variable reference nodes.

Parameters
xthe variable AST node
Returns
true if variable is declared, false otherwise

Definition at line 220 of file PProgram.cpp.

◆ operator()() [11/12]

bool mupp::prog::PProgram::operator() ( ast::variable_declaration const & x)

Visitor for variable declaration nodes.

Parameters
xthe variable declaration AST node
Returns
true if declaration is valid and RHS (if present) is valid, false otherwise

Definition at line 239 of file PProgram.cpp.

◆ operator()() [12/12]

bool mupp::prog::PProgram::operator() ( double x)

Visitor for numeric literal nodes.

Parameters
xthe numeric value
Returns
true (literals are always valid)

Definition at line 79 of file PProgram.cpp.

◆ pos_to_var()

std::string mupp::prog::PProgram::pos_to_var ( std::string const & name,
bool & ok )

Converts position-based variable reference to variable name.

Supports syntax like $0, $1, etc. to reference variables by position.

Parameters
namethe variable reference (name or position number)
okoutput parameter indicating whether conversion succeeded
Returns
the resolved variable name, or "??" if position is out of range

Definition at line 313 of file PProgram.cpp.

Member Data Documentation

◆ error_handler

boost::function< void(int tag, std::string const& what)> mupp::prog::PProgram::error_handler
private

Error handler function for reporting semantic errors.

Definition at line 319 of file PProgram.hpp.

◆ fVariable

std::vector<PVarHandler> mupp::prog::PProgram::fVariable
private

Symbol table of declared variables.

Definition at line 314 of file PProgram.hpp.

◆ fVarPos

std::map<int, std::string> mupp::prog::PProgram::fVarPos
private

Map from position index to variable name.

Definition at line 315 of file PProgram.hpp.


The documentation for this struct was generated from the following files: