Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
amrex::Parser Class Reference

Front-end for parsing scalar expressions into GPU/CPU executors. More...

#include <AMReX_Parser.H>

Public Member Functions

 Parser (std::string const &func_body)
 Construct a parser by immediately parsing func_body.
 
 Parser ()=default
 Default-construct; call define() before compilation.
 
void define (std::string const &func_body)
 Parse and own a new expression, replacing any previous state.
 
 operator bool () const
 True when an expression has been parsed successfully.
 
void setConstant (std::string const &name, double c)
 Bind a named constant to c.
 
void registerVariables (Vector< std::string > const &vars)
 Register the ordered list of variable names used by the expression.
 
void registerUserFn1 (std::string const &name, ParserUserFn1 fh, ParserUserFn1 fd)
 Register a user-defined unary function available to expressions.
 
void registerUserFn2 (std::string const &name, ParserUserFn2 fh, ParserUserFn2 fd)
 Register a user-defined binary function.
 
void registerUserFn3 (std::string const &name, ParserUserFn3 fh, ParserUserFn3 fd)
 Register a user-defined ternary function.
 
void registerUserFn4 (std::string const &name, ParserUserFn4 fh, ParserUserFn4 fd)
 Register a user-defined function with four arguments.
 
void print () const
 Print a human-readable representation of the parser tree to stdout.
 
void printExe () const
 Dump the compiled bytecode to stdout for debugging.
 
int depth () const
 Return the maximum tree depth of the parsed expression.
 
int maxStackSize () const
 Return the maximum stack usage recorded during compilation.
 
std::string expr () const
 Return the sanitized expression string currently managed by the Parser.
 
std::set< std::string > symbols () const
 Return the set of symbols (variables and functions) referenced by the expression.
 
std::map< std::string, int > const & userFunctions () const
 Access the map of registered user-defined functions.
 
template<int N>
ParserExecutor< N > compile () const
 Compile the current expression into a host/device executor.
 
template<int N>
ParserExecutor< N > compileHost () const
 Compile the current expression into a host-only executor.
 

Detailed Description

Front-end for parsing scalar expressions into GPU/CPU executors.

A Parser instance owns the expression string, parser state, and registered variables. After calling define(), registerVariables(), and (optionally) registerUserFn*, the expression can be compiled into a ParserExecutor that evaluates it efficiently on host or device.

Constructor & Destructor Documentation

◆ Parser() [1/2]

amrex::Parser::Parser ( std::string const &  func_body)

Construct a parser by immediately parsing func_body.

Parameters
func_bodyExpression text; pass an empty string to defer parsing.

◆ Parser() [2/2]

amrex::Parser::Parser ( )
default

Default-construct; call define() before compilation.

Member Function Documentation

◆ compile()

template<int N>
ParserExecutor< N > amrex::Parser::compile ( ) const

Compile the current expression into a host/device executor.

Template Parameters
NNumber of registered variables.
Returns
ParserExecutor callable on both host and GPU (if available).

◆ compileHost()

template<int N>
ParserExecutor< N > amrex::Parser::compileHost ( ) const

Compile the current expression into a host-only executor.

Template Parameters
NNumber of registered variables.
Returns
ParserExecutor that executes only on the host.

◆ define()

void amrex::Parser::define ( std::string const &  func_body)

Parse and own a new expression, replacing any previous state.

Parameters
func_bodyExpression text to parse.

◆ depth()

int amrex::Parser::depth ( ) const

Return the maximum tree depth of the parsed expression.

Returns
Depth measured in parse-tree levels (0 if undefined).

◆ expr()

std::string amrex::Parser::expr ( ) const

Return the sanitized expression string currently managed by the Parser.

Returns
Copy of the parsed expression, or an empty string if undefined.

◆ maxStackSize()

int amrex::Parser::maxStackSize ( ) const

Return the maximum stack usage recorded during compilation.

Returns
Stack depth in parser instructions (0 if undefined).

◆ operator bool()

amrex::Parser::operator bool ( ) const
explicit

True when an expression has been parsed successfully.

◆ print()

void amrex::Parser::print ( ) const

Print a human-readable representation of the parser tree to stdout.

◆ printExe()

void amrex::Parser::printExe ( ) const

Dump the compiled bytecode to stdout for debugging.

◆ registerUserFn1()

void amrex::Parser::registerUserFn1 ( std::string const &  name,
ParserUserFn1  fh,
ParserUserFn1  fd 
)

Register a user-defined unary function available to expressions.

Parameters
nameFunction identifier used inside expressions.
fhHost function pointer (double(double)).
fdDevice function pointer (double(double)); may be nullptr on CPU-only builds.

◆ registerUserFn2()

void amrex::Parser::registerUserFn2 ( std::string const &  name,
ParserUserFn2  fh,
ParserUserFn2  fd 
)

Register a user-defined binary function.

Parameters
nameFunction identifier.
fhHost implementation taking two doubles.
fdDevice implementation taking two doubles; may be nullptr on CPU builds.

◆ registerUserFn3()

void amrex::Parser::registerUserFn3 ( std::string const &  name,
ParserUserFn3  fh,
ParserUserFn3  fd 
)

Register a user-defined ternary function.

Parameters
nameFunction identifier.
fhHost implementation taking three doubles.
fdDevice implementation taking three doubles; may be nullptr on CPU builds.

◆ registerUserFn4()

void amrex::Parser::registerUserFn4 ( std::string const &  name,
ParserUserFn4  fh,
ParserUserFn4  fd 
)

Register a user-defined function with four arguments.

Parameters
nameFunction identifier.
fhHost implementation taking four doubles.
fdDevice implementation taking four doubles; may be nullptr on CPU builds.

◆ registerVariables()

void amrex::Parser::registerVariables ( Vector< std::string > const &  vars)

Register the ordered list of variable names used by the expression.

The ordering determines how arguments are passed to ParserExecutor::operator().

Parameters
varsVariable names supplied in parser argument order.

◆ setConstant()

void amrex::Parser::setConstant ( std::string const &  name,
double  c 
)

Bind a named constant to c.

Parameters
nameIdentifier referenced inside the expression.
cConstant value substituted when compiling.

◆ symbols()

std::set< std::string > amrex::Parser::symbols ( ) const

Return the set of symbols (variables and functions) referenced by the expression.

Returns
Alphabetically sorted symbol names.

◆ userFunctions()

std::map< std::string, int > const & amrex::Parser::userFunctions ( ) const

Access the map of registered user-defined functions.

Returns
Map from function name to argument count.

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