11#ifndef AMREX_IPARSER_H_
22#define AMREX_IPARSER_H_
33
4+ /* *
5+ * \file AMReX_IParser.H
6+ * \brief Integer-only runtime expression parser and executor helpers.
7+ */
8+
49#include < AMReX_Arena.H>
510#include < AMReX_Array.H>
611#include < AMReX_GpuDevice.H>
1318
1419namespace amrex {
1520
21+ /* *
22+ * \struct amrex::IParserExecutor
23+ * \brief Callable wrapper around an integer parser expression with \p N variables.
24+ *
25+ * Instances are returned by IParser::compile() / IParser::compileHost() and expose
26+ * host/device operator() overloads that evaluate to 64-bit integers.
27+ */
1628template <int N>
1729struct IParserExecutor
1830{
31+ /* *
32+ * \brief Evaluate constant expressions (\p N == 0) and return the integer result.
33+ */
1934 template <int M=N, std::enable_if_t <M==0 ,int > = 0 >
2035 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
2136 long long operator () () const noexcept
@@ -24,6 +39,11 @@ struct IParserExecutor
2439 AMREX_IF_ON_HOST ((return iparser_exe_eval (m_host_executor, nullptr );))
2540 }
2641
42+ /* *
43+ * \brief Evaluate the expression with \p N integral arguments.
44+ * \param var Scalar arguments supplied in registration order.
45+ * \return Result promoted to \c long long.
46+ */
2747 template <typename ... Ts>
2848 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
2949 std::enable_if_t <sizeof ...(Ts) == N && std::conjunction_v<std::is_integral<Ts>...>,
@@ -35,52 +55,107 @@ struct IParserExecutor
3555 AMREX_IF_ON_HOST ((return iparser_exe_eval (m_host_executor, l_var.data ());))
3656 }
3757
58+ /* *
59+ * \brief Evaluate using an explicit array of integer arguments.
60+ * \param var Argument array in registration order.
61+ */
3862 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
3963 long long operator () (GpuArray<long long , N> const & var) const noexcept
4064 {
4165 AMREX_IF_ON_DEVICE ((return iparser_exe_eval (m_device_executor, var.data ());))
4266 AMREX_IF_ON_HOST ((return iparser_exe_eval (m_host_executor, var.data ());))
4367 }
4468
69+ // ! True when this is a valid executor.
4570 [[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
4671 explicit operator bool () const {
4772 AMREX_IF_ON_DEVICE ((return m_device_executor != nullptr ;))
4873 AMREX_IF_ON_HOST ((return m_host_executor != nullptr ;))
4974 }
5075
51- char * m_host_executor = nullptr ;
76+ char * m_host_executor = nullptr ; // /< Pointer to host bytecode.
5277#ifdef AMREX_USE_GPU
53- char * m_device_executor = nullptr ;
78+ char * m_device_executor = nullptr ; // /< Pointer to device bytecode (if copied).
5479#endif
5580};
5681
5782// / \ingroup amrex_utilities
83+ /* *
84+ * \brief Integer-only variant of amrex::Parser.
85+ *
86+ * IParser parses expressions composed of integer literals, integer variables,
87+ * and integer-returning functions. It emits executors that evaluate to long long
88+ * on host or GPU.
89+ */
5890class IParser
5991{
6092public:
93+ /* *
94+ * \brief Construct a parser by immediately parsing \p func_body.
95+ * \param func_body Expression to parse (empty to defer define()).
96+ */
6197 IParser (std::string const & func_body);
98+ // ! Default-construct; call define() before compile().
6299 IParser () = default ;
100+ /* *
101+ * \brief Parse and own a new integer expression, replacing any previous state.
102+ * \param func_body Expression text.
103+ */
63104 void define (std::string const & func_body);
64105
106+ // ! True when an expression has been parsed successfully.
65107 explicit operator bool () const ;
66108
109+ /* *
110+ * \brief Bind a named integer constant.
111+ * \param name Identifier referenced inside the expression.
112+ * \param c Constant value substituted during compilation.
113+ */
67114 void setConstant (std::string const & name, long long c);
68115
116+ /* *
117+ * \brief Register the ordered list of integer variables referenced by the expression.
118+ *
119+ * \param vars Variable names supplied in parser argument order.
120+ */
69121 void registerVariables (Vector<std::string> const & vars);
70122
123+ // ! Print the parse tree to stdout for debugging.
71124 void print () const ;
72125
126+ /* *
127+ * \brief Return the maximum parse-tree depth.
128+ * \return Depth measured in tree levels (0 if undefined).
129+ */
73130 [[nodiscard]] int depth () const ;
131+ /* *
132+ * \brief Return the maximum parser stack usage.
133+ * \return Stack size required during execution (0 if undefined).
134+ */
74135 [[nodiscard]] int maxStackSize () const ;
75136
137+ /* *
138+ * \brief Return the sanitized expression string.
139+ * \return Copy of the parsed expression or an empty string if undefined.
140+ */
76141 [[nodiscard]] std::string expr () const ;
77142
143+ /* *
144+ * \brief Return the set of symbols referenced by the expression.
145+ * \return Sorted symbol names.
146+ */
78147 [[nodiscard]] std::set<std::string> symbols () const ;
79148
80- // ! This compiles for both GPU and CPU
149+ /* *
150+ * \brief Compile the expression into a host/device IParserExecutor.
151+ * \tparam N Number of registered variables.
152+ */
81153 template <int N> [[nodiscard]] IParserExecutor<N> compile () const ;
82154
83- // ! This compiles for CPU only
155+ /* *
156+ * \brief Compile the expression into a host-only executor.
157+ * \tparam N Number of registered variables.
158+ */
84159 template <int N> [[nodiscard]] IParserExecutor<N> compileHost () const ;
85160
86161private:
0 commit comments